程序的目的是实现 八位数码管实时显示时分秒,循环显示周期为“00-00-00 至 23-59-59”。
按键1设置时加 按键2设置时减1 按键3进入设置模式 按键4切换闹钟和时钟显示界面 按键5设置时位选
大部分内容已经实现 在我想加入T1做为定时检测闹钟与时钟数值并在闹钟与时钟数值相等时使得蜂鸣器响1分钟 但T1定时器中断无法正常运作。
单片机源程序如下:
- #include <reg51.h>
- #define uc unsigned char
- #define ui unsigned int
- uc code duan[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
- uc code weima[]={0xfe,0xfd,0xf7,0xef,0xbf,0x7f};
- int zancun1[6];
- int zancun2[6];
- uc second1=0,minute1=0,hour1=0,second2=0,minute2=0,hour2=0;
- ui mode=0,set=0,setw=0,set1=0,setw1=0;
- sbit K1=P3^0;
- sbit K2=P3^1;
- sbit K3=P3^2;
- sbit K4=P3^3;
- sbit K5=P3^4;
- sbit P00=P0^0;
- void chu1(uc x,y,z,o,p,q)
- {
- zancun1[0]=x%10;
- zancun1[1]=x/10;
- zancun1[2]=y%10;
- zancun1[3]=y/10;
- zancun1[4]=z%10;
- zancun1[5]=z/10;
- zancun2[0]=o%10;
- zancun2[1]=o/10;
- zancun2[2]=p%10;
- zancun2[3]=p/10;
- zancun2[4]=q%10;
- zancun2[5]=q/10;
- }
- void save1()
- {
- second1=zancun1[1]*10+zancun1[0];
- minute1=zancun1[3]*10+zancun1[2];
- hour1=zancun1[5]*10+zancun1[4];
- }
- void save2()
- {
- second2=zancun2[1]*10+zancun2[0];
- minute2=zancun2[3]*10+zancun2[2];
- hour2=zancun2[5]*10+zancun2[4];
- }
- ui chouse(int a,int b)
- {
- switch(a)
- {
- case 0: if(b<=0||b>=10){b=0;}zancun1[0]=b;break;
- case 1: if(b<=0||b>=6){b=0;}zancun1[1]=b;break;
- case 2: if(b<=0||b>=10){b=0;}zancun1[2]=b;break;
- case 3: if(b<=0||b>=6){b=0;}zancun1[3]=b;break;
- case 4: if(b<=0||b>=5){b=0;}zancun1[4]=b;break;
- case 5: if(b<=0||b>=3){b=0;}zancun1[5]=b;break;
- }
- return (set=b);
- }
- ui chouse2(int a,int b)
- {
- switch(a)
- {
- case 0: if(b<=0||b>=10){b=0;}zancun2[0]=b;break;
- case 1: if(b<=0||b>=6){b=0;}zancun2[1]=b;break;
- case 2: if(b<=0||b>=10){b=0;}zancun2[2]=b;break;
- case 3: if(b<=0||b>=6){b=0;}zancun2[3]=b;break;
- case 4: if(b<=0||b>=5){b=0;}zancun2[4]=b;break;
- case 5: if(b<=0||b>=3){b=0;}zancun2[5]=b;break;
- }
- return (set1=b);
- }
- void delay(unsigned int y)
- {
- unsigned char j;
- unsigned int i;
- for(i=0;i<y;i++)
- for(j=0;j<120;j++);
- }
- void shizhong(ui o,p,q)
- {
- P1=weima[0];
- P2=duan[o%10];
- delay(8);
- P1=weima[1];
- P2=duan[o/10];
- delay(8);
- P1=weima[2];
- P2=duan[p%10];
- delay(8);
- P1=weima[3];
- P2=duan[p/10];
- delay(8);
- P1=weima[4];
- P2=duan[q%10];
- delay(8);
- P1=weima[5];
- P2=duan[q/10];
- delay(8);
- P1=0xdb;
- P2=0x40;
- delay(8);
- P1=0xff;
- }
- void time0s()
- {
- TMOD|=0X01;
- TH0=0X3c;
- TL0=0Xb0;
- ET0=1;
- TR0=1;
- PT0=1;
- PT1=0;
- }
- void time1sON()
- {
- TMOD|=0X10;
- TH1=0X3c;
- TL1=0Xb0;
- ET1=1;
- TR1=1;
- }
- void Time0() interrupt 1
- {
- static ui i=0;
- TH0=0X3C;
- TL0=0Xb0;
- i++;
- if(i==20)
- {
- i=0;
- second1++;
- if(second1==60)
- {
- second1=0;
- minute1++;
- }
- if(minute1==60)
- {
- minute1=0;
- hour1++;
- }
- if(hour1==24)
- {
- hour1=0;
- }
- }
- }
- void Time1() interrupt 3
- {
- static ui i=0,r=0;
- TH1=0X3C;
- TL1=0Xb0;
- if(i==20)
- {
- r++;
- if(r==60)
- {
- if(minute1==minute2&&hour1==hour2)
- {
- for(r=0;r<10;r++)
- {
- P00=~P00;
- delay(100);
- r=0;
- }
- }
- }
- }
- }
- void key()
- {
- EA=1;
- EX1=1;
- IT1=1;
- EX0=1;
- IT0=1;
- }
- void key3() interrupt 0
- {
- EX1=0;
- chu1(second1,minute1,hour1,second2,minute2,hour2);
- while(1)
- {
- if(mode==0)
- {
- set=zancun1[setw];
- P1=weima[setw];
- P2=duan[set];
- delay(400);
- P1=0xff;
- delay(400);
- if(K1==0){delay(50);set++;chouse(setw,set);}
- if(K2==0){delay(50);set--;chouse(setw,set);}
- if(K5==0){delay(50);setw++;set=0;if(setw>=6){setw=0;}}
- if(K3==0){delay(50);save1();set=0;setw=0;break;}
- }
- else
- {
- set1=zancun2[setw1];
- P1=weima[setw1];
- P2=duan[set1];
- delay(400);
- P1=0xff;
- delay(400);
- if(K1==0){delay(50);set1++;chouse2(setw1,set1);}
- if(K2==0){delay(50);set1--;chouse2(setw1,set1);}
- if(K5==0){delay(50);setw1++;set1=0;if(setw1>=6){setw1=0;}}
- if(K3==0){delay(50);save2();set1=0;setw1=0;break;}
- }
- }
- IE0=0;
- IE1=0;
- EX1=1;
- }
-
- void key4() interrupt 2
- {
- mode=~mode;
- IE1=0;
- }
- void main()
- {
- key();
- time0s();
- time1sON();
- while(1)
- {
- if(mode==0)
- {
- shizhong(second1,minute1,hour1);
-
- }
- if(mode!=0)
- {
- shizhong(second2,minute2,hour2);
- }
- }
- }
复制代码
|