#include <STC12C5A60S2.H> //此文件中定义了单片机的一些特殊功能寄存器#include "Delay.h" //全局通用延时头文件#include "19264ks0108ZK.h" //19264屏字库#include "19264ks0108.h" //19264屏驱动#include "ds3231.h" //ds3231时钟芯片驱动#include "rqzh.h" //日期转换#include "dht11.h" //DHT11湿度温度驱动#define uchar unsigned char //宏定义无符号字符形数据类型进行声明定义 定义名称uchar 所占位数8位 范围0~255#define uint unsigned int //宏定义无符号整形数据类型进行声明定义 定义名称uint 所占位数16位 范围0~65535sbit LED_NO=P1^7; //LEDD开关按键sbit SJTZ_k1=P1^6; //调整时钟按键sbit SJTZ_k2=P1^5; //调整时钟加按键sbit SJTZ_k3=P1^4; //调整时钟减按键sbit SJTZ_k4=P1^3; //调整时钟OK按键sbit fmq=P1^2; //闹钟蜂鸣器引脚sbit LED=P3^7; //LEDD灯引脚sbit pingbg=P3^6; //屏背光引脚sbit nz_KG=P2^0; //闹钟开关引脚uint pingbgjs; //用于屏背光自动控制定时计数标志uchar SJTZ=0; //调整时钟标志uchar jia=0; //调整时钟或闹钟加标志uchar jian=0; //调整时钟或闹钟减标志uchar fb_nian,fb_yue,fb_ri,fb_shi,fb_fen,fb_nz1_shi,fb_nz1_fen;//调整时钟反白标志uchar NZ_xl=0; //闹钟状态标志 NZ_xl=0不响铃 NZ_xl=1响铃static uint D=0,L=0; //声明定时器static静态变量标志 用于某些功能计数/*按键程序*/void anjian(){if(LED_NO==0||SJTZ_k1==0||SJTZ_k2==0||SJTZ_k3==0||SJTZ_k4==0) //任意键开屏背光{ if(pingbg==1)pingbg=0; //屏背光开 pingbgjs=0; //定时计数清零}if(pingbg==1||LED==0) //当背光关闭或LED台灯打开时关闭定时器{ //即在LED台灯打开下屏背光常亮 直至LED台灯关闭为止定时计数又开始重新计数 TR0 = 0; pingbgjs=0; //定时计数清零} else TR0 = 1; //当背光打开时定时器开始计数 用于屏背光自动关闭控制 if(pingbg==1)SJTZ=0; //如果在屏背光关闭后 强行退出时钟设置 if(LED==0&&(SJTZ==0||SJTZ==1))SJTZ=1; if(LED_NO==0) //LED台灯开关{ Delay_ms(10); //按键消抖延时10微秒 if(LED_NO==0) { LED=~LED; } while(!LED_NO); //检测按键是否松开}if(SJTZ_k1==0) //时钟调整{ Delay_ms(10); //按键消抖延时10微秒 if(SJTZ_k1==0) { SJTZ++; if(SJTZ==9)SJTZ=2; } while(!SJTZ_k1); //检测按键是否松开}if(SJTZ_k2==0) //时钟调整加{ jia=1; //设置时钟+1 Delay_ms(150); //延时一段时间 如果还没松开 继续加}else(jia=0);if(SJTZ_k3==0) //时钟调整减{ jian=1; //设置时钟-1 Delay_ms(150); //延时一段时间 如果还没松开 继续减}else(jian=0);if(SJTZ_k4==0&&NZ_xl==0) //完成退出{ if(SJTZ!=0&&SJTZ!=1) { Disp_16x16(8,0,0,ZM_16x16[22]); Disp_16x16(9,0,0,ZM_16x16[23]); Disp_16x16(10,0,0,ZM_16x16[24]); } SJTZ=1;}if(SJTZ_k4==0&&NZ_xl==1||SJTZ!=0&&SJTZ!=1||nz_KG==1) //如果闹钟响时 此按键可以关闭闹钟响声{ fmq=1; NZ_xl=0; //关闭响铃 TR1 = 0; //关闭定时器1 D=0; L=0;}}/*时钟设置程序*/void SZSZ(){if(SJTZ==0||SJTZ==1){ Ds3231ReadTime(); //运行时钟数据}else //如果SJTZ!=0时 证明进入了时钟时钟{ Ds3231Init(); //停止时钟 调整时钟数据写入ds3231 rilijis(); //读取日历自动计算函数}if(SJTZ==2){ fb_fen=1; if(jia==1)if(fen<59)fen=fen+1;else(fen=0); //调整分+ if(jian==1)if(fen>0)fen=fen-1;else(fen=59); //调整分-1}else(fb_fen=0);if(SJTZ==3){ fb_shi=1; if(jia==1)if(shi<23)shi=shi+1;else(shi=0); //调整时+1 if(jian==1)if(shi>0)shi=shi-1;else(shi=23); //调整时-1}else(fb_shi=0);if(SJTZ==4){ fb_ri=1; if(yue!=2&&yue_dx==0&&jia==1)if(ri<31)ri=ri+1;else(ri=1); //调整日+1 if(yue!=2&&yue_dx==0&&jian==1)if(ri>1)ri=ri-1;else(ri=31); //调整日-1 /*4,6,9,11月只有30天 为小月*/ if(yue!=2&&yue_dx==1&&jia==1)if(ri<30)ri=ri+1;else(ri=1); //调整日+1 if(yue!=2&&yue_dx==1&&jian==1)if(ri>1)ri=ri-1;else(ri=30); //调整日-1 /*...如果是闰年只有29天.....*/ if(yue==2&&nian_r==0&&jia==1)if(ri<29)ri=ri+1;else(ri=1); //调整日+1 if(yue==2&&nian_r==0&&jian==1)if(ri>1)ri=ri-1;else(ri=29); //调整日-1 /*....如果是平年只有28天.....*/ if(yue==2&&nian_r==1&&jia==1)if(ri<28)ri=ri+1;else(ri=1); //调整日+1 if(yue==2&&nian_r==1&&jian==1)if(ri>1)ri=ri-1;else(ri=28); //调整日-1}else(fb_ri=0);if(SJTZ==5){ fb_yue=1; if(jia==1)if(yue<12)yue=yue+1;else(yue=1); //调整月+1 if(jian==1)if(yue>1)yue=yue-1;else(yue=12); //调整月-1}else(fb_yue=0);if(SJTZ==6){ fb_nian=1; if(jia==1)if(nian<99)nian=nian+1;else(nian=0); //调整年+1 if(jian==1)if(nian>0)nian=nian-1;else(nian=99); //调整年-1}else(fb_nian=0);/*............以下是闹钟设置函数....................*/if(SJTZ==7) { fb_nz1_fen=1; if(jia==1)if(nz1_fen<59)nz1_fen=nz1_fen+1;else(nz1_fen=0); //闹钟1钟分+1 if(jian==1)if(nz1_fen>0)nz1_fen=nz1_fen-1;else(nz1_fen=59); //闹钟钟分-1}else (fb_nz1_fen=0);if(SJTZ==8) { fb_nz1_shi=1; if(jia==1)if(nz1_shi<23)nz1_shi=nz1_shi+1;else(nz1_shi=0); //闹钟1时加+1 if(jian==1)if(nz1_shi>0)nz1_shi=nz1_shi-1;else(nz1_shi=23); //闹钟时-1}else (fb_nz1_shi=0);}/*农历生肖其他节日--------------------------------------------------------------------*/void jshs(){nonglixs(); //显示农历jiqijs(); //节气计算nonglijrjs(); //农历节日计算gljr(); //公历节日计算 if(jieqijs!=0&&nljs==0&&gljrjs==0) //如果当天只有节气 生肖与节气轮番显示 { if(miao>=0&&miao<30)shengxiaoxs();//显示生肖 if(miao>=30&&miao<=59)jieqixs(); //显示节气 }else if(jieqijs==0&&nljs!=0&&gljrjs==0) //如果当天只有农历节日 生肖与节气轮番显示 { if(miao>=0&&miao<30)shengxiaoxs(); //显示生肖 if(miao>=30&&miao<=59)nonglijrxs(); //农历节日显示}else if(jieqijs==0&&nljs==0&&gljrjs!=0) //如果当天只有公历节日 生肖与节气轮番显示{ if(miao>=0&&miao<30)shengxiaoxs(); //显示生肖 if(miao>=30&&miao<=59)gljxs(); //公历节日显示}else if(jieqijs!=0&&nljs!=0&&gljrjs==0) //如果当天同时有节气农历节日 生肖与农历节日与节气轮番显{ if(miao>=0&&miao<20)shengxiaoxs(); //显示生肖 if(miao>=20&&miao<40)jieqixs(); //显示节气 if(miao>=40&&miao<=59)nonglijrxs(); //农历节日显示 }else if(jieqijs==0&&nljs!=0&&gljrjs!=0) //如果当天同时有农历节日公历节日 农历节日与公历节日轮番显{ if(miao>=0&&miao<20)shengxiaoxs(); //显示生肖 if(miao>=20&&miao<40)nonglijrxs(); //农历节日显示 if(miao>=40&&miao<=59)gljxs(); //公历节日显示 }else if(jieqijs!=0&&nljs==0&&gljrjs!=0) //如果当天同时有生肖公历节日 生肖与公历节日轮番显{ if(miao>=0&&miao<20)shengxiaoxs(); //显示生肖 if(miao>=20&&miao<40)jieqixs(); //显示节气 if(miao>=40&&miao<=59)gljxs(); //公历节日显示 }else if(jieqijs!=0&&nljs!=0&&gljrjs!=0) //如果当天同时有生肖农历节日公历节日 生肖与农历与公历节日轮番显{ if(miao>=0&&miao<15)shengxiaoxs(); //显示生肖 if(miao>=15&&miao<30)jieqixs(); //显示节气 if(miao>=30&&miao<45)nonglijrxs(); //农历节日显示 if(miao>=45&&miao<=59)gljxs(); //公历节日显示}else shengxiaoxs(); //如果没有其他节日只显示生肖}/*闹钟相关--------------------------------------------------------------------*/void naozhong(){if(nz_KG==0||(SJTZ!=0&&SJTZ!=1))Disp_16x16(7,0,0,ZM_16x16[21]); //显示闹钟图标else Disp_16x16(7,0,0,ZM_16x16[0]); if((SJTZ!=0&&SJTZ!=1)) //显示闹钟 { Disp_8x16(16,0,fb_nz1_shi,ZM_8x16[nz1_shi/10]); // Disp_8x16(17,0,fb_nz1_shi,ZM_8x16[nz1_shi%10]); //闹钟时 Disp_8x16(18,0,0,ZM_8x16[15]); Disp_8x16(19,0,fb_nz1_fen,ZM_8x16[nz1_fen/10]); // Disp_8x16(20,0,fb_nz1_fen,ZM_8x16[nz1_fen%10]); //闹钟分 Disp_8x16(21,0,0,ZM_8x16[16]);}if(shi==nz1_shi&&fen==nz1_fen&&miao==0&&nz_KG==0) //闹钟对比{ TR1 = 1; //打开定时器1 NZ_xl=1; //NZ_xl=1响铃}}/*主函数--------------------------------------------------------------------*/void main()//无返回值主函数{TMOD = 0x11; //定时器0与定时器1打开 TH0 = 0x0D1; TL0 = 0x20; //给定时器0赋初值,定时1msTH1 = 0x0D1; TL1 = 0x20; //给定时器1赋初值,定时1ms ET0 = 1; //打开定时器0中断允许 TR0 = 1; //打开定时器0ET1 = 1; //打开定时器1中断允许 //TR1 = 1; //打开定时器1EA = 1; //打开总中断pingbg=0; //刚通电屏背光开SJTZ=1;LCD19264AInit(); //LCD19264A初始化ClearDisplay(); //LCD19264A清屏zhoucqh(); //DS3231芯片初次通电时钟初始化 while(1){ anjian(); //按键程序 SZSZ(); //时钟设置程序 jshs(); //农历生肖其他节日显示 naozhong();//闹钟相关 Disp_8x16(0,0,fb_nian,ZM_8x16[2]); // Disp_8x16(1,0,fb_nian,ZM_8x16[0]); // Disp_8x16(2,0,fb_nian,ZM_8x16[nian/10]); // Disp_8x16(3,0,fb_nian,ZM_8x16[nian%10]); //年 Disp_8x16(4,0,0,ZM_8x16[10]); //- Disp_8x16(5,0,fb_yue,ZM_8x16[yue/10]); // Disp_8x16(6,0,fb_yue,ZM_8x16[yue%10]); //月 Disp_8x16(7,0,0,ZM_8x16[10]); //- Disp_8x16(8,0,fb_ri,ZM_8x16[ri/10]); // Disp_8x16(9,0,fb_ri,ZM_8x16[ri%10]); //日 Disp_16x32(0,8,fb_shi,ZM_16x32[shi/10]); // Disp_16x32(1,8,fb_shi,ZM_16x32[shi%10]); //时 Disp_16x32(2,8,0,ZM_16x32[10]); //: Disp_16x32(3,8,fb_fen,ZM_16x32[fen/10]); // Disp_16x32(4,8,fb_fen,ZM_16x32[fen%10]); //分 Disp_16x32(5,8,0,ZM_16x32[10]); //: Disp_16x32(6,8,0,ZM_16x32[miao/10]); // Disp_16x32(7,8,0,ZM_16x32[miao%10]); //秒 Disp_16x16(5,3,0,ZM_16x16[18]); //星 Disp_16x16(6,3,0,ZM_16x16[19]); //期 switch(zhou) { case 1:Disp_16x16(7,3,0,ZM_16x16[1]);break; //星期一 case 2:Disp_16x16(7,3,0,ZM_16x16[2]);break; //星期二 case 3:Disp_16x16(7,3,0,ZM_16x16[3]);break; //星期三 case 4:Disp_16x16(7,3,0,ZM_16x16[4]);break; //星期四 case 5:Disp_16x16(7,3,0,ZM_16x16[5]);break; //星期五 case 6:Disp_16x16(7,3,0,ZM_16x16[6]);break; //星期六 case 7:Disp_16x16(7,3,0,ZM_16x16[17]);break; //星期天 } DHT11_receive(); //读取湿度温度函数 Disp_8x16(18,1,0,ZM_8x16[11]); Disp_8x16(20,1,0,ZM_8x16[RH/10]); //湿度 Disp_8x16(21,1,0,ZM_8x16[RH%10]); Disp_8x16(22,1,0,ZM_8x16[12]); Disp_8x16(18,2,0,ZM_8x16[13]); Disp_8x16(20,2,0,ZM_8x16[TH/10]); //温度 Disp_8x16(21,2,0,ZM_8x16[TH%10]); Disp_8x16(22,2,0,ZM_8x16[14]); if(LED==0)Disp_16x16(6,0,0,ZM_16x16[20]); //台灯图标显示 else Disp_16x16(6,0,0,ZM_16x16[0]); /*显示小动物图标*/ if(SJTZ==0||SJTZ==1) { if(miao>=0&&miao<5)Disp_16x16(8,0,0,ZM_16x16[22]); else if(miao>=5&&miao<10) { Disp_16x16(8,0,0,ZM_16x16[0]); Disp_16x16(9,0,0,ZM_16x16[23]); } else if(miao>=10&&miao<15) { Disp_16x16(9,0,0,ZM_16x16[0]); Disp_16x16(10,0,0,ZM_16x16[24]); } else if(miao>=15&&miao<20)Disp_16x16(10,0,0,ZM_16x16[0]); else { Disp_16x16(8,0,0,ZM_16x16[22]); Disp_16x16(9,0,0,ZM_16x16[23]); Disp_16x16(10,0,0,ZM_16x16[24]); } } }}/*定时器1函数*/void Timer0() interrupt 1{ TH0 = 0x0D1; TL0 = 0x20; //给定时器0赋初值,定时1msif(pingbg==0) //当背光打开时定时器开始计数 用于屏背光自动关闭控制{ pingbgjs++; if(pingbgjs==3000) pingbg=1; //屏背光开后如果无操作大约30秒后关闭屏背光}else pingbgjs=0; //定时计数清零}/*定时器1函数*/void Timer1() interrupt 3{ TH1 = 0x0D1; TL1 = 0x20; //给定时器1赋初值,定时1ms D++; if(D==100)D=0; if(D>0&&D<50) { L++; if(L==10) { L=0; fmq=~fmq; //闹钟时间到时蜂鸣器响铃 }}else(L=0);}复制代码