|
#include <reg52.h>
#define uchar unsigned char
#define uint unsigned int
uint bai,shi,ge,aa,bb,x,y,z;
int miao=00,fen=0;
uchar i=0;
uchar code table[]={ //数码管0-9
0xc0,0xf9,0xa4,0xb0,
0x99,0x92,0x82,0xf8,
0x80,0x90};
sbit key0=P1^0; //暂停开始
sbit key1=P1^1; //分钟
sbit key2=P1^2; //秒
sbit been=P1^5; //声光报警
sbit led=P1^4;
void delay(uint z) //延时函数1ms
{
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
void init() //定时器初始化函数
{
TMOD=0x01;
TH0=(65536-9216)/256;
TL0=(65536-9216)%256;
EA=1;
ET0=1;
}
void display() //数码管显示函数
{
P2=0xfd;
P0=table[bai]-0x80; //分钟加点
delay(1);
P0=0xff;
P2=0xfb;
P0=table[shi]; //秒
delay(1);
P0=0xff;
P2=0xf7;
P0=table[ge]; //秒
delay(1);
P0=0xff;
}
void keyscan() //键盘扫描
{
if(key0==0)
{
delay(5);
if(key0==0)
{
while(!key0);
i++;
if(i==3)
{
i=1;
}
if(i==1)
{
TR0=1;
}
if(i==2)
{
TR0=0;
}
}
}
if(key1==0&&TR0==0) //分钟设置按键
{
delay(5);
if(key1==0&&TR0==0)
{
while(!key1);
fen--;
bai=fen;
display();
if(fen==0)
{
fen=9;
bai=fen;
display();
}
}
}
if(key2==0&&TR0==0) //秒设置按键
{
delay(5);
if(key2==0&&TR0==0)
{
while(!key2);
miao--;
shi=miao/10;
ge=miao%10;
display();
if(miao==0)
{
miao=59;
shi=miao/10;
ge=miao%10;
display();
}
}
}
}
void main()
{
init();
miao=60,fen=9;
while(1)
{
keyscan();
display();
if(bai==0&&shi==0&&ge==0&&TR0==1) //声光报警
{
TR0=0;
led=0;
for(i = 0; i < 100; i ++)
{
for(x=2000;x>0;x--)
been=~been;
}
led=1;
}
}
}
void timer0() interrupt 1 //定时器
{
TH0=(65536-10000)/256;
TL0=(65536-10000)%256;
aa++;
if(aa==100)
{
aa=0;
bb++;
if(bb>0)
{
miao--;
if(miao==-1)
{
miao=59;
fen--;
if(fen==-1)
{
fen=9;
}
}
bai=fen;
shi=miao/10;
ge=miao%10;
}
}
}
|
|