#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--);
}
|