找回密碼
 立即注冊(cè)

QQ登錄

只需一步,快速開始

搜索
查看: 6163|回復(fù): 10
打印 上一主題 下一主題
收起左側(cè)

ds1302+1602+18b20+四個(gè)按鍵調(diào)時(shí)間制作成功

  [復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
樓主
ID:242950 發(fā)表于 2017-11-17 15:30 | 只看該作者 |只看大圖 回帖獎(jiǎng)勵(lì) |倒序?yàn)g覽 |閱讀模式
按鍵兩個(gè)控制加減,再兩個(gè)按鍵控制選擇,選擇的時(shí)候屏幕顯示調(diào)整哪個(gè),例如調(diào)整年,屏幕就顯示1。
16.9代表是溫度
溫度顯示是實(shí)時(shí)溫度



正常顯示

調(diào)年份顯示1


調(diào)月份顯示2


單片機(jī)源程序如下:
  1. #include<reg52.h> //包含頭文件,一般情況不需要改動(dòng),頭文件包含特殊功能寄存器的定義
  2. #include <stdio.h>
  3. #include "ds1302.h"
  4. #include "delay.h"
  5. #include"18b20.h"
  6. #include "1602.h"
  7. #define KeyPort P3 //定義按鍵端口

  8. unsigned char KeyScan(void);//鍵盤掃描
  9. bit ReadTimeFlag;//定義讀時(shí)間標(biāo)志
  10. bit SetFlag;    //更新時(shí)間標(biāo)志位
  11. unsigned char time_buf2[16];
  12. sbit K1=P3^0;                //選擇
  13. sbit K2=P3^1;                //加
  14. sbit K3=P3^2;                //減
  15. sbit K4=P3^3;         
  16. void Init_Timer0(void);//定時(shí)器初始化
  17. void UART_Init(void);
  18. unsigned char Change_Flag[]= "0123456789AB";
  19. unsigned char Adjust_Index=0;  //當(dāng)前調(diào)節(jié)的時(shí)間對(duì)象:,,分,是,日,月,年(1,2,3,4,6)
  20. void DateTime_Adjust(char x);


  21. /*------------------------------------------------
  22.                     主函數(shù)
  23. ------------------------------------------------*/
  24. void main (void)
  25. {

  26. unsigned char temp1;
  27. float temperature;
  28. char displaytemp[16];
  29. char temp3[16];


  30.         //外部中斷0開
  31.    
  32. LCD_Init();           //初始化液晶
  33. Init_Timer0();        //定時(shí)器0初始化
  34. Ds1302_Init();        //ds1302初始化
  35.       
  36. Init_DS18B20();

  37.   Ds1302_Write_Time();
  38.   K1=K2=K3=K4=1;
  39.    while(1)
  40.    {
  41.         if(K1==0)          //選擇調(diào)整對(duì)象(Y M D H M)
  42.         {       
  43.                 DelayMs(10);
  44.                 if(K1==0)
  45.    {   DelayMs(10);
  46.      
  47.             DelayMs(10);       
  48.           DelayMs(10);       
  49.           Adjust_Index++;
  50.           LCD_Write_Char(12,0,Change_Flag[Adjust_Index]);
  51.           if(Adjust_Index==13)
  52.                  Adjust_Index=0;

  53.           switch( Adjust_Index)
  54.           {                            //第一行
  55.                     case 1:                LCD_Write_Char(0,0,0xff); break;       
  56.                     case 2:     LCD_Write_Char(1,0,0xff); break;
  57.                                 case 3:            LCD_Write_Char(3,0,0xff); break;
  58.                                 case 4:            LCD_Write_Char(4,0,0xff); break;
  59.                                 case 5:                LCD_Write_Char(6,0,0xff); break;       
  60.                                 case 6:     LCD_Write_Char(7,0,0xff); break;
  61.                            //第二行
  62.                                 case 7:                LCD_Write_Char(0,1,0xff); break;
  63.                                 case 8:           LCD_Write_Char(1,1,0xff); break;
  64.                                 case 9:                LCD_Write_Char(3,1,0xff); break;
  65.                                 case 10:        LCD_Write_Char(4,1,0xff); break;
  66.                                 case 11:        LCD_Write_Char(6,1,0xff); break;       
  67.                                 case 12:    LCD_Write_Char(7,1,0xff); break;



  68.                

  69.                 }
  70.         }          }
  71.         if(K2==0)                          //加
  72.         {        //while(K2==0);
  73.                        
  74.                 if(K2==0)
  75.                
  76.          switch(Adjust_Index)
  77.           {                  
  78.                              case 1:time_buf1[1]--;Ds1302_Write_Time();break;
  79.                                                   case 2:if(--time_buf1[2]<1)time_buf1[2]=12;Ds1302_Write_Time();break;
  80.                                                   case 3:{if(time_buf1[2]==1||time_buf1[2]==3||time_buf1[2]==5||time_buf1[2]==7||time_buf1[2]==8
  81.                                                   ||time_buf1[2]==10||time_buf1[2]==12)
  82.                                                   {if(--time_buf1[3]<1)time_buf1[3]=31;}else if(time_buf1[2]==2)
  83.                                                   {if(--time_buf1[3]<1)time_buf1[3]=28;}else {if(--time_buf1[3]<1)time_buf1[3]=30;}} Ds1302_Write_Time();
  84.                                                   break;
  85.                                                   case 4:if((--time_buf1[4])<1)time_buf1[4]=23;Ds1302_Write_Time();break;
  86.                                                   case 5:if((--time_buf1[5])<1)time_buf1[5]=59;Ds1302_Write_Time();break;
  87.                                         }
  88.         }
  89.          if(K3==0)                         //減
  90.         {
  91.           
  92.            DelayMs(10);
  93.            if(K3==0)
  94.            switch(Adjust_Index)
  95.                                           {
  96.                                                   case 1:time_buf1[1]++;Ds1302_Write_Time();break;
  97.                                                   case 3:if(++time_buf1[2]>12)time_buf1[2]=1;Ds1302_Write_Time();break;
  98.                                                   case 4:{if(time_buf1[2]==1||time_buf1[2]==3||time_buf1[2]==5||time_buf1[2]==7||time_buf1[2]==8
  99.                                                   ||time_buf1[2]==10||time_buf1[2]==12)
  100.                                                   {if(++time_buf1[3]>31)time_buf1[3]=1;}else if(time_buf1[2]==2)
  101.                                                   {if(++time_buf1[3]>28)time_buf1[3]=1;}else {if(++time_buf1[3]>30)time_buf1[3]=1;}}Ds1302_Write_Time();
  102.                                                   break;
  103.                                                   case 5:if(++time_buf1[4]>23)time_buf1[4]=0;Ds1302_Write_Time();break;
  104.                                                   case 6:if(++time_buf1[5]>59)time_buf1[5]=0;Ds1302_Write_Time();break;
  105.                                                    }   }
  106.                
  107.          if(K4==0)                           //確定
  108.         {
  109.                 //while(K4==0);
  110.                 DelayMs(10);
  111.                 TR0=1;
  112.                 if(K4==0)
  113.                 {
  114.                 Ds1302_Write_Time() ;                         //將調(diào)整后的時(shí)間寫入DS1302
  115.                
  116.                 Adjust_Index=0;
  117.                    }}       




  118.            if(ReadTimeFlag==1) //定時(shí)讀取ds1302 定時(shí)時(shí)間到 則標(biāo)志位置1,處理過(guò)時(shí)間參數(shù)標(biāo)志位清零
  119.         {
  120.           ReadTimeFlag=0;  //標(biāo)志位清零
  121.           Ds1302_Read_Time(); temp1=ReadTemperature();
  122.   temperature=(float)temp1*0.0625+15;
  123.   sprintf(displaytemp,"%0.3f",temperature);//打印溫度值
  124.   LCD_Write_String(12,1,displaytemp);
  125.         }
  126.          Ds1302_Read_Time();
  127.          LCD_Write_Char(0,0,(time_buf1[1]/10+0x30));
  128.          LCD_Write_Char(1,0,(time_buf1[1]%10+0x30));
  129.          LCD_Write_Char(2,0,'-');
  130.          LCD_Write_Char(3,0,(time_buf1[2]%10+0x30));
  131.          LCD_Write_Char(4,0,(time_buf1[2]/10+0x30));
  132.          LCD_Write_Char(5,0,'-');
  133.          LCD_Write_Char(6,0,(time_buf1[3]/10+0x30));
  134.          LCD_Write_Char(7,0,(time_buf1[3]%10+0x30));
  135.          LCD_Write_Char(0,1,(time_buf1[4]/10+0x30));
  136.          LCD_Write_Char(1,1,(time_buf1[4]%10+0x30));
  137.          LCD_Write_Char(2,1,':');
  138.          LCD_Write_Char(3,1,(time_buf1[5]/10+0x30));
  139.          LCD_Write_Char(4,1,(time_buf1[5]%10+0x30));
  140.          LCD_Write_Char(5,1,':');
  141.          LCD_Write_Char(6,1,(time_buf1[6]/10+0x30));
  142.          LCD_Write_Char(7,1,(time_buf1[6]%10+0x30));
  143.   
  144.      
  145.                   
  146.            }

  147. }   
  148.                  
  149.           



  150. ///*------------------------------------------------
  151. //                    定時(shí)器初始化子程序
  152. //------------------------------------------------*/
  153. void Init_Timer0(void)
  154. {
  155. TMOD |= 0x01;          //使用模式1,16位定時(shí)器,使用"|"符號(hào)可以在使用多個(gè)定時(shí)器時(shí)不受影響                     
  156. TH0=(65536-2000)/256;                  //重新賦值 2ms
  157. TL0=(65536-2000)%256;
  158. EA=1;            //總中斷打開
  159. ET0=1;           //定時(shí)器中斷打開
  160. TR0=1;           //定時(shí)器開關(guān)打開
  161. }
  162. ……………………

  163. …………限于本文篇幅 余下代碼請(qǐng)從51黑下載附件…………
