按键两个控制加减,再两个按键控制选择,选择的时候屏幕显示调整哪个,例如调整年,屏幕就显示1。
16.9代表是温度
温度显示是实时温度
正常显示
调年份显示1
调月份显示2
单片机源程序如下:
- #include<reg52.h> //包含头文件,一般情况不需要改动,头文件包含特殊功能寄存器的定义
- #include <stdio.h>
- #include "ds1302.h"
- #include "delay.h"
- #include"18b20.h"
- #include "1602.h"
- #define KeyPort P3 //定义按键端口
- unsigned char KeyScan(void);//键盘扫描
- bit ReadTimeFlag;//定义读时间标志
- bit SetFlag; //更新时间标志位
- unsigned char time_buf2[16];
- sbit K1=P3^0; //选择
- sbit K2=P3^1; //加
- sbit K3=P3^2; //减
- sbit K4=P3^3;
- void Init_Timer0(void);//定时器初始化
- void UART_Init(void);
- unsigned char Change_Flag[]= "0123456789AB";
- unsigned char Adjust_Index=0; //当前调节的时间对象:,,分,是,日,月,年(1,2,3,4,6)
- void DateTime_Adjust(char x);
- /*------------------------------------------------
- 主函数
- ------------------------------------------------*/
- void main (void)
- {
- unsigned char temp1;
- float temperature;
- char displaytemp[16];
- char temp3[16];
- //外部中断0开
-
- LCD_Init(); //初始化液晶
- Init_Timer0(); //定时器0初始化
- Ds1302_Init(); //ds1302初始化
-
- Init_DS18B20();
- Ds1302_Write_Time();
- K1=K2=K3=K4=1;
- while(1)
- {
- if(K1==0) //选择调整对象(Y M D H M)
- {
- DelayMs(10);
- if(K1==0)
- { DelayMs(10);
-
- DelayMs(10);
- DelayMs(10);
- Adjust_Index++;
- LCD_Write_Char(12,0,Change_Flag[Adjust_Index]);
- if(Adjust_Index==13)
- Adjust_Index=0;
- switch( Adjust_Index)
- { //第一行
- case 1: LCD_Write_Char(0,0,0xff); break;
- case 2: LCD_Write_Char(1,0,0xff); break;
- case 3: LCD_Write_Char(3,0,0xff); break;
- case 4: LCD_Write_Char(4,0,0xff); break;
- case 5: LCD_Write_Char(6,0,0xff); break;
- case 6: LCD_Write_Char(7,0,0xff); break;
- //第二行
- case 7: LCD_Write_Char(0,1,0xff); break;
- case 8: LCD_Write_Char(1,1,0xff); break;
- case 9: LCD_Write_Char(3,1,0xff); break;
- case 10: LCD_Write_Char(4,1,0xff); break;
- case 11: LCD_Write_Char(6,1,0xff); break;
- case 12: LCD_Write_Char(7,1,0xff); break;
-
- }
- } }
- if(K2==0) //加
- { //while(K2==0);
-
- if(K2==0)
-
- switch(Adjust_Index)
- {
- case 1:time_buf1[1]--;Ds1302_Write_Time();break;
- case 2:if(--time_buf1[2]<1)time_buf1[2]=12;Ds1302_Write_Time();break;
- case 3:{if(time_buf1[2]==1||time_buf1[2]==3||time_buf1[2]==5||time_buf1[2]==7||time_buf1[2]==8
- ||time_buf1[2]==10||time_buf1[2]==12)
- {if(--time_buf1[3]<1)time_buf1[3]=31;}else if(time_buf1[2]==2)
- {if(--time_buf1[3]<1)time_buf1[3]=28;}else {if(--time_buf1[3]<1)time_buf1[3]=30;}} Ds1302_Write_Time();
- break;
- case 4:if((--time_buf1[4])<1)time_buf1[4]=23;Ds1302_Write_Time();break;
- case 5:if((--time_buf1[5])<1)time_buf1[5]=59;Ds1302_Write_Time();break;
- }
- }
- if(K3==0) //减
- {
-
- DelayMs(10);
- if(K3==0)
- switch(Adjust_Index)
- {
- case 1:time_buf1[1]++;Ds1302_Write_Time();break;
- case 3:if(++time_buf1[2]>12)time_buf1[2]=1;Ds1302_Write_Time();break;
- case 4:{if(time_buf1[2]==1||time_buf1[2]==3||time_buf1[2]==5||time_buf1[2]==7||time_buf1[2]==8
- ||time_buf1[2]==10||time_buf1[2]==12)
- {if(++time_buf1[3]>31)time_buf1[3]=1;}else if(time_buf1[2]==2)
- {if(++time_buf1[3]>28)time_buf1[3]=1;}else {if(++time_buf1[3]>30)time_buf1[3]=1;}}Ds1302_Write_Time();
- break;
- case 5:if(++time_buf1[4]>23)time_buf1[4]=0;Ds1302_Write_Time();break;
- case 6:if(++time_buf1[5]>59)time_buf1[5]=0;Ds1302_Write_Time();break;
- } }
-
- if(K4==0) //确定
- {
- //while(K4==0);
- DelayMs(10);
- TR0=1;
- if(K4==0)
- {
- Ds1302_Write_Time() ; //将调整后的时间写入DS1302
-
- Adjust_Index=0;
- }}
- if(ReadTimeFlag==1) //定时读取ds1302 定时时间到 则标志位置1,处理过时间参数标志位清零
- {
- ReadTimeFlag=0; //标志位清零
- Ds1302_Read_Time(); temp1=ReadTemperature();
- temperature=(float)temp1*0.0625+15;
- sprintf(displaytemp,"%0.3f",temperature);//打印温度值
- LCD_Write_String(12,1,displaytemp);
- }
- Ds1302_Read_Time();
- LCD_Write_Char(0,0,(time_buf1[1]/10+0x30));
- LCD_Write_Char(1,0,(time_buf1[1]%10+0x30));
- LCD_Write_Char(2,0,'-');
- LCD_Write_Char(3,0,(time_buf1[2]%10+0x30));
- LCD_Write_Char(4,0,(time_buf1[2]/10+0x30));
- LCD_Write_Char(5,0,'-');
- LCD_Write_Char(6,0,(time_buf1[3]/10+0x30));
- LCD_Write_Char(7,0,(time_buf1[3]%10+0x30));
- LCD_Write_Char(0,1,(time_buf1[4]/10+0x30));
- LCD_Write_Char(1,1,(time_buf1[4]%10+0x30));
- LCD_Write_Char(2,1,':');
- LCD_Write_Char(3,1,(time_buf1[5]/10+0x30));
- LCD_Write_Char(4,1,(time_buf1[5]%10+0x30));
- LCD_Write_Char(5,1,':');
- LCD_Write_Char(6,1,(time_buf1[6]/10+0x30));
- LCD_Write_Char(7,1,(time_buf1[6]%10+0x30));
-
-
-
- }
-
- }
-
-
- ///*------------------------------------------------
- // 定时器初始化子程序
- //------------------------------------------------*/
- void Init_Timer0(void)
- {
- TMOD |= 0x01; //使用模式1,16位定时器,使用"|"符号可以在使用多个定时器时不受影响
- TH0=(65536-2000)/256; //重新赋值 2ms
- TL0=(65536-2000)%256;
- EA=1; //总中断打开
- ET0=1; //定时器中断打开
- TR0=1; //定时器开关打开
- }
- ……………………
- …………限于本文篇幅 余下代码请从51黑下载附件…………
复制代码
所有资料51hei提供下载:
可调时钟.rar
(51.07 KB, 下载次数: 123)
|