//P0口段选,P2口位选,P1.2LED指示灯,P1.3蜂鸣器 按键不足 误差:24小时差20s
- #include <reg51.h>
- #include <intrins.h>
- #define uchar unsigned char
- #define uint unsigned int
- sbit KEY1=P3^0;
- sbit KEY2=P3^1;
- sbit KEY3=P3^2;
- sbit KEY4=P3^3;
- sbit LED=P1^2;
- sbit BEEP=P1^3;
- uchar code tab[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};
- uchar StrTab[8];
- uchar minute=30,hour=14,second;
- //yanshi
- void delay(unsigned int cnt)
- { while(--cnt);
- }
- //chaizi
- void Displaypro(void)
- {
- StrTab[0]=tab[hour/10];
- StrTab[1]=tab[hour%10];
- StrTab[2]=0XBF;
- StrTab[3]=tab[minute/10];
- StrTab[4]=tab[minute%10];
- StrTab[6]=0XBF;
- StrTab[5]=tab[second/10];
- StrTab[7]=tab[second%10];
- }
- //zhuhanshu
- void main()
- {
- TMOD|=0x01;
- TH0=0xd8;
- TL0=0xf0;
- ET0=1;
- TR0=1;
- TMOD|=0x10;
- TH1=0xf8;
- TL1=0xf0;
- ET1=1;
- TR1=1;
- EA=1;
-
- Displaypro();
- //按键部分可以这样写
- /*while(1)
- {
- static uchar P3=0xff;
- if(P3&&(KEY1==0||KEY2==0||KEY3==0||KEY4==0))
- {
- delay(1000);
- //xiaodou
- P3=0x00;
- if(KEY1==0)
- {delay(1000);hour++;
- if(hour==24)hour=0;Displaypro(); }
- else if(KEY4==0){ delay(1000);second--;
- if(second==0)second=59;Displaypro(); }
- else if(KEY2==0){delay(1000);minute++ ;
- if(minute==59)minute=0;Displaypro(); }
- else if(KEY3==0){ delay(1000);minute--;
- if(minute==0)minute=59;Displaypro(); }
- }
- else if(KEY1==1&&KEY2==1&&KEY3==1&&KEY4==1)P3=0xff;
- }*/
- while(1)
- {
-
- if(minute==14)
- {if(hour==2)
- {P1=0x00; }
- }
- if(!KEY1)
- {
- delay(10000);
- if(!KEY1)
- { delay(20000);delay(20000);
- hour++;
- if(hour==24)hour=0;
- delay(20000);delay(20000);
- Displaypro();
-
- }
- }
- if(!KEY2)
- {
- delay(20000);
- if(!KEY2)
- { delay(20000);delay(20000);
- hour--;
- if(hour==0)hour=23;
- delay(20000);delay(20000);
- Displaypro();
-
- }
- }
- if(!KEY3)
- {
- delay(20000);
- if(!KEY3)
- {delay(20000);delay(20000);
- minute++;
- if(minute==59)minute=0;
- delay(20000);delay(20000);
- Displaypro();
-
- }
- }
- if(!KEY4)
- {
- delay(20000);
- if(!KEY4)
- {delay(20000);delay(20000);
- minute--;
- if(minute==0)minute=59;
- delay(20000);delay(20000);
- Displaypro();
-
- }
- }
- }
-
- }
-
- //T1
- void time1_isr(void) interrupt 3 using 0
- {
- static unsigned char num;
- TH1=0xF8;
- TL1=0xf0;
- P2=0XFF;
- {switch (num)
- {
- case 0:P2=0XFE;P0=~StrTab[num];break;
- case 1:P2=0XFD;P0=~StrTab[num];break;
- case 2:P2=0XFB;P0=~StrTab[num];break;
- case 3:P2=0XF7;P0=~StrTab[num];break;
- case 4:P2=0XEF;P0=~StrTab[num];break;
- case 5:P2=0XBf;P0=~StrTab[num];break;
- case 6:P2=0XDf;P0=~StrTab[num];break;
- case 7:P2=0X7F;P0=~StrTab[num];break;
- default:break;
- }
- }
- num++;
- if(num==8)
- num=0;
- }
-
- //T0
- void tim(void) interrupt 1 using 1
- {
- static unsigned char count;
- TH0=0xd8;
- TL0=0xf0;
- count++;
- switch (count)
- {
- case 0:
- case 20:
- case 40:
- case 60:
- case 80:Displaypro(); break;
- case 50:LED=!LED; break;
- default:break;
- }
-
- if (count==92)//将100改为92可以缩小至最小误差
- {
- count=0;
- second++;
- if(second==60)
- {
- second=0;
- minute++;
- if(minute==60)
- {
- minute=0;
- hour++;
- if(hour==24)
- {
- hour=0;
- }
- }
- }
- }
- }
复制代码
|