资源描述:
《自编程序模块》由会员上传分享,免费在线阅读,更多相关内容在工程资料-天天文库。
1、头函数定义:#include#include#defineuintunsignedint#defineucharunsignedchar延时1m程序:voiddelay(uintz){uintx,y;for(x=z;x>0;x--)for(y=61;y>0;y--);}数码管:位选择:ucharcodetable1[]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};共阴定义:ucharcodetable[]={0x3f,0x06,0x5b,0x4f,0x66,0x
2、6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71}共阳定义:ucharcodetable[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e};选择模块:switch(P3){case0xfe:P1=table[1];break;case0xfd:P1=table[2];break;case0xfb:P1=table[3];break;case0xf7:P1=table[4
3、];break;case0xef:P1=table[5];break;case0xdf:P1=table[6];break;case0xbf:P1=table[7];break;case0x7f:P1=table[8];break;default:break;}数码管显示函数:voiddisplay(ucharbai,ucharshi,ucharge){dula=1;P0=table[bai];dula=0;P0=0xff;//消隐wela=1;P0=0xfe;wela=0;delay(5);dula=1;P0=table[shi];dul
4、a=0;P0=0xff;//消隐wela=1;P0=0xfd;wela=0;delay(5);dula=1;P0=table[ge];dula=0;P0=0xff;//消隐wela=1;P0=0xfb;wela=0;delay(5);}数码管显示另一个模块:voidDisplay(unsignedcharFirstBit,unsignedcharNum){unsignedchari;for(i=0;i5、y_WeiMa[i+FirstBit];//取位码LATCH2=1;//位锁存LATCH2=0;DataPort=TempData[7-i];//取显示数据,段码这里只需要把扫描方向更换一下即可LATCH1=1;//段锁存LATCH1=0;Delay(200);//扫描间隙延时,时间太长会闪烁,太短会造成重影}}定时器:定时器0:Init(){TMOD=0x01;//设置定时器0为工作方式1TH0=(65536-50000)/256;TL0=(65536-50000)%256;EA=1;//开总中断ET0=1;//开外定时器0中断TR0=1
6、;//启动定时器0}voidtimer0()interrupt1{TH0=(65536-50000)/256;TL0=(65536-50000)%256;//开始写程序i++;if(i==16){}}定时器1:init(){TMOD=0x03;//设置定时器1为工作方式1TH1=(65536-50000)/256;TL1=(65536-50000)%256;EA=1;//开总中断ET1=1;//开外定时器1中断TR1=1;//启动定时器1}voidtimer1()interrupt3{TH1=(65536-50000)/256;TL1=(6
7、5536-50000)%256;//开始写程序i++;if(i==16){}}中断0:init(){EA=1;EX0=1;IT0=0;}voidINIT0()interrupt0{}中断1:init(){EA=1;EX1=1;IT1=0;}voidINIT1()interrupt2{}1602程序模块:voidwrite_com(ucharcom)//写指令{lcdrs=0;//选择命令输入P0=com;delay(5);lcden=1;delay(5);lcden=0;}voidwrite_data(uchardate)//写数据{lcd
8、rs=1;//选择数据输入P0=date;delay(5);lcden=1;delay(5);lcden=0;}voidinit(){lcden=0;write_com(0x38