定时器准确定时八位时钟
/* 定时器准确定时八位时钟 */ #include <reg51.h> #include <stdio.h> #define uchar unsigned char #define uint unsigned int uchar code duanca[10]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F}; uchar code weica[8] = {0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80}; int i,j,z; sbit LED = P1^0; uint hour = 23,minute = 59,second = 50,t50ms = 0;//所有参数从零开始 //含参数延时 void delays(int d) { while(d--); } //定时器0 void kinson() { TMOD = 0x09; TH0 = 0x3C; TL0 = 0x0B0; EA = 1; ET0 = 1; TR0 = 1; } //主函数 void main(void) { kinson(); LED = 0; while(1)//大循环 { for(i = 0;i<= 7;i++)//for循环 { P3 = ~weica[i];//以此选择数码管显示,一个循环一次 switch(i)//switch开关 i = 0 1 2 3 4 5 6 7 { //时,分,秒,分别看作一个两位数,在通过这个两位数分别除以10和取余10,得到十位和各位 case 0:P2 = duanca[hour/10];break;//读取选择十位 case 1:P2 = duanca[hour%10];break;//读取选择个位 case 2:P2 = ~0xbf;break;//显示那个横杠 case 3:P2 = duanca[minute/10];break;//同理 case 4:P2 = duanca[minute%10];break; case 5:P2 = ~0xbf;break; case 6:P2 = duanca[second/10];break; case 7:P2 = duanca[second%10];break; default:break; } delays(30); } } } //中断 void kinson1() interrupt 1 { j++; if(j == 13) { j = 0; LED = ~LED; delays(1000); //这里是的秒分时分别进一,59秒后分就进一,59分后时就进一,23时后重新循环 second++; if(second == 60) { second = 0; minute++; if(minute==60) { minute = 0; hour++; if(hour==24) hour = 0; } } } }