找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 1739|回复: 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 单片机教程网

快速回复 返回顶部 返回列表