#include "at89x52.h"
#include "intrins.h" //_nop_();延时函数用
#define uchar unsigned char
#define uint unsigned int
uchar code discode[10]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};
//共阳LED段码表 "0" "1" "2" "3" "4" "5" "6" "7" "8" "9"
uint disdata[2]={0,0};
/*********************延迟1ms**********************/
void delayms(uint ms)
// 延时子程序
{
unsigned char i;
while(ms--)
{
for(i = 0; i <105; i++);
}
}
/********************显示函数*********************/
void scan(uint time)
{ uint t,s;
if(time>=10)t=time/10 ;else t=0;
s=time-t*10;
disdata[0]=t;
disdata[1]=s;
P3=0xff;
P3_6=0;
P0=discode[disdata[0]];
delayms(5);
P3=0xff;
P3_7=0;
P0=discode[disdata[1]];
delayms(5);
P3=0xff;
}
/*******************显示+延迟0.1秒************************/
void delay_scan(uint t,uint codes)
{ t=t*10;
while(t--){
scan(codes);
}
}
/*********************声音函数**********************/
void beep(uint t,uint s)
{ if(s==0)s=360;
else if(s==1) s=90;
else if(s==2) s=30;
else s=300;
while(t--)
{
P2_1=0 ;
delayms(s);
P2_1=1;
delayms(s);
}
delayms(s/6);
}
/****************主函数************************/
main()
{
uint time=0;
P1=0xFF;
while(1)
{
scan(time);
if(P1_0==0){
delayms(5);
if(P1_0==0){
if(time<60) time++;else beep(1,1);
scan(time);
delay_scan(1,time);
}
}
//----
if(P1_1==0){
delayms(5);
if(P1_1==0){
if(time>0) time--;else beep(1,1);
scan(time);
delay_scan(1,time);
}
}
//----
if(P1_2==0)
{
delayms(5);
if(P1_2==0 && time>0)
{ P2_0=1;
delay_scan(3,time);
while(time--)
{ beep(1,2);
P2_0=0;
delay_scan(10,time);
}
P2_0=1;
beep(1,1);
time=0;
}
else
beep(1,0);
time=0;
}
//----
if(P1_3==0){
delay_scan(7,time);
if(P1_3==0){
beep(2,1);
P2_0=~P2_0;
delay_scan(10,time);
}
}
}
}