#include<reg52.h>
#define uchar unsigned char
#define uint unsigned int
#define LCD1602_DB P0
sbit LCD1602_RS=P1^0;
sbit LCD1602_RW=P1^1;
sbit LCD1602_E =P1^5;
unsigned char code KeyCode[]={
0xE7,0xD7,0xB7,0x77,0xEB,0xDB,0xBB,0x7B,
0xED,0xDD,0xBD,0x7D,0xEE,0xDE,0xBE,0x7E
};
void LCD1602_INIT();
void LcdSetCursor(uchar x,uchar y);
void LcdShowDat(uchar x,uchar y,uchar dat);
void LCD1602_WCMD(uchar cmd);
void delay_ms(uint cnt);
void an();
uint cnt=0;
uchar sec=0;min=0;hour=0;key=16;
void ScanKeyBoard()
{
unsigned char scan1,scan2,KeyBoard;
unsigned char i;
P2=0xf0;
scan1=P2;
TR0=0;
if((scan1&0xf0)!=0xf0)
{
delay_ms(10);
scan1=P2;
if((scan1&0xf0)!=0xf0)
{
P2=0x0f;
scan2=P2;
KeyBoard=scan1|scan2;
while((P2&0x0f)!=0x0f);
for(i=0;i<=15;i++)
{
if(KeyBoard==KeyCode[i])
key=i;
}
}
}
}
void an()
{
uchar a=key;
ScanKeyBoard();
switch(a)
{
case 0:hour=hour+1;break;
case 1:hour=hour-1;break;
case 2:min++;break;
case 3:min--;break;
default: break;
}
}
void delay_ms(unsigned int cnt)
{
unsigned char i;
while(cnt--)
{
for(i=0;i<=110;i++);
}
}
void main()
{
uchar temp;
TMOD=0x01;
TH0=0xfc;
TL0=0x67;
TR0=1;
EA=1;
ET0=1;
LCD1602_INIT();
while(1)
{
temp=hour/10;
temp+=0x30;
LcdShowDat(2,0,temp);
temp=hour%10;
temp+=0x30;
LcdShowDat(3,0,temp);
LcdShowDat(4,0,':');
temp=min/10;
temp+=0x30;
LcdShowDat(5,0,temp);
temp=min%10;
temp+=0x30;
LcdShowDat(6,0,temp);
LcdShowDat(7,0,0x3a);
temp=sec/10;
temp+=0x30;
LcdShowDat(8,0,temp);
temp=sec%10;
temp+=0x30;
LcdShowDat(9,0,temp);
}
}
void LcdWaitReady()
{
unsigned char sta;
LCD1602_DB=0xff;
LCD1602_RS=0;
LCD1602_RW=1;
do{
LCD1602_E=1;
sta=LCD1602_DB;
LCD1602_E=0;
}while(sta&0x80);
}
void LCD1602_WCMD(unsigned char cmd)
{
LcdWaitReady();
LCD1602_RS=0;
LCD1602_RW=0;
LCD1602_DB=cmd;
LCD1602_E=1;
LCD1602_E=0;
}
void LCD1602_WDat(unsigned char dat)
{
LcdWaitReady();
LCD1602_RS=1;
LCD1602_RW=0;
LCD1602_DB=dat;
LCD1602_E=1;
LCD1602_E=0;
}
void LcdSetCursor(uchar x,uchar y)
{
unsigned char addr;
if(y==0)
addr=0x00+x;
else
addr=0x40+x;
LCD1602_WCMD(addr|0x80);
}
void LcdShowDat(uchar x,uchar y,uchar dat)
{
LcdSetCursor(x,y);
LCD1602_WDat(dat);
}
void LCD1602_INIT()
{
LCD1602_E=0;
LCD1602_WCMD(0x38);
LCD1602_WCMD(0x0c);
LCD1602_WCMD(0x06);
LCD1602_WCMD(0x01);
}
void clok()interrupt 1
{
cnt++;
TH0=0xfc;
TL0=0x67;
if(cnt==1000)
{
cnt=0;
sec++;
if(sec==60)
{
sec=0;
min++;
if(min==60)
{
min=0;
hour++;
if(hour==24)
{
hour=0;
}
}
}
}
}
|