復(fù)制代碼

所有資料51hei提供下載:
可調(diào)時(shí)鐘.rar (51.07 KB, 下載次數(shù): 123)




評(píng)分

參與人數(shù) 1黑幣 +100 收起 理由
admin + 100 共享資料的黑幣獎(jiǎng)勵(lì)!

查看全部評(píng)分

分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏3 分享淘帖 頂 踩
回復(fù)

使用道具 舉報(bào)

沙發(fā)
ID:164050 發(fā)表于 2018-1-29 23:08 | 只看該作者
這個(gè)不錯(cuò),找了很久,謝謝樓主
回復(fù)

使用道具 舉報(bào)

板凳
ID:183763 發(fā)表于 2018-4-14 21:32 | 只看該作者
樓主的截圖里面怎么會(huì)有70和80的顯示呢,似乎是月份的位置,應(yīng)該還有bug吧
回復(fù)

使用道具 舉報(bào)

地板
ID:308346 發(fā)表于 2018-4-19 10:19 | 只看該作者
下載了之后打不開
回復(fù)

使用道具 舉報(bào)

5#
ID:248394 發(fā)表于 2018-4-20 15:24 | 只看該作者
666收藏了
回復(fù)

使用道具 舉報(bào)

6#
ID:288065 發(fā)表于 2018-5-5 14:45 | 只看該作者
月份為啥有80啊,亂碼還是錯(cuò)了
回復(fù)

