基于51单片机DS1302时钟LCD显示Word格式.docx
《基于51单片机DS1302时钟LCD显示Word格式.docx》由会员分享,可在线阅读,更多相关《基于51单片机DS1302时钟LCD显示Word格式.docx(17页珍藏版)》请在冰豆网上搜索。
DateTime,TimeString);
if(!
s1num)
{
LCDDisplay(6,0,DateString);
LCDDisplay(6,1,TimeString);
}
keyscan();
}
}
mytype.h
******************************************************************************/
#ifndef_MYTYPE_H_
#define_MYTYPE_H_
#defineuint8unsignedchar
#defineuint16unsignedint
voidDelayms(uint16ms);
voidDelayms(uint16ms)
uint16i,j;
for(i=ms;
i>
0;
i--)
for(j=112;
j>
j--);
#endif
ds1302.h
#ifndef_DS1302_H_
#define_DS1302_H_
sbitSCLK=P1^0;
sbitIO=P1^1;
sbitRST=P1^2;
sbitACC0=ACC^0;
sbitACC7=ACC^7;
typedefstruct
uint8Second;
uint8Minute;
uint8Hour;
uint8Week;
uint8Day;
uint8Month;
uint8Year;
}SYSTEMTIME;
//定义系统时间类型
#defineSECOND0X80
#defineMINUTE0X82
#defineHOUR0X84
#defineDAY0X86
#defineMONTH0X88
#defineWEEK0X8A
#defineYEAR0X8C
#defineCONTROL0X8E
/*向1302写入一个字节*/
voidWriteByte(uint8Data)
uint8i;
ACC=Data;
for(i=8;
IO=ACC0;
SCLK=0;
SCLK=1;
ACC>
>
=1;
/*同1302读取一个字节*/
uint8ReadByte(void)
for(i=0;
i<
8;
i++)
ACC7=IO;
returnACC;
/*向1302指定地址写入一个字节*/
voidWriteAddress(uint8Addr,uint8Data)
RST=0;
SCLK=0;
RST=1;
WriteByte(Addr);
WriteByte(Data);
//SCLK=1;
/*同1302指定地址读取一个字节*/
uint8ReadAddress(uint8Addr)
uint8Data;
WriteByte(Addr|0x01);
//告诉1302要读取的地址
Data=ReadByte();
SCLK=1;
returnData;
/*设置写保护*/
voidSetWP(bitflag)
if(flag)
WriteAddress(CONTROL,0X80);
else
WriteAddress(CONTROL,0X00);
/*设置时间*/
voidSetTime(uint8Addr,uint8Data)
SetWP(0);
WriteAddress(Addr,((Data/10)<
<
4)|(Data%10));
/*获取当前时间*/
voidGetTime(SYSTEMTIME*Time)
uint8ReadValue;
//秒
ReadValue=ReadAddress(SECOND);
Time->
Second=((ReadValue&
0x70)>
4)*10+(ReadValue&
0x0f);
//分
ReadValue=ReadAddress(MINUTE);
Minute=((ReadValue&
//时
ReadValue=ReadAddress(HOUR);
if(ReadValue&
0x80)//12小时制
Time->
Hour=((ReadValue&
0x10)>
else
0x30)>
//天
ReadValue=ReadAddress(DAY);
Day=((ReadValue&
//星期
ReadValue=ReadAddress(WEEK);
Week=(ReadValue&
//月
ReadValue=ReadAddress(MONTH);
Month=((ReadValue&
//年
ReadValue=ReadAddress(YEAR);
Year=((ReadValue&
0xf0)>
/*日期转化为字符串*/
voidDateToString(SYSTEMTIME*Time,char*DateStr)
*DateStr=Time->
Year/10+48;
*(DateStr+1)=Time->
Year%10+48;
*(DateStr+2)='
-'
;
*(DateStr+3)=Time->
Month/10+48;
*(DateStr+4)=Time->
Month%10+48;
*(DateStr+5)='
*(DateStr+6)=Time->
Day/10+48;
*(DateStr+7)=Time->
Day%10+48;
*(DateStr+8)='
\0'
/*时间转化为字符串*/
voidTimeToString(SYSTEMTIME*Time,char*TimeStr)
*TimeStr=Time->
Hour/10+48;
*(TimeStr+1)=Time->
Hour%10+48;
*(TimeStr+2)='
:
'
*(TimeStr+3)=Time->
Minute/10+48;
*(TimeStr+4)=Time->
Minute%10+48;
*(TimeStr+5)='
*(TimeStr+6)=Time->
Second/10+48;
*(TimeStr+7)=Time->
Second%10+48;
*(TimeStr+8)='
/*系统初始化*/
voidInit1302(void)
uint8Second=ReadAddress(SECOND);
if(Second==0x80)
SetTime(SECOND,0);
1602.h
#ifndef_1602_H_
#define_1602_H_
#defineDataPortP2
sbitE=P1^3;
//使能信号,给E一个高脉冲将数据送入液晶控制器,完成写操作,高脉冲E=0;
E=1;
sbitRW=P1^4;
//读写选择端
sbitRS=P1^5;
//数据命令选择端
#definecmd0
#defineData1
voidWrite_Cmd_Data(uint8Control,uint8Port)
RS=Control;
//确定写命令还是数据
RW=0;
DataPort=Port;
Delayms(5);
//延迟为了数据稳定
E=0;
E=1;
//产生上升沿
Delayms
(1);
voidLCDInit()
Write_Cmd_Data(cmd,0x38);
//显示模式设置CMD=0,RS=0说明是命令,RW=0是写,DataPort=1有效数据,E=0,E=1高脉冲
Write_Cmd_Data(cmd,0x0c);
//设置开显示,不显示光标
Write_Cmd_Data(cmd,0x06);
//写一个字符后地址加一
Write_Cmd_Data(cmd,0x01);
//显示清0
voidLCDDisplay(uint8x,uint8y,uint8*temp)
Write_Cmd_Data(cmd,0x80+y*0x40+x);
while(*temp!
='
)
Write_Cmd_Data(Data,*(temp++));
Delayms(5);
Key.h
***************************************************************************/
#ifndef_key_H_
#define_key_H_
sbits1=P3^0;
//定义按键--功能键
sbits2=P3^1;
//定义按键--增加键
sbits3=P3^2;
//定义按键--减小键
sbitbeep=P2^3;
//定义蜂鸣器端
uint8count,s1num,timer1,timer2;
voiddi()//蜂鸣器发声函数
beep=0;
Delayms(100);
beep=1;
voidkeyscan()//按键扫描函数
GetTime(&
if(s1==0)
if(s1==0)//确认功能键被按下
{s1num++;
//功能键按下次数记录
while(!
s1);
//释放确认
di();
//每当有按键释放蜂鸣器发出滴声
if(s1num==1)//第一次被按下时
{
Write_Cmd_Data(cmd,0x80+0x40+12);
//光标定位到秒位置
Write_Cmd_Data(cmd,0x0f);
//光标开始闪烁
}
if(s1num==2)//第二次按下光标闪烁定位到分钟位置
Write_Cmd_Data(cmd,0x80+0x40+9);
if(s1num==3)//第三次按下光标闪烁定位到小时位置
Write_Cmd_Data(cmd,0x80+0x40+6);
if(s1num==4)//第三次按下光标闪烁定位到小时位置
Write_Cmd_Data(cmd,0x80+12);
if(s1num==5)//第三次按下光标闪烁定位到小时位置
Write_Cmd_Data(cmd,0x80+9);
if(s1num==6)//第三次按下光标闪烁定位到小时位置
Write_Cmd_Data(cmd,0x80+6);
if(s1num==7)//第四次按下
s1num=0;
//记录按键数清零
Write_Cmd_Data(cmd,0x0c);
//取消光标闪烁
}
if(s1num!
=0)//只有功能键被按下后,增加和减小键才有效
if(s2==0)
Delayms(5);
if(s2==0)//增加键确认被按下
while(!
s2);
//按键释放
di();
if(s1num==1)//若功能键第一次按下
{
SetWP(0);
timer1=ReadAddress(SECOND);
timer2=(((timer1&
4)*10+(timer1&
0x0f));
timer2++;
if(timer2>
59)timer2=0;
DateTime.Second=timer2;
SetTime(SECOND,DateTime.Second);
TimeToString(&
LCDDisplay(6,1,TimeString);
Write_Cmd_Data(cmd,0x80+0x40+12);
//显示位置重新回到调节处
}
if(s1num==2)//若功能键第二次按下
timer1=ReadAddress(MINUTE);
DateTime.Minute=timer2;
SetTime(MINUTE,DateTime.Minute);
Write_Cmd_Data(cmd,0x80+0x40+9);
if(s1num==3)//若功能键第三次按下
timer1=ReadAddress(HOUR);
if(timer1&
{
timer2=((timer1&
12)timer2=0;
}
else
23)timer2=0;
DateTime.Hour=timer2;
SetTime(HOUR,DateTime.Hour);
Write_Cmd_Data(cmd,0x80+0x40+6);
if(s1num==4)//若功能键第四次按下
timer1=ReadAddress(DAY);
28)timer2=0;
DateTime.Day=timer2;
SetTime(DAY,DateTime.Day);
DateToString(&
LCDDisplay(6,0,DateString);
Write_Cmd_Data(cmd,0x80+12);
if(s1num==5)//若功能键第五次按下
timer1=ReadAddress(MONTH);
DateTime.Month=timer2;
SetTime(MONTH,DateTime.Month);
Write_Cmd_Data(cmd,0x80+9);
if(s1num==6)//若功能键第六次按下
timer1=ReadAddress(YEAR);
99)timer2=0;
DateTime.Year=timer2;
SetTime(YEAR,DateTime.Year);
Write_Cmd_Data(cmd,0x80+6);
if(s3==0)
if(s3==0)//增加键确认被按下
s3);
timer2--;