1 功能:
2 参考程序
2.1 主程序
#include
#include "LCD1602.H"
#include "DS1302.H"
#include "Key.H"
#include "Timer0.H"
unsigned char KeyNum; //记录按键值
unsigned char MODE; //功能模式选择,0为时间显示模式,1为时间调整模式
unsigned char TimeSetSelect; //时间设置选择位,0-5,年、月、日、时、分、秒
unsigned char TimeSetFlashFlag;//闪烁标志位,0为熄灭,1为显示
void TimeShow(void) //显示时间
{
DS1302_ReadTime( ); //读取时间
LCD1602_ShowNum(1,1,DS1302_Time[0],2); //显示年
LCD1602_ShowNum(1,4,DS1302_Time[1],2); //显示月
LCD1602_ShowNum(1,7,DS1302_Time[2],2); //显示日
LCD1602_ShowNum(2,1,DS1302_Time[3],2); //显示时
LCD1602_ShowNum(2,4,DS1302_Time[4],2); //显示分
LCD1602_ShowNum(2,7,DS1302_Time[5],2); //显示秒
}
void TimeSet(void) //调整时间
{
if(KeyNum == 2) //按键2按下,调整时间设置选择位
{
TimeSetSelect++; //时间设置选择位加1
TimeSetSelect%=6; //0-6循环,越界清零
}
if(KeyNum == 3) //调整数值,加1操作
{
DS1302_Time[TimeSetSelect]++;
if(DS1302_Time[0]>99) {DS1302_Time[0] = 0;} //年越界判断
if(DS1302_Time[1]>12) {DS1302_Time[1] = 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)) //大月31天
{
if(DS1302_Time[2]>31) {DS1302_Time[2] = 1;}
}
else if((DS1302_Time[1]==4)||(DS1302_Time[1]==6)||(DS1302_Time[1]==9)||(DS1302_Time[1]==11))
{
if(DS1302_Time[2]>30) {DS1302_Time[2] = 1;}
}
else if(DS1302_Time[1]==2)
{
if(DS1302_Time[0]%4==0)
{
if(DS1302_Time[2]>29) {DS1302_Time[2] = 1;}
}
else
{
if(DS1302_Time[2]>28) {DS1302_Time[2] = 1;}
}
}
if(DS1302_Time[3]>23) {DS1302_Time[3] = 0;} //时越界判断
if(DS1302_Time[4]>59) {DS1302_Time[4] = 0;} //分越界判断
if(DS1302_Time[5]>59) {DS1302_Time[5] = 0;} //秒越界判断
}
if(KeyNum == 4)
{
DS1302_Time[TimeSetSelect]--;
if(DS1302_Time[0]<0) {DS1302_Time[0] = 99;} //年越界判断
if(DS1302_Time[1]<1) {DS1302_Time[1] = 12;} //月越界判断
//日越界判断
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)) //大月31天
{
if(DS1302_Time[2]<1) {DS1302_Time[2] = 31;}
if(DS1302_Time[2]>31) {DS1302_Time[2] = 1;} //在减的过程中,出现越界现象,即1131现象
}
else if((DS1302_Time[1]==4)||(DS1302_Time[1]==6)||(DS1302_Time[1]==9)||(DS1302_Time[1]==11))
{
if(DS1302_Time[2]<1) {DS1302_Time[2] = 30;}
if(DS1302_Time[2]>30) {DS1302_Time[2] = 1;}
}
else if(DS1302_Time[1]==2)
{
if(DS1302_Time[0]%4==0)
{
if(DS1302_Time[2]<1) {DS1302_Time[2] = 29;}
if(DS1302_Time[2]>29) {DS1302_Time[2] = 1;}
}
else
{
if(DS1302_Time[2]<1) {DS1302_Time[2] = 28;}
if(DS1302_Time[2]>28) {DS1302_Time[2] = 1;}
}
}
if(DS1302_Time[3]<0) {DS1302_Time[3] = 23;} //时越界判断
if(DS1302_Time[4]<0) {DS1302_Time[4] = 59;} //分越界判断
if(DS1302_Time[5]<0) {DS1302_Time[5] = 59;} //秒越界判断
}
//更新显示,根据TimeSetSelect和TimeSetFlashFlag判断可完成闪烁功能
if((TimeSetSelect==0)&&(TimeSetFlashFlag==1)) {LCD1602_ShowString(1,1," ");} //显示年
else {LCD1602_ShowNum(1,1,DS1302_Time[0],2);}
if((TimeSetSelect==1)&&(TimeSetFlashFlag==1)) {LCD1602_ShowString(1,4," ");} //显示月
else {LCD1602_ShowNum(1,4,DS1302_Time[1],2);}
if((TimeSetSelect==2)&&(TimeSetFlashFlag==1)) {LCD1602_ShowString(1,7," ");} //显示日
else {LCD1602_ShowNum(1,7,DS1302_Time[2],2);}
if((TimeSetSelect==3)&&(TimeSetFlashFlag==1)) {LCD1602_ShowString(2,1," ");} //显示时
else {LCD1602_ShowNum(2,1,DS1302_Time[3],2);}
if((TimeSetSelect==4)&&(TimeSetFlashFlag==1)) {LCD1602_ShowString(2,4," ");} //显示分
else {LCD1602_ShowNum(2,4,DS1302_Time[4],2);}
if((TimeSetSelect==5)&&(TimeSetFlashFlag==1)) {LCD1602_ShowString(2,7," ");} //显示秒
else {LCD1602_ShowNum(2,7,DS1302_Time[5],2);}
}
void main()
{
//初始化操作
LCD1602_Init();
DS1302_Init();
Timer0Init();
LCD1602_ShowString(1,1," - - "); //静态字符初始化显示
LCD1602_ShowString(2,1," : : ");
DS1302_SetTime( ); //设置初始时间
while(1)
{
KeyNum = Key(); //读取按键值
if(KeyNum == 1) //按键1按下,模式切换
{
if(MODE == 0) {MODE = 1; TimeSetSelect = 0;}
else if(MODE == 1) {MODE = 0;DS1302_SetTime( );} //切换回时间显示模式,同时更新时间数据
}
switch(MODE)
{
case 0: {TimeShow();break;} //时间显示功能
case 1: {TimeSet(); break;} //时间调整功能
}
}
}
//定时器中断,每500ms闪烁标志位TimeSetFlashFlag状态反转一次
void Timer0_Routine() interrupt 1
{
static unsigned int T0Count;
TL0 = 0x18; //设置定时初值
TH0 = 0xFC; //设置定时初值
T0Count++;
if(T0Count>=500) //每500ms进入一次
{
T0Count=0;
TimeSetFlashFlag=!TimeSetFlashFlag; //闪烁标志位取反