/*---------------------------------------------------------------------------
电子智能时钟程序
单 片 机:STC89C52RC
晶 振:12MHz
时钟芯片:DS12887
液 晶 屏:LCM-12864-ST7920
时 间:2010年3月28日
LCM12864使用并口连接方式,PSB、RST接高电平
头文件----------------------------------------------------------------------*/
#include
#include
#include "LCD12864.h"
#include "DS12CR887.h"
#include "DS18B20.h"
#include "nongli.h"
#include "displaytime.h"
#define uint unsigned int
#define uchar unsigned char
/*--------------------定义按键-----------------------------------------------*/
sbit K1 = P3^2; //K1-设置
sbit K2 = P3^1; //K2-确认、返回
sbit K3 = P3^0; //K3-加
sbit K4 = P1^7; //K4-减
sbit K5 = P1^6; //液晶背光控制按键,按一下亮,再按一下灭
sbit BLK = P1^3; //液晶背光控制输出,低电平有效,PNP三极管控制。
void DelayM(uint);
/*定义变量-------------------------------------------------------------------*/
bit q=0, w=0; //调时标志位
/*延时函数 1MS/次------------------------------------------------------------*/
void DelayM(uint a)
{
uchar i;
while( --a != 0) { for(i = 0; i < 125; i++); }
}
/*日期、时间设置函数---------------------------------------------------------*/
void Set_time(uchar sel)
{
signed char address,item;
signed char max,mini;
unsigned char yy,mo,moontemp1,moontemp2;
ds_write(0x0a,0x00); //进入调时,关闭时钟振荡器
write_com(0x30); write_com(0x06);
write_com(0x98); lcm_w_word("设置");
if(sel==5) {lcm_w_word("秒钟");address=0; max=59;mini=0;} //秒钟0
if(sel==4) {lcm_w_word("分钟");address=2; max=59;mini=0;} //分钟2
if(sel==3) {lcm_w_word("小时");address=4; max=23;mini=0;} //小时4
// if(sel==3) {lcm_w_word("星期");address=6; max=7; mini=1;} //星期6 修改为自动计算
// if(sel==2) {lcm_w_word("日期");address=7; max=31;mini=1;} //日期7
if(sel==2) {lcm_w_word("日期"); //日期修改为按月份实际天数调整
address=7; //日期7
mo = ds_read(0x08);//读月
moontemp1=mo/16;
moontemp2=mo%16;
mo=moontemp1*10+moontemp2;//转换成10进制月份数据
yy = ds_read(0x09);//读年
moontemp1=yy/16;
moontemp2=yy%16;
yy=moontemp1*10+moontemp2;//转换成10进制年份数据
if(mo==2&&yy%4!=0){max=28;mini=1;}//平年2月28天
if(mo==2&&yy%4==0){max=29;mini=1;}//闰年2月29天
if(mo==1||mo==3||mo==5||mo==7||mo==8||mo==10||mo==12){max=31;mini=1;}//每月31天的月份
if(mo==4||mo==6||mo==9||mo==11){max=30;mini=1;} //每月30天的月份
}
if(sel==1) {lcm_w_word("月份");address=8; max=12;mini=1;} //月份8
if(sel==0) {lcm_w_word("年份");address=9; max=99;mini=0;} //年份9
item=ds_read(address); //读取DS12CR887某地址上的数值赋给item
if(K3 == 0) {item++;} //K3-加数:1
if(K4 == 0) {item--;} //K4-减数:1
if(item>max) item=mini; //限制数值最大有效范围
if(item
ds_write(address,item); //将调整好的item值写入DS12CR887
displaydate(); //显示日期
displaytime(); //显示时间
displaynl(); //显示农历
displayxq(); //显示星期
}
/*主函数---------------------------------------------------------------------*/
void main()
{
uchar e=0;
K1=1;K2=1;K3=1;K4=1;K5=1;BLK=0;
ds_int(); //时钟芯片初始化
lcm_init(); //液晶初始化
welcome(); //调用欢迎信息
DelayM(2000); //延时
lcm_clr(); //清屏
Clean_12864_GDRAM(); //清屏
while(1)
{
if (w == 0) //正常走时
{
displaydate(); //显示日期
displaynl(); //显示农历
displaytime(); //显示时间
read_temp(); //读取温度
ds1820disp(); //显示温度
displayxq();
}
else {} //否则启动调时
/*----------------------------设置时间--------------------------------------*/
if (K1 == 0)
{
DelayM(20); //按键消抖
if(K1 == 0 && w == 1) //当是调时状态 本键用于调整下一项
{
e++;
if (e >= 7 ) {e = 0;}
while(! K1 ); //等待键松开
Set_time(e); //调整
}
if(K1 == 0 && w == 0) //当是正常状态时就进入调时状态
{
lcm_clr();
Clean_12864_GDRAM(); //清屏
w=1; //进入调时
Set_time(e);
}
while(K1 == 0); //等待键松开
}
/*--------------------------------------------------------------------------*/
if (K2 == 0) // 当在调时状态时就退出调时
{
DelayM(20);
if(K2 == 0 && w == 1)
{
w = 0; //退出调时
e = 0; //"下一项"计数器清0
}
if(K2 == 0 && w == 0)
{
lcm_clr(); Clean_12864_GDRAM();
write_com(0x30); write_com(0x06);
welcome();
while(K2 == 0);
}
lcm_clr(); Clean_12864_GDRAM();
ds_write(0x0a,0x20); //调时完毕,开启时钟振荡器
displaydate();
displaynl();
displaytime();
read_temp();
ds1820disp();
while(K2 == 0);
}
/*加调整--------------------------------------------------------------------*/
if (K3 == 0 && w == 1)
{
DelayM(20);
if(K3 == 0 && w == 1) { Set_time(e); }
// while(K3 == 0);
while(! K3 );
}
/*减调整--------------------------------------------------------------------*/
if (K4 == 0 && w == 1)
{
DelayM(20);
if(K4 == 0 && w == 1) { Set_time(e); }
// while(K4 == 0);
while(! K4 );
}
/*液晶背光控制,按一下亮,再按一下灭----------------------------------------*/
if(K5 == 0)
{
DelayM(20); q = ~q; //标志位取反
if(q){BLK = BLK | 1;}
else {BLK = BLK & 0;}
while(K5 == 0);
}
}
}
/*结束----------------------------------------------------------------------*/