数字时钟C程序及仿真(共6页).doc
精选优质文档-倾情为你奉上*/#include <reg52.H>unsigned char code SEG712=0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90,0xbf,0xff,;unsigned short int tcount;/sbit set=P10;/sbit up=P11;/sbit down=P12; unsigned char DisSecondL,DisSecondH,DisMinutL,DisMinutH,DisHourL,DisHourH;/结构体变量struct time unsigned char second; unsigned char minute; unsigned char hour;time1; /延时子程序void delay(k)unsigned int k; unsigned char j; while(k-)!=0) for(j=0;j<125;j+); /时钟调整子程序TimeUpDown(n,x,y)unsigned char n,x,y; if(up=0) delay(50); /消除抖动 if(up=0) n+; if(n=x)/上限越界显示为0 n=0; while(up=0); if(down=0) delay(50); if(down=0) if(n=0)/下限越界显示为最大可显示 n=y; else n-; while(down=0); return n;/返回调整后的值 */ /显示子程序void display(DisSecondL,DisSecondH,DisMinutL,DisMinutH,DisHourL,DisHourH) / 日 日 日 日 日 日 / 1 2 3 4 5 6 / hH hL mH mL sH sL/ 01 02 04 08 10 20 P2=0x00; P0=SEG7DisSecondL; /显示秒钟的个位 P2=0x20; delay(3); P2=0x00; P0=SEG7DisSecondH; /显示秒钟的十位 P2=0x10; delay(3); P2=0x00; P0=SEG7DisMinutL; /显示分钟的个位 P2=0x08; delay(3); P2=0x00; P0=SEG7DisMinutH; /显示分钟的十位 P2=0x04; delay(3); P2=0x00; P0=SEG7DisHourL; /显示小时的个位 P2=0x02; delay(3); P2=0x00; if(DisHourH=0) ; else P0=SEG7DisHourH; /显示小时的十位 P2=0x01; delay(3); void main() /unsigned char Flag; time1.second=40; time1.minute=03; time1.hour=05; TMOD=0x01; TH0=(65536-50000)/256; TL0=(65536-50000)%256; EA=1; ET0=1; TR0=1; while(1) display(DisSecondL,DisSecondH,DisMinutL,DisMinutH,DisHourL,DisHourH); if(set = 0) /设置标志位 Flag delay(50); if(set = 0) TR0 = 0; Flag+; if(Flag > 3) /0:显示时间,1:小时调整,2:分钟调整,3:秒调整 Flag = 0; while(set = 0); switch(Flag) case 0: TR0=1; display(time1.second%10,time1.second/10,time1.minute%10,time1.minute/10,time1.hour%10,time1.hour/10); break; case 1: /时钟小时调整 TR0=0; time1.hour=TimeUpDown(time1.hour,24,23); display(10,10,10,10,time1.hour%10,time1.hour/10); break; case 2: /时钟分钟调整 time1.minute=TimeUpDown(time1.minute,60,59); display(10,10,time1.minute%10,time1.minute/10,10,10); break; case 3: /时钟秒调整 time1.second=TimeUpDown(time1.second,60,59); display(time1.second%10,time1.second/10,10,10,10,10); break; /default: / break; void t0(void) interrupt 1 TH0=(65536-50000)/256; TL0=(65536-50000)%256; tcount+; if(tcount=20) tcount=0; time1.second+; if (time1.second>=60) time1.second=0; time1.minute+; if (time1.minute>=60) time1.minute=0; time1.hour+; if(time1.hour>=12) time1.hour=1; DisSecondL=time1.second%10; DisSecondH=time1.second/10; DisMinutL=time1.minute%10; DisMinutH=time1.minute/10; DisHourL=time1.hour%10; DisHourH=time1.hour/10; 专心-专注-专业