[color=rgb(51, 51, 51) !important]此程序模拟普通的电子表的功能,实现了时,分,秒的显示,并有调表功能。程序简单,好多地方并没有注意细节,只是实现了部分功能。 [color=rgb(51, 51, 51) !important]//数码管为8位七段共阳数码管,数据端接在P0口,选择端接P2口 [color=rgb(51, 51, 51) !important]//外部中断0,选择调时,分或秒,并闪烁,外部中断1,闪烁位加1,实现调表 [color=rgb(51, 51, 51) !important]//注:些程序只是模拟,并无精确校准 [color=rgb(51, 51, 51) !important] [color=rgb(51, 51, 51) !important]#include [color=rgb(51, 51, 51) !important]unsigned char LED7Code[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8, [color=rgb(51, 51, 51) !important]0x80,0x90,0x88,0x83,0x0c6,0xa1,0x86,0x8e, [color=rgb(51, 51, 51) !important]0xbf, //‘-’ [color=rgb(51, 51, 51) !important]}; //七段共阳数码管的编码0-f [color=rgb(51, 51, 51) !important]unsigned char LED7Addr[]={0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80}; //数码管的地址 [color=rgb(51, 51, 51) !important]unsigned char flag=0; [color=rgb(51, 51, 51) !important]unsigned char loop=0; [color=rgb(51, 51, 51) !important]unsigned char second=0; [color=rgb(51, 51, 51) !important]unsigned char minute=0; [color=rgb(51, 51, 51) !important]unsigned char hour=0; [color=rgb(51, 51, 51) !important]void delay(unsigned int n) //n=10000,延时90.05毫秒 [color=rgb(51, 51, 51) !important]{ [color=rgb(51, 51, 51) !important]while(n--); [color=rgb(51, 51, 51) !important]} [color=rgb(51, 51, 51) !important]void display(unsigned char addr,unsigned char LEDCode) //通用显示函数 [color=rgb(51, 51, 51) !important]{ [color=rgb(51, 51, 51) !important]P2=LED7Addr[addr]; //P2送数码管的地址 [color=rgb(51, 51, 51) !important]P0=LED7Code[LEDCode]; //P0送要显示的数据 [color=rgb(51, 51, 51) !important]delay(500); //延时 [color=rgb(51, 51, 51) !important]} [color=rgb(51, 51, 51) !important]void INT_EX0() interrupt 0 [color=rgb(51, 51, 51) !important]{ [color=rgb(51, 51, 51) !important]delay(2000); [color=rgb(51, 51, 51) !important]if (!P3_2) [color=rgb(51, 51, 51) !important]{ [color=rgb(51, 51, 51) !important]if(flag++》=3) flag=0; [color=rgb(51, 51, 51) !important]} [color=rgb(51, 51, 51) !important]} [color=rgb(51, 51, 51) !important]void INT_EX1() interrupt 2 [color=rgb(51, 51, 51) !important]{ [color=rgb(51, 51, 51) !important]delay(2000); [color=rgb(51, 51, 51) !important]if (!P3_3) [color=rgb(51, 51, 51) !important]{ [color=rgb(51, 51, 51) !important]if(flag==1) second++; [color=rgb(51, 51, 51) !important]if(flag==2) minute++; [color=rgb(51, 51, 51) !important]if(flag==3) hour++; [color=rgb(51, 51, 51) !important]} [color=rgb(51, 51, 51) !important]} [color=rgb(51, 51, 51) !important]void INT_T0() interrupt 1 //定时器0 [color=rgb(51, 51, 51) !important]{ [color=rgb(51, 51, 51) !important]TL0=0xb0; //装入初值,15536,定时50000us [color=rgb(51, 51, 51) !important]TH0=0x3c; [color=rgb(51, 51, 51) !important]loop++; [color=rgb(51, 51, 51) !important]} [color=rgb(51, 51, 51) !important]void main() [color=rgb(51, 51, 51) !important]{ [color=rgb(51, 51, 51) !important]TMOD=0x01; //定时器0,工作方式2,16位定时器 [color=rgb(51, 51, 51) !important]TL0=0xb0; //装入初值,15536,定时50000us [color=rgb(51, 51, 51) !important]TH0=0x3c; [color=rgb(51, 51, 51) !important]ET0=1; [color=rgb(51, 51, 51) !important]TR0=1; [color=rgb(51, 51, 51) !important]EX0=1; [color=rgb(51, 51, 51) !important]EX1=1; [color=rgb(51, 51, 51) !important]IT0=1; //跳变方式 [color=rgb(51, 51, 51) !important]IT1=1; [color=rgb(51, 51, 51) !important]EA=1; [color=rgb(51, 51, 51) !important]while (1) [color=rgb(51, 51, 51) !important]{ [color=rgb(51, 51, 51) !important]if(loop》=20) [color=rgb(51, 51, 51) !important]{ [color=rgb(51, 51, 51) !important]second++; [color=rgb(51, 51, 51) !important]loop=0; [color=rgb(51, 51, 51) !important]} [color=rgb(51, 51, 51) !important]if (second》=60) [color=rgb(51, 51, 51) !important]{ [color=rgb(51, 51, 51) !important]second=0; [color=rgb(51, 51, 51) !important]minute++; [color=rgb(51, 51, 51) !important]} [color=rgb(51, 51, 51) !important]if (minute==60) [color=rgb(51, 51, 51) !important]{ [color=rgb(51, 51, 51) !important]minute=0; [color=rgb(51, 51, 51) !important]hour++; [color=rgb(51, 51, 51) !important]} [color=rgb(51, 51, 51) !important]if (!(flag==3&&loop《10)) [color=rgb(51, 51, 51) !important]{ [color=rgb(51, 51, 51) !important]display(0,hour/10); [color=rgb(51, 51, 51) !important]display(1,hour%10); [color=rgb(51, 51, 51) !important]} [color=rgb(51, 51, 51) !important]display(2,16); [color=rgb(51, 51, 51) !important]if (!(flag==2&&loop《10)) [color=rgb(51, 51, 51) !important]{ [color=rgb(51, 51, 51) !important]display(3,minute/10); [color=rgb(51, 51, 51) !important]display(4,minute%10); [color=rgb(51, 51, 51) !important]} [color=rgb(51, 51, 51) !important]display(5,16); [color=rgb(51, 51, 51) !important]if (!(flag==1&&loop《10)) [color=rgb(51, 51, 51) !important]{ [color=rgb(51, 51, 51) !important]display(6,second/10); [color=rgb(51, 51, 51) !important]display(7,second%10); [color=rgb(51, 51, 51) !important]} [color=rgb(51, 51, 51) !important]} [color=rgb(51, 51, 51) !important]}
|