(1)在1602液晶上显示年、月、日、时、分、秒,并且按秒实时更新显示。
(2)在1602液晶上显示当前采集到的环境温度。
(3)当环境温度低于0℃和高于34℃时,蜂鸣器报警。
(4)每次开机时,蜂鸣器响。
(5)能够使用板上的按键调节各参数,按键可设计4个有效键,分别为切换键、加键、减键、跳出键。
(6)利用 DS1302时钟芯片通过简单的串行通信与单片机进行通信,时钟/日历电路能够实时提供年、月、日、时、分、秒信息,采用双电源供电,当外部电源掉电时能够利用后备电池准确计时的特性,设计实现断电时间不停、再次上电时时间仍然准确显示在液晶上的功能。
每次开机时,蜂鸣器长鸣一身,按下功能键可以调节时间、年、月、日,加键和减键可以实现时间、年、月、日的加减,跳出键可以结束当前正在执行的时间调整,该电子表附带有温度报警功能,当温度超过34℃和低于0℃时,蜂鸣器长鸣报警。
单片机源程序如下:
- /***************************************************************************************************************
- 基于DS18B20、DS1302、单片机的时钟与温度测控
- ***************************************************************************************************************/
- #include <REG52.H>
- #include "LCD1602.H"
- #include "DS18B20.H"
- #include "DS1302.H"
- #define uint unsigned int
- #define uchar unsigned char
- #define TH 35 //设置温度上限
- extern unsigned char week_value[2],TempBuffer[5]; //声明外部变量
- extern int temp_value;
- char hide_sec,hide_min,hide_hour,hide_day,hide_week,hide_month,hide_year; //秒,分,时到日,月,年位闪的计数
- sbit Set = P3^0; //模式切换键
- sbit Up = P3^1; //加法按钮
- sbit Down = P3^2; //减法按钮
- sbit out = P3^3; //立刻跳出调整模式按钮
- sbit deng = P1^1;
- char done,count,temp,flag,up_flag,down_flag;
- char SD=1;
- void show_time(); //液晶显示函数声明
- /**************************************************************
- 延时子程序
- **************************************************************/
- void mdelay(uint delay)
- { uint i;
- for(;delay>0;delay--)
- {for(i=0;i<62;i++) //1ms延时.
- {;}
- }
- }
- /**************************************************************
- 升序按键
- **************************************************************/
- void Upkey()
- {
- Up=1;
- if(Up==0)
- {
- mdelay(8);
- switch(count)
- {case 1:
- temp=Read1302(DS1302_SECOND); //读取秒数
- temp=temp+1; //秒数加1
- up_flag=1; //数据调整后更新标志
- if((temp&0x7f)>0x59) //超过59秒,清零
- temp=0;
- break;
- case 2:
- temp=Read1302(DS1302_MINUTE); //读取分数
- temp=temp+1; //分数加1
- up_flag=1;
- if(temp>0x59) //超过59分,清零
- temp=0;
- break;
- case 3:
- temp=Read1302(DS1302_HOUR); //读取小时数
- temp=temp+1; //小时数加1
- up_flag=1;
- if(temp>0x23) //超过23小时,清零
- temp=0;
- break;
- /*case 4:
- temp=Read1302(DS1302_WEEK); //读取星期数
- temp=temp+1; //星期数加1
- up_flag=1;
- if(temp>0x7)
- temp=1;
- break; */
- case 5:
- temp=Read1302(DS1302_DAY); //读取日数
- temp=temp+1; //日数加1
- up_flag=1;
- if(temp>0x31)
- temp=1;
- break;
- case 6:
- temp=Read1302(DS1302_MONTH); //读取月数
- temp=temp+1; //月数加1
- up_flag=1;
- if(temp>0x12)
- temp=1;
- break;
- case 7:
- temp=Read1302(DS1302_YEAR); //读取年数
- temp=temp+1; //年数加1
- up_flag=1;
- if(temp>0x85)
- temp=0;
- break;
- default:break;
- }
-
- while(Up==0);
-
- }
- }
- /**************************************************************
- 降序按键
- **************************************************************/
- void Downkey()
- {
- Down=1;
- if(Down==0)
- {
- mdelay(8);
- switch(count)
- {case 1:
- temp=Read1302(DS1302_SECOND); //读取秒数
- temp=temp-1; //秒数减1
- down_flag=1; //数据调整后更新标志
- if(temp==0x7f) //小于0秒,返回59秒
- temp=0x59;
- break;
- case 2:
- temp=Read1302(DS1302_MINUTE); //读取分数
- temp=temp-1; //分数减1
- down_flag=1;
- if(temp==-1)
- temp=0x59; //小于0分,返回59分
- break;
- case 3:
- temp=Read1302(DS1302_HOUR); //读取小时数
- temp=temp-1; //小时数减1
- down_flag=1;
- if(temp==-1)
- temp=0x23;
- break;
- /*case 4:
- temp=Read1302(DS1302_WEEK); //读取星期数
- temp=temp-1; //星期数减1
- down_flag=1;
- if(temp==0)
- temp=0x7;;
- break; */
- case 5:
- temp=Read1302(DS1302_DAY); //读取日数
- temp=temp-1; //日数减1
- down_flag=1;
- if(temp==0)
- temp=31;
- break;
- case 6:
- temp=Read1302(DS1302_MONTH); //读取月数
- temp=temp-1; //月数减1
- down_flag=1;
- if(temp==0)
- temp=12;
- break;
- case 7:
- temp=Read1302(DS1302_YEAR); //读取年数
- temp=temp-1; //年数减1
- down_flag=1;
- if(temp==-1)
- temp=0x85;
- break;
- default:break;
- }
-
- while(Down==0);
-
- }
- }
- /**************************************************************
- 模式选择按键
- **************************************************************/
- void Setkey()
- {
- Set=1;
- if(Set==0)
- {
- mdelay(8);
- count=count+1; //Setkey按一次,count就加1
- done=1; //进入调整模式
- while(Set==0);
-
- }
- }
- /**************************************************************
- 跳出调整模式,返回默认显示
- **************************************************************/
- void outkey()
- { uchar Second;
- out=1;
- if(out==0)
- {
- count=0;
- hide_sec=0,hide_min=0,hide_hour=0,hide_day=0,hide_week=0,hide_month=0,hide_year=0;
- Second=Read1302(DS1302_SECOND);
- Write1302(0x8e,0x00); //写入允许
- Write1302(0x80,Second&0x7f);
- Write1302(0x8E,0x80); //禁止写入
- done=0;
- while(out==0);
- }
- }
- /**************************************************************
- 按键功能执行
- **************************************************************/
- void keydone()
- { uchar Second;
- if(flag==0) //关闭时钟,停止计时
- { Write1302(0x8e,0x00); //写入允许
- temp=Read1302(0x80);
- Write1302(0x80,temp|0x80);
- Write1302(0x8e,0x80); //禁止写入
- flag=1;
- }
- Setkey(); //扫描模式切换按键
- switch(count)
- {case 1:do //count=1,调整秒
- {
- outkey(); //扫描跳出按钮
- Upkey(); //扫描加按钮
- Downkey(); //扫描减按钮
- if(up_flag==1||down_flag==1) //数据更新,重新写入新的数据
- {
- Write1302(0x8e,0x00); //写入允许
- Write1302(0x80,temp|0x80); //写入新的秒数
- Write1302(0x8e,0x80); //禁止写入
- up_flag=0;
- down_flag=0;
- }
- hide_sec++; //位闪计数
- if(hide_sec>3)
- hide_sec=0;
- show_time(); //液晶显示数据
- }while(count==2);break;
- case 2:do //count=2,调整分
- {
- hide_sec=0;
- outkey();
- Upkey();
- Downkey();
- if(temp>0x60)
- temp=0;
- if(up_flag==1||down_flag==1)
- {
- Write1302(0x8e,0x00); //写入允许
- Write1302(0x82,temp); //写入新的分数
- Write1302(0x8e,0x80); //禁止写入
- up_flag=0;
- down_flag=0;
- }
- hide_min++;
- if(hide_min>3)
- hide_min=0;
- show_time();
- }while(count==3);break;
- case 3:do //count=3,调整小时
- {
- hide_min=0;
- outkey();
- Upkey();
- Downkey();
- if(up_flag==1||down_flag==1)
- {
- Write1302(0x8e,0x00); //写入允许
- Write1302(0x84,temp); //写入新的小时数
- Write1302(0x8e,0x80); //禁止写入
- up_flag=0;
- down_flag=0;
- }
- hide_hour++;
- if(hide_hour>3)
- hide_hour=0;
- show_time();
- }while(count==4);break;
- /* case 4:do //count=4,调整星期
- {
- hide_hour=0;
- outkey();
- Upkey();
- Downkey();
- if(up_flag==1||down_flag==1)
- {
- Write1302(0x8e,0x00); //写入允许
- Write1302(0x8a,temp); //写入新的星期数
- Write1302(0x8e,0x80); //禁止写入
- up_flag=0;
- down_flag=0;
- }
- hide_week++;
- if(hide_week>3)
- hide_week=0;
- show_time();
- }while(count==5);break; */
- case 5:do //count=5,调整日
- {
- hide_week=0;
- outkey();
- Upkey();
- Downkey();
- if(up_flag==1||down_flag==1)
- {
- Write1302(0x8e,0x00); //写入允许
- Write1302(0x86,temp); //写入新的日数
- Write1302(0x8e,0x80); //禁止写入
- up_flag=0;
- down_flag=0;
- }
- hide_day++;
- if(hide_day>3)
- hide_day=0;
- show_time();
- }while(count==6);break;
- case 6:do //count=6,调整月
- {
- hide_day=0;
- outkey();
- Upkey();
- Downkey();
- if(up_flag==1||down_flag==1)
- {
- Write1302(0x8e,0x00); //写入允许
- Write1302(0x88,temp); //写入新的月数
- Write1302(0x8e,0x80); //禁止写入
- up_flag=0;
- down_flag=0;
- }
- hide_month++;
- if(hide_month>3)
- hide_month=0;
- show_time();
- }while(count==7);break;
- case 7:do //count=7,调整年
- {
- hide_month=0;
- outkey();
- Upkey();
- Downkey();
- if(up_flag==1||down_flag==1)
- {
- Write1302(0x8e,0x00); //写入允许
- Write1302(0x8c,temp); //写入新的年数
- Write1302(0x8e,0x80); //禁止写入
- up_flag=0;
- down_flag=0;
- }
- hide_year++;
- if(hide_year>3)
- hide_year=0;
- show_time();
- }while(count==8);break;
- case 8: count=0;hide_year=0; //count8, 跳出调整模式,返回默认显示状态
- Second=Read1302(DS1302_SECOND);
- Write1302(0x8e,0x00); //写入允许
- Write1302(0x80,Second&0x7f);
- Write1302(0x8E,0x80); //禁止写入
- done=0;
- break; //count=7,开启中断,标志位置0并退出
- default:break;
- }
- }
- /**************************************************************
- 液晶显示程序
- **************************************************************/
- void show_time()
- {
- DS1302_GetTime(&CurrentTime); //获取时钟芯片的时间数据
- TimeToStr(&CurrentTime); //时间数据转换液晶字符
- DateToStr(&CurrentTime); //日期数据转换液晶字符
- ReadTemp(); //开启温度采集程序
- temp_to_str(); //温度数据转换成液晶字符
- GotoXY(12,1); //液晶字符显示位置
- Print(TempBuffer); //显示温度
- GotoXY(0,1);
- Print(CurrentTime.TimeString); //显示时间
- GotoXY(0,0);
- Print(CurrentTime.DateString); //显示日期
- GotoXY(15,0);
- /*Print(week_value); //显示星期
- GotoXY(11,0);
- Print("Week"); //在液晶上显示 字母 week */
- mdelay(500); //扫描延时
- }
- /**************************************************************
- 报警检测与取消
- **************************************************************/
- void warming()
- {
- if(temp_value>=TH)
- {if(SD==1&&SD!=0)ET0=1;}
- else {deng=0;ET0=0;SD=1;}
- out=1;
- if(out==0){ET0=0;SD=0;}
- }
- /**************************************************************
- 主程序
- **************************************************************/
- main()
- {
- TMOD=0x02; //设置模式为定时器T0的模式2 (8位自动重装计数初值的计数值)
- TH0=0x03; //设置计数器初值,靠TH0存储重装的计数值
- TL0=0x03;
- TR0=1; //启动T0
- ET0=0; //关定时器T0中断
- EA=1;
- P1=0;
- flag=1; //时钟停止标志
- LCD_Initial(); //液晶初始化
- Init_DS18B20( ) ; //DS18B20初始化
- Initial_DS1302(); //时钟芯片初始化
- up_flag=0; //调整标志位置零
- down_flag=0;
- done=0; //进入默认液晶显示
- while(1)
- {
- while(done==1)
- {keydone(); //进入调整模式
- warming();
- }
- while(done==0)
- {
- show_time(); //液晶显示数据
- flag=0;
- Setkey(); //扫描各功能键
- warming();
- }
- warming();
- }
- }
- /**************************************************************
- 定时器中断
- **************************************************************/
- void t0 (void) interrupt 1 using 1
- {deng=!deng;}
复制代码
所有资料51hei提供下载:
仿真程序.7z
(409.03 KB, 下载次数: 106)
|