- #include<reg52.h>
- unsigned char code LedChar[10] = {0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F};
- unsigned int cnt = 0;
- #define uchar unsigned char//宏定义
- #define uint unsigned int//宏定义
- bit flag=0;
- uchar b=0;//秒计数
- unsigned char i = 0;
- //unsigned char s1;
- sbit k1=P1^1;
- sbit k2=P1^4;
- sbit L1=P3^4;
- void Timer0Init(void);
- void key_scan();
- void delay(uint z)
- {
- uint x,y;
- for(x = z; x > 0; x--)
- for(y = 114; y > 0 ; y--);
- }
- void main()
- {
- P0=0X00;
- Timer0Init();
- // display(0);
- while(1)
- {
- key_scan();
- if(cnt>1000)
- {
- cnt=0;
- b++;
- }
- if(L1==0)
- {
- ET0=1;
- P0=LedChar[b];
- }
- if(b>9)
- {
- L1=1;
- b=0;
- ET0=0;
- }
- }
- }
- void key_scan()
- {
- if(k1==0) //夜晚
- {
- if(k1==0)
- {
- if(k2==0) //有声音
- {
- delay(10);//延时10毫秒
- if(k2==0)
- {
- L1=0;
- }
- }
- }
- }
- }
- void InterruptTime0() interrupt 1
- {
- TL0 = 0x18; //设置定时初值
- TH0 = 0xFC; //设置定时初值
- cnt++;
- }
- void Timer0Init(void) //1毫秒@12.000MHz
- {
- P1&= 0x7F; //定时器时钟12T模式
- TMOD &= 0xF0; //设置定时器模式
- TMOD |= 0x01; //设置定时器模式
- TL0 = 0x18; //设置定时初值
- TH0 = 0xFC; //设置定时初值
- TF0 = 0; //清除TF0标志
- TR0 = 1; //定时器0开始计时
- EA=1;
- ET0=0;
- }
复制代码 |