使用道具 舉報(bào)

7#
ID:242950 發(fā)表于 2018-6-4 17:04 | 只看該作者
YQQ 發(fā)表于 2018-5-5 14:45
月份為啥有80啊,亂碼還是錯(cuò)了

這是好久之前寫的,這個(gè)月份亂碼了,或者程序本身有點(diǎn)問(wèn)題
回復(fù)

使用道具 舉報(bào)

8#
ID:242950 發(fā)表于 2018-6-7 22:40 | 只看該作者
減應(yīng)該到255置0,這個(gè)沒(méi)有注意,大家改一下就好了
回復(fù)

使用道具 舉報(bào)

9#
ID:347372 發(fā)表于 2018-6-8 09:25 | 只看該作者
請(qǐng)問(wèn)如何用通信來(lái)實(shí)現(xiàn)另一個(gè)單片機(jī)來(lái)調(diào)節(jié)時(shí)間了
回復(fù)

使用道具 舉報(bào)

10#
ID:242950 發(fā)表于 2018-10-14 18:44 | 只看該作者
這個(gè)程序希望你們能看懂,而不是模仿,拷貝
回復(fù)

使用道具 舉報(bào)

11#
ID:510084 發(fā)表于 2019-4-12 19:56 | 只看該作者

裝WinRAR再試試
回復(fù)

使用道具 舉報(bào)

本版積分規(guī)則

手機(jī)版|小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機(jī)教程網(wǎng)

快速回復(fù) 返回頂部 返回列表