樓主的程序源碼:
- #include<reg52.h>
- #define uint unsigned int
- #define uchar unsigned char
- sbit w1=P2^0;
- sbit w2=P2^1;
- sbit w3=P2^2;
- sbit w4=P2^3;
- sbit key1=P3^2;
- sbit Mod_key=P3^3;
- sbit Up_key=P3^4;
- sbit Down_key=P3^5;
- sbit D1=P2^5;
- uchar flag,alarm_flag,Buzzer_flag,back,aa,bb,minutes,seconds,hours,hours_alarm,minutes_alarm,t0,Hourshi,Hourge,Minuteshi,Minutege,Secondshi,Secondge;
- uchar code table[]={
- 0xc0,0xf9,0xa4,0xb0,0x99,
- 0x92,0x82,0xf8,0x80,0x90,
- 0xbf,0xff};
- uchar code tablehours[]={ //小時的個位單獨一個數組,因為需要一直保持顯示小數點
- 0x40,0x79,0x24,0x30,0x19,
- 0x12,0x02,0x78,0x00,0x10};
- void init(); //初始化函數
- void delay(uint z); //延時函數
- void display(uchar hours,uchar minutes); //正常顯示函數
- void display_seconds(uchar aa,uchar Secondshi,uchar Secondge,uchar bb); //顯示秒鐘函數
- void keyscan(); //鍵盤掃描函數
- void Buzzer(); //蜂鳴器開關函數
- void main()
- {
- init();
- while(1)
- {
- keyscan();
- Buzzer();
- if(flag==1)
- display_seconds(10,Secondshi,Secondge,10);
- if(alarm_flag==1)
- display(hours_alarm,minutes_alarm);
- if(flag!=1&&alarm_flag!=1)
- display(hours,minutes);
- }
- }
- void init() //初始化函數
- {
- flag=0;
- alarm_flag=0;
- Buzzer_flag=1; //蜂鳴器初始化時默認關閉
- back=0;
- hours_alarm=0;
- minutes_alarm=0;
- hours=0;
- minutes=0;
- seconds=0;
- Hourshi=0;
- Hourge=0;
- Minuteshi=0;
- Minutege=0;
- Secondshi=0;
- Secondge=0;
- t0=0;
- TMOD=0x01;
- TH0=(65536-50000)/256;
- TL0=(65536-50000)%256;
- EA=1;
- ET0=1;
- TR0=1;
- }
- void delay(uint z) //延時函數
- {
- uint x,y;
- for(x=z;x>0;x--)
- for(y=110;y>0;y--);
- }
- void display(uchar hours,uchar minutes) //正常顯示函數
- {
- P0=0xff;
- w1=0;
- P0=table[hours/10];
- w1=1;
- delay(1);
- P0=0xff;
- w2=0;
- P0=tablehours[hours%10];
- w2=1;
- delay(1);
- P0=0xff;
- w3=0;
- P0=table[minutes/10];
- w3=1;
- delay(1);
- P0=0xff;
- w4=0;
- P0=table[minutes%10];
- w4=1;
- delay(1);
- }
- void display_seconds(uchar aa,uchar Secondshi,uchar Secondge,uchar bb) //顯示秒鐘函數
- {
- P0=0xff;
- w1=0;
- P0=table[aa];
- w1=1;
- delay(1);
- P0=0xff;
- w2=0;
- P0=table[Secondshi];
- w2=1;
- delay(1);
- P0=0xff;
- w3=0;
- P0=table[Secondge];
- w3=1;
- delay(1);
- P0=0xff;
- w4=0;
- P0=table[bb];
- w4=1;
- delay(1);
- }
- void keyscan() //鍵盤掃描函數
- {
- if(key1==0)
- flag=1;
- else
- flag=0;
- if(Up_key==0)
- alarm_flag=1;
- else
- alarm_flag=0;
- if(Down_key==0)
- {
- while(!Down_key);
- Buzzer_flag=~Buzzer_flag;
- }
-
- if(Mod_key==0)
- {
- while(!Mod_key);
- while(1)
- {
- display(hours,minutes);
- delay(50);
- if(back==1)
- {
- back=0;
- break;
- }
- if(key1==0)
- {
- while(!key1);
- while(1)
- {
- display(hours,minutes);
- if(Up_key==0)
- {
- while(!Up_key);
- minutes++;
- if(minutes==60)
- minutes=0;
- }
-
- if(Down_key==0)
- {
- while(!Down_key);
- minutes--;
- if(minutes==-1)
- minutes=59;
- }
- if(back==1)
- {
- back==1;
- break;
- }
-
- if(Mod_key==0)
- {
- while(!Mod_key);
- while(1)
- {
- display(hours,minutes);
- if(Up_key==0)
- {
- while(!Up_key);
- hours++;
- if(hours==24)
- hours=0;
-
- }
- if(Down_key==0)
- {
- while(!Down_key);
- hours--;
- if(hours==-1)
- hours=23;
- }
- if(key1==0)
- {
- while(!key1);
- back=1;
- break;
- }
- }
- }
- }
- }
- if(Mod_key==0)
- {
- while(!Mod_key);
- while(1)
- {
- display(hours_alarm,minutes_alarm);
- if(Up_key==0)
- {
- while(!Up_key);
- minutes_alarm++;
- if(minutes_alarm==60)
- minutes_alarm=0;
- }
-
- if(Down_key==0)
- {
- while(!Down_key)
- minutes_alarm--;
- if(minutes_alarm==-1)
- minutes_alarm=59;
- }
- if(back==1)
- {
- back==1;
- break;
- }
-
- if(key1==0)
- {
- while(!key1);
- while(1)
- {
- display(hours_alarm,minutes_alarm);
- if(Up_key==0)
- {
- while(!Up_key);
- hours_alarm++;
- if(hours_alarm==24)
- hours_alarm=0;
- }
- if(Down_key==0)
- {
- while(!Down_key);
- hours_alarm--;
- if(hours_alarm==-1)
- hours_alarm=23;
- }
- if(Mod_key==0)
- {
- while(!Mod_key);
- back=1;
- Buzzer_flag=0; //設定鬧鐘時間后自動開啟蜂鳴器
- break;
- }
-
- }
- }
- }
- }
- }
- }
- }
- void Buzzer() //蜂鳴器開關函數
- {
- if(hours==hours_alarm&&minutes==minutes_alarm)
- {
- if(Buzzer_flag==0)
- D1=0;
- else
- D1=1;
- }
- else
- D1=1;
- }
-
- void timer0() interrupt 1
- {
- TH0=(65536-50000)/256;
- TL0=(65536-50000)%256;
- t0++;
- if(t0==20)
- {
- t0=0;
- seconds++;
- Secondshi=seconds/10;
- Secondge=seconds%10;
- if(seconds==60)
- {
- seconds=0;
- minutes++;
- Minuteshi=minutes/10;
- Minutege=minutes%10;
- if(minutes==60)
- {
- minutes=0;
- hours++;
- Hourshi=hours/10;
- Hourge=hours%10;
- if(hours==24)
- hours==0;
- }
-
- }
-
- }
- }
復制代碼 |