#include "common.h"
#include "lcd1602.h"
#include "timer.h"
sbit key_s1 = P1^0 ;
sbit key_s2 = P1^1 ;
sbit key_s3 = P1^2 ;
sbit key_s4 = P1^3 ;
sbit bellControl = P3^7 ;
typedef struct {
int year ;
char month ;
char date ;
char day ;
char hour ;
char minute ;
char second ;
}time_info ;
time_info time = {2024,1,9 ,00,12,00} ;
time_info clock = {2024,1,9 ,4,12,00,05} ; //闹钟时间
char flag1 = 0 ;
char flag2= 0 ; /*指示当前按下设置按键s1的次数*/
char flag3 = 0 ; /*闹钟标志位*/
void timeDisplay(void) ; /*时间数据更新、显示*/
void keyscanf(void) ; /*按键扫描*/
void systemInit(void) ; /*(液晶屏、定时器的初始化)*/
void checkClock(void) ; /*检测闹钟时间*/
void nzdelay(void);
void bell(void) ;
void delay(unsigned long time)
{
unsigned long x,y ;
for(x=time ; x ; x--)
{
for(y=110 ; y ; y --)
{
;
}
}
}
void main(void)
{
systemInit() ;
while(1)
{
keyscanf();
if(changeFlag) /*如果1s的时间到了*/
{
changeFlag = 0 ;
timeDisplay() ;
}
checkClock() ;
}
}
void systemInit(void) /*系统初始化(液晶屏、定时器的初始化)*/
{
timerInit() ;
lcd1602Init() ;
lcd1602AddressWriteString(LCD1602_ROW0,5,"") ;
lcd1602AddressWriteString(LCD1602_ROW1,0,"") ;
delay(1000) ;
lcd1602CleanAll() ;
lcd1602AddressWriteByte(LCD1602_ROW0,1,time.year/1000+'0') ;
lcd1602AddressWriteByte(LCD1602_ROW0,2,time.year%1000/100+'0') ;
lcd1602AddressWriteByte(LCD1602_ROW0,3,time.year%100/10+'0') ;
lcd1602AddressWriteByte(LCD1602_ROW0,4,time.year%10+'0') ;
lcd1602AddressWriteByte(LCD1602_ROW0,5,'-') ;
lcd1602AddressWriteByte(LCD1602_ROW0,6,time.month%100/10+'0') ;
lcd1602AddressWriteByte(LCD1602_ROW0,7,time.month%10+'0') ;
lcd1602AddressWriteByte(LCD1602_ROW0,8,'-') ;
lcd1602AddressWriteByte(LCD1602_ROW0,9,time.date%100/10+'0') ;
lcd1602AddressWriteByte(LCD1602_ROW0,10,time.date%10+'0') ;
lcd1602AddressWriteByte(LCD1602_ROW0,13,time.day%10+'0') ;
lcd1602AddressWriteByte(LCD1602_ROW1,2,time.hour/10+'0') ;
lcd1602AddressWriteByte(LCD1602_ROW1,3,time.hour%10+'0') ;
lcd1602AddressWriteByte(LCD1602_ROW1,4,':') ;
lcd1602AddressWriteByte(LCD1602_ROW1,5,time.minute/10+'0') ;
lcd1602AddressWriteByte(LCD1602_ROW1,6,time.minute%10+'0') ;
lcd1602AddressWriteByte(LCD1602_ROW1,7,':') ;
lcd1602AddressWriteByte(LCD1602_ROW1,8,time.second/10+'0') ;
lcd1602AddressWriteByte(LCD1602_ROW1,9,time.second%10+'0') ;
}
void timeDisplay(void)
{
if(60 == ++time.second)
{
time.second = 0 ;
if(60 == ++time.minute)
{
time.minute = 0 ;
if(24 == ++time.hour)
{
time.hour = 0;
if(8 == ++time.day)
{
time.date = 1 ;
}
lcd1602AddressWriteByte(LCD1602_ROW0,13,time.day%10+'0') ;
++time.date ;
if( (time.month == 1) || (time.month == 3) || (time.month == 5) || \
(time.month == 7) || (time.month == 8) || (time.month == 10) ||\
(time.month == 12))
{
if(time.date ==32)
{
time.date = 1 ;
++time.month ;
if(time.month == 13)
{
time.month = 1;
++time.year ;
lcd1602AddressWriteByte(LCD1602_ROW0,1,time.year%10000/1000+'0') ;
lcd1602AddressWriteByte(LCD1602_ROW0,2,time.year%1000/100+'0') ;
lcd1602AddressWriteByte(LCD1602_ROW0,3,time.year%10/100+'0') ;
lcd1602AddressWriteByte(LCD1602_ROW0,4,time.year%10+'0') ;
}
lcd1602AddressWriteByte(LCD1602_ROW0,6,time.month/10+'0') ;
lcd1602AddressWriteByte(LCD1602_ROW0,7,time.month%10+'0') ;
}
}
else if ( (time.month == 4) || (time.month == 6) || (time.month == 9) || \
(time.month == 11))
{
if(time.date ==31)
{
time.date = 1 ;
++time.month ;
if(time.month == 12)
{
time.month = 1;
}
lcd1602AddressWriteByte(LCD1602_ROW0,6,time.month/10+'0') ;
lcd1602AddressWriteByte(LCD1602_ROW0,7,time.month%10+'0') ;
}
}
else if ( ((time.year%4 ==0) && (time.year %100 !=0)) || \
time.year%400 == 0)
{/*闰年2月参数设置*/
if(time.date ==30)
{
time.date = 1 ;
++time.month;
lcd1602AddressWriteByte(LCD1602_ROW0,6,time.month/10+'0') ;
lcd1602AddressWriteByte(LCD1602_ROW0,7,time.month%10+'0') ;
}
}
else
{/*平年2月参数设置*/
if(time.date ==29)
{
time.date = 1 ;
++time.month ;
lcd1602AddressWriteByte(LCD1602_ROW0,6,time.month/10+'0') ;
lcd1602AddressWriteByte(LCD1602_ROW0,7,time.month%10+'0') ;
}
}
lcd1602AddressWriteByte(LCD1602_ROW0,9,time.date/10+'0') ;
lcd1602AddressWriteByte(LCD1602_ROW0,10,time.date%10+'0') ;
}
if(!flag3)/*不是闹钟模式,显示当前时间*/
{
lcd1602AddressWriteByte(LCD1602_ROW1,2,time.hour/10+'0') ;
lcd1602AddressWriteByte(LCD1602_ROW1,3,time.hour%10+'0') ;
}
}
if(!flag3)
{
lcd1602AddressWriteByte(LCD1602_ROW1,5,time.minute/10+'0') ;
lcd1602AddressWriteByte(LCD1602_ROW1,6,time.minute%10+'0') ;
}
}
if(!flag3)
{
lcd1602AddressWriteByte(LCD1602_ROW1,8,time.second/10+'0') ;
lcd1602AddressWriteByte(LCD1602_ROW1,9,time.second%10+'0') ;
}
}
void keyscanf(void) /*按键扫描*/
{
if( !key_s1 )
{
delay(10) ;
if( !key_s1 )
{
flag1 = 1 ;
++flag2 ; /*设置按键次数自加一次*/
if(4 == flag2)/*调整年*/
{
TR0 = 0 ;/*停止实时时钟的走动*/
lcd1602CleanAll() ;
lcd1602AddressWriteByte(LCD1602_ROW0,1,time.year/1000+'0') ;
lcd1602AddressWriteByte(LCD1602_ROW0,2,time.year%1000/100+'0') ;
lcd1602AddressWriteByte(LCD1602_ROW0,3,time.year%100/10+'0') ;
lcd1602AddressWriteByte(LCD1602_ROW0,4,time.year%10+'0') ;
lcd1602AddressWriteByte(LCD1602_ROW0,5,'-') ;
lcd1602AddressWriteByte(LCD1602_ROW0,6,time.month%100/10+'0') ;
lcd1602AddressWriteByte(LCD1602_ROW0,7,time.month%10+'0') ;
lcd1602AddressWriteByte(LCD1602_ROW0,8,'-') ;
lcd1602AddressWriteByte(LCD1602_ROW0,9,time.date%100/10+'0') ;
lcd1602AddressWriteByte(LCD1602_ROW0,10,time.date%10+'0') ;
lcd1602AddressWriteByte(LCD1602_ROW0,13,time.day%10+'0') ;
lcd1602AddressWriteByte(LCD1602_ROW1,2,time.hour/10+'0') ;
lcd1602AddressWriteByte(LCD1602_ROW1,3,time.hour%10+'0') ;
lcd1602AddressWriteByte(LCD1602_ROW1,4,':') ;
lcd1602AddressWriteByte(LCD1602_ROW1,5,time.minute/10+'0') ;
lcd1602AddressWriteByte(LCD1602_ROW1,6,time.minute%10+'0') ;
lcd1602AddressWriteByte(LCD1602_ROW1,7,':') ;
lcd1602AddressWriteByte(LCD1602_ROW1,8,time.second/10+'0') ;
lcd1602AddressWriteByte(LCD1602_ROW1,9,time.second%10+'0') ;
}
if((flag2==1) ||(flag2==11))/*设置闹钟,先显示闹钟参数*/
{
if(flag2 == 11)
flag2 = 0 ;
flag3 = 1;/*设置闹钟模式标志位*/
lcd1602CleanAll() ;
lcd1602AddressWriteString(LCD1602_ROW0,1,"Clock Setting") ;
lcd1602AddressWriteByte(LCD1602_ROW1,2,clock.hour/10+'0') ;
lcd1602AddressWriteByte(LCD1602_ROW1,3,clock.hour%10+'0') ;
lcd1602AddressWriteByte(LCD1602_ROW1,4,':') ;
lcd1602AddressWriteByte(LCD1602_ROW1,5,clock.minute/10+'0') ;
lcd1602AddressWriteByte(LCD1602_ROW1,6,clock.minute%10+'0') ;
lcd1602AddressWriteByte(LCD1602_ROW1,7,':') ;
lcd1602AddressWriteByte(LCD1602_ROW1,8,clock.second/10+'0') ;
lcd1602AddressWriteByte(LCD1602_ROW1,9,clock.second%10+'0') ;
}
while(!key_s1) ; /*松手检测*/
}
}
if(flag1)
{
lcd1602WriteCommand(0x0f) ;/*设置光标显示、光标闪烁*/
switch(flag2) /*光标显示位置*/
{
case 1:
lcd1602WriteCommand(0x80+0x40+3);
break ;
case 2:
lcd1602WriteCommand(0x80+0x40+6);
break ;
case 3:
lcd