久久久久久久999_99精品久久精品一区二区爱城_成人欧美一区二区三区在线播放_国产精品日本一区二区不卡视频_国产午夜视频_欧美精品在线观看免费

 找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索
查看: 1729|回復: 0
打印 上一主題 下一主題
收起左側

單片機時鐘套件資料

[復制鏈接]
跳轉到指定樓層
樓主
分享網淘的時鐘套件資料
興向榮時鐘套件配套資料.rar (2.41 MB, 下載次數: 16)







  1. /*   忠興那個寶店編輯
  2. */
  3. /*====================================================================
  4.   調試要求:
  5.   1.MCU:AT89S52芯片或AT89C52
  6.   2.晶振:12MHz
  7.   功能:多功能時鐘+溫度計
  8. ====================================================================*/
  9. #include <reg52.h>
  10. #include <intrins.h>
  11. [size=14px]
  12. [/size]
  13. #define uchar unsigned char
  14. #define uint  unsigned int
  15. [size=14px]
  16. [/size]
  17. sbit    dis_bit1=P2^7;//定義數碼管控制口
  18. sbit    dis_bit2=P2^6;//定義數碼管控制口
  19. sbit    dis_bit3=P2^4;//定義數碼管控制口
  20. sbit    dis_bit4=P2^3;//定義數碼管控制口
  21. sbit    dis_bit5=P2^1;//定義數碼管控制口
  22. sbit    dis_bit6=P2^0;//定義數碼管控制口
  23. sbit    led1_bit=P2^2;//定時LED的控制口
  24. sbit    led2_bit=P2^5;//定時LED的控制口
  25. sbit    s1_bit=P1^0;  //定義S1控制口
  26. sbit    s2_bit=P1^1;  //定義S2控制口
  27. sbit    s3_bit=P1^2;  //定義S3控制口
  28. sbit    dq_ds18b20=P3^3;//定義控制DS18B20
  29. sbit    speak=P3^7;   //定義蜂鳴器控制口
  30. sbit    clk_ds1302=P3^6;//定義控制DS1302的時鐘線
  31. sbit    io_ds1302=P3^5;//定義控制DS1302的串行數據
  32. sbit    rest_ds1302=P3^4;
  33. #define smg_data    P0//定義數碼管數據口
  34. [size=14px]
  35. [/size]
  36. void  delay_3us();//3US的延時程序
  37. void  delay_8us(uint t);//8US延時基準程序
  38. void  delay_50us(uint t);//延時50*T微妙函數的聲明
  39. void  display1(uchar dis_data);//數碼管1顯示子程序
  40. void  display2(uchar dis_data);//數碼管2顯示子程序
  41. void  display3(uchar dis_data);//數碼管3顯示子程序
  42. void  display4(uchar dis_data);//數碼管4顯示子程序
  43. void  display5(uchar dis_data);//數碼管5顯示子程序
  44. void  display6(uchar dis_data);//數碼管6顯示子程序
  45. void  init_t0();//定時器0初始化函數
  46. void  dis_led();//LED處理函數
  47. void  judge_s1();//S1按鍵處理函數
  48. void  judge_s2();//S2按鍵處理函數
  49. void  judge_s3();//S3按鍵處理函數
  50. void  dis(uchar s6,uchar s5,uchar s4,uchar s3,uchar s2,uchar s1);//顯示子程序
  51. void  dis_san(uchar s6,uchar s5,uchar s4,uchar s3,uchar s2,uchar s1,uchar san);//閃爍顯示子程序
  52. void  judge_dis();//顯示處理函數
  53. void  judge_clock();//顯示處理函數
  54. void  set_ds1302();//設置時間
  55. void  get_ds1302();//讀取當前時間
  56. [size=14px]
  57. [/size]
  58. void  w_1byte_ds1302(uchar t);//向DS1302寫一個字節的數據
  59. uchar r_1byte_ds1302();//從DS1302讀一個字節的數據
  60. //***********************************************************************
  61. //DS18B20測溫函數定義
  62. void w_1byte_ds18b20(uchar value);//向DS18B20寫一個字節
  63. uchar r_1byte_ds18b20(void);//從DS18B20讀取一個字節的數據
  64. void rest_ds18b20(void);//DS18B20復位程序
  65. void readtemp_ds18b20(void);//讀取溫度
  66. void dis_temp();//溫度顯示函數
  67. [size=14px]
  68. [/size]
  69. [size=14px]
  70. [/size]
  71. [size=14px]
  72. [/size]
  73. [size=14px]
  74. [/size]
  75. //共陽數碼管斷碼表
  76. const uchar tabl1[16]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,
  77.                        // 0  1    2    3    4    5
  78.                        0x82,0xf8,0x80,0x90,0x86,0x87,0xFF,
  79.                        //6   7     8    9     E    T   B
  80.                        0xc6,0xbf,0xff};
  81.                         //    C      -
  82. const uchar tabl3[]={0x00,0x01,0x01,0x02,0x03,0x03,0x04,0x04,0x05,0x06,0x06,0x07,0x08,0x08,0x09,0x09};
  83. uchar t0_crycle;
  84. uchar hour_count,minute_count,second_count,msecond_count;
  85. uchar clock_hour,clock_minute;
  86. uchar countdown_second;
  87. uchar countdown_hour,countdown_minute;
  88. uchar clock_en;//鬧鐘關閉和開啟的標志,1開啟,0關閉
  89. uchar flag1,second_flag,zancun1,zancun2,zancun3;
  90. uchar zancun4,zancun5,zancun6,zancun7;
  91. uchar clock_flag,countdown_flag;
  92. uchar msecond_minute,msecond_second,msecond_msecond,msecond_flag;//秒表相關參數
  93. uint  speak_count;
  94. uchar templ,temph,temp_flag;
  95. uchar  t_b,t_s,t_g,t_x,temp_flag2;//從左到右分別存儲溫度百位,十位,個位,小數位
  96. uchar tab23[3];//={0x40,0x59,0x23,0x28,0x11,0x06,0x09};//上電時默認的時間
  97. //主程序
  98. void main()
  99. {
  100.     P3=0x00;
  101.     flag1=0;   
  102.     zancun3=0;
  103.     msecond_minute=0;//置秒表相關參數為0
  104.     msecond_second=0;
  105.     msecond_msecond=0;
  106.     speak=1;//關閉蜂鳴器
  107.     speak_count=0;
  108.     clock_hour=0;
  109.     clock_minute=0;
  110.     clock_flag=0;
  111.     countdown_flag=0;//倒計時標志位為0
  112.     clock_en=0;//開機時默認關閉鬧鐘
  113.     init_t0();
  114.     TR0=1;//
  115. [size=14px]
  116. [/size]
  117.    // set_ds1302();//設置DS1302的初始時間
  118.    //接下來開始編寫讓數碼管顯示的程序
  119.     while(1)
  120.     {
  121.       get_ds1302();
  122.       judge_dis();//顯示處理
  123.       judge_s1();
  124.       judge_s2();
  125.       judge_s3();
  126.       judge_clock();//鬧鐘處理程序
  127.     }
  128. }
  129. void timer0() interrupt 1
  130. {
  131. [size=14px]        TH0=(65536-50000)/256;[/size]
  132. [size=14px]        TL0=(65536-50000)%256;[/size]
  133. [size=14px]        t0_crycle++;[/size]
  134. [size=14px]        if(t0_crycle==2)// 0.1秒[/size]
  135. [size=14px]        {[/size]
  136. [size=14px]          t0_crycle=0;[/size]
  137.       msecond_flag=1;
  138. [size=14px]          msecond_count++;[/size]
  139.       if(msecond_count==10)//1秒
  140.       {
  141.         msecond_count=0;
  142.         second_flag=1;
  143.       }   
  144. [size=14px]        }[/size]
  145. }
  146. //**************************************************
  147. //顯示處理函數
  148. void  judge_dis()
  149. {     
  150.     if(flag1==0)
  151.     {
  152.      if(second_flag==1)
  153.       {
  154.         zancun7++;
  155.         second_flag=0;
  156.       }
  157.       if(zancun7<1)
  158.       {
  159.         if(temp_flag2==1)
  160.         {
  161.           readtemp_ds18b20();//讀取溫度  
  162.           temp_flag2=0;
  163.         }
  164.         dis_temp();//溫度顯示函數
  165.       }
  166.      if(zancun7>=1)
  167.       {

  168.          temp_flag2=1;
  169.          zancun4=hour_count&0xf0;
  170.          zancun4>>=4;
  171.          zancun5=minute_count&0xf0;
  172.          zancun5>>=4;
  173.          zancun6=second_count&0xf0;
  174.          zancun6>>=4;
  175.          dis(zancun4,hour_count&0x0f,zancun5,minute_count&0x0f,zancun6,second_count&0x0f);
  176.          dis_led();
  177.          if(zancun7==5)zancun7=0;
  178.       }
  179.     }
  180.     if(flag1!=0)
  181.     {
  182.        switch(flag1)
  183.        {
  184.            case 1:
  185.                 dis(5,10,11,1,12,12);//顯示SET1
  186.                 led1_bit=1;
  187.                 led2_bit=1;
  188.                 break;
  189.            case 2:
  190.                 dis(5,10,11,2,12,12);//顯示SET2
  191.                 break;
  192.            case 3:
  193.                 dis(5,10,11,3,12,12);//顯示SET3
  194.                 break;
  195.            case 4:
  196.                 dis(5,10,11,4,12,12);//顯示SET4
  197.                 break;
  198.            case 5:
  199.                 dis(5,10,11,5,12,12);//顯示SET5
  200.                 break;
  201.            case 6:
  202.                 dis_san(zancun1/10,zancun1%10,zancun2/10,zancun2%10,12,12,1);
  203.                 break;
  204.            case 7:
  205.                 dis_san(zancun1/10,zancun1%10,zancun2/10,zancun2%10,12,12,2);
  206.                 break;
  207.            case 8:
  208.                 dis_san(zancun1/10,zancun1%10,zancun2/10,zancun2%10,12,12,3);
  209.                 break;
  210.            case 9://進入修改時間,時間分位個位閃爍
  211.                 dis_san(zancun1/10,zancun1%10,zancun2/10,zancun2%10,12,12,4);
  212.                 break;
  213.            case 10://進入修改鬧鐘,鬧鐘小時十位閃爍
  214.                 dis_san(zancun1/10,zancun1%10,zancun2/10,zancun2%10,12,zancun3,1);
  215.                 break;
  216.            case 11://進入修改鬧鐘,鬧鐘小時個位閃爍
  217.                 dis_san(zancun1/10,zancun1%10,zancun2/10,zancun2%10,12,zancun3,2);
  218.                 break;
  219.            case 12://進入修改鬧鐘,鬧鐘小時十位閃爍
  220.                 dis_san(zancun1/10,zancun1%10,zancun2/10,zancun2%10,12,zancun3,3);
  221.                 break;
  222.            case 13://進入修改鬧鐘,鬧鐘小時個位閃爍
  223.                 dis_san(zancun1/10,zancun1%10,zancun2/10,zancun2%10,12,zancun3,4);
  224.                 break;
  225.            case 14://進入修改鬧鐘的開關
  226.                 dis_san(zancun1/10,zancun1%10,zancun2/10,zancun2%10,12,zancun3,6);
  227.                 break;
  228.            case 15:
  229.                 dis_san(zancun1/10,zancun1%10,zancun2/10,zancun2%10,zancun3/10,zancun3%10,1);
  230.                 break;
  231.            case 16:
  232.                 dis_san(zancun1/10,zancun1%10,zancun2/10,zancun2%10,zancun3/10,zancun3%10,2);
  233.                 break;
  234.            case 17:
  235.                 dis_san(zancun1/10,zancun1%10,zancun2/10,zancun2%10,zancun3/10,zancun3%10,3);
  236.                 break;
  237.            case 18:
  238.                 dis_san(zancun1/10,zancun1%10,zancun2/10,zancun2%10,zancun3/10,zancun3%10,4);
  239.                 break;
  240.            case 19:
  241.                 dis_san(zancun1/10,zancun1%10,zancun2/10,zancun2%10,zancun3/10,zancun3%10,5);
  242.                 break;
  243.            case 20:
  244.                 dis_san(zancun1/10,zancun1%10,zancun2/10,zancun2%10,zancun3/10,zancun3%10,6);
  245.                 break;
  246.            case 21:
  247.                 if(second_flag==1)
  248.                 {
  249.                     second_flag=0;
  250.                     countdown_second--;
  251.                     if(countdown_second==255)
  252.                     {
  253.                         countdown_second=59;
  254.                         countdown_minute--;
  255.                         if(countdown_minute==255)
  256.                         {
  257.                             countdown_minute=59;
  258.                             countdown_hour--;
  259.                             if(countdown_hour==255)
  260.                             {
  261.                                flag1=22;
  262.                                countdown_minute=0;
  263.                                countdown_hour=0;
  264.                                countdown_second=0;
  265.                                countdown_flag=1;
  266.                             }
  267.                         }
  268.                     }

  269.                 }
  270.                 dis(countdown_hour/10,countdown_hour%10,countdown_minute/10,countdown_minute%10,countdown_second/10,countdown_second%10);//
  271.                 break;
  272.            case 22:
  273.                 if(countdown_flag>0 && countdown_flag<7)
  274.                 {
  275.                    speak=0;
  276.                    if(second_flag==1)
  277.                    {
  278.                        second_flag=0;
  279.                        countdown_flag++;
  280.                    }
  281.                 }
  282.                 else
  283.                 {
  284.                    speak=1;
  285.                 }
  286.                  dis(countdown_hour/10,countdown_hour%10,countdown_minute/10,countdown_minute%10,countdown_second/10,countdown_second%10);//
  287.                 break;
  288.             case 23:
  289.                 dis(msecond_minute/10,msecond_minute%10,msecond_second/10,msecond_second%10,msecond_msecond%10,12);
  290.                 break;
  291.             case 24:
  292.                 if(msecond_flag==1)
  293.                 {
  294.                      msecond_flag=0;
  295.                      msecond_msecond++;
  296.                      if(msecond_msecond==10)
  297.                      {
  298.                          msecond_msecond=0;
  299.                          msecond_second++;
  300.                          if(msecond_second==60)
  301.                          {
  302.                            msecond_second=0;
  303.                            msecond_minute++;
  304.                            if(msecond_minute==100)
  305.                            {
  306.                              msecond_minute=99;
  307.                              flag1=23;
  308.                            }
  309.                          }
  310.                      }
  311.                 }
  312.                 dis(msecond_minute/10,msecond_minute%10,msecond_second/10,msecond_second%10,msecond_msecond%10,12);
  313.                 break;
  314.             case 25:
  315.                 dis(zancun3/10,zancun3%10,zancun2/10,zancun2%10,zancun1/10,zancun1%10);
  316.                 break;
  317.             default:
  318.                 break;
  319.         }
  320.     }
  321. }
  322. //**************************************************
  323. //S1按鍵處理函數
  324. void  judge_s1()
  325. {
  326.     s1_bit=1;//置IO為1,準備讀入收據
  327.     if(s1_bit==0)//判斷是否有按鍵按下
  328.     {
  329.         delay_50us(1);// 延時,去除機械抖動
  330.         if(s1_bit==0)
  331.         {
  332.            switch(flag1)
  333.            {
  334.                case 0:
  335.                case 1:
  336.                case 2:
  337.                case 3:
  338.                case 4:
  339.                case 6:
  340.                case 7:
  341.                case 8:
  342.                case 10:
  343.                case 11:
  344.                case 12:
  345.                case 13:
  346.                case 15:
  347.                case 16:
  348.                case 17:
  349.                case 18:
  350.                case 19:
  351.                     flag1++;
  352.                     break;
  353.                case 9:
  354.                     flag1=6;
  355.                     break;
  356.                case 14:
  357.                     flag1=10;
  358.                     break;
  359.                case 20:
  360.                     flag1=15;
  361.                     break;
  362.                case 5:
  363.                case 21:
  364.                case 22:
  365.                case 23://系統從秒表狀態復位
  366.                case 24://系統從秒表狀態復位
  367.                case 25://系統從計數器復位
  368.                     flag1=0;
  369.                     break;
  370.                 default:
  371.                     break;
  372.            }           
  373.            while(s1_bit==0)
  374.            {
  375.               judge_dis();
  376.            }//等待按鍵釋放
  377.         }
  378.     }
  379. }
  380. //**************************************************
  381. //S2按鍵處理函數
  382. void  judge_s2()
  383. {
  384.     s2_bit=1;//置IO為1,準備讀入收據
  385.     if(s2_bit==0)//判斷是否有按鍵按下
  386.     {
  387.         delay_50us(1);// 延時,去除機械抖動
  388.         if(s2_bit==0)
  389.         {
  390.            switch (flag1)
  391.            {
  392.               case 1: //在顯示SET1狀態下按S2件,進入修改時間
  393.                  flag1=6;
  394.                  zancun4=hour_count&0xf0;
  395.                  zancun4>>=4;
  396.                  zancun6=hour_count&0x0f;
  397.                  zancun1=zancun4*10+zancun6;
  398.                  //zancun1=hour_count;
  399.                  zancun5=minute_count&0xf0;
  400.                  zancun5>>=4;
  401.                  zancun6=minute_count&0x0f;
  402.                  zancun2=zancun5*10+zancun6;
  403.                  //  zancun2=minute_count;
  404.                  break;
  405.               case 2://在顯示SET2狀態下按S2,進入設置鬧鐘
  406.                  zancun1=clock_hour;
  407.                  zancun2=clock_minute;
  408.                  flag1=10;
  409.                  break;
  410.               case 6://修改時鐘小時十位狀態下按S2件
  411.               case 7://修改時鐘小時個位狀態下按S2件
  412.               case 8://修改時鐘分鐘十位狀態下按S2件
  413.               case 9://修改時鐘分鐘個位狀態下按S2件
  414.                  //zancun4=zancun1/10;
  415. [size=14px]
  416. [/size]
  417.                  tab23[2]=zancun1/10*16+zancun1%10;

  418.                  //zancun5=zancun2&0xf0;
  419.                  //zancun5>>=4;
  420.                  tab23[1]=zancun2/10*16+zancun2%10;
  421.                  hour_count=tab23[2];
  422.                  minute_count=tab23[1];
  423.                  second_count=0;
  424.                  tab23[0]=0;
  425.                  set_ds1302();//設置DS1302的初始時間
  426.                  flag1=0;
  427.                  break;
  428.               case 10://修改鬧鐘小時十位狀態下按S2
  429.               case 11://修改鬧鐘小時個位狀態下按S2
  430.               case 12://修改鬧鐘分鐘十位狀態下按S2
  431.               case 13://修改鬧鐘分鐘個位狀態下按S2
  432.               case 14://修改鬧鐘使能狀態下按S2
  433.                  clock_hour=zancun1;
  434.                  clock_minute=zancun2;
  435.                  clock_en=zancun3;
  436.                  flag1=0;
  437.                  break;
  438.               case 3:
  439.                  flag1=15;
  440.                  zancun1=countdown_hour;
  441.                  zancun2=countdown_minute;
  442.                  zancun3=countdown_second;
  443.                  break;
  444.               case 15:
  445.               case 16:
  446.               case 17:
  447.               case 18:
  448.               case 19:
  449.               case 20:
  450.                  countdown_hour=zancun1;
  451.                  countdown_minute=zancun2;
  452.                  countdown_second=zancun3;
  453.                  flag1=21;
  454.                  countdown_flag=0;
  455.                  break;
  456.               case 22:
  457.                  flag1=21;
  458.                  break;
  459.               case 21:
  460.                  flag1=22;
  461.                  break;
  462.               case 4:
  463.                  flag1=23;//秒表暫停
  464.                  msecond_minute=0;
  465.                  msecond_second=0;
  466.                  msecond_msecond=0;
  467.                  break;
  468.               case 23:
  469.                  flag1=24;
  470.                  break;
  471.               case 24:
  472.                  flag1=23;
  473.                  break;
  474.               case 5:
  475.                  flag1=25;//進入計數器模式
  476.                  zancun1=0;
  477.                  zancun2=0;
  478.                  zancun3=0;
  479.                  break;
  480.               default:
  481.                  break;

  482.            }   
  483.            while(s2_bit==0)
  484.            {
  485.               judge_dis();
  486.            }//等待按鍵釋放
  487.         }
  488.     }
  489. }
  490. //**************************************************
  491. //S3按鍵處理函數
  492. void  judge_s3()
  493. {
  494.     s3_bit=1;//置IO為1,準備讀入收據
  495.     if(s3_bit==0)//判斷是否有按鍵按下
  496.     {
  497.         delay_50us(1);// 延時,去除機械抖動
  498.         if(s3_bit==0)
  499.         {
  500.            switch (flag1)
  501.            {

  502.               case 6://修改時間小時的十位數
  503.                  zancun1+=10;
  504.                  if(zancun1>=24)zancun1=zancun1%10;
  505.                  break;
  506.               case 7://修改時間小時的個位數
  507.                  zancun1=zancun1/10*10+(zancun1%10+1)%10;
  508.                  if(zancun1>=24)zancun1=20;
  509.                  break;
  510.               case 8://修改時間分鐘的十位數
  511.                  zancun2+=10;
  512.                  if(zancun2>=60)zancun2-=60;
  513.                  break;
  514.               case 9://修改時間分鐘的個位數
  515.                  zancun2=zancun2/10*10+(zancun2%10+1)%10;
  516.                  break;
  517.               case 10://修改鬧鐘小時的十位數
  518.                  zancun1+=10;
  519.                  if(zancun1>=24)zancun1=zancun1%10;
  520.                  break;
  521.               case 11://修改鬧鐘小時的個位數
  522.                  zancun1=zancun1/10*10+(zancun1%10+1)%10;
  523.                  if(zancun1>=24)zancun1=20;
  524.                  break;
  525.               case 12://修改鬧鐘分鐘的十位數
  526.                  zancun2+=10;
  527.                  if(zancun2>=60)zancun2-=60;
  528.                  break;
  529.               case 13://修改鬧鐘分鐘的個位數
  530.                  zancun2=zancun2/10*10+(zancun2%10+1)%10;
  531.                  break;
  532.               case 14:
  533.                  zancun3^=1;
  534.                  break;
  535.               case 15://修改倒計時小時的十位數
  536.                  zancun1+=10;
  537.                  if(zancun1>=100)zancun1-=100;
  538.                  break;
  539.               case 16: //修改倒計時小時的個位數
  540.                  zancun1=zancun1/10*10+(zancun1%10+1)%10;
  541.                  break;
  542.               case 17://修改倒計時分鐘的十位數
  543.                  zancun2+=10;
  544.                  if(zancun2>=60)zancun2-=60;
  545.                  break;
  546.               case 18: //修改倒計時分鐘的個位數
  547.                  zancun2=zancun2/10*10+(zancun2%10+1)%10;
  548.                  break;
  549.               case 19://修改倒計時秒的十位數
  550.                  zancun3+=10;
  551.                  if(zancun3>=60)zancun3-=60;
  552.                  break;
  553.               case 20: //修改倒計時秒的個位數
  554.                  zancun3=zancun3/10*10+(zancun3%10+1)%10;
  555.                  break;
  556.               case 21:
  557.               case 22://
  558.                  countdown_hour=zancun1;
  559.                  countdown_minute=zancun2;
  560.                  countdown_second=zancun3;
  561.                  flag1=21;
  562.                  break;
  563.               case 23:
  564.               case 24://秒表復位
  565.                  flag1=24;
  566.                  msecond_minute=0;
  567.                  msecond_second=0;
  568.                  msecond_msecond=0;
  569.                  break;
  570.               case 25:
  571.                  zancun1++;
  572.                  if(zancun1==100)
  573.                  {
  574.                      zancun1=0;
  575.                      zancun2++;
  576.                      if(zancun2==100)
  577.                      {
  578.                        zancun2=0;
  579.                        zancun3++;
  580.                      }
  581.                  }
  582.                  break;
  583.               default:
  584.                  break;
  585.            }   
  586.            while(s3_bit==0)
  587.            {
  588.               judge_dis();
  589.            }//等待按鍵釋放
  590.         }
  591.     }
  592. }
  593. //****************************************
  594. //顯示處理函數
  595. void  judge_clock()
  596. {
  597.     zancun4=hour_count&0xf0;
  598.     zancun4>>=4;
  599.     zancun6=hour_count&0x0f;
  600.     zancun4*=10;
  601.     zancun4+=zancun6;
  602. [size=14px]
  603. [/size]
  604.     zancun5=minute_count&0xf0;
  605.     zancun5>>=4;
  606.     zancun6=minute_count&0x0f;
  607.     zancun5*=10;
  608.     zancun5+=zancun6;
  609. [size=14px]
  610. [/size]
  611.     if(clock_hour==zancun4 && clock_minute==zancun5)
  612.     {
  613.          if(clock_en==1 && clock_flag==0)
  614.          {         
  615.               speak_count=0;//開啟蜂鳴器
  616.               clock_flag=1;
  617.               speak_count=0;
  618.          }
  619.     }
  620.     else
  621.     {
  622.      clock_flag=0;
  623.     }
  624.     if(clock_flag==1 && speak_count<400)
  625.     {

  626.         if(msecond_count<=5)
  627.         {
  628.            speak=0;
  629.            speak_count++;
  630.         }
  631.         else
  632.         {
  633.           speak=1;

  634.         }
  635.     }
  636.     else
  637.     {
  638.         speak=1;
  639.     }
  640. }
  641. //****************************************
  642. ////閃爍顯示子程序
  643. void  dis_san(uchar s6,uchar s5,uchar s4,uchar s3,uchar s2,uchar s1,uchar san)
  644. {   
  645.      if(san==1)
  646.      {
  647.         if(msecond_count<5)
  648.         {
  649.            display1(s6);
  650.         }
  651.      }
  652.      else
  653.      {
  654.          display1(s6);
  655.      }
  656.      if(san==2)
  657.      {
  658.         if(msecond_count<5)
  659.         {
  660.            display2(s5);
  661.         }
  662.      }
  663.      else
  664.      {
  665.          display2(s5);
  666.      }
  667.      if(san==3)
  668.      {
  669.         if(msecond_count<5)
  670.         {
  671.            display3(s4);
  672.         }
  673.      }
  674.      else
  675.      {
  676.          display3(s4);
  677.      }
  678.      if(san==4)
  679.      {
  680.         if(msecond_count<5)
  681.         {
  682.            display4(s3);
  683.         }
  684.      }
  685.      else
  686.      {
  687.          display4(s3);
  688.      }
  689.      if(san==5)
  690.      {
  691.         if(msecond_count<5)
  692.         {
  693.            display5(s2);
  694.         }
  695.      }
  696.      else
  697.      {
  698.          display5(s2);
  699.      }
  700.      if(san==6)
  701.      {
  702.         if(msecond_count<5)
  703.         {
  704.            display6(s1);
  705.         }
  706.      }
  707.      else
  708.      {
  709.          display6(s1);
  710.      }
  711. }
  712. //****************************************
  713. //時鐘顯示程序
  714. void  dis(uchar s6,uchar s5,uchar s4,uchar s3,uchar s2,uchar s1)
  715. {
  716.      display1(s6);
  717.      display2(s5);
  718.      display3(s4);
  719.      display4(s3);
  720.      display5(s2);
  721.      display6(s1);
  722. }
  723. //********************************************************************************************
  724. void init_t0()
  725. {
  726.            TMOD=0x01;//設定定時器工作方式1,定時器定時50毫秒
  727. [size=14px]        TH0=(65536-50000)/256;[/size]
  728. [size=14px]        TL0=(65536-50000)%256;[/size]
  729. [size=14px]        EA=1;//開總中斷[/size]
  730. [size=14px]        ET0=1;//允許定時器0中斷[/size]
  731. [size=14px]        t0_crycle=0;//定時器中斷次數計數單元[/size]
  732. }
  733. //**************************************************
  734. //LED處理函數
  735. void  dis_led()
  736. {
  737.      if(msecond_count<5)
  738.      {
  739.          led1_bit=1;
  740.          led2_bit=1;
  741.      }
  742.      else
  743.      {
  744.          led1_bit=0;
  745.          led2_bit=0;
  746.      }
  747. }
  748. //***************************************************************
  749. //功能:把數據1顯示在數碼管1上
  750. void display6(uchar dis_data)
  751. {  
  752.    smg_data=tabl1[dis_data];//送顯示斷碼
  753.    dis_bit6=0;//鎖存數據
  754.    delay_50us(40);
  755.    dis_bit6=1;
  756. }
  757. //***************************************************************
  758. //功能:把數據1顯示在數碼管1上
  759. void display5(uchar dis_data)
  760. {  
  761.    smg_data=tabl1[dis_data];//送顯示斷碼
  762.    dis_bit5=0;//鎖存數據
  763.    delay_50us(40);
  764.    dis_bit5=1;
  765. }
  766. //***************************************************************
  767. //功能:把數據1顯示在數碼管1上
  768. void display4(uchar dis_data)
  769. {  
  770.    smg_data=tabl1[dis_data];//送顯示斷碼
  771.    dis_bit4=0;//鎖存數據
  772.    delay_50us(40);
  773.    dis_bit4=1;
  774. }//***************************************************************
  775. //功能:把數據1顯示在數碼管1上
  776. void display3(uchar dis_data)
  777. {  
  778.    smg_data=tabl1[dis_data];//送顯示斷碼
  779.    dis_bit3=0;//鎖存數據
  780.    delay_50us(40);
  781.    dis_bit3=1;
  782. }
  783. //***************************************************************
  784. //功能:把數據1顯示在數碼管1上
  785. void display1(uchar dis_data)
  786. {  
  787.    smg_data=tabl1[dis_data];//送顯示斷碼
  788.    dis_bit1=0;//鎖存數據
  789.    delay_50us(40);
  790.    dis_bit1=1;
  791. }
  792. //***************************************************************
  793. //功能:把數據1顯示在數碼管1上
  794. void display2(uchar dis_data)
  795. {  
  796.    smg_data=tabl1[dis_data];//送顯示斷碼
  797.    dis_bit2=0;//鎖存數據
  798.    delay_50us(40);
  799.    dis_bit2=1;
  800. }
  801. //**************************************************************************************************
  802. //函數名稱:void delay_50US(unsigned int t)
  803. //功能: 延時50*t(us)
  804. void delay_50us(uint t)
  805. {
  806.   unsigned char j;
  807.   for(;t>0;t--)
  808.   {
  809.     for(j=19;j>0;j--);
  810.   }
  811. }
  812. //*******************************************************************************
  813. //8微秒延時基準程序
  814. void delay_8us(uint t)
  815. {
  816. while(--t);
  817. }
  818. //*******************************************************************************
  819. //3微秒延時程序
  820. void delay_3us()
  821. {
  822.   ;
  823.   ;
  824. }
  825. //*******************************************************************************
  826. //子程序功能:向DS18B20寫一字節的數據
  827. void w_1byte_ds18b20(uchar value)
  828. {
  829.    uchar i=0;
  830.    for(i=0;i<8;i++)
  831.    {
  832.     dq_ds18b20=0;
  833.     delay_3us();
  834.     if (value & 0x01) dq_ds18b20=1; //DQ = 1
  835.     delay_50us(1); //延時50us 以上
  836.     value>>=1;
  837.      dq_ds18b20=1; //DQ = 1
  838.    }
  839.     delay_50us(1);
  840. }
  841. //讀一個字節
  842. uchar r_1byte_ds18b20(void)
  843. {
  844.   uchar i=0;
  845.   uchar value = 0;
  846.   for (i=0;i<8;i++)
  847.   {
  848.    value>>=1;
  849.    dq_ds18b20=0;// DQ_L;
  850.    delay_3us();
  851.    dq_ds18b20=1;
  852.    delay_3us();
  853.    delay_3us();
  854.    if(dq_ds18b20==1) value|=0x80;
  855.    delay_50us(1); //延時40us
  856.   }
  857.   return value;
  858. }
  859. //;**************************************************
  860. //ds18b20復位子程序
  861. void rest_ds18b20(void)
  862. {
  863. rest:delay_3us(); //稍做延時
  864.      delay_3us();
  865.      dq_ds18b20=1;
  866.      delay_3us();
  867.      dq_ds18b20=0;// DQ_L;
  868.      delay_8us(75);//480us<T<960us
  869.      dq_ds18b20=1;//拉高總線
  870.      delay_8us(8);
  871.      if(dq_ds18b20==1)
  872.      {  
  873.        return;
  874.      }  
  875.      delay_8us(11); //延時90us
  876.      if(dq_ds18b20==1)
  877.      {
  878.        return;
  879.       }  
  880.       else
  881.       {
  882.        goto rest;
  883.       }
  884. }
  885. //****************************************************
  886. //讀取溫度
  887. void readtemp_ds18b20(void)
  888. {
  889.     uchar temp32;
  890.     rest_ds18b20();
  891.     w_1byte_ds18b20(0xcc); //跳過讀序列號的操作
  892.     w_1byte_ds18b20(0x44); //啟動溫度轉換
  893.     delay_8us(2);
  894.     rest_ds18b20();
  895.     w_1byte_ds18b20(0xcc); //跳過讀序列號的操作
  896.     w_1byte_ds18b20(0xbe); //讀取溫度寄存器等(共可讀9個寄存器) 前兩個就是溫度
  897.     templ=r_1byte_ds18b20();
  898.     temph=r_1byte_ds18b20();
  899.     if((temph&0xf0))//判斷溫度的正負性
  900.     {
  901.       temp_flag=0;//溫度為負數標志
  902.       temph=-temph;
  903.       templ=-templ;
  904.       t_x=tabl3[templ & 0x0f];//計算溫度的小數
  905.       temp32=temph & 0x0f;
  906.       temp32<<=4;
  907.       templ>>=4;
  908.       temp32=temp32 | templ;
  909.       t_b=temp32/100%10;//計算溫度的百位數據
  910.       t_s=temp32/10%10;//計算溫度的十位數據
  911.       t_g=temp32%10;//計算溫度的個位數據
  912.     }
  913.     else//為正數
  914.     {
  915.       t_x=tabl3[templ & 0x0f];//計算溫度的小數
  916.       temp32=temph & 0x0f;
  917.       temp32<<=4;
  918.       templ>>=4;
  919.       temp32=temp32 | templ;
  920.       t_b=temp32/100%10;//計算溫度的百位數據
  921.       t_s=temp32/10%10;//計算溫度的十位數據
  922.       t_g=temp32%10;//計算溫度的個位數據
  923.       temp_flag=1;
  924.      }
  925. }
  926. void dis_temp()//溫度顯示函數
  927. {
  928.      if(temp_flag=1)
  929.      {
  930.         if(t_b==0)
  931.        {
  932.          dis(12,12,t_s,t_g,13,12);
  933.        }
  934.        else
  935.        {
  936.          dis(12,t_b,t_s,t_g,13,12);
  937.        }
  938.      }
  939.      else
  940.      {
  941.         dis(14,t_b,t_s,t_g,13,12);
  942.      }
  943. }
  944. //;##############################################################################
  945. //;子程序名:w_1byte_ds1302
  946. //;功能:    向DS1302寫一個字節的數據
  947. void w_1byte_ds1302(uchar t)
  948. {
  949.   uchar i;
  950.   for(i=0;i<8;i++)
  951.   {
  952.     if(t & 0x01)
  953.      {io_ds1302=1;}
  954.     else
  955.      {io_ds1302=0;}
  956.     clk_ds1302=1;
  957.     delay_3us();
  958.     delay_3us();
  959.     clk_ds1302=0;
  960.     delay_3us();
  961.     delay_3us();
  962.     t>>=1;
  963.   }  
  964. }
  965. //;########################################################################
  966. //;子程序名:r_1byte_ds1302()
  967. //;功能:    從DS1302讀一個字節的數據
  968. uchar r_1byte_ds1302()
  969. {
  970.   uchar i,temp11=0;
  971.   io_ds1302=1;//置IO為1,準備讀入數據
  972.   for(i=0;i<8;i++)
  973.   {
  974.     temp11>>=1;
  975.     if(io_ds1302) temp11 |= 0x80;
  976.     clk_ds1302=1;
  977.     delay_3us();
  978.     delay_3us();
  979.     clk_ds1302=0;
  980.     delay_3us();
  981.   }
  982.   return(temp11);
  983. }  
  984. //;#################################################################################
  985. //;子程序名:setbds1302
  986. //;功能:   設置DS1302初始時間,并啟動計時
  987. void set_ds1302()
  988. {
  989.   uchar i,j;
  990.   rest_ds1302=0;
  991.   delay_3us();
  992.   clk_ds1302=0;
  993.   delay_3us();
  994.   rest_ds1302=1;
  995.   delay_3us();
  996.   w_1byte_ds1302(0x8e);//寫控制命令字
  997.   delay_3us();
  998.   w_1byte_ds1302(0x00);//寫保護關閉
  999.   clk_ds1302=1;
  1000.   delay_3us();
  1001.   rest_ds1302=0;
  1002.   for(i=0,j=0x80;i<7;i++,j+=2)
  1003.   {
  1004.     rest_ds1302=0;
  1005.     delay_3us();
  1006.     clk_ds1302=0;
  1007.     delay_3us();
  1008.     rest_ds1302=1;
  1009.     delay_3us();
  1010.     w_1byte_ds1302(j);
  1011.     delay_3us();
  1012.     w_1byte_ds1302(tab23[i]);
  1013.     delay_3us();
  1014.     delay_3us();
  1015.     clk_ds1302=1;
  1016.     delay_3us();
  1017.     rest_ds1302=0;
  1018.     delay_3us();
  1019.     delay_3us();
  1020.   }
  1021.   rest_ds1302=0;
  1022.   delay_3us();
  1023.   clk_ds1302=0;
  1024.   delay_3us();
  1025.   rest_ds1302=1;
  1026.   delay_3us();
  1027.   w_1byte_ds1302(0x8e);
  1028.   delay_3us();
  1029.   w_1byte_ds1302(0x80);
  1030.   clk_ds1302=1;
  1031.   delay_3us();
  1032.   rest_ds1302=0;
  1033.   delay_3us();
  1034. }  
  1035. //;#################################################################-------
  1036. //;子程序名:get1302
  1037. void get_ds1302()
  1038. {
  1039.    uchar temp11[7],i,j;
  1040.    for(i=0;i<7;i++)
  1041.    {temp11[i]=0;}
  1042.    for(i=0,j=0x81;i<7;i++,j+=2)
  1043.    {  
  1044.     rest_ds1302=0;
  1045.     delay_3us();
  1046.     clk_ds1302=0;
  1047.     delay_3us();
  1048.     rest_ds1302=1;
  1049.     delay_3us();
  1050.     w_1byte_ds1302(j);
  1051.     temp11[i]=r_1byte_ds1302();
  1052.     delay_3us();
  1053.     clk_ds1302=1;
  1054.     delay_3us();
  1055.     rest_ds1302=0;
  1056.     delay_3us();
  1057.   }
  1058.   if(temp11[0]!=0xff)
  1059.   {second_count=temp11[0];}
  1060.   if(temp11[1]!=0xff)// 數據驗證
  1061.   {minute_count=temp11[1];}
  1062.   if(temp11[2]!=0xff)//數據驗證
  1063.   {hour_count=temp11[2];}
  1064. // date=temp[3];
  1065.   //month=temp[4];
  1066. // week=temp[5];
  1067.   //year=temp[6];
  1068. }
