#include
#define uchar unsigned char
#define uint unsigned int
uchar SETFlag;//时钟状态标志位
uchar LEDBuffer[6];//显示数据缓存数组
uchar ALMFlag;//闹钟开关标志位
uchar Beepflag;//蜂鸣器开关标志位
uchar code LEDBITDATA[]={0xdf,0xef,0xf7,0xfb,0xfd,0xfe};//位选
uchar code LEDDATA[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x00,0x71,0x79};//段选
uchar Second,Minute,Hour,second_tick,Hourrom,Minuterom,Time;
***it SET_KEY=P3^5;//模式键
***it UP_KEY=P3^6;//加计数键
***it DOWN_KEY=P3^7;//减计数键
***it ALM_KEY=P3^4;//显示定时时间的按键
***it Beep=P2^3;
***it dula=P2^6;
***it wela=P2^7;
void init()//初始化函数
{
TMOD=0x01;//确定定时器工作方式
TH0=(65536-45872)/256;//装初值,每次计时5ms
TL0=(65536-45872)%256;
EA=1;
ET0=1;
TR0=1;
}
void delay(uint xms)
{
uint x,y;
for(x=xms;x>0;x--)
for(y=110;y>0;y--);
}
void keyscan()
{
uchar i;//i为缓冲数组位数标志
char num;//临时数字,储存数组合并数
if(SET_KEY==0)
{
delay(10);
if(SET_KEY==0)
{
SETFlag++;
if(SETFlag==7)SETFlag=0;//返回正常模式
if(SETFlag==1)i=4;
if(SETFlag==2)i=2;
if(SETFlag==3)i=0;//前三位调时钟,后三位调闹钟
if(SETFlag==4)i=4;
if(SETFlag==5)i=2;
if(SETFlag==6)i=0;
/*首先实现时钟的秒,分,时调整*/
}
}
while(SET_KEY==0);
if((SETFlag!=0)&&(UP_KEY==0))
{
delay(10);
if(UP_KEY==0)
{
num=(LEDBuffer[i+1]*10+LEDBuffer[i]);
num++;
if(num>=60)num=0;
if((num>=24)&&(SETFlag==1))num=0;//在调整小时的情况下
switch(SETFlag)//把调整值写回
{
case 0:;break;//case后面需要加break!!!!
case 1:Hour=num;
break;
case 2:Minute=num;
break;
case 3:Second=num;
break;
case 4:Hourrom=num;
break;
case 5:Minuterom=num;
break;
case 6:ALMFlag=!ALMFlag;
break;
}
}
}
while(UP_KEY==0);
if((SETFlag!=0)&&(DOWN_KEY==0))
{
delay(10);
if(DOWN_KEY==0)
{
num=(LEDBuffer[i+1]*10+LEDBuffer[i]);
num--;
if((num<=0)&&(SETFlag==1))num=23;//此处需要把这个判断放在前面!!!!!
if(num<=0)num=59;
switch(SETFlag)//把调整值写回
{
case 0:;break;
case 1:Hour=num;
break;
case 2:Minute=num;
break;
case 3:Second=num;
break;
case 4:Hourrom=num;
break;
case 5:Minuterom=num;
break;
case 6:ALMFlag=!ALMFlag;
break;
}
}
}
while(DOWN_KEY==0);
if(ALM_KEY==0)
{
SETFlag=0;
if(ALMFlag==0)LEDBuffer[0]=11;//显示F
else LEDBuffer[0]=12;//显示E
LEDBuffer[1]=10;
LEDBuffer[2]=Minuterom%10;
LEDBuffer[3]=Minuterom/10;
LEDBuffer[4]=Hourrom%10;
LEDBuffer[5]=Hourrom/10;
if(ALMFlag==1)//闹铃响时关闭闹铃
{
Beep=1;
Beepflag=0;
}
}
}
void display()
{
uchar LEDScanCount,i;//L为扫描标志位,i是闪烁控制位
P0=0xff;
if((UP_KEY==0)||(DOWN_KEY==0)) i=0x00;
else i=0xff*second_tick;//闪烁成功
wela=1;
P0=LEDBITDATA[LEDScanCount];
wela=0;
P0=0xff;
dula=1;
switch(SETFlag)
{
case 0:P0=LEDDATA[LEDBuffer[LEDScanCount]];
break;
case 1:if(LEDScanCount>=4)
P0=i&LEDDATA[LEDBuffer[LEDScanCount]];
else P0=LEDDATA[LEDBuffer[LEDScanCount]];
break;
case 2:if((LEDScanCount==2)||(LEDScanCount==3))
P0=i&LEDDATA[LEDBuffer[LEDScanCount]];
else P0=LEDDATA[LEDBuffer[LEDScanCount]];
break;
case 3:if(LEDScanCount<=1)
P0=i&LEDDATA[LEDBuffer[LEDScanCount]];
else P0=LEDDATA[LEDBuffer[LEDScanCount]];
break;
case 4:if(LEDScanCount>=4)
P0=i&LEDDATA[LEDBuffer[LEDScanCount]];
else P0=LEDDATA[LEDBuffer[LEDScanCount]];
break;
case 5:if((LEDScanCount==2)||(LEDScanCount==3))
P0=i&LEDDATA[LEDBuffer[LEDScanCount]];
else P0=LEDDATA[LEDBuffer[LEDScanCount]];
break;
case 6:if(LEDScanCount<=1)
P0=i&LEDDATA[LEDBuffer[LEDScanCount]];
else P0=LEDDATA[LEDBuffer[LEDScanCount]];
break;
}
dula=0;
delay(1);
LEDScanCount++;
if(LEDScanCount==6)LEDScanCount=0;
}
void main()
{
init();
while(1)
{
keyscan();
display();
if(ALMFlag==1)
{
if(Minuterom!=Minute)Beepflag=1;
if((Hour==Hourrom)&&(Minuterom==Minute)&&(Beepflag==1))Beep=0;//时分都相同,并且闹铃打开的时候,蜂鸣器响
}
if(SETFlag!=0)//十秒不操作,自动返回
{
if(Time>=10){SETFlag=0;Time=0;}
if((SET_KEY==0)||(UP_KEY==0)||(DOWN_KEY==0))Time=0;//任意键有操作则放弃计时
}
}
}
void time0() interrupt 1
{
uint timecount,Secondcount;
TH0=(65536-45872)/256;//装初值,每次计时5ms
TL0=(65536-45872)%256;
timecount++;
if(timecount==10)
{
//Beep=!Beep;
timecount=0;
second_tick=!second_tick;
Secondcount++;
if(Secondcount==2)
{
Secondcount=0;
Second++;
Time++;
if(Second>=60)
{
Second=0;
Minute++;
if(Minute>=60)
{
Minute=0;
Hour++;
if(Hour>=24)
{
Hour=0;
}
}
}
}
}
if(SETFlag<=3)
{
LEDBuffer[0]=Second%10;
LEDBuffer[1]=Second/10;
LEDBuffer[2]=Minute%10;
LEDBuffer[3]=Minute/10;
LEDBuffer[4]=Hour%10;
LEDBuffer[5]=Hour/10;
}
else
{
if(ALMFlag==0)LEDBuffer[0]=11;//显示F
else LEDBuffer[0]=12;//显示E
LEDBuffer[1]=10;
LEDBuffer[2]=Minuterom%10;
LEDBuffer[3]=Minuterom/10;
LEDBuffer[4]=Hourrom%10;
LEDBuffer[5]=Hourrom/10;
}
}
|