1.引入上一个程序的代码
2.引入Key和Timer0文件
3.获取按键值
定义全局变量unsigned char keynum
main函数中
keynum=Key();
4.设置第一个按键的两种模式,以此来控制时钟的设定和显示
if(keynum==1)
{
if(MODE==0)
{
MODE=1;
}
else if(MODE==1)
{
//待写设定修改后的新日期的代码
MODE=0;
}
}
switch(MODE)
{
case 0:time_show();break;
case 1:time_set();break;
}
5.模块化展示时间代码
void time_show()
{
DS1302_showTime();
LCD_ShowNum(1,1,DS1302_Time[0],2);
LCD_ShowNum(1,4,DS1302_Time[1],2);
LCD_ShowNum(1,7,DS1302_Time[2],2);
LCD_ShowNum(2,1,DS1302_Time[3],2);
LCD_ShowNum(2,4,DS1302_Time[4],2);
LCD_ShowNum(2,7,DS1302_Time[5],2);
}
6.模块化更改时间代码
//此函数捕捉按键并暂停,可以修改时间,但不可设定修改后的日期时间
void time_set()
{
if(keynum == 2)//按键2按下
{
timeSet_select++;
timeSet_select %= 6;//越界清零
}
if(keynum == 3)//按键3按下
{
DS1302_Time[timeSet_select]++;
if(DS1302_Time[0]+1>100)
{
DS1302_Time[0]=0;
}
//月超越界限
if(DS1302_Time[1]+1>13)
{
DS1302_Time[1]=1;
}
//日的判断
if(DS1302_Time[0]%4==0)
{
if(DS1302_Time[1]==2)
{
if(DS1302_Time[2]+1>30)
DS1302_Time[2]=1;
}
}
else
{
if(DS1302_Time[1]==2)
{
if(DS1302_Time[2]+1>29)
DS1302_Time[2]=1;
}
}
if(DS1302_Time[1]==1||DS1302_Time[1]==3||DS1302_Time[1]==5||DS1302_Time[1]==7||DS1302_Time[1]==8||
DS1302_Time[1]==10||DS1302_Time[1]==12)
{
if(DS1302_Time[2]+1>32)
DS1302_Time[2]=1;
}
if(DS1302_Time[1]==2||DS1302_Time[1]==4||DS1302_Time[1]==6||DS1302_Time[1]==11)
{
if(DS1302_Time[2]+1>31)
DS1302_Time[2]=1;
}
//小时
if(DS1302_Time[3]+1>25){DS1302_Time[3]=1;}
//分钟
if(DS1302_Time[4]+1>60){DS1302_Time[4]=0;}
//秒钟
if(DS1302_Time[5]+1>60){DS1302_Time[5]=0;}
}
//减减的判断
if(keynum == 4)
{
DS1302_Time[timeSet_select]--;
if(DS1302_Time[0]-1>100)
{
DS1302_Time[0]=99;
}
//月超越界限
if(DS1302_Time[1]-1>12)
{
DS1302_Time[1]=12;
}
//日的判断
if(DS1302_Time[0]%4==0)
{
if(DS1302_Time[1]==2)
{
if(DS1302_Time[2]-1>29)
DS1302_Time[2]=29;
if(DS1302_Time[2]+1>30)
DS1302_Time[2]=1;
}
}
else
{
if(DS1302_Time[1]==2)
{
if(DS1302_Time[2]-1>28)
DS1302_Time[2]=28;
if(DS1302_Time[2]+1>29)
DS1302_Time[2]=1;
}
}
if(DS1302_Time[1]==1||DS1302_Time[1]==3||DS1302_Time[1]==5||DS1302_Time[1]==7||DS1302_Time[1]==8||
DS1302_Time[1]==10||DS1302_Time[1]==12)
{
if(DS1302_Time[2]-1>31)
DS1302_Time[2]=31;
if(DS1302_Time[2]+1>32)
DS1302_Time[2]=1;
}
if(DS1302_Time[1]==2||DS1302_Time[1]==4||DS1302_Time[1]==6||DS1302_Time[1]==11)
{
if(DS1302_Time[2]-1>30)
DS1302_Time[2]=30;
if(DS1302_Time[2]+1>31)
DS1302_Time[2]=1;
}
//小时
if(DS1302_Time[3]-1>24){DS1302_Time[3]=24;}
//分钟
if(DS1302_Time[4]-1>60){DS1302_Time[4]=59;}
//秒钟
if(DS1302_Time[5]-1>60){DS1302_Time[5]=59;}
}
LCD_ShowNum(1,1,DS1302_Time[0],2);
LCD_ShowNum(1,4,DS1302_Time[1],2);
LCD_ShowNum(1,7,DS1302_Time[2],2);
LCD_ShowNum(2,1,DS1302_Time[3],2);
LCD_ShowNum(2,4,DS1302_Time[4],2);
LCD_ShowNum(2,7,DS1302_Time[5],2);
}
//此时可以更改时间,但是不能设定更改后的时候,以及不能修改时间的时候闪烁
7.最终主函数代码
#include <REGX52.H>
#include "LCD1602.h"
#include "DS1302_con.h"
#include "Delay.h"
#include "Key.h"
#include "Time0.h"
unsigned char keynum,MODE=0,timeSet_select=0,selectflag;
void time_show()
{
DS1302_showTime();
LCD_ShowNum(1,1,DS1302_Time[0],2);
LCD_ShowNum(1,4,DS1302_Time[1],2);
LCD_ShowNum(1,7,DS1302_Time[2],2);
LCD_ShowNum(2,1,DS1302_Time[3],2);
LCD_ShowNum(2,4,DS1302_Time[4],2);
LCD_ShowNum(2,7,DS1302_Time[5],2);
}
//捕捉按键并暂停,可以修改时间,但不可设定修改后的日期时间
void time_set()
{
if(keynum == 2)//按键2按下
{
timeSet_select++;
timeSet_select %= 6;//越界清零
}
if(keynum == 3)//按键3按下
{
DS1302_Time[timeSet_select]++;
if(DS1302_Time[0]+1>100)
{
DS1302_Time[0]=0;
}
//月超越界限
if(DS1302_Time[1]+1>13)
{
DS1302_Time[1]=1;
}
//日的判断
if(DS1302_Time[0]%4==0)
{
if(DS1302_Time[1]==2)
{
if(DS1302_Time[2]+1>30)
DS1302_Time[2]=1;
}
}
else
{
if(DS1302_Time[1]==2)
{
if(DS1302_Time[2]+1>29)
DS1302_Time[2]=1;
}
}
if(DS1302_Time[1]==1||DS1302_Time[1]==3||DS1302_Time[1]==5||DS1302_Time[1]==7||DS1302_Time[1]==8||
DS1302_Time[1]==10||DS1302_Time[1]==12)
{
if(DS1302_Time[2]+1>32)
DS1302_Time[2]=1;
}
if(DS1302_Time[1]==2||DS1302_Time[1]==4||DS1302_Time[1]==6||DS1302_Time[1]==11)
{
if(DS1302_Time[2]+1>31)
DS1302_Time[2]=1;
}
//小时
if(DS1302_Time[3]+1>25){DS1302_Time[3]=1;}
//分钟
if(DS1302_Time[4]+1>60){DS1302_Time[4]=0;}
//秒钟
if(DS1302_Time[5]+1>60){DS1302_Time[5]=0;}
}
if(keynum == 4)
{
DS1302_Time[timeSet_select]--;
if(DS1302_Time[0]-1>100)
{
DS1302_Time[0]=99;
}
//月超越界限
if(DS1302_Time[1]-1>12)
{
DS1302_Time[1]=12;
}
//日的判断
if(DS1302_Time[0]%4==0)
{
if(DS1302_Time[1]==2)
{
if(DS1302_Time[2]-1>29)
DS1302_Time[2]=29;
if(DS1302_Time[2]+1>30)
DS1302_Time[2]=1;
}
}
else
{
if(DS1302_Time[1]==2)
{
if(DS1302_Time[2]-1>28)
DS1302_Time[2]=28;
if(DS1302_Time[2]+1>29)
DS1302_Time[2]=1;
}
}
if(DS1302_Time[1]==1||DS1302_Time[1]==3||DS1302_Time[1]==5||DS1302_Time[1]==7||DS1302_Time[1]==8||
DS1302_Time[1]==10||DS1302_Time[1]==12)
{
if(DS1302_Time[2]-1>31)
DS1302_Time[2]=31;
if(DS1302_Time[2]+1>32)
DS1302_Time[2]=1;
}
if(DS1302_Time[1]==2||DS1302_Time[1]==4||DS1302_Time[1]==6||DS1302_Time[1]==11)
{
if(DS1302_Time[2]-1>30)
DS1302_Time[2]=30;
if(DS1302_Time[2]+1>31)
DS1302_Time[2]=1;
}
//小时
if(DS1302_Time[3]-1>24){DS1302_Time[3]=24;}
//分钟
if(DS1302_Time[4]-1>60){DS1302_Time[4]=59;}
//秒钟
if(DS1302_Time[5]-1>60){DS1302_Time[5]=59;}
}
if(timeSet_select==0 && selectflag==1){LCD_ShowString(1,1," ");}
else {LCD_ShowNum(1,1,DS1302_Time[0],2);}
if(timeSet_select==1 && selectflag==1){LCD_ShowString(1,4," ");}
else {LCD_ShowNum(1,4,DS1302_Time[1],2);}
if(timeSet_select==2 && selectflag==1){LCD_ShowString(1,7," ");}
else {LCD_ShowNum(1,7,DS1302_Time[2],2);}
if(timeSet_select==3 && selectflag==1){LCD_ShowString(2,1," ");}
else {LCD_ShowNum(2,1,DS1302_Time[3],2);}
if(timeSet_select==4 && selectflag==1){LCD_ShowString(2,4," ");}
else {LCD_ShowNum(2,4,DS1302_Time[4],2);}
if(timeSet_select==5 && selectflag==1){LCD_ShowString(2,7," ");}
else {LCD_ShowNum(2,7,DS1302_Time[5],2);}
}
void main()
{
Timer0Init();
LCD_Init();
DS1302_Init();
LCD_ShowString(1,1," - - ");
LCD_ShowString(2,1," : : ");
DS1302_SetTime();
while(1)
{
keynum=Key();
if(keynum==1)
{
if(MODE==0)
{
MODE=1;
}
else if(MODE==1)
{
//设定修改后的新日期
MODE=0;DS1302_SetTime();
}
}
switch(MODE)
{
case 0:time_show();break;
case 1:time_set();break;
}
}
}
void Timer0_Routine() interrupt 1
{
static unsigned int T0Count;
TL0 = 0x18; //设置定时初值
TH0 = 0xFC; //设置定时初值
T0Count++;
if(T0Count>=500)
{
T0Count=0;
selectflag=!selectflag;
}
}
8.结果展示
DS1302可控时钟