找回密码
 立即注册

QQ登录

只需一步,快速开始

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

将单片机闹铃功能删了(删的时候注意整个程序循环),改成整点报时功能 求思路

[复制链接]
跳转到指定楼层
楼主
ID:530516 发表于 2019-5-8 18:18 | 只看该作者 回帖奖励 |正序浏览 |阅读模式
  1. #include<reg51.h>              
  2. #include<intrins.h>
  3. //****************************宏定义********************************
  4. typedef unsigned char uchar;   
  5. typedef unsigned int  uint;
  6. //****************************位声明********************************
  7. sbit key1=P1^0;               
  8. sbit key2=P1^1;
  9. sbit key3=P1^2;
  10. sbit key4=P1^3;
  11. sbit beep=P3^0;        
  12. //************************数码管显示的数值**************************
  13. uchar code table[]=        {
  14.                                                 0x3f,0x06,0x5b,0x4f,0x66,0x6d,
  15.                                                 0x7d,0x07,0x7f,0x6f,0x40,0x00
  16.                                         };                       
  17. //****************************函数声明******************************
  18. void jia();   
  19. void jian();
  20. //********************数组定义,数组内含有8个数值******************
  21. uchar table1[8],table2[8],table3[8],table4[8];
  22. //**************************时间显示初始值**************************
  23. uchar shi=12,fen=0,miao=0;  
  24. //**************************定义全局变量****************************
  25. uchar shi1,fen1,miao1,shi2,fen2,miao2,shi3,fen3,miao3;
  26. uchar shi4,fen4,miao4;
  27. uchar flag, flag1, wss, cnt, cnt1, alm1, alm2, alm3;
  28. //    1秒   等时  位闪 次数 校时   闹1  闹2   闹3
  29. uint flag2;
  30. //   蜂鸣
  31. //*********************延时函数,用于动态扫描数码管*****************
  32. void delay(uchar i)                                    
  33. {  
  34.         uchar x,y;
  35.            for(x=i;x>0;x--)
  36.              for(y=120;y>0;y--);
  37. }
  38. //*******************************初始化函数*************************
  39. void init()   
  40. {  
  41.         TMOD=0x01;         //工作方式1                                                                                
  42.            TH0=0x3c;          //定时时间为:50ms        (65536-50000)/256
  43.            TL0=0x0b0;         //(65536-50000)%256
  44.         EA=1;              //开总中断
  45.            ET0=1;             //打开定时器
  46.            TR0=1;             //启动定时器
  47.         beep=0;
  48. }
  49. //********************显示子函数,用于显示时间数值*****************
  50. void display()  
  51. {  
  52.         uchar i,j;
  53.            if(cnt!=10||wss==0)
  54.     {
  55.                 table1[0]=miao%10;        //分离秒的个位与十位
  56.                table1[1]=miao/10;
  57.         }
  58.         else
  59.                  table1[0]=table1[1]=11;
  60.         if(cnt!=11||wss==0)
  61.         {
  62.                 table1[3]=fen%10;         //分离分的个位与十位
  63.                table1[4]=fen/10;
  64.         }
  65.         else
  66.                   table1[3]=table1[4]=11;
  67.         if(cnt!=12||wss==0)
  68.         {
  69.                 table1[6]=shi%10;         //分离时的个位与十位
  70.                table1[7]=shi/10;
  71.         }
  72.         else
  73.                 table1[6]=table1[7]=11;
  74.     table1[2]=table1[5]=10;
  75.     j=0xfb;
  76.     for(i=0;i<=7;i++)          //从秒到时的扫描
  77.     {  
  78.                 P2=j;
  79.         P0=table[table1[i]];  //显示数值
  80.         delay(10);
  81.         j=_cror_(j,1);        //循环右移
  82.     }
  83. }
  84. //*******************显示子函数,用于显示定时1时间*****************
  85. void display1()
  86. {  
  87.         uchar i,j;
  88.            if(alm1==0)
  89.     {
  90.                 if(cnt!=1||wss==0)
  91.             {
  92.                         table2[0]=miao1%10;        //以下含义同上
  93.                    table2[1]=miao1/10;
  94.                 }
  95.                    else
  96.                       table2[0]=table2[1]=11;
  97.                    if(cnt!=2||wss==0)
  98.             {
  99.                         table2[3]=fen1%10;
  100.                    table2[4]=fen1/10;
  101.                 }
  102.                    else
  103.                      table2[3]=table2[4]=11;
  104.                    if(cnt!=3||wss==0)
  105.             {
  106.                         table2[6]=shi1%10;
  107.                    table2[7]=shi1/10;
  108.                 }
  109.                    else
  110.                      table2[6]=table2[7]=11;
  111.         }
  112.         else
  113.                    table2[0]=table2[1]=table2[3]=table2[4]=table2[6]=table2[7]=10;
  114.            table2[2]=table2[5]=10;
  115.            j=0xfb;
  116.            for(i=0;i<=7;i++)
  117.            {  
  118.                 P2=j;
  119.         P0=table[table2[i]];
  120.                 delay(10);
  121.         j=_cror_(j,1);
  122.            }
  123. }
  124. //********************显示子函数,用于显示定时2时间******************
  125. void display2()
  126. {  
  127.         uchar i,j;
  128.            if(alm2==0)
  129.     {
  130.                 if(cnt!=4||wss==0)
  131.             {
  132.                         table3[0]=miao2%10;        //以下含义同上
  133.                    table3[1]=miao2/10;
  134.                    }
  135.                    else
  136.                       table3[0]=table3[1]=11;
  137.                    if(cnt!=5||wss==0)
  138.             {
  139.                         table3[3]=fen2%10;
  140.                    table3[4]=fen2/10;
  141.                 }
  142.                 else
  143.                           table3[3]=table3[4]=11;
  144.                 if(cnt!=6||wss==0)
  145.                 {
  146.                         table3[6]=shi2%10;
  147.                    table3[7]=shi2/10;
  148.                 }
  149.                 else
  150.                          table3[6]=table3[7]=11;
  151.         }
  152.         else
  153.            table3[0]=table3[1]=table3[3]=table3[4]=table3[6]=table3[7]=10;
  154.            table3[2]=table3[5]=10;
  155.            j=0xfb;
  156.            for(i=0;i<=7;i++)
  157.     {         
  158.                 P2=j;
  159.         P0=table[table3[i]];
  160.                 delay(10);
  161.         j=_cror_(j,1);
  162.     }
  163. }
  164. //***************显示子函数,用于显示定时3时间数值****************//
  165. void display3()  
  166. {
  167.          uchar i,j;
  168.            if(alm3==0)
  169.     {  
  170.                 if(cnt!=7||wss==0)
  171.             {
  172.                         table4[0]=miao3%10;        //分离秒的个位与十位
  173.             table4[1]=miao3/10;
  174.         }
  175.                 else
  176.                            table4[0]=table4[1]=11;
  177.                 if(cnt!=8||wss==0)
  178.                 {
  179.                         table4[3]=fen3%10;         //分离分的个位与十位
  180.             table4[4]=fen3/10;
  181.                 }
  182.                 else
  183.                           table4[3]=table4[4]=11;
  184.                 if(cnt!=9||wss==0)
  185.                 {
  186.                         table4[6]=shi3%10;         //分离时的个位与十位
  187.             table4[7]=shi3/10;
  188.                 }
  189.                 else
  190.                           table4[6]=table4[7]=11;
  191.     }
  192.     else
  193.             table4[0]=table4[1]=table4[3]=table4[4]=table4[6]=table4[7]=10;
  194.            table4[2]=table4[5]=10;
  195.            j=0xfb;                    //从秒到时的扫描
  196.            for(i=0;i<=7;i++)
  197.            {  
  198.                 P2=j;
  199.         P0=table[table4[i]];  //显示数值
  200.                 delay(10);
  201.         j=_cror_(j,1);        //循环右移
  202.            }
  203. }
  204. //***********************时间子函数 *****************************//
  205. void shijian()         
  206. {  
  207.         if(flag>=20)               //判断是否到一秒
  208.     {
  209.                 wss=~wss;
  210.                    flag=0;                                 //到了,则标志位清零
  211.                    if(cnt1!=0)
  212.             {
  213.                         miao4++;          //秒加1
  214.                    if(miao4>59)    //判断秒是否到60s
  215.             {
  216.                                 miao4=0;      //到了,则清零
  217.                 fen4++;       //分加1
  218.                 if(fen4>59)  //以下含义同上
  219.                 {
  220.                                         fen4=0;
  221.                     shi4++;
  222.                     if(shi4>23)
  223.                       shi4=0;                                                                                                                  
  224.                  }        
  225.              }
  226.         }
  227.                    else
  228.                {
  229.                         miao++;          //秒加1
  230.                  if(miao>59)    //判断秒是否到60s
  231.                    {
  232.                                 miao=0;      //到了,则清零
  233.                      fen++;       //分加1
  234.                      if(fen>59)  //以下含义同上
  235.                        {
  236.                                         fen=0;
  237.                          shi++;
  238.                          if(shi>23)
  239.                                    shi=0;                                                                                                                  
  240.                 }        
  241.             }
  242.         }
  243.     }
  244. }
  245. //**************************键盘扫描子函数************************//
  246. void key_scan()
  247. {  
  248.         if(key1==0)
  249.     {  
  250.                 while(!key1)          //防止掉显
  251.         {  
  252.                         if(cnt==1||cnt==2||cnt==3)
  253.                 display1();
  254.                         if(cnt==4||cnt==5||cnt==6)
  255.                 display2();
  256.                         if(cnt==7||cnt==8||cnt==9)
  257.                 display3();
  258.                         if(cnt==0||cnt==10||cnt==11||cnt==12||cnt==13)
  259.                     display();
  260.         }
  261.         cnt++;     //记下按键key1按下的次数                  
  262.                 if(cnt==10&&cnt1==0)
  263.                 {
  264.                         miao4=miao;
  265.                     fen4=fen;
  266.                         shi4=shi;
  267.                         cnt1++;
  268.                 }
  269.                 if(cnt==13)
  270.                 {
  271.                         cnt=0;
  272.                     if(cnt1==1)
  273.                         {
  274.                                 miao=miao4;
  275.                                 fen=fen4;
  276.                                 shi=shi4;
  277.                         }
  278.                     cnt1=0;
  279.                 }         
  280.         }
  281.            if(key2==0)              //判断key2是否按下
  282.            {  
  283.                 while(!key2)          //防止掉显
  284.         {  
  285.                         if(cnt==1||cnt==2||cnt==3)
  286.                 display1();
  287.                         if(cnt==4||cnt==5||cnt==6)
  288.                 display2();
  289.                         if(cnt==7||cnt==8||cnt==9)
  290.                 display3();
  291.                         if(cnt==0||cnt==10||cnt==11||cnt==12||cnt==13)
  292.                 display();
  293.         }
  294.         jia();
  295.            }
  296.            if(key3==0)              //判断key3是否按下
  297.            {  
  298.                 while(!key3)          //防止掉显
  299.         {  
  300.                         if(cnt==1||cnt==2||cnt==3)
  301.                 display1();
  302.                         if(cnt==4||cnt==5||cnt==6)
  303.                 display2();
  304.                         if(cnt==7||cnt==8||cnt==9)
  305.                 display3();  
  306.                         if(cnt==0||cnt==10||cnt==11||cnt==12||cnt==13)
  307.                 display();
  308.         }
  309.         jian();                    //调用减1子函数           
  310.            }
  311.         if(key4==0)                   //判断key4是否按下
  312.         {
  313.                 while(!key4)            //防止掉
  314.             {
  315.                         if(cnt==1||cnt==2||cnt==3)
  316.                     {  
  317.                                 alm1=~alm1;
  318.                             display1();
  319.                         }
  320.                     if(cnt==4||cnt==5||cnt==6)
  321.                     {  
  322.                                 alm2=~alm2;
  323.                             display2();
  324.                         }
  325.                     if(cnt==7||cnt==8||cnt==9)
  326.                     {  
  327.                                 alm3=~alm3;
  328.                             display3();
  329.                         }
  330.                         if(cnt==0||cnt==10||cnt==11||cnt==12||cnt==13)
  331.                        display();
  332.                  }
  333.         }      
  334. }
  335. //****************************加1子函数***************************
  336. void jia()            
  337. {
  338.         if(cnt==1)                         //判断key1按下的次数是否为1
  339.     {  
  340.                 miao1++;                 //是,则秒加1
  341.                if(miao1>59)         //判断秒是否大于60,是,则秒清零
  342.            miao1=0;
  343.     }
  344.          if(cnt==2)                         //以下含意同上
  345.     {  
  346.                 fen1++;
  347.         if(fen1>59)
  348.            fen1=0;
  349.     }
  350.           if(cnt==3)
  351.     {  
  352.                 shi1++;
  353.         if(shi1>23)
  354.            shi1=0;
  355.     }
  356.           if(cnt==4)
  357.     {  
  358.                 miao2++;
  359.         if(miao2>59)
  360.            miao2=0;
  361.     }
  362.           if(cnt==5)
  363.     {  
  364.                 fen2++;
  365.         if(fen2>59)
  366.            fen2=0;
  367.     }
  368.           if(cnt==6)
  369.     {  
  370.                 shi2++;
  371.         if(shi2>23)
  372.            shi2=0;
  373.     }
  374.           if(cnt==7)
  375.     {  
  376.                 miao3++;
  377.         if(miao3>59)
  378.            miao3=0;
  379.     }
  380.           if(cnt==8)
  381.     {  
  382.                 fen3++;
  383.         if(fen3>59)
  384.            fen3=0;
  385.     }
  386.           if(cnt==9)
  387.     {  
  388.                 shi3++;
  389.         if(shi3>23)
  390.            shi3=0;
  391.     }
  392.           if(cnt==10)         
  393.     {  
  394.                 miao++;         
  395.                if(miao>59)      
  396.           miao=0;
  397.                 cnt1++;
  398.     }
  399.           if(cnt==11)         
  400.     {  
  401.                 fen++;
  402.         if(fen>59)
  403.            fen=0;
  404.                 cnt1++;
  405.     }
  406.           if(cnt==12)
  407.     {  
  408.                 shi++;
  409.         if(shi>23)
  410.            shi=0;
  411.                 cnt1++;
  412.     }
  413. }
  414. //***************************减1子函数**************************//
  415. void jian()   
  416. {  
  417.         if(cnt==1)                        //判断key1按下的次数是否为1,是则秒减1
  418.     {  
  419.                 miao1--;
  420.         if(miao1==255)        //判断秒是否减到255,是,则秒置59
  421.            miao1=59;
  422.     }
  423.            if(cnt==2)                        //以下含意同上
  424.     {  
  425.                 fen1--;
  426.         if(fen1==255)
  427.            fen1=59;
  428.     }
  429.            if(cnt==3)
  430.     {  
  431.                 shi1--;
  432.                if(shi1==255)
  433.            shi1=23;
  434.     }
  435.            if(cnt==4)
  436.     {  
  437.                 miao2--;
  438.                if(miao2==255)
  439.            miao2=59;
  440.     }
  441.            if(cnt==5)
  442.     {  
  443.                 fen2--;
  444.         if(fen2==255)
  445.            fen2=59;
  446.     }
  447.            if(cnt==6)
  448.     {  
  449.                 shi2--;
  450.         if(shi2==255)
  451.            shi2=23;
  452.     }
  453.         if(cnt==7)
  454.     {  
  455.                 miao3--;
  456.         if(miao3==255)
  457.             miao3=59;
  458.     }
  459.     if(cnt==8)
  460.     {  
  461.                 fen3--;
  462.         if(fen3==255)
  463.             fen3=59;
  464.     }
  465.     if(cnt==9)
  466.     {  
  467.                 shi3--;
  468.         if(shi3==255)
  469.             shi3=23;
  470.     }
  471.     if(cnt==10)         
  472.     {  
  473.                 miao--;
  474.         if(miao==255)  
  475.             miao=59;
  476.                 cnt1++;
  477.     }
  478.     if(cnt==11)
  479.     {  
  480.                 fen--;
  481.         if(fen==255)
  482.             fen=59;
  483.                 cnt1++;
  484.     }
  485.     if(cnt==12)
  486.     {  
  487.                 shi--;
  488.         if(shi==255)
  489.             shi=23;
  490.                 cnt1++;
  491.     }
  492. }
  493. //***************************闹铃子函数***************************//
  494. void clock()           
  495. {  
  496.         //判断秒的数值是否相等
  497.         if(miao==miao1&&alm1==0||miao==miao2&&alm2==0||miao==miao3&&alm3==0)
  498.         //是,在判断分是否相等                                   
  499.             if(fen==fen1&&alm1==0||fen==fen2&&alm2==0||fen==fen3&&alm3==0)
  500.         //是,再判断时是否相等            
  501.                     if(shi==shi1&&alm1==0||shi==shi2&&alm2==0||shi==shi3&&alm3==0)
  502.                     {
  503.                                 flag2=0;                       //是,则标志位,flag2清零
  504.                         while(!(flag2==1200)&&(cnt==0)) //判断flag2是否到1200且不                          
  505.                         {
  506.                                         //             为调时状态
  507.                                         if(key4==0)                                 //没有,则继续驱动蜂鸣器响
  508.                                         {
  509.                                                 while(!key4)
  510.                                                     flag2=1200;
  511.                                         }
  512.                                         if(flag1>1)               //等时方波驱动蜂鸣器
  513.                                 {
  514.                                                 beep=~beep;
  515.                                                 flag1=0;
  516.                                         }
  517.                             shijian();         //调用时间子函数
  518.                             display();         //调用显示子函数  
  519.                         }
  520.                         beep=0;                  //关闭蜂鸣器
  521.                         }
  522. }
  523. //**************************主函数********************************//
  524. void main()
  525. {  
  526.         init();                         //调用初始化子函数
  527.            while(1)
  528.     {  
  529.                 clock();                   //闹钟子函数                    
  530.         if(cnt==1||cnt==2||cnt==3) //显示子函数
  531.                    display1();
  532.                 if(cnt==4||cnt==5||cnt==6)
  533.                    display2();
  534.                 if(cnt==7||cnt==8||cnt==9)
  535.                    display3();
  536.                 if(cnt==0||cnt==10||cnt==11||cnt==12||cnt==13)
  537.                    display();
  538.             shijian();                 //调用时间子函数
  539.                 key_scan();                //调用键盘扫描子函数
  540.     }
  541. }
  542. //**************************定时中断******************************//
  543. void t0() interrupt 1         
  544. {   
  545.         TH0=0x3c;        //初值50ms (65536-50000)/256
  546.     TL0=0x0b0;                 // (65536-50000)%256
  547.     flag++;          //标志位
  548.     flag1++;
  549.         flag2++;                 }
复制代码



评分

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

查看全部评分

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

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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