#include <reg51.h>
#include <intrins.h>
#define uchar unsigned char
#define uint unsigned int
#define DelayNOP() {_nop_();_nop_();_nop_();_nop_();}
sbit K1 =P1^0;
sbit K2 =P1^1;
sbit K3 =P1^2;
sbit K4 =P1^3;
sbit SPK=P3^0;
sbit RS =P2^0;
sbit RW =P2^1;
sbit E =P2^2;
uchar HMS_Str1[] = " 2000.01.01 O ";
uchar HMS_Str2[]= " 00:00:00 ";
uchar code Str1[] = " Set New Time ";
uchar code Str2[] = " Set Alarm ";
uchar code Str3[] = " Miao Biao ";
uchar MilliSecond,Hour =0,Minute=0, Second =0;
uchar AHour =7,AMinute=0, MMinute=0, MSecond =0;
uchar year=0,month=1,date=1;// 年、月、日
uchar Settime,miaob, Setdate=0;
#define one 0x80 //LCD第一行的初始位置,因为LCD1602字符地址首位D7恒定为1
#define two 0x80+0x40 //LCD第二行初始位置(因为第二行第一个字符位置地址是0x40
bit AR=0,MB=0 ,MBk=0,shan=0 ,alarm=1;
void DelayMS(uint x)
{
uchar i;
while(x--) for(i=0;i<120;i++);
}
bit LCD_Busy_Check()
{
bit result;
RS = 0;RW = 1;E = 1;DelayNOP();result = (bit)(P0 & 0x80);E = 0;
return result;
}
void LCD_Write_Command(uchar cmd)
{
while(LCD_Busy_Check());
RS = 0;RW = 0;E = 0;_nop_();_nop_();P0 = cmd; DelayNOP();
E = 1;DelayNOP();E = 0;
}
void LCD_Write_Data(uchar dat)
{
while(LCD_Busy_Check());
RS = 1;RW = 0;E = 0;
P0 = dat; DelayNOP();
E = 1;DelayNOP();E = 0;
}
void LCD_Initialize()
{
LCD_Write_Command(0x38);DelayMS(1);
LCD_Write_Command(0x0c);DelayMS(1);
LCD_Write_Command(0x06);DelayMS(1);
LCD_Write_Command(0x01);DelayMS(1);
}
void Display_String(uchar*str,uchar LineNo)
{
uchar k;
LCD_Write_Command(LineNo);
for(k=0;k<16;k++) LCD_Write_Data(str[k]);
}
void Beep()
{
uchar i, j = 70;
for(i=0;i<200;i++)
{
while(--j);SPK= ~SPK;
}
DelayMS(300); SPK=0;
}
void Display_HMS2()
{
if(Settime)
HMS_Str2[3] = '>';
else
HMS_Str2[3] = ' ';
if(AR==1)
{
if(Settime==4 && shan==1)
{
HMS_Str2[4] = ' ';
HMS_Str2[5] = ' ';
}
else
{
HMS_Str2[4] = AHour /10 + '0';
HMS_Str2[5] = AHour %10 + '0';
}
if(Settime==5 && shan==1)
{
HMS_Str2[7] = ' ';
HMS_Str2[8] = ' ';
}
else
{
HMS_Str2[7] = AMinute /10 + '0';
HMS_Str2[8] = AMinute %10 + '0';
}
HMS_Str2[9]= ' ';
HMS_Str2[10]= ' ';
HMS_Str2[11]= ' ';
}
else if(MB==1)
{
HMS_Str2[4] = ' ';
HMS_Str2[5] = ' ';
HMS_Str2[6] = ' ';
HMS_Str2[7] = MMinute /10 + '0';
HMS_Str2[8] = MMinute %10 + '0';
HMS_Str2[10]= MSecond/10 + '0';
HMS_Str2[11]= MSecond%10 + '0';
}
else
{
if(Settime==1 && shan==1)
{
HMS_Str2[4] = ' ';
HMS_Str2[5] = ' ';
}
else
{
HMS_Str2[4] = Hour /10 + '0';
HMS_Str2[5] = Hour %10 + '0';
}
HMS_Str2[6] = ':';
if(Settime==2 && shan==1)
{
HMS_Str2[7] = ' ';
HMS_Str2[8] = ' ';
}
else
{
HMS_Str2[7] = Minute /10 + '0';
HMS_Str2[8] = Minute %10 + '0';
}
HMS_Str2[9]= ':';
if(Settime==3 && shan==1)
{
HMS_Str2[10] = ' ';
HMS_Str2[11] = ' ';
}
else
{
HMS_Str2[10]= Second/10 + '0';
HMS_Str2[11]= Second%10 + '0';
}
}
Display_String(HMS_Str2, two);
}
void Display_HMS1()
{
if(Setdate) HMS_Str1[2] = '>';
else HMS_Str1[2] = ' ';
if(AR==1)
Display_String(Str2, one);
else if(Settime)
Display_String(Str1, one);
else if(MB==1)
Display_String(Str3, one);
else
{
if(Setdate==1 && shan==1)
{
HMS_Str1[5] = ' ';
HMS_Str1[6] = ' ';
}
else
{
HMS_Str1[5] = year/10+ '0';
HMS_Str1[6] = year %10 + '0';
}
if(Setdate==2 && shan==1)
{
HMS_Str1[8] = ' ';
HMS_Str1[9] = ' ';
}
else
{
HMS_Str1[8] = month /10 + '0';
HMS_Str1[9] = month %10 + '0';
}
if(Setdate==3 && shan==1)
{
HMS_Str1[11] = ' ';
HMS_Str1[12] = ' ';
}
else
{
HMS_Str1[11]= date /10 + '0';
HMS_Str1[12]= date %10 + '0';
}
if(alarm==1)
HMS_Str1[14] = 'O';
else
HMS_Str1[14] = ' ';
Display_String(HMS_Str1, one);
}
}
//闰年的计算
bit leap_year()
{
bit leap;
if(year%4==0&&year%100!=0||year%400==0)// 闰年的条件
leap=1;
else
leap=0;
return leap;
}
void keyscan()
{
if(Setdate==0)
if (K1==0)
{ DelayMS(5);
if (K1==0)
{ while(!K1);
Settime++;
if(Settime==1)
{
TR0 = 0;
TR1 = 1;
Display_String(Str1,one);
}
if(Settime==4)
{ TR0 = 1;
AR=1;
Display_HMS2();
Display_String(Str2,one);
}
if(Settime>=6)
{
AR=0;
TR1 = 0;
Display_String(HMS_Str2, two);
Settime=0;
}
}
}
if (Settime==0 && miaob ==0)
if (K2==0)
{ DelayMS(5);
if (K2==0)
{ while(!K2);
Setdate++;
TR1 = 1;
if(Setdate>=4)
{
TR1 = 0;
Display_String(HMS_Str2, two);
Setdate=0;
}
}
}
}
void Change_Time()
{ if (Settime)
{ if (K3==0)
{ DelayMS(5);
if(K3==0)
{ while(!K3);
switch (Settime)
{
case 1:
Hour ++;
if(Hour == 24) Hour = 0;break;
case 2:
Minute ++;
if(Minute == 60) Minute = 0; break;
case 3:
Second ++;
if(Second == 60) Second = 0; break;
case 4:
AHour ++;
if(AHour == 24) AHour = 0; break;
case 5:
AMinute ++;
if(AMinute == 60) AMinute = 0; break;
}
}
}
if (K4==0)
{ DelayMS(5);
if(K4==0)
{ while(!K4);
switch(Settime)
{
case 1:
Hour --;
if(Hour ==-1) Hour = 23; break;
case 2:
Minute --;
if(Minute ==-1) Minute = 59; break;
case 3:
Second --;
if(Second==-1) Second = 59; break;
case 4:
AHour--;
if(AHour ==-1) AHour = 23; break;
case 5:
AMinute --;
if(AMinute ==-1) AMinute = 59; break;
}
}
}
}
if(Setdate)
{
if (K1==0)
{ DelayMS(5);
if(K1==0)
{ while(!K1);
alarm=~alarm;
}
}
if (K3==0)
{ DelayMS(5);
if(K3==0)
{ while(!K3);
switch (Setdate)
{
case 1:
year ++;
if(year == 100) year = 0; break;
case 2:
month ++;
if(month == 13) month = 1; break;
case 3:
date ++;
if (month==1||month==3||month==5||month==7||month==8||month==10||month==12)
if (date>31) date=1; //大月31 天
if (month==4||month==6||month==9||month==11)
if (date>30) date=1; //小月30 天
if (month==2)
if( leap_year()) //闰年的条件
if (date>29) date=1; //闰年2 月为29 天
else
if (date>28) date=1; break;
}
}
}
if (K4==0)
{ DelayMS(5);
if(K4==0)
{ while(!K4);
switch(Setdate)
{
case 1:
year --;
if(year ==0) year =99; break;
case 2:
month --;
if(month ==0) month =12; break;
case 3:
date --;
if (month==1||month==3||month==5||month==7||month==8||month==10||month==12)
if (date==0) date=31; //大月31 天
if (month==4||month==6||month==9||month==11)
if (date==0) date=30; //小月30 天
if (month==2)
if( leap_year()) //闰年的条件
if (date==0) date=29; //闰年2 月为29 天
else
if (date==0) date=28; break;
}
}
}
}
}
void set_miaob()
{
if (Settime==0 && Setdate==0)
{
if (K3==0)
{DelayMS(5);
if(K3==0)
{
while(!K3);
miaob++;
if(miaob==1)
{
MB =1;
Display_String(Str3,one);
Display_HMS2();
}
if(miaob==2)
MBk=1;
if(miaob==3)
MBk=0;
if(miaob==4)
{
MBk=0;
MB =0;
Display_String(HMS_Str2, two);
Display_String(HMS_Str1, one);
miaob=0;
}
}
}
}
}
void dingshi()
{
if(alarm)
if (Hour == AHour && Minute == AMinute && Second == 0)
Beep();
}
void Timer0() interrupt 1
{
TH0 = (65536 -50000)/256;
TL0 = (65536 -50000)%256;
if(++MilliSecond == 20)
{
MilliSecond = 0;
Second++;
if(Second > 59)
{Second = 0;
Minute++;
if(Minute>59)
{Minute=0;
Hour++;
if(Hour>23)
{Hour=0;
date++;
if (month==1||month==3||month==5||month==7||month==8||month==10||month==12)
if (date>31) {date=1;month++;} //大月31 天
if (month==4||month==6||month==9||month==11)
if (