#include<reg51.h>
#include<intrins.h>
#define uchar unsigned char
#define uint unsigned int
uchar code digit[]={"0123456789"}; //定义字符数组显示数字
uchar code Error[]="Error!Check!"; //说明没有检测到DS18B20
uchar code table[]="Alarm Clock";
uchar code table1[]="0123456789";
uchar code table2[]="WEEK";
uchar code table3[]="Today is fine!";
uchar code table4[]="HELLO!";
uchar code table5[]="Enjoy Your Life!";
uchar code table6[]="Happy new year!!";//1.1
uchar code table7[]="Valentine's Day"; //2.14
uchar code table8[]="Qing Ming jie!";//4.5
uchar code table9[]="Happy Labor Day!";//5.1
uchar code table10[]="Duan Wu jie!";//6.15
uchar code table11[]="Army day!";//8.1
uchar code table12[]="Teacher's Day!";//9.10
uchar code table13[]="National Day!";//10.1
uchar code table14[]="zhenzhen happy brithday!";//11.24
/*******************************************************************************
以下是对液晶模块的操作程序
*******************************************************************************/
sbit RS=P2^0; //寄存器选择位,将RS位定义为P2.0引脚
sbit RW=P2^1; //读写选择位,将RW位定义为P2.1引脚
sbit E=P2^2; //使能信号位,将E位定义为P2.2引脚
sbit BF=P0^7; //忙碌标志位,,将BF位定义为P0.7引脚
sbit DQ=P2^6;
sbit T_RST=P1^5;
sbit T_CLK=P1^3;
sbit T_IO=P1^4;
sbit beep=P2^4;
sbit key1=P3^0; //调闹钟的小时即x
sbit key2=P3^1; //调闹钟的分钟即y
sbit key3=P3^2; //显示切换
sbit key4=P3^3;
sbit ACC0=ACC^0;
sbit ACC7=ACC^7;
uchar time; //设置全局变量,专门用于严格延时
static uchar x=0x08,y=30;
uchar t_sec,sec1,sec2;
uchar t_min,min1,min2;
uchar t_hour,hour1,hour2;
uchar t_mon,mon1,mon2;
uchar t_day,day1,day2;
uchar t_year,year1,year2;
uchar week,week1;
uchar com,inf;
void tixing(unsigned char mm,unsigned dd);
void show();
void delay1(uchar t)
{
uchar a,b;
for(a=t;a>0;a--)
for(b=110;b>0;b--);
}
/*****************************************************
函数功能:延时1ms
(3j+2)*i=(3×33+2)×10=1010(微秒),可以认为是1毫秒
***************************************************/
void delay1ms()
{
uchar i,j;
for(i=0;i<10;i++)
for(j=0;j<33;j++)
;
}
/*****************************************************
函数功能:延时若干毫秒
入口参数:n
***************************************************/
void delaynms(uchar n)
{
uchar i;
for(i=0;i<n;i++)
delay1ms();
}
/*****************************************************
函数功能:判断液晶模块的忙碌状态
返回值:result。result=1,忙碌;result=0,不忙
***************************************************/
bit BusyTest(void)
{
bit result;
RS=0; //根据规定,RS为低电平,RW为高电平时,可以读状态
RW=1;
E=1; //E=1,才允许读写
_nop_(); //空操作
_nop_();
_nop_();
_nop_(); //空操作四个机器周期,给硬件反应时间
result=BF; //将忙碌标志电平赋给result
E=0; //将E恢复低电平
return result;
}
/*****************************************************
函数功能:将模式设置指令或显示地址写入液晶模块
入口参数:dictate
***************************************************/
void WriteInstruction (uchar dictate)
{
while(BusyTest()==1); //如果忙就等待
RS=0; //根据规定,RS和R/W同时为低电平时,可以写入指令
RW=0;
E=0; //E置低电平(根据表8-6,写指令时,E为高脉冲,
// 就是让E从0到1发生正跳变,所以应先置"0"
_nop_();
_nop_(); //空操作两个机器周期,给硬件反应时间
P0=dictate; //将数据送入P0口,即写入指令或地址
_nop_();
_nop_();
_nop_();
_nop_(); //空操作四个机器周期,给硬件反应时间
E=1; //E置高电平
_nop_();
_nop_();
_nop_();
_nop_(); //空操作四个机器周期,给硬件反应时间
E=0; //当E由高电平跳变成低电平时,液晶模块开始执行命令
}
/*****************************************************
函数功能:指定字符显示的实际地址
入口参数:x
***************************************************/
void WriteAddress(unsigned char x)
{
WriteInstruction(x|0x80); //显示位置的确定方法规定为"80H+地址码x"
}
/*****************************************************
函数功能:将数据(字符的标准ASCII码)写入液晶模块
入口参数:y(为字符常量)
***************************************************/
void WriteData(uchar y)
{
while(BusyTest()==1);
RS=1; //RS为高电平,RW为低电平时,可以写入数据
RW=0;
E=0; //E置低电平(根据表8-6,写指令时,E为高脉冲,
// 就是让E从0到1发生正跳变,所以应先置"0"
P0=y; //将数据送入P0口,即将数据写入液晶模块
_nop_();
_nop_();
_nop_();
_nop_(); //空操作四个机器周期,给硬件反应时间
E=1; //E置高电平
_nop_();
_nop_();
_nop_();
_nop_(); //空操作四个机器周期,给硬件反应时间
E=0; //当E由高电平跳变成低电平时,液晶模块开始执行命令
}
/*****************************************************
函数功能:对LCD的显示模式进行初始化设置
***************************************************/
void LcdInitiate(void)
{
delaynms(15); //延时15ms,首次写指令时应给LCD一段较长的反应时间
WriteInstruction(0x38); //显示模式设置:16×2显示,5×7点阵,8位数据接口
delaynms(5); //延时5ms ,给硬件一点反应时间
WriteInstruction(0x38);
delaynms(5); //延时5ms ,给硬件一点反应时间
WriteInstruction(0x38); //连续三次,确保初始化成功
delaynms(5); //延时5ms ,给硬件一点反应时间
WriteInstruction(0x0c); //显示模式设置:显示开,无光标,光标不闪烁
delaynms(5); //延时5ms ,给硬件一点反应时间
WriteInstruction(0x06); //显示模式设置:光标右移,字符不移
delaynms(5); //延时5ms ,给硬件一点反应时间
WriteInstruction(0x01); //清屏幕指令,将以前的显示内容清除
delaynms(5); //延时5ms ,给硬件一点反应时间
}
void write_com(uchar com)//向1602写指令
{
RS=0;
RW=0;
P0=com;
delaynms(10);
E=1;
delaynms(10);
E=0;
}
/*和DS1302时钟芯片有关的函数*/
/*******************************************
向1302写一个字节
*******************************************/
void input_BYTE(uchar dat)
{
uchar i;
ACC=dat;
for(i=8;i>0;i--)
{
T_IO=ACC0;
T_CLK=1;
T_CLK=0;
ACC=(ACC>>1);
}
}
/*******************************************
1302读出一个字节
*******************************************/
uchar output_BYTE()
{
uchar i;
for(i=8;i>0;i--)
{
ACC=(ACC>>1);
ACC7=T_IO;
T_CLK=1;
T_CLK=0;
}
return (ACC);
}
/*******************************************
写数据
*******************************************/
void write_1302(uchar add,uchar dat)
{
T_RST=0;
T_CLK=0;
T_RST=1;
input_BYTE(add);
input_BYTE(dat);
T_CLK=1;
T_RST=0;
}
/*******************************************
读数据
*******************************************/
uchar read_1302(uchar add)
{
uchar inf;
T_RST=0;
T_CLK=0;
T_RST=1;
input_BYTE(add);
inf=output_BYTE();
T_CLK=1;
T_RST=0;
return (inf);
}
void init_1302()
{
write_1302(0x8e,0x00);//关闭写保护;
// write_1302(0x90,0xaa);//设置充电方式;
write_1302(0x80,0x00);//秒寄存器初始化;
write_1302(0x82,0x35);//分.......
write_1302(0x84,0x20);//时.......
write_1302(0x86,0x25);//日........
write_1302(0x88,0x11);//月.......
write_1302(0x8a,0x05);//星期...
write_1302(0x8c,0x16);//年......
write_1302(0x8e,0x80);//打开写保护;
}
/*以下是温度芯片DS18b20的相关函数*/
/*****************************************************
函数功能:将DS18B20传感器初始化,读取应答信号
出口参数:flag
***************************************************/
bit Init_DS18B20(void)
{
bit flag; //储存DS18B20是否存在的标志,flag=0,表示存在;flag=1,表示不存在
DQ = 1; //先将数据线拉高
for(time=0;time<2;time++) //略微延时约6微秒
;
DQ = 0; //再将数据线从高拉低,要求保持480~960us
for(time=0;time<200;time++) //略微延时约600微秒
; //以向DS18B20发出一持续480~960us的低电平复位脉冲
DQ = 1; //释放数据线(将数据线拉高)
for(time=0;time<10;time++)
; //延时约30us(释放总线后需等待15~60us让DS18B20输出存在脉冲)
flag=DQ; //让单片机检测是否输出了存在脉冲(DQ=0表示存在)
for(time=0;time<200;time++) //延时足够长时间,等待存在脉冲输出完毕
;
return (flag); //返回检测成功标志
}
/*****************************************************
函数功能:从DS18B20读取一个字节数据
出口参数:dat
***************************************************/
uchar ReadOneChar(void)
{
uchar i=0;
uchar dat; //储存读出的一个字节数据
for (i=0;i<8;i++)
{
DQ =1; // 先将数据线拉高
_nop_(); //等待一个机器周期
DQ = 0; //单片机从DS18B20读书据时,将数据线从高拉低即启动读时序
dat>>=1;
_nop_(); //等待一个机器周期
DQ = 1; //将数据线"人为"拉高,为单片机检测DS18B20的输出电平作准备
for(time=0;time<2;time++)
; //延时约6us,使主机在15us内采样
if(DQ==1)
dat|=0x80; //如果读到的数据是1,则将1存入dat
else
dat|=0x00;//如果读到的数据是0,则将0存入dat
//将单片机检测到的电平信号DQ存入r[i]
for(time=0;time<8;time++)
; //延时3us,两个读时序之间必须有大于1us的恢复期
}
return(dat); //返回读出的十进制数据
}
/*****************************************************
函数功能:向DS18B20写入一个字节数据
入口参数:dat
***************************************************/
void WriteOneChar(uchar dat)
{
uchar i=0;
for (i=0; i<8; i++)
{
DQ =1; // 先将数据线拉高
_nop_(); //等待一个机器周期
DQ=0; //将数据线从高拉低时即启动写时序
DQ=dat&0x01; //利用与运算取出要写的某位二进制数据,
//并将其送到数据线上等待DS18B20采样
for(time=0;time<10;time++)
;//延时约30us,DS18B20在拉低后的约15~60us期间从数据线上采样
DQ=1; //释放数据线
for(time=0;time<1;time++)
;//延时3us,两个写时序间至少需要1us的恢复期
dat>>=1; //将dat中的各二进制位数据右移1位
}
for(time=0;time<4;time++)
; //稍作延时,给硬件一点反应时间
}
/*****************************************************
函数功能:显示没有检测到DS18B20
***************************************************/
void display_error(void)
{
uchar i;
WriteAddress(0x00); //写显示地址,将在第1行第1列开始显示
i = 0; //从第一个字符开始显示
while(Error[i] != '\0') //只要没有写到结束标志,就继续写
{
WriteData(Error[i]); //将字符常量写入LCD
i++; //指向下一个字符
delaynms(100); //延时100ms较长时间,以看清关于显示的说明
}
while(1) //进入死循环,等待查明原因
;
}
/*****************************************************
函数功能:显示温度的小数点
***************************************************/
void display_dot(void)
{
WriteAddress(0x80+0x40+0x03); //写显示地址,将在第2行第10列开始显示
WriteData('.'); //将小数点的字符常量写入LCD
delaynms(50); //延时1ms给硬件一点反应时间
}
/*****************************************************
函数功能:显示温度的单位(Cent)
***************************************************/
void display_cent(void)
{
WriteAddress(0x80+0x40+0x05); //写显示地址,将在第2行第13列开始显示
WriteData(0xdf);
WriteAddress(0x80+0x40+0x06);
WriteData('C');
}
/*****************************************************
函数功能:显示温度的整数部分
入口参数:x
***************************************************/
void display_temp1(unsigned char x)
{
uchar j,k,l; //j,k,l分别储存温度的百位、十位和个位
j=x/100; //取百位
k=(x%100)/10; //取十位
l=x%10; //取个位
WriteAddress(0x80+0x40); //写显示地址,将在第2行第7列开始显示
WriteData(digit[j]); //将百位数字的字符常量写入LCD
WriteData(digit[k]); //将十位数字的字符常量写入LCD
WriteData(digit[l]); //将个位数字的字符常量写入LCD
delaynms(50); //延时1ms给硬件一点反应时间
}
/*****************************************************
函数功能:显示温度的小数数部分
入口参数:x
***************************************************/
void display_temp2(uchar x)
{
WriteAddress(0x80+0x40+0x04); //写显示地址,将在第2行第11列开始显示
WriteData(digit[x]); //将小数部分的第一位数字字符常量写入LCD
delaynms(50); //延时1ms给硬件一点反应时间
}
/*****************************************************
函数功能:做好读温度的准备
***************************************************/
void ReadyReadTemp(void)
{
Init_DS18B20(); //将DS18B20初始化
WriteOneChar(0xCC); // 跳过读序号列号的操作
WriteOneChar(0x44); // 启动温度转换
for(time=0;time<100;time++)
; //温度转换需要一点时间
Init_DS18B20(); //将DS18B20初始化
WriteOneChar(0xCC); //跳过读序号列号的操作
WriteOneChar(0xBE); //读取温度寄存器,前两个分别是温度的低位和高位
}
//按键扫描函数
void keyscan()
{
if(key1==0)//设置键按下
{
delay(5);//延时
if(key1==0)
{
beep=0;//蜂鸣器短响一声
delay(20);
beep=1;
alarmflag=0;/*按下标志,用于防止调节时间时鸣叫*/
while(!key1);
keynum++;
if(keynum==9)
keynum=1;//返回
switch(keynum)
{
case 1:TR0=0;//关闭定时器
writecom(h2+0x07);//秒的位置
writecom(0x0f);//设置为光标闪烁
temp=(miao)/10*16+(miao)%10;//秒化为bcd码
write_1302(0x8e,0x00);
write_1302(0x80,0x80|temp);//秒数据写入
write_1302(0x8e,0x80);
break;
case 2:writecom(h2+4);//分的位置
break;//不用再次设置为闪烁状态了
case 3:writecom(h2+1);//时的位置
break;
case 4:writecom(h1+0x0e);//星期的位置
break;
case 5:writecom(h1+0x09);//日的位置
break;
case 6:writecom(h1+0x06);//月的位置
break;
case 7:writecom(h1+0x3);//年的位置
break;
case 8:writecom(0x0c);//第8次,光标不闪烁
alarmflag=1;/*设置标志重新设置为1*/
TR0=1;//重新打开定时器
temp=(miao)/10*16+(miao)%10;
write_1302(0x8e,0x00);
write_1302(0x80,0x00|temp);//写入秒
write_1302(0x8e,0x80);
break;
}
}
}
if(keynum!=0)//当设置键按下时才能操作
{
if(key2==0)//加键
{
delay(5);
if(key2==0)
{
beep=0;//蜂鸣器短响
delay(20);
beep=1;
while(!key2);//按键松开
switch(keynum)
{
case 1:miao++;//
if(miao==60) miao=0;
writetime(0x06,miao);/*在十位的位置写入,因为为两位数,个位数自动再后面显示*/
temp=(miao)/10*16+(miao)%10;//转换为bcd码
write_1302(0x8e,0x00);//允许写
write_1302(0x80,temp);// 写入秒
write_1302(0x8e,0x80);//打开保护
writecom(h2+0x07);//液晶模式为写入后自动右移,在此返回原来位置
break;
case 2:fen++;
if(fen==60) fen=0;
writetime(0x03,fen);//在十位数位置开始写入
temp=(fen)/10*16+(fen)%10;//转换为bcd码
write_1302(0x8e,0x00);//允许写
write_1302(0x82,temp);//写入分
write_1302(0x8e,0x80);//打开保护
writecom(h2+4);//返回个位数的位置
break;
case 3:shi++;
if(shi==24) shi=0;
writetime(0,shi);//在0位开始写入
temp=(shi)/10*16+(shi)%10;//转换为bcd码
write_1302(0x8e,0x00);//允许写
write_1302(0x84,temp);//写入时
write_1302(0x8e,0x80);//打开保护
writecom(h2+1);//返回到个位位置
break;
case 4:week++;
if(week==8) week=1;
writecom(h1+0x0e);//显示位置
writeweek(week);//写入星期
temp=(week)/10*16+(week)%10;
write_1302(0x8e,0x00);//允许写入
write_1302(0x8a,temp);//写入周
write_1302(0x8e,0x80);//打开保护
writecom(h1+0x0e);/*此处有疑惑:返回原来位置?*/
break;
case 5:ri++;
if(ri==32) ri=1;
writeday(8,ri);//注意是在十位开始写入
temp=(ri)/10*16+(ri)%10;//转换为bcd码
write_1302(0x8e,0x00);//允许写
write_1302(0x86,temp);//写入日
write_1302(0x8e,0x80);//打开保护
writecom(h1+9);//返回个位数
break;
case 6:yue++;
if(yue==13) yue=1;
writeday(5,yue);//在十位开始写入
temp=(yue)/10*16+(yue)%10;//转换为bcd码
write_1302(0x8e,0x00);//允许写
write_1302(0x88,temp);//写入月
write_1302(0x8e,0x80);//打开保护
writecom(h1+6);//返回个位位置
break;
case 7:nian++;
if(nian==100) nian=0;
writeday(2,nian);//在第一行第三个字符开始写入
temp=(nian)/10*16+(nian)%10;//转换为bcd码
write_1302(0x8e,0x00);//允许写
write_1302(0x8c,temp);//写入年
write_1302(0x8e,0x80);//打开保护
writecom(h1+3);//返回个位位置
break;
}
}
}
//以下是减的函数
if(key3==0)
{
delay(5);//消抖
if(key3==0)
{
beep=0;//蜂鸣器短响一下
delay(20);
beep=1;
while(!key3);
switch(keynum)
{
case 1:miao--;/*此处有疑问:无符号数据是否要修改*/
if(miao==-1) miao=59;//减到-1返回59
writetime(0x06,miao);//在十位数写入
temp=(miao)/10*16+(miao)%10;//转换为bcd码
write_1302(0x8e,0x00);//允许写
write_1302(0x80,temp);//写入秒
write_1302(0x8e,0x80);//打开保护
writecom(h2+0x07);//返回个位位置
break;
case 2:fen--;
if(fen==-1) fen=59;
writetime(0x03,fen);//在十位数位置写入
temp=(fen)/10*16+(fen)%10;//转换为bcd码
write_1302(0x8e,0x00);//允许写入
write_1302(0x82,temp);//写入分
write_1302(0x8e,0x80);//打开保护
writecom(h2+4);//返回个位数位置
break;
case 3:shi--;
if(shi==-1) shi=23;
writetime(0,shi);//在0位开始写入
temp=(shi)/10*16+(shi)%10;//转换为bcd码
write_1302(0x8e,0x00);//允许写入
write_1302(0x84,temp);//写入时
write_1302(0x8e,0x80);//打开保护
writecom(h2+1);//返回到个位位置
break;
case 4:week--;
if(week==0) week=7;
writecom(h1+0x0e);//显示位置
writeweek(week);//写入星期
temp=(week)/10*16+(week)%10;//转换为bcd码
write_1302(0x8e,0x00);//允许写入
write_1302(0x8a,temp);//写入周
write_1302(0x8e,0x80);//打开保护
writecom(h1+0x0e);//返回原来位置
break;
case 5:ri--;
if(ri==0) ri=31;
writeday(8,ri);//在十位开始显示
temp=(ri)/10*16+(ri)%10;//转换为bcd码
write_1302(0x8e,0x00);//允许写入
write_1302(0x86,temp);//写入日
write_1302(0x8e,0x80);//打开保护
writecom(h1+9);//返回个位数
break;
case 6:yue--;
if(yue==0) yue=12;
writeday(5,yue);//在十位数位置开始写入
temp=(yue)/10*16+(yue)%10;//转换为bcd码
write_1302(0x8e,0x00);//允许写入
write_1302(0x88,temp);//写入月
write_1302(0x8e,0x80);//打开保护
writecom(h1+6);//返回到个位位置
break;
case 7:nian--;
if(nian==-1) nian=99;
writeday(2,nian);//第一行第三个字符开始写入
temp=(nian)/10*16+(nian)%10;//转换为bcd码
write_1302(0x8e,0x00);//允许写入
write_1302(0x8c,temp);//写入年
write_1302(0x8e,0x80);//打开保护
writecom(h1+3);//返回在年的尾数位置
break;
}
}
}
}
}
//初始化的函数
void init()//定时器初始化函数
{
TMOD=0x11;//设置为定时器0和1的工作方式1
TH0=(65536-60000)/256;//10毫秒
TL0=(65536-60000)%256;
TH1=(65536-50000)/256;//设置定时器1的初值
TL1=(65536-50000)%256;
EA=1;
ET0=1;//允许T0中断
TR0=1;//启动中断
ET1=1;/*允许T1中断(未打开)*/
TR1=1;//打开
}
/*////////以下为转换农历的相关函数////////////////// */
code uchar year_code[597]={
0x04,0xAe,0x53, //1901 0
0x0A,0x57,0x48, //1902 3
0x55,0x26,0xBd, //1903 6
0x0d,0x26,0x50, //1904 9
0x0d,0x95,0x44, //1905 12
0x46,0xAA,0xB9, //1906 15
0x05,0x6A,0x4d, //1907 18
0x09,0xAd,0x42, //1908 21
0x24,0xAe,0xB6, //1909
0x04,0xAe,0x4A, //1910
0x6A,0x4d,0xBe, //1911
0x0A,0x4d,0x52, //1912
0x0d,0x25,0x46, //1913
0x5d,0x52,0xBA, //1914
0x0B,0x54,0x4e, //1915
0x0d,0x6A,0x43, //1916
0x29,0x6d,0x37, //1917
0x09,0x5B,0x4B, //1918
0x74,0x9B,0xC1, //1919
0x04,0x97,0x54, //1920
0x0A,0x4B,0x48, //1921
0x5B,0x25,0xBC, //1922
0x06,0xA5,0x50, //1923
0x06,0xd4,0x45, //1924
0x4A,0xdA,0xB8, //1925
0x02,0xB6,0x4d, //1926
0x09,0x57,0x42, //1927
0x24,0x97,0xB7, //1928
0x04,0x97,0x4A, //1929
0x66,0x4B,0x3e, //1930
0x0d,0x4A,0x51, //1931
0x0e,0xA5,0x46, //1932
0x56,0xd4,0xBA, //1933
0x05,0xAd,0x4e, //1934
0x02,0xB6,0x44, //1935
0x39,0x37,0x38, //1936
0x09,0x2e,0x4B, //1937
0x7C,0x96,0xBf, //1938
0x0C,0x95,0x53, //1939
0x0d,0x4A,0x48, //1940
0x6d,0xA5,0x3B, //1941
0x0B,0x55,0x4f, //1942
0x05,0x6A,0x45, //1943
0x4A,0xAd,0xB9, //1944
0x02,0x5d,0x4d, //1945
0x09,0x2d,0x42, //1946
0x2C,0x95,0xB6, //1947
0x0A,0x95,0x4A, //1948
0x7B,0x4A,0xBd, //1949
0x06,0xCA,0x51, //1950
0x0B,0x55,0x46, //1951
0x55,0x5A,0xBB, //1952
0x04,0xdA,0x4e, //1953
0x0A,0x5B,0x43, //1954
0x35,0x2B,0xB8, //1955
0x05,0x2B,0x4C, //1956
0x8A,0x95,0x3f, //1957
0x0e,0x95,0x52, //1958
0x06,0xAA,0x48, //1959
0x7A,0xd5,0x3C, //1960
0x0A,0xB5,0x4f, //1961
0x04,0xB6,0x45, //1962
0x4A,0x57,0x39, //1963
0x0A,0x57,0x4d, //1964
0x05,0x26,0x42, //1965
0x3e,0x93,0x35, //1966
0x0d,0x95,0x49, //1967
0x75,0xAA,0xBe, //1968
0x05,0x6A,0x51, //1969
0x09,0x6d,0x46, //1970
0x54,0xAe,0xBB, //1971
0x04,0xAd,0x4f, //1972
0x0A,0x4d,0x43, //1973
0x4d,0x26,0xB7, //1974
0x0d,0x25,0x4B, //1975
0x8d,0x52,0xBf, //1976
0x0B,0x54,0x52, //1977
0x0B,0x6A,0x47, //1978
0x69,0x6d,0x3C, //1979
0x09,0x5B,0x50, //1980
0x04,0x9B,0x45, //1981
0x4A,0x4B,0xB9, //1982
0x0A,0x4B,0x4d, //1983
0xAB,0x25,0xC2, //1984
0x06,0xA5,0x54, //1985
0x06,0xd4,0x49, //1986
0x6A,0xdA,0x3d, //1987
0x0A,0xB6,0x51, //1988
0x09,0x37,0x46, //1989
0x54,0x97,0xBB, //1990
0x04,0x97,0x4f, //1991
0x06,0x4B,0x44, //1992
0x36,0xA5,0x37, //1993
0x0e,0xA5,0x4A, //1994
0x86,0xB2,0xBf, //1995
0x05,0xAC,0x53, //1996
0x0A,0xB6,0x47, //1997
0x59,0x36,0xBC, //1998
0x09,0x2e,0x50, //1999 294
0x0C,0x96,0x45, //2000 297
0x4d,0x4A,0xB8, //2001
0x0d,0x4A,0x4C, //2002
0x0d,0xA5,0x41, //2003
0x25,0xAA,0xB6, //2004
0x05,0x6A,0x49, //2005
0x7A,0xAd,0xBd, //2006
0x02,0x5d,0x52, //2007
0x09,0x2d,0x47, //2008
0x5C,0x95,0xBA, //2009
0x0A,0x95,0x4e, //2010
0x0B,0x4A,0x43, //2011
0x4B,0x55,0x37, //2012
0x0A,0xd5,0x4A, //2013
0x95,0x5A,0xBf, //2014
0x04,0xBA,0x53, //2015
0x0A,0x5B,0x48, //2016
0x65,0x2B,0xBC, //2017
0x05,0x2B,0x50, //2018
0x0A,0x93,0x45, //2019
0x47,0x4A,0xB9, //2020
0x06,0xAA,0x4C, //2021
0x0A,0xd5,0x41, //2022
0x24,0xdA,0xB6, //2023
0x04,0xB6,0x4A, //2024
0x69,0x57,0x3d, //2025
0x0A,0x4e,0x51, //2026
0x0d,0x26,0x46, //2027
0x5e,0x93,0x3A, //2028
0x0d,0x53,0x4d, //2029
0x05,0xAA,0x43, //2030
0x36,0xB5,0x37, //2031
0x09,0x6d,0x4B, //2032
0xB4,0xAe,0xBf, //2033
0x04,0xAd,0x53, //2034
0x0A,0x4d,0x48, //2035
0x6d,0x25,0xBC, //2036
0x0d,0x25,0x4f, //2037
0x0d,0x52,0x44, //2038
0x5d,0xAA,0x38, //2039
0x0B,0x5A,0x4C, //2040
0x05,0x6d,0x41, //2041
0x24,0xAd,0xB6, //2042
0x04,0x9B,0x4A, //2043
0x7A,0x4B,0xBe, //2044
0x0A,0x4B,0x51, //2045
0x0A,0xA5,0x46, //2046
0x5B,0x52,0xBA, //2047
0x06,0xd2,0x4e, //2048
0x0A,0xdA,0x42, //2049
0x35,0x5B,0x37, //2050
0x09,0x37,0x4B, //2051
0x84,0x97,0xC1, //2052
0x04,0x97,0x53, //2053
0x06,0x4B,0x48, //2054
0x66,0xA5,0x3C, //2055
0x0e,0xA5,0x4f, //2056
0x06,0xB2,0x44, //2057
0x4A,0xB6,0x38, //2058
0x0A,0xAe,0x4C, //2059
0x09,0x2e,0x42, //2060
0x3C,0x97,0x35, //2061
0x0C,0x96,0x49, //2062
0x7d,0x4A,0xBd, //2063
0x0d,0x4A,0x51, //2064
0x0d,0xA5,0x45, //2065
0x55,0xAA,0xBA, //2066
0x05,0x6A,0x4e, //2067
0x0A,0x6d,0x43, //2068
0x45,0x2e,0xB7, //2069
0x05,0x2d,0x4B, //2070
0x8A,0x95,0xBf, //2071
0x0A,0x95,0x53, //2072
0x0B,0x4A,0x47, //2073
0x6B,0x55,0x3B, //2074
0x0A,0xd5,0x4f, //2075
0x05,0x5A,0x45, //2076
0x4A,0x5d,0x38, //2077
0x0A,0x5B,0x4C, //2078
0x05,0x2B,0x42, //2079
0x3A,0x93,0xB6, //2080
0x06,0x93,0x49, //2081
0x77,0x29,0xBd, //2082
0x06,0xAA,0x51, //2083
0x0A,0xd5,0x46, //2084
0x54,0xdA,0xBA, //2085
0x04,0xB6,0x4e, //2086
0x0A,0x57,0x43, //2087
0x45,0x27,0x38, //2088
0x0d,0x26,0x4A, //2089
0x8e,0x93,0x3e, //2090
0x0d,0x52,0x52, //2091
0x0d,0xAA,0x47, //2092
0x66,0xB5,0x3B, //2093
0x05,0x6d,0x4f, //2094
0x04,0xAe,0x45, //2095
0x4A,0x4e,0xB9, //2096
0x0A,0x4d,0x4C, //2097
0x0d,0x15,0x41, //2098
0x2d,0x92,0xB5, //2099
};
///月份数据表
code uchar day_code1[9]={0x0,0x1f,0x3b,0x5a,0x78,0x97,0xb5,0xd4,0xf3};
code uint day_code2[3]={0x111,0x130,0x14e};
bit get_moon_day(uchar month_p,uint table_addr)
{
uchar temp;
switch (month_p){
case 1:{temp=year_code[table_addr]&0x08;
if (temp==0)return(0);else return(1);}
case 2:{temp=year_code[table_addr]&0x04;
if (temp==0)return(0);else return(1);}
case 3:{temp=year_code[table_addr]&0x02;
if (temp==0)return(0);else return(1);}
case 4:{temp=year_code[table_addr]&0x01;
if (temp==0)return(0);else return(1);}
case 5:{temp=year_code[table_addr+1]&0x80;
if (temp==0) return(0);else return(1);}
case 6:{temp=year_code[table_addr+1]&0x40;
if (temp==0)return(0);else return(1);}
case 7:{temp=year_code[table_addr+1]&0x20;
if (temp==0)return(0);else return(1);}
case 8:{temp=year_code[table_addr+1]&0x10;
if (temp==0)return(0);else return(1);}
case 9:{temp=year_code[table_addr+1]&0x08;
if (temp==0)return(0);else return(1);}
case 10:{temp=year_code[table_addr+1]&0x04;
if (temp==0)return(0);else return(1);}
case 11:{temp=year_code[table_addr+1]&0x02;
if (temp==0)return(0);else return(1);}
case 12:{temp=year_code[table_addr+1]&0x01;
if (temp==0)return(0);else return(1);}
case 13:{temp=year_code[table_addr+2]&0x80;
if (temp==0)return(0);else return(1);}
}
}
void Conversion(bit c,uchar year,uchar month,uchar day)
{ //c=0 为21世纪,c=1 为20世纪 输入输出数据均为BCD数据
uchar temp1,temp2,temp3,month_p;
uint temp4,table_addr;
bit flag2,flag_y;
temp1=year/16; //BCD->hex 先把数据转换为十六进制
temp2=year%16;
year=temp1*10+temp2;
temp1=month/16;
temp2=month%16;
month=temp1*10+temp2;
temp1=day/16;
temp2=day%16;
day=temp1*10+temp2;
//定位数据表地址
if(c==0){
table_addr=(year+0x64-1)*0x3;
}
else {
table_addr=(year-1)*0x3;
}
//定位数据表地址完成
//取当年春节所在的公历月份
temp1=year_code[table_addr+2]&0x60;
temp1=_cror_(temp1,5);
//取当年春节所在的公历月份完成
//取当年春节所在的公历日
temp2=year_code[table_addr+2]&0x1f;
//取当年春节所在的公历日完成
// 计算当年春年离当年元旦的天数,春节只会在公历1月或2月
if(temp1==0x1){
temp3=temp2-1;
}
else{
temp3=temp2+0x1f-1;
}
if (month<10){
temp4=day_code1[month-1]+day-1;
}
else{
temp4=day_code2[month-10]+day-1;
}
if ((month>0x2)&&(year%0x4==0)){ //如果公历月大于2月并且该年的2月为闰月,天数加1
temp4+=1;
}
//计算公历日离当年元旦的天数完成
//判断公历日在春节前还是春节后
if (temp4>=temp3){ //公历日在春节后或就是春节当日使用下面代码进行运算
temp4-=temp3;
month=0x1;
month_p=0x1; //month_p为月份指向,公历日在春节前或就是春节当日month_p指向首月
flag2=get_moon_day(month_p,table_addr); //检查该农历月为大小还是小月,大月返回1,小月返回0
flag_y=0;
if(flag2==0)temp1=0x1d; //小月29天
else temp1=0x1e; //大小30天
temp2=year_code[table_addr]&0xf0;
temp2=_cror_(temp2,4); //从数据表中取该年的闰月月份,如为0则该年无闰月
while(temp4>=temp1){
temp4-=temp1;
month_p+=1;
if(month==temp2){
flag_y=~flag_y;
if(flag_y==0)month+=1;
}
else month+=1;
flag2=get_moon_day(month_p,table_addr);
if(flag2==0)temp1=0x1d;
else temp1=0x1e;
}
day=temp4+1;
}
else{ //公历日在春节前使用下面代码进行运算
temp3-=temp4;
if (year==0x0){year=0x63;c=1;}
else year-=1;
table_addr-=0x3;
month=0xc;
temp2=year_code[table_addr]&0xf0;
temp2=_cror_(temp2,4);
if (temp2==0)month_p=0xc;
else month_p=0xd; //
flag_y=0;
flag2=get_moon_day(month_p,table_addr);
if(flag2==0)temp1=0x1d;
else temp1=0x1e;
while(temp3>temp1){
temp3-=temp1;
month_p-=1;
if(flag_y==0)month-=1;
if(month==temp2)flag_y=~flag_y;
flag2=get_moon_day(month_p,table_addr);
if(flag2==0)temp1=0x1d;
else temp1=0x1e;
}
day=temp1-temp3+1;
}
c_moon=c; //HEX->BCD ,运算结束后,把数据转换为BCD数据
temp1=year/10;
temp1=_crol_(temp1,4);
temp2=year%10;
year_moon=temp1|temp2;
temp1=month/10;
temp1=_crol_(temp1,4);
temp2=month%10;
month_moon=temp1|temp2;
temp1=day/10;
temp1=_crol_(temp1,4);
temp2=day%10;
day_moon=temp1|temp2;
}
/*///////////农历转换分界线 */
void festival()//显示节日函数
{
if ( yue == 1 && ri == 1 ){ print(0x80," New Year's Day ");print(0x40,"Happy New Year!!"); }//1月
else if (month_moon== 0x01 && day_moon == 0x01 ){print(0x80,"Happy New Year!!");print(0x40,"Spring Festival!"); }
else if (month_moon== 0x01 && day_moon == 0x15 ){print(0x80," yuan xiao jie: ");print(0x40,"Lantern Festival"); }//元宵节
else if ( yue == 2 && ri == 2 ){ print(0x80," World Wetlands ");print(0x40," Day "); }//2月
else if ( yue == 2 && ri == 14 ){ print(0x80," Today is: ");print(0x40,"Valentine's Day!"); }
else if ( yue == 3 && ri == 8 ){ print(0x80," International ");print(0x40," Women' Day "); }
else if ( yue == 3 && ri == 12 ){ print(0x80," Today is: ");print(0x40,"China Arbor Day "); }
//else if ( yue == 3 && ri == 14 ){ print(0x80," Today is: ");print(0x40," White Day "); }
else if ( yue == 3 && ri == 15 ){ print(0x80," World Consumer ");print(0x40," Right Day "); }
else if ( yue == 3 && ri == 17 ){ print(0x80," International ");print(0x40," sailing day "); }
else if ( yue == 3 && ri == 21 ){ print(0x80," Today is: ");print(0x40,"World Forest Day"); }
else if ( yue == 3 && ri == 22 ){ print(0x80," Today is: ");print(0x40,"World Water Day "); }
//if ( yue == 3 && ri == 23 ){ print(0x80,"World Meteorolo-");print(0x40," gical Day "); }
//if ( yue == 3 && ri == 24 ){ print(0x80,"World Tubercul-");print(0x40," osis Day "); }
else if ( yue == 4 && ri == 1 ){ print(0x80," Today is: ");print(0x40,"April Fools' Day"); }//4
else if ( yue == 4 && ri == 5 ){ print(0x80," Ching Ming ");print(0x40," Festival "); }
else if ( yue == 4 && ri == 7 ){ print(0x80," Today is: ");print(0x40,"World Health Day"); }
else if ( yue == 4 && ri == 8 ){ print(0x80," Today is: ");print(0x40," Easter Day "); }
else if (month_moon== 0x05 && day_moon == 0x05 ){print(0x80,"the Dragon-Boat ");print(0x40," Festival "); }
else if ( yue == 5 && ri == 1 ){ print(0x80," International ");print(0x40," Labour Day "); }//5
else if ( yue == 5 && ri == 4 ){ print(0x80," Chinese Youth ");print(0x40," Day "); }
else if ( yue == 5 && ri == 8 ){ print(0x80,"World Red-Cross ");print(0x40," Day "); }
else if ( yue == 5 && ri == 12 ){ print(0x80," International ");print(0x40," Nurse Day "); }
else if ( yue == 5 && ri == 5 ){ print(0x80," Mother's Day ");print(0x40," is coming! "); }
else if ( yue == 5 && ri == 15 ){ print(0x80," International ");print(0x40," Family Day "); }
else if ( yue == 5 && ri == 31 ){ print(0x80,"World No-Smoking");print(0x40," Day "); }
else if ( yue == 6 && ri == 1 ){ print(0x80," International ");print(0x40," Children's Day "); }//6
else if ( yue == 6 && ri == 5 ){ print(0x80," International ");print(0x40,"Environment Day "); }
else if ( yue == 6 && ri == 26 ){ print(0x80," International ");print(0x40,"Against Drug Day"); }
//else if ( yue == 6 && ri == 6 ){ print(0x80," National Eyes "); print(0x40," Caring Day "); }
else if ( yue == 6 && ri == 13 ){ print(0x80," Father's Day ");print(0x40," is coming! "); }
else if (month_moon== 0x07 && day_moon == 0x07 ){ print(0x80," Double-Seventh ");print(0x40," Day! "); }
else if ( yue == 7 && ri == 1 ){ print(0x80," The return of ");print(0x40," Hong Kong Day "); }//7
else if ( yue == 7 && ri == 7 ){ print(0x80," Anti-Japanese ");print(0x40,"War memorial day"); }
else if ( yue == 7 && ri == 11 ){ print(0x80,"World Population");print(0x40," Day "); }
else if (month_moon== 0x08 && day_moon == 0x15 ){ print(0x80," the Mid-Autumn ");print(0x40," Festival "); }
else if ( yue == 8 && ri == 1 ){ print(0x80," Today is: ");print(0x40," the Army Day "); }//8
else if ( yue == 8 && ri == 8 ){ print(0x80," Chinese man ");print(0x40," Festival "); }
else if ( yue == 8 && ri == 15 ){ print(0x80," The victory of ");print(0x40," war anniversary"); }
else if (month_moon== 0x09 && day_moon == 0x09 ){ print(0x80,"the Double Ninth");print(0x40," Festival "); }
else if ( yue == 9 && ri == 10 ){ print(0x80," Today is: ");print(0x40," Teacher's Day "); }//9
//else if ( yue == 9 && ri == 20 ){ print(0x80," International ");print(0x40,"Teeth-loving Day"); }
else if ( yue == 9 && ri == 27 ){ print(0x80," World Tourism ");print(0x40," Day "); }
else if ( yue == 10 && ri == 1 ){ print(0x40," Today is: ");print(0x40," National Day "); }//10
else if ( yue == 10 && ri == 4 ){ print(0x80," Today is: ");print(0x40,"World Animal Day"); }
else if ( yue == 10 && ri == 24 ){ print(0x80," United Nations ");print(0x40," Day "); }
else if ( yue == 11 && ri == 10 ){ print(0x80," Today is: ");print(0x40,"World Youth Day "); }//11
else if ( yue == 11 && ri == 11 ){ print(0x80,"Today is 1,1,1..");print(0x40," One's Day "); }//光棍节
else if ( yue == 11 && ri == 17 ){ print(0x80," The student ");print(0x40," section "); }
else if (month_moon== 0x12 && day_moon == 0x08 ){ print(0x80,"the laba Rice P-");print(0x40,"orridge Festival"); }
else if (month_moon== 0x12 && day_moon == 0x15 ){ print(0x80," guo xiao nian ");print(0x40," a lunar year "); }
//if (month_moon== 0x12 && day_moon == 0x29 ){ print(" 腊月二十九 "); }
else if (month_moon== 0x12 && day_moon == 0x30 ){ print(0x80," Today is: ");print(0x40," New Year's Eve "); }
else if ( yue == 12 && ri == 1 ){ print(0x80," Today is: ");print(0x40," World AIDS Day "); }//12
//if ( yue == 12 && ri == 23 ){ print(" 明晚平安夜 "); }
else if ( yue == 12 && ri == 24 ){print(0x80,"Tonight is "); print(0x40,"The Silent Night"); }
else if ( yue == 12 && ri == 25 ){print(0x80,"Merry Christmas!"); print(0x40," Christmas Day "); }
else if ( yue == 12 && ri == 31 ){ print(0x80," The last day ");print(0x40," of the year "); }
else{
print(0x40," ...(*^_^*)... ");//显示笑脸。。。
//print(0x40," ...0(n_n)0... ");
print(0x80,"Have a Good Day!");
}
}
/*报时函数*/
void timealarm()
{
if(shi>7)//大于7点才启动报时
{
if(alarmflag==1)//不按下设置按键时才有效
{
if(fen==0 && miao<2)
{
beep=0;//启动蜂鸣器
}
else if(fen==30 && miao==0)
{
beep=0;
}
else beep=1;
}
}
}
//按键初始化时间函数
void chushihua()//初始化时间函数
{
if(key2==0)
{
if(key3==0)
{
delay(5);
if(key3==0)
{
RST=0;
SCLK=0;
write_1302(0x8e,0x00);//允许写
write_1302(0x80,0x00);//秒:00
write_1302(0x82,0x00);//分:00
write_1302(0x84,0x12);//时:12
write_1302(0x8a,0x06);//周:4
write_1302(0x86,0x01);//日:15
write_1302(0x88,0x01);//月:3
write_1302(0x8c,0x11);//年:11
write_1302(0x8e,0x80);//打开保护
}
}
}
}
/*void ledcontrol()//背光led灯的控制函数
{
if(key3==0)
{
delay(5);
if(key3==0)
{
while(!key3);
lednum++;
if(lednum==3) lednum=1;
}
}
if(lednum==1) led=0;//关闭背光
if(lednum==2) led=1;//打开背光
} */
/*以下是主函数部分*/
void main()
{
alarmflag=1;
//led=0;
lcdinit();//初始化液晶函数
ds1302_init();//DS1302时钟芯片初始化函数
init();//定时器初始化函数
beep=0;
delay(200);
beep=1;
//led=0;
while(1)
{
year1=(nian)/10*16+(nian)%10;//转换为bcd码
month1=(yue)/10*16+(yue)%10;
day1=(ri)/10*16+(ri)%10;
chushihua();//初始化时间函数
Conversion(0,year1,month1,day1);//
timealarm();//报时程序
keyscan();//不断扫面按键函数
/*if(key2==1)//加键不按下时才有用
{
ledcontrol();//背光控制
} */
}
}
void timer0() interrupt 1//中断任务:取数据并显示
{
TH0=(65536-60000)/256;//重新赋初值
TL0=(65536-60000)%256;
//读取数据
flag=ReadTemperature();//读取温度(考虑是否放在此处)
miao=turnBCD(read_1302(0x81));//读出秒
fen=turnBCD(read_1302(0x83));//读出分
shi=turnBCD(read_1302(0x85));//读出时
ri=turnBCD(read_1302(0x87));//读出日
yue=turnBCD(read_1302(0x89));//读出月
nian=turnBCD(read_1302(0x8d));//读出年
week=turnBCD(read_1302(0x8b));//读出周
//显示数据
if(((0<=miao)&&(miao<15))||(((miao/10==2)||(miao/10==4))&&(miao%10<=5))||((54<=miao)&&(miao<60))) /*显示所有信息的时间段*/
{
print(0x80,"20 / / W < >");
writecom(h1+0x0c);
writedata(0x7e);//→右箭头
writeday(8,ri);//显示日
writeday(5,yue);//显示月
writeday(2,nian);//显示年
writeweek(week);//显示星期
print(0x40," : : ");
writetemp(9,flag);//显示温度,第二行显示
writetime(6,miao);//显示出秒
writetime(3,fen);//显示出分
writetime(0,shi);//显示出时,第二行第一个开始
}
if(15<=miao)/*当秒大于15时才显示*/
{
if(((miao/10==1)||(miao/10==3))&&(miao%10>=5))
{
switch(flag/100)//取出温度的十位数
{
case 0:print(0x80,"Pretty cold now!");
break;
case 1:print(0x80,"little cold now!");
break;
case 2:print(0x80,"It is warm now. ");
break;
case 3:print(0x80,"It's hot !!! ");
break;
case 4:print(0x80,"Very very hot!!!");
break;
case 5:print(0x80,"Very very hot!!!");
break;
case 6:print(0x80,"Very very hot!!!");
break;
}
print(0x80,"It is warm now. ");
print(0x40," : : ");
writetemp(9,flag);//显示温度,第二行显示
writetime(6,miao);//显示出秒
writetime(3,fen);//显示出分
writetime(0,shi);//显示出时,第二行第一个开始
}
if(((miao/10==2)||(miao/10==4))&&(miao%10>=5))/*25-30 45-50*/
{
if((5<=shi)&&(shi<=11)) flagT=1;//5-11时为早上
if((11<shi)&&(shi<=14)) flagT=2;//11-14为中午
if((14<shi)&&(shi<=18)) flagT=3;//14-18为下午
if((18<shi)||(shi<5)) flagT=4;//18-24或者0-5为晚上
switch(flagT)//分段时间
{
case 1:print(0x80," Good Morning! ");
break;
case 2:print(0x80," Good Noon! ");
break;
case 3:print(0x80,"Good afternoon! ");
break;
case 4:print(0x80," Good night! ");
break;
}
print(0x40," : : ");
writetemp(9,flag);//显示温度,第二行显示
writetime(6,miao);//显示出秒
writetime(3,fen);//显示出分
writetime(0,shi);//显示出时,第二行第一个开始
}
if(((30<=miao)&&(miao<35))||((50<=miao)&&(miao<54)))//30-35 50-54
{//else改为范围
festival();//选择显示节日
}
}
}
void timer1() interrupt 3//任务:计时用作报时
{
TH1=(65536-50000)/256;//中断后重新赋初值
TL1=(65536-50000)%256;
count++;//计算时间
if(alarmflag==1)//设置键按下才有效
{
if(fen==0 && miao<4)
{
count++;
ledcount=count/10;//分开时间段
switch(ledcount)
{
case 0:Gled=0;Yled=1;break;//从零开始
case 1:Gled=1;Yled=0;break;
case 2:Gled=0;Yled=1;break;
case 3:Gled=1;Yled=0;break;
case 4:Gled=0;Yled=0;break;
case 5:Gled=1;Yled=1;break;
case 6:Gled=0;Yled=0;break;
case 7:Gled=0;Yled=0;count=0;break;//在这里面清零
}
}
else if(fen==30 && miao<2)
{
Gled=0;
Yled=0;
}
else {
Gled=1;
Yled=1;
}
}
}
|