我有一个通过按键进行时间选择,并倒计时的程序,你参考一下吧。功能:K1时间加,K2时间减,K3时间回零,K4进行倒计时
电路如图所示:
- /*
- 使用定时器中断进行的倒计时程序。
- */
- #include <STC89C5xRC.H>
- #include <intrins.h>
- #define SMG P0
- sbit K1=P3^4;
- sbit K2=P3^5;
- sbit K3=P3^6;
- sbit K4=P3^7;
- sbit DUAN=P2^6;
- sbit WEI=P2^7;
- sbit FMQ=P2^3;
- unsigned char ZF[]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,
- 0x6F,0x77,0x7C,0x39,0x5E,0x79,0x71};
- unsigned char Wei[]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};
- unsigned char PLAY[8]={0,0,0,0,0,0,0,0};
- unsigned char n=0,TT=0;
- void Delay1ms() //@11.0592MHz
- {
- unsigned char i, j;
- _nop_();
- i = 2;
- j = 199;
- do
- {
- while (--j);
- } while (--i);
- }
- void Delay10ms() //@11.0592MHz
- {
- unsigned char i, j;
- i = 18;
- j = 235;
- do
- {
- while (--j);
- } while (--i);
- }
- void Delay100ms() //@11.0592MHz
- {
- unsigned char i, j, k;
- _nop_();
- _nop_();
- i = 5;
- j = 52;
- k = 195;
- do
- {
- do
- {
- while (--k);
- } while (--j);
- } while (--i);
- }
- void Timer0Init(void) //50毫秒@11.0592MHz,定时器设置
- {
- AUXR &= 0x7F;
- TMOD &= 0xF0;
- TMOD |= 0x01;
- TL0 = 0x00;
- TH0 = 0x4C;
- EA = 1;
- ET0 = 1;
- TF0 = 0;
- TR0 = 0;
- }
- void XS(void)
- {
- unsigned char i;
- for (i=0;i<8;i++)
- {
- SMG=Wei[i];
- WEI=1;
- WEI=0;
- SMG=PLAY[i];
- DUAN=1;
- DUAN=0;
- Delay1ms();
- SMG=0x00;
- DUAN=1;
- DUAN=0;
- }
- }
- void DISPLAY(void)
- {
- PLAY[5]=ZF[n/100];
- PLAY[6]=ZF[(n%100)/10];
- PLAY[7]=ZF[(n%10)/1];
- }
- void main(void)
- {
- unsigned char m;
- bit KS=0;
- Timer0Init();
- while(1)
- {
- if (K1==0)
- {
- Delay10ms();
- if (K1==0)
- {
- n=n+1;
- }
- for(m=0;((m<50)&&(K1==0));m++)
- {
- Delay10ms();
- }
- }
- if (K2==0)
- {
- Delay10ms();
- if (K2==0)
- {
- n=n-1;
- }
- for(m=0;((m<50)&&(K2==0));m++)
- {
- Delay10ms();
- }
- }
- if (K3==0)
- {
- Delay10ms();
- if (K3==0)
- {
- n=0;
- }
- for(m=0;((m<50)&&(K3==0));m++)
- {
- Delay10ms();
- }
- }
- if (K4==0)
- {
- Delay10ms();
- if (K4==0)
- {
- if (n!=0)
- {
- TR0=1;
- }
- }
- for(m=0;((m<50)&&(K4==0));m++)
- {
- Delay10ms();
- }
- }
- DISPLAY();
- XS();
- }
- }
- void Time0 (void) interrupt 1
- {
- unsigned char m;
- TL0 = 0x00;
- TH0 = 0x4C;
- TT++;
- if (TT==20)
- {
- n--;
- if (n==0)
- {
- for (m=0;m<100;m++)
- {
- DISPLAY();
- XS();
- }
- TR0=0;
- FMQ=0;
- Delay100ms();
- FMQ=1;
- }
- TT=0;
- }
- }
复制代码
|