#include <reg52.h>
#include <stdio.h>
#include "delay.h"
#include "string.h"
#include "ds1302.h"
#include "LCD1602.H"
#include "NONGLICONVERSION.H"
#define uchar unsigned char
#define uint unsigned int
sbit key1=P1^0//日期设置;时间设置;模式选择
sbit key2=P1^1//加
sbit key3=P1^2//减
sbit key4=P1^3//确认复位
sbit bell=P1^7//闹钟
uint count
bit flag,dp
uchar displaytemp[16]//定义显示区域临时存储数组
code uchar tab[] = {'0','1','2','3','4','5','6','7','8','9'}
uchar timeUPtab[6] = {99,12,31,24,59,59}
uchar timeDOWNtab[6] = {0,1,1,0,0,0}
uchar timetab[6] = {20,11,6,16,4,23}//年 月 日 时 分 秒
uchar state
//状态0:工作状态
//状态1:年调整
//状态2:月调整
//状态3:日调整
//状态4:时调整
//状态5:分调整
//状态6:秒调整
//****************函数声明************************************//
void InitTIMER0(void)//定时器初始化
void KeyDo()//按键 *** 作函数
void Read_time()//时间处理函数
void display() // 显函数
void Conver_week(bit c,uchar year,uchar month,uchar day)
void Conversion(bit c,uchar year,uchar month,uchar day)
//***********************************************************************//
void Read_time()
{
uchar i = 0
Read_RTC()//读日历
for(i=0i<6i++)
timetab[i] = l_tmpdate[i]
}
void Set_time()
{
uchar i
for(i=0i<6i++)
{
l_tmpdate[i]=timetab[i]
Set_RTC(i)
}
}
void NL_read(uchar nian,uchar yue,uchar ri)
{
uchar tmp
tmp=nian/10
nian=nian%10
nian=nian+tmp*16//十进制转BCD处理
tmp=yue/10
yue=yue%10
yue=yue+tmp*16//十进制转BCD处理
tmp=ri/10
ri=ri%10
ri=ri+tmp*16//十进制转BCD处理
Conversion(0,nian,yue,ri)
tmp=year_moon/16
year_moon=year_moon%16
year_moon=year_moon+tmp*10//BCD转十进制
tmp=month_moon/16
month_moon=month_moon%16
month_moon=month_moon+tmp*10//BCD转十进制
tmp=day_moon/16
day_moon=day_moon%16
day_moon=day_moon+tmp*10//BCD转十进制
}
void display() // 显函数
{
uchar displaytemp1[8]={0,0,0,0,0,0,0,0}
uchar displaytemp2[8]={0,0,0,0,0,0,0,0}
// 分状态显示
switch(state)
{
case 0:
{
sprintf(displaytemp1," - - ")//日期
displaytemp1[0]=tab[timetab[0]/10]
displaytemp1[1]=tab[timetab[0]%10]
displaytemp1[3]=tab[timetab[1]/10]
displaytemp1[4]=tab[timetab[1]%10]
displaytemp1[6]=tab[timetab[2]/10]
displaytemp1[7]=tab[timetab[2]%10]
displaytemp2[0]=tab[timetab[3]/10]
displaytemp2[1]=tab[timetab[3]%10]
displaytemp2[2]=':'
displaytemp2[3]=tab[timetab[4]/10]
displaytemp2[4]=tab[timetab[4]%10]
displaytemp2[5]=':'
displaytemp2[6]=tab[timetab[5]/10]
displaytemp2[7]=tab[timetab[5]%10]
}break
case 1:
{
displaytemp1[0]=tab[timetab[0]/10]
displaytemp1[1]=tab[timetab[0]%10]
}break
case 2:
{
displaytemp1[3]=tab[timetab[1]/10]
displaytemp1[4]=tab[timetab[1]%10]
}break
case 3:
{
displaytemp1[6]=tab[timetab[2]/10]
displaytemp1[7]=tab[timetab[2]%10]
}break
case 4:
{
displaytemp2[0]=tab[timetab[3]/10]
displaytemp2[1]=tab[timetab[3]%10]
}break
case 5:
{
displaytemp2[3]=tab[timetab[4]/10]
displaytemp2[4]=tab[timetab[4]%10]
}break
case 6:
{
displaytemp2[6]=tab[timetab[5]/10]
displaytemp2[7]=tab[timetab[5]%10]
}break
default:break
}
// 输出到屏幕
disstring(0x80,displaytemp1,8)
disstring(0xc0,displaytemp2,8)
}
void main()
{
LCD_init() //lcd初始化
InitTIMER0() //定时器初始化
Set_time() //qq:289440
Read_time() //426
bell=1
while(1)
{
display()
if(flag)
{
KeyDo()
flag=0
}
delay(100)
}
}
/**********************定时器初始化***************************/
void InitTIMER0(void)
{
TMOD|=0x01//定时器设置 16位
TH0=0xfc//初始化值
TL0=0x18
ET0=1
TR0=1
EA=1
}
void tim(void) interrupt 1 using 1//中断,用于扫描键盘
{
TH0 = 0xfc
TL0 = 0x18
flag=1
}
void KeyDo()//按键 *** 作函数
{
char up = 0,down = 0
key1=1key4=1
if(key1==0)
{
while(key1==0)
state++
if(state>6)
state = 1
}
if(key4==0)
{
while(key4==0)
state = 0
Set_time()
}
if(key2==0)
{
while(key2==0)
up = 1
}
if(key3==0)
{
while(key3==0)
down = 1
}
switch(state)
{
case 0:
{
Read_time()
}break
case 1:
case 2:
case 3:
case 4:
case 5:
case 6:
{
// 每月时间计算
switch(timetab[1])
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:timeUPtab[2] = 31break
case 4:
case 6:
case 9:
case 11:timeUPtab[2] = 30break
case 2:
{
if(timetab[0] % 4 == 0)
timeUPtab[2] =29
else
timeUPtab[2] =28
}
default:break
}
if(up)
timetab[state-1]++
if(timetab[state-1] >timeUPtab[state-1])
timetab[state-1] = timeUPtab[state-1]
if(down)
timetab[state-1]--
if(timetab[state-1] <timeDOWNtab[state-1])
timetab[state-1] = timeDOWNtab[state-1]
}break
default:break
}
}
while(1){
if(SetState==0)
{
Ds1302ReadTime()
}
else
{
if(K1==0) //检测按键K1是否按下
{
Delay10ms() //消除抖动
if(K1==0)
{
SetPlace++
if(SetPlace>=7)
SetPlace=0
}
while((i<50)&&(K1==0)) //检测按键是否松开
{
Delay10ms()
i++
}
i=0
}
if(K2==0) //检测按键K2是否按下
{
Delay10ms() //消除抖动
if(K2==0)
{
TIME[SetPlace]++
if((TIME[SetPlace]&0x0f)>9) //换成BCD码。
{
TIME[SetPlace]=TIME[SetPlace]+6
}
if((TIME[SetPlace]>=0x60)&&(SetPlace<2)) //分秒只能到59
{
TIME[SetPlace]=0
}
if((TIME[SetPlace]>=0x24)&&(SetPlace==2)) //小时只能到23
{
TIME[SetPlace]=0
}
if((TIME[SetPlace]>=0x32)&&(SetPlace==3)) //日只能到31
{
TIME[SetPlace]=0
}
if((TIME[SetPlace]>=0x13)&&(SetPlace==4)) //月只能到12
{
TIME[SetPlace]=0
}
if((TIME[SetPlace]>=0x7)&&(SetPlace==5)) //周只能到7
{
TIME[SetPlace]=1
}
}
while((i<50)&&(K2==0)) //检测按键是否松开
{
Delay10ms()
i++
}
i=0
}
}
//------------------------------------
if(K3==0) //检测按键K3是否按下
{
Delay10ms() //消除抖动
if(K3==0)
{
if(TIME[SetPlace]!=0){
TIME[SetPlace]--
if((TIME[SetPlace]&0x0f)>9) //换成BCD码。
{
TIME[SetPlace]=TIME[SetPlace]+6
}
}
else {
if((TIME[SetPlace]>=0x00)&&(SetPlace<2)) //分秒只能到59
{
TIME[SetPlace]=0
}
if((TIME[SetPlace]>=0x00)&&(SetPlace==2)) //小时只能到23
{
TIME[SetPlace]=0
}
if((TIME[SetPlace]>=0x00)&&(SetPlace==3)) //日只能到31
{
TIME[SetPlace]=0
}
if((TIME[SetPlace]>=0x00)&&(SetPlace==4)) //月只能到12
{
TIME[SetPlace]=0
}
if((TIME[SetPlace]>=0x00)&&(SetPlace==5)) //周只能到7
{
TIME[SetPlace]=1
}
}
}
while((i<50)&&(K2==0)) //检测按键是否松开
{
Delay10ms()
i++
}
i=0
}
}
//------------------------------------
LcdDisplay()
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)