#include<reg52.h>
#define uchar unsigned char
#define uint unsigned int
sbit LCD_RS = P2^0;
sbit LCD_RW = P2^1;
sbit LCD_EN = P2^2;
sbit LED = P2^4;
bit new_s=0, g = 0,k=0;
char t0, sec = 00, min = 00, hour = 00;
char code LCD_line1[] = "GOOD MORNING!";
char code LCD_line2[] = "Timer: 00:00:00 ";
char Timer_buf[] = "00:00:00";
void delay(uint z)//延时
{
uint x, y;
for(x = z; x > 0; x--)
for(y = 100; y > 0; y--);
}
void W_LCD_Com(uchar com) //写指令
{
LCD_RS = 0;
LCD_RW = 0;
P3 = com;
LCD_EN = 1;
delay(5);
LCD_EN = 0;
}
void W_LCD_Dat(uchar dat)//写数据
{
LCD_RS = 1;
LCD_RW = 0;
P3 = dat;
LCD_EN = 1;
delay(5);
LCD_EN = 0;
}
void W_LCD_STR(uchar *s) //写字符
{
while(*s) W_LCD_Dat(*s++);
}
void W_BUFF(void) //显示
{
Timer_buf[0] = hour / 10 + 48;
Timer_buf[1] = hour % 10 + 48;
Timer_buf[3] = min / 10 + 48;
Timer_buf[4] = min % 10 + 48;
Timer_buf[6] = sec / 10 + 48;
Timer_buf[7] = sec % 10 + 48;
W_LCD_Com(0xc0 + 7);
W_LCD_STR(Timer_buf);
}
uchar read_key(void)//扫描按键
{
uchar x1, x2;
P1 = 255;
x1 = P1;
if (x1 != 255) {
delay(100);
x2 = P1;
if (x1 != x2)
return 255;
while(x2 != 255)
x2 = P1;
if (x1 == 0xfe) return 0;
else if (x1 == 0xfb) return 1;
else if (x1 == 0xfd) return 2;
else if (x1 == 0xf7) return 3;
else if (x1 == 0xef) return 4;
else if(x1==0xdf)return 5;
}
return 255;
}
void Init()
{
LCD_RW = 0;
W_LCD_Com(0x38); //显示模式
delay(50);
W_LCD_Com(0x0c);
W_LCD_Com(0x06);//显示光标移动位置
W_LCD_Com(0x01);//清屏
W_LCD_Com(0x80);//第一行
W_LCD_STR(LCD_line1);
W_LCD_Com(0xC0);//第二行
W_LCD_STR(LCD_line2);
TMOD = 0x01; //T0定时方式1
TH0 = 0x3c;
TL0= 0xB0;
TR0 = 1;
ET0 = 1;
EA = 1;
}
void CountDown()
{
if (k)
while(!(sec==0&&min==0&&hour==0))
{
if(new_s==1)
{
sec--;
new_s=0;
sec %=60;
if(sec<0)
sec=58-sec;
if(sec==59)
{
min--; min %=60;
if(min<0&&hour!=0&&hour>0)
min=58-min;
if(min==59)
{
hour--; hour %= 24;
}
}
W_BUFF();
}
}
k=0;
}
void main()
{
uint i, j;
uchar Key;
Init();
while(1)
{
if (new_s)
{
new_s=0;sec++; sec %= 60;
if(!sec)
{
min++; min %= 60;
if(!min)
{ hour++; hour %= 24;}
}
W_BUFF();
}
Key = read_key();
switch(Key) {
case 0: g = 1; break;
case 1: if(g) {min++; min %= 60; W_BUFF(); break;}
case 2: if(g) {hour++; hour %= 24; W_BUFF(); break;}
case 3: g = 0; break;
case 4:k=1;CountDown();break;
}
}
}
void timer0(void) interrupt 1
{
TH0 = 0x3c;
TL0= 0xB0;
t0++; t0 %= 20; //20次
if(t0 == 0){new_s = 1; LED = ~LED;}
if(g) LED = 0;
}
|