按鍵兩個控制加減,再兩個按鍵控制選擇,選擇的時候屏幕顯示調整哪個,例如調整年,屏幕就顯示1。
16.9代表是溫度
溫度顯示是實時溫度
IMG_20171117_153129.jpg (2.13 MB, 下載次數: 52)
下載附件
2017-11-17 15:32 上傳
正常顯示
IMG_20171117_151721.jpg (2.77 MB, 下載次數: 54)
下載附件
2017-11-17 15:25 上傳
調年份顯示1
IMG_20171117_151737.jpg (2.65 MB, 下載次數: 50)
下載附件
2017-11-17 15:25 上傳
調月份顯示2
IMG_20171117_151751.jpg (2.71 MB, 下載次數: 43)
下載附件
2017-11-17 15:28 上傳
單片機源程序如下:
- #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)
2017-11-17 15:30 上傳
點擊文件名下載附件
下載積分: 黑幣 -5
|