找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 2562|回复: 0
打印 上一主题 下一主题
收起左侧

单片机蓝桥杯第八届省赛程序参考 电子钟程序设计与调试

[复制链接]
跳转到指定楼层
楼主


单片机源程序如下:
  1. #include<STC15F2K60S2.H>
  2. #include<intrins.h>
  3. #include "ds18b20.h"
  4. #include "ds1302.h"
  5. //#include "abc.h"
  6. #include "2.h"
  7. uchar code duanxuan[]={0XC0,0XF9,0XA4,0XB0,0X99,0X92,0X82,0XF8,0X80,0X90,0XBF,0XFF,0XC6};//0,1,2,3,4,5,6,7,8,9,-,空,c
  8. uchar code duanxuan1[]={0x40, 0x79, 0x24, 0x30, 0x19,0x12, 0x02, 0x78, 0x00, 0x10, 0xBF};//0~9带点1
  9. uchar code weixuan[]={0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80};
  10. uchar baojing=0,moshi=0,hour,min,sec,hour_i,hour_n=0,min_i,min_n=0,sec_i,sec_n=0,flase=0xff;
  11. uchar s1=0,s2=0,s3=0,s4=0,com=1;
  12. uint count0,count1,count3=0,wendu;
  13. bit flag,wendub=0;
  14. uchar time[7]={20,1,1,1,23,59,50};
  15. uchar du;  //数码显示扫描变量
  16. extern uchar shijian13[2];
  17. extern uchar ds[];
  18. void keyscan4();
  19. void all_chushihua();
  20. void xianshi0();
  21. void xianshi1();
  22. void xianshi2();
  23. void xianshi3();
  24. void xianshi4();
  25. void xianshi5();
  26. void xianshi6();
  27. void xian();
  28. void display();
  29. void writetime1();
  30. void readtime1();
  31. void Delay5ms();
  32. void main()
  33. {
  34.         all_chushihua();
  35.   writetime1();
  36.         TMOD &= 0xF0;                //设置定时器模式        //2ms T0定时器
  37.         TMOD |= 0x01;                //设置定时器模式
  38.         TL0 = 0xCD;                //设置定时初值
  39.         TH0 = 0xF8;                //设置定时初值
  40.         TF0 = 0;                //清除TF0标志
  41.         EA=1;
  42.         ET0=1;
  43.         TR0 = 1;                //定时器0开始计时
  44.         while(1)
  45.         {
  46.                 wendu=temget();
  47.                 readtime1();
  48.                 hour=time[4];min=time[5];sec=time[6];
  49.                 keyscan4();
  50.                 if((hour==hour_n)&&(min==min_n)&&(sec==sec_n))   
  51.                 {
  52.                         s1=0;
  53.                         s2=0;
  54.                         s3=0;
  55.                         s4=0;
  56.                         baojing=1;
  57.                         count0=0;
  58.                         count1=0;
  59.                 //        count2=0;
  60.                 }
  61.                
  62.         }
  63. }
  64. //***************资料包*********************//
  65. //void writetime1()
  66. //{
  67. //        uchar i,a,d=0x80;    //
  68. //        Ds1302_Single_Byte_Write(0x8e,0);  //
  69. //        for (i=0;i<7;i++)
  70. //        {
  71. //                a=time[6-i]/10;   //高四位
  72. //                a<<=4;

  73. //                a|=time[6-i]%10;    //低四位
  74. //                Ds1302_Single_Byte_Write(d,a);   //
  75. //                d+=2;     //
  76. //        }
  77. //        Ds1302_Single_Byte_Write(0x8e,0x80);   //
  78. //}
  79. //void readtime1()   //时间读取
  80. //{
  81. //        uchar i,a,d=0x81;
  82. //        Ds1302_Single_Byte_Write(0x8e,0);
  83. //        for (i=0;i<7;i++)
  84. //        {
  85. //                EA=0;/////!!!!!!!!!!!读数据加中断禁止,否则会出现偶然错误
  86. //                a=Ds1302_Single_Byte_Read(d);
  87. //                EA=1;
  88. //                SDA_R=0;        ///////!!!!每次读取一个字节要把IO口拉低或者延时
  89. //               
  90. //                time[6-i]=a/16*10+a%16;

  91. //                d+=2;
  92. //        }
  93. //        Ds1302_Single_Byte_Write(0x8e,0x80);
  94. //}
  95. //****************王玉帆资料驱动*****************//
  96. void writetime1()
  97. {
  98.         uchar i,a,d=0x80;    //
  99.         Write_Ds1302_Byte(0x8e,0);  //
  100.         for (i=0;i<7;i++)
  101.         {
  102.                 a=time[6-i]/10;   //高四位
  103.                 a<<=4;

  104.                 a|=time[6-i]%10;    //低四位
  105.                 Write_Ds1302_Byte(d,a);   //
  106.                 d+=2;     //
  107.         }
  108.         Write_Ds1302_Byte(0x8e,0x80);   //
  109. }
  110. void readtime1()   //时间读取
  111. {
  112.         uchar i,a,d=0x81;
  113.         Write_Ds1302_Byte(0x8e,0);
  114.         for (i=0;i<7;i++)
  115.         {
  116.                 EA=0;/////!!!!!!!!!!!读数据加中断禁止,否则会出现偶然错误
  117.                 a=Read_Ds1302_Byte(d);
  118.                 EA=1;
  119.                 SDA=0;        ///////!!!!每次读取一个字节要把IO口拉低或者延时
  120.                
  121.                 time[6-i]=a/16*10+a%16;

  122.                 d+=2;
  123.         }
  124.         Write_Ds1302_Byte(0x8e,0x80);
  125. }
  126. void all_chushihua()
  127. {
  128.         P0=0XFF;  //关闭LED灯
  129.         P2=(P2&0X1F)|0X80;  //Y4C=1;
  130.   P2=(P2&0X1F);
  131.         P0=0X00;   //关闭蜂鸣器,继电器
  132.   P2=(P2&0X1F)|0XA0;  //Y5C=1;
  133.   P2=(P2&0X1F);
  134.         P0=0XFF;  //选择所有数码管
  135.         P2=(P2&0X1F)|0XC0;  //Y6C=1;
  136.         P2=(P2&0X1F);
  137.         P0=0XFF;  //关闭所有数码管
  138.         P2=(P2&0X1F)|0XE0;  //Y7C=1;
  139.         P2=(P2&0X1F);
  140. }
  141. void keyscan4()
  142. {
  143.         if(P30==0)
  144.         {
  145.                 Delay5ms();
  146.                 if(P30==0)
  147.                 {
  148.                         s1=1;
  149.                         if(moshi==0)
  150.                         {
  151.                                 hour_i=hour;
  152.                           min_i=min;
  153.                           sec_i=sec;
  154.                         }
  155.                         if(moshi<4)
  156.                         moshi++;
  157.                         if(moshi>=4)
  158.                         {
  159.                                 time[6]=sec_i;
  160.                                 time[5]=min_i;
  161.                           time[4]=hour_i;
  162.                                 writetime1();
  163.                                 moshi=0;
  164.                         }
  165.                 }
  166.                 while(!P30);
  167.         }
  168.         if(P31==0)
  169.         {
  170.                 Delay5ms();
  171.                 if(P31==0)
  172.                 {
  173.                         s2=1;
  174.                  
  175.                   if(moshi>=4) moshi++;
  176.                   if(moshi==0) moshi=4;
  177.                   if(moshi>=7) moshi=0;
  178.                 }
  179.                  while(!P31);
  180.         }
  181.         if(P32==0)
  182.         {
  183.                 Delay5ms();
  184.                 if(P32==0)
  185.                 {
  186.                         s3=1;
  187.                         switch(moshi)
  188.                         {
  189.                                 case 1: hour_i++;if(hour_i>23) hour_i=0;break;
  190.                                 case 2: min_i++;if(min_i>59) min_i=0;break;
  191.                                 case 3: sec_i++;if(sec_i>=59) sec_i=0;break;
  192.                                 case 4: hour_n++;if(hour_n>=23) hour_n=0;break;
  193.                                 case 5: min_n++;if(min_n>=59) min_n=0;break;
  194.                                 case 6: sec_n++;if(sec_n>=59) sec_n=0;break;
  195.                         }
  196.                 }
  197.                 while(!P32);
  198.         }
  199.         if(P33==1)
  200.                 wendub=0;
  201.                 if(P33==0)
  202.                 {
  203.                         Delay5ms();
  204.                         if(P33==0)
  205.                         {
  206.                                 s4=1;
  207.                                 if(moshi==0)
  208.                                 {
  209.                                         wendu=temget();
  210.                                         wendub=1;
  211.                                 }
  212.                                 else
  213.                                 {        
  214.                                 
  215.                                 switch(moshi)
  216.                                 {
  217.                                         case 1: if(hour_i!=0) hour_i--;else hour_i=23;break;
  218.                                         case 2: if(min_i!=0) min_i--; else min_i=59;break;
  219.                                         case 3: if(sec_i!=0) sec_i--; else sec_i=59;break;
  220.                                         case 4: if(hour_n!=0) hour_n--;else  hour_n=23;break;
  221.                                         case 5: if(min_n!=0) min_n--; else min_n=59;break;
  222.                                         case 6: if(sec_n!=0) sec_n--; else sec_n=59;break;
  223.                                 }        
  224.                         }                                
  225.                         }
  226.                 }        
  227.                 while(!P33);
  228. }
  229. void xianshi0()
  230. {
  231.         if(!wendub)
  232.         {
  233.                 switch(du)
  234.                 {
  235.                         case 0:P0=duanxuan[hour/10];break;
  236.                         case 1:P0=duanxuan[hour%10];break;
  237.                         case 2:P0=duanxuan[10];break;
  238.                         case 3:P0=duanxuan[min/10];break;
  239.                         case 4:P0=duanxuan[min%10];break;
  240.                         case 5:P0=duanxuan[10];break;
  241.                         case 6:P0=duanxuan[sec/10];break;
  242.                         case 7:P0=duanxuan[sec%10];break;
  243.                 }
  244.   }
  245.         else
  246.         {
  247.                 switch(du)
  248.         {
  249.                 case 0:P0=duanxuan[11];break;
  250.                 case 1:P0=duanxuan[11];break;
  251.                 case 2:P0=duanxuan[11];break;
  252.                 case 3:P0=duanxuan[11];break;
  253.                 case 4:P0=duanxuan[wendu/1000%10];break;
  254.                 case 5:P0=duanxuan1[wendu/100%10];break;
  255.                 case 6:P0=duanxuan[wendu/10%10];break;
  256.                 case 7:P0=duanxuan[wendu%10];break;
  257.         }
  258.         }
  259. }
  260. void xianshi1()
  261. {
  262.         switch(du)
  263.         {
  264.                 case 0:P0=duanxuan[hour_i/10]|flase;break;
  265.                 case 1:P0=duanxuan[hour_i%10]|flase;break;
  266.                 case 2:P0=duanxuan[10];break;
  267.                 case 3:P0=duanxuan[min_i/10];break;
  268.                 case 4:P0=duanxuan[min_i%10];break;
  269.                 case 5:P0=duanxuan[10];break;
  270.                 case 6:P0=duanxuan[sec_i/10];break;
  271.                 case 7:P0=duanxuan[sec_i%10];break;
  272.         }

  273. }
  274. void xianshi2()
  275. {
  276.         switch(du)
  277.         {
  278.                 case 0:P0=duanxuan[hour_i/10];break;
  279.                 case 1:P0=duanxuan[hour_i%10];break;
  280.                 case 2:P0=duanxuan[10];break;
  281.                 case 3:P0=duanxuan[min_i/10]|flase;break;
  282.                 case 4:P0=duanxuan[min_i%10]|flase;break;
  283.                 case 5:P0=duanxuan[10];break;
  284.                 case 6:P0=duanxuan[sec_i/10];break;
  285.                 case 7:P0=duanxuan[sec_i%10];break;
  286.         }
  287. }
  288. void xianshi3()
  289. {
  290.         switch(du)
  291.         {
  292.                 case 0:P0=duanxuan[hour_i/10];break;
  293.                 case 1:P0=duanxuan[hour_i%10];break;
  294.                 case 2:P0=duanxuan[10];break;
  295.                 case 3:P0=duanxuan[min_i/10];break;
  296.                 case 4:P0=duanxuan[min_i%10];break;
  297.                 case 5:P0=duanxuan[10];break;
  298.                 case 6:P0=duanxuan[sec_i/10]|flase;break;
  299.                 case 7:P0=duanxuan[sec_i%10]|flase;break;
  300.         }
  301. }
  302. void xianshi4()
  303. {
  304.         switch(du)
  305.         {
  306.                 case 0:P0=duanxuan[hour_n/10]|flase;break;
  307.                 case 1:P0=duanxuan[hour_n%10]|flase;break;
  308.                 case 2:P0=duanxuan[10];break;
  309.                 case 3:P0=duanxuan[min_n/10];break;
  310.                 case 4:P0=duanxuan[min_n%10];break;
  311.                 case 5:P0=duanxuan[10];break;
  312.                 case 6:P0=duanxuan[sec_n/10];break;
  313.                 case 7:P0=duanxuan[sec_n%10];break;
  314.         }
  315. }
  316. void xianshi5()
  317. {
  318.         switch(du)
  319.         {
  320.                 case 0:P0=duanxuan[hour_n/10];break;
  321.                 case 1:P0=duanxuan[hour_n%10];break;
  322.                 case 2:P0=duanxuan[10];break;
  323.                 case 3:P0=duanxuan[min_n/10]|flase;break;
  324.                 case 4:P0=duanxuan[min_n%10]|flase;break;
  325.                 case 5:P0=duanxuan[10];break;
  326.                 case 6:P0=duanxuan[sec_n/10];break;
  327.                 case 7:P0=duanxuan[sec_n%10];break;
  328.         }
  329. }
  330. void xianshi6()
  331. {
  332.         switch(du)
  333.         {
  334.                 case 0:P0=duanxuan[hour_n/10];break;
  335.                 case 1:P0=duanxuan[hour_n%10];break;
  336.                 case 2:P0=duanxuan[10];break;
  337.                 case 3:P0=duanxuan[min_n/10];break;
  338.                 case 4:P0=duanxuan[min_n%10];break;
  339.                 case 5:P0=duanxuan[10];break;
  340.                 case 6:P0=duanxuan[sec_n/10]|flase;break;
  341.                 case 7:P0=duanxuan[sec_n%10]|flase;break;
  342.         }
  343. }

  344. void xian()
  345. {
  346.         switch(moshi)
  347.         {
  348.     case 0:xianshi0();break;
  349.                 case 1:xianshi1();break;
  350.                 case 2:xianshi2();break;
  351.                 case 3:xianshi3();break;
  352.                 case 4:xianshi4();break;
  353.                 case 5:xianshi5();break;
  354.                 case 6:xianshi6();break;
  355.         }
  356. }        
  357. void display()
  358. {
  359.         
  360.   P0=weixuan[du];
  361.         P2=(P2&0X1F)|0xc0;
  362.         P2=(P2&0X1F);
  363.         xian();
  364.         P2=(P2&0X1F)|0XE0;
  365.         P2=(P2&0X1F);
  366.         if(++du>7) du=0;
  367. }

  368. void led(uchar liang)
  369. {
  370.         P0=liang;
  371.         P2=(P2&0x1f)|0x80;
  372.         P2=(P2&0x1f);
  373. }
  374. void T1dingshiqi() interrupt 1
  375. {
  376.         
  377.         display();                                                                //用于控制整体时钟变化
  378.         count1++;                                                                //用于控制LED灯闪烁
  379.         count0++;
  380.         count3++;
  381.         if(baojing==1)                                        //让灯闪烁时间间隔位0.2秒
  382.                 {

  383.                         if(count1>=100)
  384.                                 {
  385.                                                 count1=0;
  386.                                         if(flag==0)
  387.                                                 {
  388.                                                         flag=1;
  389.                                                         led(0xfe);
  390.                                                 }
  391.                                         else
  392.                                                 {
  393.                                                         flag=0;
  394.                                                         led(0xff);
  395.                                                 }
  396.                                 }
  397.                                 if((s1==1)||(s2==1)||(s3==1)||(s4==1))
  398.                                 {
  399.                                         s1=0;
  400.                                         s2=0;
  401.                                         s3=0;
  402.                                         s4=0;
  403.                                         led(0xff);
  404.                                         baojing=0;
  405.                                 }
  406.                         //count0++;
  407.                                 if(count0>=2500)
  408.                                         {
  409.                                                 count0=0;
  410.                                                 led(0xff);
  411.                                                 baojing=0;
  412. //                                                 count2++;                //限定灯就闪烁5秒
  413. //                                                if(count2>=5)
  414. //                                                        {
  415. //                                                                led(0xff);
  416. //                                                                baojing=0;
  417. //                                                        }                                                         //在设定时间的时候让设置的数字以间隔为一秒闪烁
  418.                                         }        
  419.                 }
  420.         else
  421.                 {
  422.                         led(0xff);
  423.                 }        
  424.   if(count3>=500)
  425.         {
  426.                 count3=0;
  427.                 flase=~flase;
  428.         }
  429.         TL0 = 0xCD;                //设置定时初值
  430.         TH0 = 0xF8;                //设置定时初值

  431. }
  432. void Delay5ms()                //@11.0592MHz
  433. {
  434.         unsigned char i, j;

  435.         i = 54;
  436.         j = 199;
  437.         do
  438.         {
  439.                 while (--j);
  440.         } while (--i);
  441. }
复制代码

所有资料51hei提供下载:
蓝桥杯大赛第8届.rar (209.71 KB, 下载次数: 24)

评分

参与人数 1黑币 +50 收起 理由
admin + 50 共享资料的黑币奖励!

查看全部评分

分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏1 分享淘帖 顶1 踩
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

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