復制代碼

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

使用道具 舉報

您需要登錄后才可以回帖 登錄 | 立即注冊

本版積分規則

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

Powered by 單片機教程網

快速回復 返回頂部 返回列表
主站蜘蛛池模板: 亚洲一区 | 在线观看免费av网 | 2019天天操 | 中文字幕国产视频 | 中文字幕成人 | 亚洲欧美成人 | 亚洲一区二区三区在线播放 | 国产精品a久久久久 | 亚洲狠狠爱 | 欧美成人a∨高清免费观看 色999日韩 | 影音先锋欧美资源 | 午夜影院在线播放 | 81精品国产乱码久久久久久 | 欧美日韩三级在线观看 | 久久久久久久香蕉 | 精品福利在线 | 中文在线观看视频 | 久久一级 | 久久久久久久综合 | 一级网站 | 91在线看片| 天天草夜夜骑 | 成人做爰www免费看 午夜精品久久久久久久久久久久 | 午夜寂寞影院列表 | 自拍视频网| 欧美6一10sex性hd| 久久激情网| 色婷婷九月 | 精品欧美一区免费观看α√ | 国产亚洲成av人片在线观看桃 | 日韩成人免费中文字幕 | 噜噜噜噜狠狠狠7777视频 | 国产精品欧美一区二区三区不卡 | 仙人掌旅馆在线观看 | 亚洲人成网亚洲欧洲无码 | 亚洲九色 | 免费中文字幕 | 日韩欧美一级片 | 影视一区 | 成人在线中文字幕 | 一区二区三区不卡视频 |