📄 使用1602液晶显示ds1302+c51时钟.txt
字号:
case 4:
lampsetup();break; //背光灯设置
case 5:
timereset();break; //时间清零
case 6:
model=0;break; //退出系统设置
}
}
}
}
void timesetup() //时间调整
{
id=6;
while(model==1) //model为1时进入时间调整
{
b=1;
Modset();
if(b==0)
{KeySound(); //蜂鸣声
if(id++==9) //6..9为时分秒调。 .
id=6;
}
id_case1_key(); //调用按键处理
Out_LCM(); //显示输出
}
}
void datesetup() //对日期进行调整
{
id=1;
while(model==2) //model为2时进入日期调整
{b=1;
Modset(); //模式键是否动作
if(b==0)
{KeySound(); //蜂鸣声
if(id++==5) //1..5为年月日星期 exit
id=1;
}
id_case1_key(); //日期调整
Out_LCM(); //显示输出
}
}
void alarmclockset(void) //闹铃调整
{id=12;
while(model==3) //进入闹铃设置
{b=1;
Modset(); //模式键是否动作
if(b==0)
{KeySound(); //蜂鸣声
if(R1302(0xc3)%16==0)
{id=0;model++;} //0xc2为闹铃开关寄存器。当为关时按下模式键时将退出闹铃设置,当开时进入闹铃时间设置
else if(id++==15) //12..15为闹铃开关和时间设置 exit
id=12;
}
id_case1_key(); //调整
Out_LCM(); //显示输出
}
}
void lampsetup(void) //背光灯时间设置
{id=10; //背光灯存储空间DS1302(0xc0)
while(model==4) //model为3时进入背光灯调整
{b=1;
Modset(); //模式键是否动作
if(b==0)
{KeySound(); //蜂鸣声
if(id++==11) //10为背光灯 exit
id=10;
}
id_case1_key();
Out_LCM(); //显示输出
}
}
void timereset() //时间清零
{
Set1302(inittime); //清时间
W1302(0x90,0xa5); //打开充电二级管 一个二级管串联一个2K电阻
model++;
}
//对相应的项目进行加、减调整。
void id_case1_key() //按键处理
{
if (TimerUp==0) //增加
{
Delay5Ms();
if(TimerUp==0)
{a=0;msec=0;KeySound(); //蜂鸣声
if((id==9)||(id==5)||(id==11)||(id==15)) //当ID为9、5时按下加、减键将退出调整
{ model++; //退出时间、日期设置
id=0;
}
else
Set(id,1);
}
while(TimerUp==0);
}
if (TimerDown==0) //减少
{
Delay5Ms();
if(TimerDown==0)
{a=0;msec=0;KeySound(); //蜂鸣声
if((id==9)||(id==5)||(id==11)||(id==15)) //当ID为9、5时按下加、减键将退出调整
{ model++; //退出时间、日期设置
id=0;
}
else
Set(id,0); //调用DS1302写入函数
}
while(TimerDown==0);
}
}
void timer_1(void) interrupt 1 //中断入口,闪烁
{
TH0=0x3c; //50ms定时
TL0=0xb0;
if(msec++==10) //500ms
{msec=0;a=~a; //闪烁标志反转
if(a==0) //1秒后背光时间减一秒。
LedTime--;
}
}
//根据选择调整相应项目并写入DS1302
void Set(uchar sel,uchar sel_1)
{
uchar address,item;
uchar max,min;
if(sel==1) {address=0x8c; max=99;min=0;} //年
if(sel==2) {address=0x88; max=12;min=1;} //月
if(sel==3) {address=0x86; max=31;min=1;} //日
if(sel==4) {address=0x8a; max=7; min=1;} //星期
if(sel==6) {address=0x84; max=23;min=0;} //小时
if(sel==7) {address=0x82; max=59;min=0;} //分钟
if(sel==8) {address=0x80; max=59;min=0;} //秒
if(sel==10) {address=0xc0; max=59;min=0;} //背光时间,最长1分钟
if(sel==12) {address=0xc2; max=1;min=0;} //闹铃开关寄存器
if(sel==13) {address=0xc4; max=23;min=0;} //闹铃时寄存器
if(sel==14) {address=0xc6; max=59;min=0;} //闹铃分寄存器
item=R1302(address+1)/16*10+R1302(address+1)%16;
if (sel_1==0) item++; else item--;
if(item>max) item=min;
if(item<MIN) i for(i="8;" ACC="d;" i; uchar { d) RTInputByte(uchar void *********************************************************************** 值:无 回 返 写入的数据 入口参数:d (内部函数) 明:往DS1302写入1Byte数据 说 能:实时时钟写入一字节 功 名:RTInputByte() 数 函 ******************************************************************** ********DS1302读写程序*************** } while(TempCycB--); TempCycB="7269;" while(TempCycA--) TempCycB; int unsigned TempCycA="5;" Delay400Ms(void) 400ms延时 while(TempCyc--); TempCyc="5552;" Delay5Ms(void) 5ms延时 X++; 显示单个字符 DData[j]); Y, OutputInformation(X, for(j="0;j<ListLength;j++)" X坐标应小于0xF <="0xF)" (X if 限制X不能大于15,Y不能大于1 & X Y ListLength="strlen(DData);" ListLength,j; *DData) code X, DisplayListChar(uchar ***原来的遇到空格0x20就不显示*** 按指定位置显示一串字符 WriteDataLCM(DData); 这里不检测忙信号,发送地址码 0); WriteCommandLCM(X, 算出指令码 |="0x40;" 当要显示第二行时地址码+0x40; (Y) DData) OutputInformation(uchar 按指定位置显示一个字符 显示开及光标设置 WriteCommandLCM(0x0C,1); 显示光标移动设置 WriteCommandLCM(0x06,1); 显示清屏 WriteCommandLCM(0x01,1); 关闭显示 WriteCommandLCM(0x08,1); 显示模式设置,开始要求每次检测忙信号 WriteCommandLCM(0x38,1); Delay5Ms(); WriteCommandLCM(0x38,0); 三次显示模式设置,不检测忙信号 LCM_Data="WDLCM;" LCMInit(void) LCM初始化 return(LCM_Data); 检测忙信号 Busy); (LCM_Data while LCM_E="0;" LCM_RW="0;" LCM_RS="1;" ReadStatusLCM(void) 读状态 根据需要检测忙 ReadStatusLCM(); (BuysC) BuysC为0时忽略忙检测 WCLCM,BuysC) WriteCommandLCM(uchar 写指令 延时 若晶振速度太高可以在这后加小的延时 检测忙 WDLCM) WriteDataLCM(uchar 写数据 *************** LCM1602驱动程序 ********* 显示exit DisplayListChar(12,1,exit); else 不显示 OutputInformation(15,1,0x20); OutputInformation(14,1,0x20); OutputInformation(13,1,0x20); {OutputInformation(12,1,0x20); if(a="=1)" 显示reset DisplayListChar(5,1,reset); 显示lamp DisplayListChar(0,1,lamp); OutputInformation(11,1,0x20); OutputInformation(10,1,0x20); model6() **********************************退出*********************************************** OutputInformation(9,1,0x20); OutputInformation(8,1,0x20); OutputInformation(7,1,0x20); OutputInformation(6,1,0x20); OutputInformation(5,1,0x20); OutputInformation(4,1,0x20); model5() *********************************时间归零设置***************************************** if((id="=6)&&(a==1))" OutputInformation(8,1,R1302(0xc1)%16+0x30); 读出背光灯延时时间 16+0x30); OutputInformation(7,1,R1302(0xc1) OutputInformation(8,1,0x20);} 显示sec DisplayListChar(9,1,sec); 显示set DisplayListChar(4,1,set); {DisplayListChar(0,1,lamp); id不为零(10)进入背光灯时间设置 OutputInformation(3,1,0x20); OutputInformation(2,1,0x20); OutputInformation(1,1,0x20); {OutputInformation(0,1,0x20); {OutputInformation(10,1,0x20); id为零未进入背光灯设置 if(id="=0)" model4() *********************************背光灯设置***************************************** exie闪烁 ************************exit闪烁显示*********************** 显示闹铃分 OutputInformation(10,1,R1302(0xc7)%16+0x30); {OutputInformation(9,1,R1302(0xc7) {OutputInformation(9,1,0x20); 闹铃分闪烁 **************************闹铃分闪烁显示******************* 显示闹铃时 OutputInformation(7,1,R1302(0xc5)%16+0x30); {OutputInformation(6,1,R1302(0xc5) 闹铃时闪烁 **************************闹铃时闪烁显示********************** ?:? 显示固定字符 OutputInformation(8,1,0x3a); 显示clock {DisplayListChar(0,1,alarmclock); 显示ON DisplayListChar(10,1,ClockOn); OutputInformation(12,1,0x20); 显示OFF {DisplayListChar(10,1,ClockOff);} 闹铃寄存器的值是否为零(关) if(R1302(0xc3)%16="=0)" A为闪烁标志 *****************************闹铃开关显示******************************** 显示ClockSet {DisplayListChar(0,1,ClockSet); 闹铃开关显示 {if(id="=12)" **************************id不为零进入闹铃设置显示******************** 显示cloak DisplayListChar(10,1,alarmclock); 显示date DisplayListChar(5,1,DATE); 显示time DisplayListChar(0,1,TIMER); model3() ********************************************************************************** *model为3时进入闹铃项目 ********************************************************************************* id为5时exit闪烁 **************************exit闪烁控制********************************* 显示星期 {OutputInformation(10,1,R1302(0x8b)%16+0x30); id为2时星期闪烁 ***************************星期闪烁控制********************************* OutputInformation(8,1,R1302(0x87)%16+0x30); 显示日 {OutputInformation(7,1,R1302(0x87) id为2日闪烁 ***************************日闪烁控制************************ OutputInformation(5,1,R1302(0x89)%16+0x30); 显示月 {OutputInformation(4,1,R1302(0x89) id为2时月闪烁 ***************************月闪烁控制************************ OutputInformation(2,1,R1302(0x8d)%16+0x30); 显示年 {OutputInformation(1,1,R1302(0x8d) id为1时年闪烁 ****************************年显示闪烁控制******************** ? OutputInformation(9,1,0x2f); OutputInformation(6,1,0x2f); OutputInformation(3,1,0x2f); OutputInformation(0,1,0x20); ***************************************************************************************************** id不为零时、进入日期调整 {OutputInformation(5,1,0x20); model2() ***************************************modelo为2时date闪烁*********************************************** DisplayListChar(11,1,exit); {OutputInformation(11,1,0x20); id为8时exit闪烁 *******************************Exit显示闪烁控制******************** OutputInformation(8,1,R1302(0x81)%16+0x30); 显示秒 {OutputInformation(7,1,R1302(0x81) id为7时秒闪烁 ******************************秒显示闪烁控制******************** OutputInformation(5,1,R1302(0x83)%16+0x30); 显示分 {OutputInformation(4,1,R1302(0x83) id为6时分闪烁 ******************************分显示闪烁控制******************** OutputInformation(2,1,R1302(0x85)%16+0x30); 显示时 {OutputInformation(1,1,R1302(0x85) id为5时时闪烁 ****************************时显示闪烁控制******************** OutputInformation(6,1,0x3a); OutputInformation(3,1,0x3a); id不为零进入时间调整项目 显示lalcak lalcak date 为0时显示TIME setup 第一行显示system DisplayListChar(2,0,systemp); OutputInformation(1,0,0x20); OutputInformation(0,0,0x20); *************************************判断ID的值来显示项目*********************** model1() model为1时time闪烁,按下模式键后进入时间调整 *************************model不为零时显示系统设置*********************************** OutputInformation(12,0,R1302(0x81)%16+0x30); OutputInformation(11,0,R1302(0x81) OutputInformation(9,0,R1302(0x83)%16+0x30); 显示分钟 OutputInformation(8,0,R1302(0x83) OutputInformation(6,0,R1302(0x85)%16+0x30); 显示小时 OutputInformation(5,0,R1302(0x85) OutputInformation(14,1,R1302(0x8b)%16+0x30); OutputInformation(12,1,R1302(0x87)%16+0x30); OutputInformation(11,1,R1302(0x87) OutputInformation(9,1,R1302(0x89)%16+0x30); OutputInformation(8,1,R1302(0x89) OutputInformation(6,1,R1302(0x8d)%16+0x30); OutputInformation(5,1,R1302(0x8d) *******************model为零时第二行显示日期,不为零显示设置项目******* OutputInformation(10,0,0x20);} {OutputInformation(7,0,0x20); OutputInformation(10,0,0x3a);} {OutputInformation(7,0,0x3a); 冒号闪烁 显示固定字符串Date(日期) DisplayListChar(0,1,DATE); OutputInformation(13,1,0x2f); OutputInformation(10,1,0x2f); OutputInformation(7,1,0x2f); OutputInformation(4,0,0x20); OutputInformation(13,0,0x20); 显示固定字符串(Time) DisplayListChar(0,0,TIMER); model0() setup****** ***********************model为零时第一行显示时间,不为零时显示system break; model6(); 6: case model5(); 5: model4(); 4: model3(); 3: 在不同的条件下显示不同的字符 model2(); 2: model1(); 1: model0(); 0: switch(model) Out_LCM(void) 屏幕显示 写保护,禁止写操作 W1302(0x8e,0x80); 转成BCD码 写入DS1302 10*16+item%10); W1302(address,item 允许写操作 W1302(0x8e,0x00); item="max;">0; i--)
{
T_IO = ACC0; /*相当于汇编中的 RRC */
T_CLK = 1;
T_CLK = 0;
ACC = ACC >> 1;
}
}
/********************************************************************
函 数 名:RTOutputByte()
功 能:实时时钟读取一字节
说 明:从DS1302读取1Byte数据 (内部函数)
入口参数:无
返 回 值:ACC
设 计:zhaojunjie 日 期:2002-03-19
修 改: 日 期:
***********************************************************************/
uchar RTOutputByte(void)
{
uchar i;
for(i=8; i>0; i--)
{
ACC = ACC >>1; /*相当于汇编中的 RRC */
ACC7 = T_IO;
T_CLK = 1;
T_CLK = 0;
}
return(ACC);
}
/********************************************************************
函 数 名:W1302()
功 能:往DS1302写入数据
说 明:先写地址,后写命令/数据 (内部函数)
调 用:RTInputByte() , RTOutputByte()
入口参数:ucAddr: DS1302地址, ucData: 要写的数据
返 回 值:无
***********************************************************************/
void W1302(uchar ucAddr, uchar ucDa)
{
T_RST = 0;
T_CLK = 0;
T_RST = 1;
RTInputByte(ucAddr); /* 地址,命令 */
RTInputByte(ucDa); /* 写1Byte数据*/
T_CLK = 1;
T_RST = 0;
}
/********************************************************************
函 数 名:R1302()
功 能:读取DS1302某地址的数据
说 明:先写地址,后读命令/数据 (内部函数)
调 用:RTInputByte() , RTOutputByte()
入口参数:ucAddr: DS1302地址
返 回 值:ucData :读取的数据
***********************************************************************/
uchar R1302(uchar ucAddr)
{
uchar ucData;
T_RST = 0;
T_CLK = 0;
T_RST = 1;
RTInputByte(ucAddr); /* 地址,命令 */
ucData = RTOutputByte(); /* 读1Byte数据 */
T_CLK = 1;
T_RST = 0;
return(ucData);
}
/********************************************************************
函 数 名:Set1302()
功 能:设置初始时间
说 明:先写地址,后读命令/数据(寄存器多字节方式)
调 用:W1302()
入口参数:pClock: 设置时钟数据地址 格式为: 秒 分 时 日 月 星期 年
7Byte (BCD码)1B 1B 1B 1B 1B 1B 1B
返 回 值:无
***********************************************************************/
void Set1302(uchar *pClock)
{
uchar i;
uchar ucAddr = 0x80;
W1302(0x8e,0x00); /* 控制命令,WP=0,写操作*/
for(i =7; i>0; i--)
{
W1302(ucAddr,*pClock); /* 秒 分 时 日 月 星期 年 */
pClock++;
ucAddr +=2;
}
W1302(0x8e,0x80); /* 控制命令,WP=1,写保护*/
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -