#include <reg52.h>
#define uchar unsigned char
#define uint unsigned int
void KeyHandle(void); /*按键处理模块*/
void Delay (); /*10ms延时*/
void DispClock();
uchar Control[6]={0xdf,0xef,0xf7,0xfb,0xfd,0xfe};//数码管控制选通
uchar DisplayArray[6]={0x00,0x00,0x00,0x00,0x00,0x00};//中间存储变量
uchar code CodeNum[16]={0x28,0xEB,0x32,0xA2,0xE1,
0xA4,0x24,0xE8,0x20,0xA0};//0-f段码
uchar Keyflag=0;/*按键处理标记*/
uchar SetFlag=0;//=0,Normal;=1,调秒; =2,调分;=3,调时;
uchar Msecond,Second,Minite,Hour;
sbit P1_0 = P1^0;
sbit P1_1 = P1^1;
sbit P3_7 = P3^7;
sbit P3_3 = P3^3;
void main(void)
{
EA=1;
ET0=1;
ET1=1;
P1_1=1;
P3_7=1;
P3_3=1;
TMOD = 0x11;
TH0=(65536-20000)/256;
TL0=(65536-20000)%256; //设定时值为20ms
TH1=(65536-500)/256;
TL1=(65536-500)%256; //设定时值为500us
TR0=1;
TR1=1; //开始定时
for(;;)//while(1)
{
DispClock();
KeyHandle();
}
}
void Time0(void) interrupt 1
{
TH0=(65536-20000)/256;
TL0=(65536-20000)%256;
Msecond++;
if(Msecond>=50)
{
Msecond=0;
Second++;
if(Second>=60)
{
Second=0;
Minite++;
if(Minite>=60)
{
Minite=0;
Hour++;
if(Hour>=24)
{
Hour=0;
}
}
}
}
}
void Time1(void) interrupt 3
{
static uchar s_count=0;//变量定义在其它语句之前,keil c(非C语言规则)
uchar temp;
TH1=(65536-500)/256;
TL1=(65536-500)%256; //设定时值为500us
P0=Control[s_count];
temp=DisplayArray[s_count];
P2=CodeNum[temp];
s_count++;
if(s_count>5)
{
s_count=0;
}
}
void KeyHandle(void) /*按键处理*/
{
P1_0=1;
if(P1_0==0)
{
Delay();
if(P1_0==0)
{
SetFlag++;
if(SetFlag>3)
SetFlag=0;
switch (SetFlag)
{ case 1:
P3_7 = 0;
P3_3 = 0;
break;
case 2:
P3_7 = 1;
P3_3 = 0;
break;
case 3:
P3_7 = 0;
P3_3 = 1;
break;
default:
P3_7 = 1;
P3_3 = 1;
break;
}
}
P1_0=1; //防止按键不放
while((P1_0&0x01)!=0x01);
}
//加法处理
P1_1=1;
if(P1_1==0)
{
Delay();
if(P1_1==0)
{
switch (SetFlag)
{
case 1:
Second++;
if(Second>=60) Second=0;
break;
case 2:
Minite++;
if(Minite>=60) Minite=0;
break;
case 3:
Hour++;
if(Hour>=24) Hour=0;break;
default: break;
}
P1_1=1; //防止按键不放
while((P1_1&0x01)!=0x01);
}
}
}
void DispClock()
{
uchar tempData=0;
tempData=Second;//秒钟分解
DisplayArray[0]=tempData%10;
DisplayArray[1]=tempData/10;
tempData=Minite;//分钟分解
DisplayArray[2]=tempData%10;
DisplayArray[3]=tempData/10;
tempData=Hour;//小时分解
DisplayArray[4]=tempData%10;
DisplayArray[5]=tempData/10;
}
void Delay()//10ms延时
{
uchar i,j;
for(i=20;i>0;i--)
for(j=249;j>0;j--);
}
|