其硬件之利用到了一个38译码器,其他的看引脚定义即可推断出
实现的按键调节定时时间,启动后进行倒计时,当倒计时完成后,蜂鸣器报警
#include<reg52.h>
#include<intrins.h>
//引脚定义,使用情况四个按键,三个译码器端口连接
sbit beep = P1^5;
sbit key1 = P1^1;
sbit key2 = P1^2;
sbit key3 = P1^3;
sbit key4 = P1^4;
sbit LSA=P2^2;
sbit LSB=P2^3;
sbit LSC=P2^4;
sbit AD = P3^2;
unsigned char code duanma[10]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
unsigned char temp[8];
char hour; minute; second, num, t, i;
void delay(unsigned int t);
void initial();
void delay20ms();
void keyscan();
void caifen();
void display();
void main()
{
initial();
while(1)
{
keyscan();
caifen();
display();
if((hour==0)&&(minute==0)&&(second ==0))
{TR0=0;
beep =1; //此处蜂鸣器并没有正常,可能是电流问题?
delay(10);
}
}
}
//对定时器进行配置初始化
void initial()
{
TMOD = 0x01;
TH0 = (65536 - 2000)/256;
TL0 = (65536 - 2000)%256;
EA = 1;
TR0 = 1;
ET0 = 1;
hour = 1;
minute = 0;
second = 1;
beep = 0;
}
//时间的标准定义
void time0(void) interrupt 1
{
TH0 = (65536 - 2000)/256;
TL0 = (65536 - 2000)%256;
num ++;
if (num == 500)
{
num = 0;
second --;
if (second == -1)
{
second = 9;
minute --;
if (minute == -1)
{
minute = 9;
hour --;
if (hour == -1)
{
hour = 0;
}
}
}
}
}
//接收按键的扫描
void keyscan()
{
if(key1 == 0)
{
delay20ms();
while(key1 == 0);
TR0 = 0;
t ++;
}
switch(t)
{
case 1:
if(key2 == 0)
{
delay20ms();
if(key2 == 0)
{
while(key2 == 0);
hour ++;
if(hour == 10)
{
hour = 0;
}
}
}
if(key3 == 0)
{
delay20ms();
if(key3 == 0)
{
while(key3 == 0);
hour --;
if(hour == -1)
{
hour =9;
}
}
}
break;
case 2:
if(key2 == 0)
{
delay20ms();
if(key2 == 0)
{
while(key2 == 0);
minute ++;
if(minute == 10)
{
minute = 0;
}
}
}
if(key3 == 0)
{
delay20ms();
if(key3 == 0)
{
while(key3 == 0);
minute --;
if(minute == -1)
{
minute = 9;
}
}
}
break;
case 3:
TR0 = 1;
t = 0;
break;
default:
break;
}
}
//对数据进行简单的处理
void caifen()
{
temp[0]=duanma[hour%10];
temp[1]=duanma[minute%10];
temp[2]=duanma[second%10];
}
//数码管进行显示
void display()
{
unsigned int i;
for(i=0;i<3;i++)
{
switch(i)
{
case(0):
LSA=0;LSB=0;LSC=0; break;
case(1):
LSA=1;LSB=0;LSC=0; break;
case(2):
LSA=0;LSB=1;LSC=0; break;
default: break;
}
P0=temp[i];
delay(100);
P0=0x00;
}
}
//20ms
void delay20ms(void)
{
unsigned char a,b;
for(b=215;b>0;b--)
for(a=45;a>0;a--);
_nop_;
_nop_;
}
void delay(unsigned int i)
{
while (i --);
}
译码器