|
- #include <reg52.h>
- sbit DGT0=P2^3;//數(shù)碼管控制位
- sbit DGT1=P2^2;
- sbit DGT2=P2^1;
- sbit DGT3=P2^0;
- sbit KEY0=P2^4;
- sbit BEEP=P2^5;
- sbit KEY1=P2^6;
- sbit KEY2=P2^7;
- unsigned char code LedChar[]={
- 0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90};
- unsigned char LedBuff[4]={0xF9,0xC0,0xC0,0xC0};//顯示數(shù)據(jù)儲(chǔ)存空間
- bit sign,flag,B_flag;//工作表示
- unsigned int cnt=0;//計(jì)數(shù)器
- unsigned char i=0;//循環(huán)變量
- unsigned int sec,sec1=500;//定義變量,倒計(jì)時(shí)起始數(shù)據(jù)
- void main()
- {
- unsigned int t1,t2,t3;
- TMOD=0x01;//MOV TMOD,#01H//12M晶振產(chǎn)生1ms定時(shí)
- TH0=0xFC;//定時(shí)器初置
- TL0=0x18;
- TR0=1;//啟動(dòng)定時(shí)計(jì)數(shù)
- EA=1;//允許中斷
- ET0=1;//允許定時(shí)器0中斷
- while(1)
- {
- if(!KEY0 && !flag)
- {
- flag=1;//啟動(dòng)倒計(jì)時(shí)并自鎖按鍵
- sec=sec1;//倒計(jì)時(shí)賦初值
- cnt=0;//定時(shí)器計(jì)數(shù)復(fù)位
- t2=0;//蜂鳴器計(jì)數(shù)復(fù)位
- BEEP=1;//蜂鳴器復(fù)位
- B_flag=0;//蜂鳴器工作標(biāo)志復(fù)位
- }
- if(!KEY1 || !KEY2)//預(yù)置時(shí)間
- {
- if(++t1>=200 && !flag)
- {
- t1=0;
- sign=1;
- if(!KEY1){if(sec1<1000)sec1++;}
- if(!KEY2){if(sec1>10)sec1--;}
- t3=3000;//顯示切換保持時(shí)間
- }
- }
- else
- {
- if(sign)
- {
- if(--t3==0)sign=0;
- }
- t1=0;
- }
- if(sign)//預(yù)置顯存
- {
- LedBuff[0]=LedChar[sec1%10];//計(jì)數(shù)值個(gè)位存入
- LedBuff[1]=LedChar[sec1%100/10];//十位
- LedBuff[2]=LedChar[sec1%1000/100];//百位
- LedBuff[3]=LedChar[sec1/1000];//千位
- }
- else//正常顯存
- {
- LedBuff[0]=LedChar[sec%10];//計(jì)數(shù)值個(gè)位存入
- LedBuff[1]=LedChar[sec%100/10];//十位
- LedBuff[2]=LedChar[sec%1000/100];//百位
- LedBuff[3]=LedChar[sec/1000];//千位
- }
- if(B_flag)//判斷蜂鳴器允許標(biāo)志
- {
- BEEP=0;//蜂鳴器工作
- if(++t2>=5000)//蜂鳴器工作時(shí)長
- {
- t2=0;//蜂鳴器計(jì)數(shù)復(fù)位
- BEEP=1;//蜂鳴器復(fù)位
- B_flag=0;//蜂鳴器工作標(biāo)志復(fù)位
- }
- }
- }
- }
- void InterruptTime0() interrupt 1//定時(shí)中斷
- {
- TH0=0xFC;//重賦初始值
- TL0=0x18;
- if(flag)
- {
- cnt++;//計(jì)數(shù)器加1
- if (cnt>=1000)//時(shí)間
- {
- cnt=0;//準(zhǔn)備下一秒記時(shí)
- sec--;
- if(sec==0)
- {
- flag=0;//停止倒計(jì)時(shí)
- B_flag=1;//允許蜂鳴器工作
- }
- }
- }
- P0=0xFF;//熄滅,P0高電平
- switch (i)
- {
- case 0:DGT3=0;DGT2=0;DGT1=0;DGT0=1;i++;P0=LedBuff[0];break;//個(gè)位
- case 1:DGT3=0;DGT2=0;DGT1=1;DGT0=0;i++;P0=LedBuff[1];break;//十位
- case 2:DGT3=0;DGT2=1;DGT1=0;DGT0=0;i++;P0=LedBuff[2];break;//百位
- case 3:DGT3=1;DGT2=0;DGT1=0;DGT0=0;i=0;P0=LedBuff[3];break;//千位
- }
- }
復(fù)制代碼
|
|