基于AT89C52的答辩倒计时。四个按键分别控制倒计时开始,暂停,时间加和减。剩下30S时蜂鸣器响,倒计时结束蜂鸣器响。
#include <REGX52.H>unsigned char min=1;
unsigned char sec=00;
sbit KEY1=P3^1;
sbit KEY2=P3^0;
sbit KEY3=P3^2;
sbit KEY4=P3^3;
sbit LSA=P2^2;
sbit LSB=P2^3;
sbit LSC=P2^4;
sbit bee=P2^5;
sbit led=P2^6;
unsigned char table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x40};void delay(unsigned int z)
{unsigned int x,y;for(x=z;x>0;x--)for(y=114;y>0;y--);
}void DSQ_INIT()
{TMOD=0x11;//定时器方式为方式一EA=1;//开总中断ET0=1;//开T0中断TR0=0;//定时器0计时TF0=0;//清除TF0标志TH0=0x3c; //设置初值,定时时间为50msTL0=0xb0;
}void Timer()interrupt 1
{static int i=0;TL0 = 0xb0; TH0 = 0x3c; i++;if(i>19){i=0;if(min>0||sec>0){ if(sec==0){sec=59;sec++;}if(sec>0){sec--;}if(sec==59){min--;}}}
}void Show_time()
{P0=table[min/10%10];LSA=0;LSB=0;LSC=1;delay(1); //数码管位选P0=table[min%10];LSA=1;LSB=1;LSC=0;delay(1);P0=table[10];LSA=0;LSB=1;LSC=0;delay(1);P0=table[sec/10%10];LSA=1;LSB=0;LSC=0;delay(1);P0=table[sec%10];LSA=0;LSB=0;LSC=0;delay(1);P0=0x00;
}void Sec_ADD()
{if(KEY4==0){delay(20);while(!KEY4);Show_time();sec++;if(sec>59){min++;sec=0;}}
}void Sec_SUB()
{if(KEY3==0){delay(20);while(!KEY3); //判断按键是否松手Show_time();if(min>0||sec>0){sec--;if(sec==0){min--;sec=59;}} }
}void Pause()
{if(KEY2==0){delay(10);if(KEY2==0){TR0=0;}}if(min==0&&sec==0){TR0=0;bee=~bee;led=0;}
}void Bee()
{if(min==0&&sec==30){bee=~bee;}
}main()
{DSQ_INIT();while(1){Show_time();if(KEY1==0){if(min>0||sec>0){TR0=1;}delay(5);while(KEY1){Show_time();Pause();Bee();Sec_ADD(); Sec_SUB();}}Pause();Bee();Sec_SUB();Sec_ADD(); }
}