找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 2540|回复: 1
收起左侧

基于51单片机数字钟的问题

[复制链接]
ID:345662 发表于 2018-7-7 14:34 | 显示全部楼层 |阅读模式
50黑币
我的仿真图 173911pmd6gc5z61o7zzh4.png
为什么会出现这种情况,我程序哪里出问题了,如果要改怎么改?能详细讲讲吗?还有程序中cnt!=3这样的代码代表什么意思?有什么作用?这个开始的12点是怎么出来的?

  1. #include"reg51.h"              
  2. #include"intrins.h"
  3. typedef unsigned char uchar;   
  4. typedef unsigned int  uint;
  5. sbit key1=P1^0;               
  6. sbit key2=P1^1;
  7. sbit key3=P1^2;
  8. sbit key4=P1^3;                 //  0        1         2          3           4        5         6         7          8                 9        -    wu
  9. uchar code table[]=        {0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x40,0x00};                       
  10. //****************************函数声明******************************
  11. void jia();   
  12. void jian();
  13. //********************数组定义,数组内含有8个数值******************
  14. uchar table1[8],table2[8],table3[8],table4[8];
  15. //**************************时间显示初始值**************************
  16. uchar shi=12,fen=0,miao=0;  
  17. //**************************定义全局变量****************************
  18. uchar shi1,fen1,miao1,shi2,fen2,miao2,shi3,fen3,miao3;
  19. uchar shi4,fen4,miao4;
  20. uchar flag, flag1, wss, cnt, cnt1, alm1, alm2, alm3,beep;
  21. //    1秒   等时  位闪 次数 校时   闹1  闹2   闹3
  22. uint flag2;
  23. //   蜂鸣
  24. //*********************延时函数,用于动态扫描数码管*****************
  25. void delay(uchar i)                                    
  26. {  
  27.         uchar x,y;
  28.            for(x=i;x>0;x--)
  29.              for(y=120;y>0;y--);
  30. }
  31. //*******************************初始化函数*************************
  32. void init()   
  33. {  
  34.         TMOD=0x01;         //工作方式1                                                                                
  35.            TH0=0x3c;          //定时时间为:50ms        (65536-50000)/256
  36.            TL0=0x0b0;         //(65536-50000)%256
  37.         EA=1;              //开总中断
  38.            ET0=1;             //打开定时器
  39.            TR0=1;             //启动定时器
  40.         beep=0;
  41. }
  42. //********************显示子函数,用于显示时间数值*****************
  43. void display()  
  44. {  
  45.         uchar i,j;
  46.            if(cnt!=10||wss==0)
  47.     {
  48.                 table1[0]=miao%10;        //分离秒的个位与十位
  49.                table1[1]=miao/10;
  50.         }
  51.         else
  52.                  table1[0]=table1[1]=11;

  53.         if(cnt!=11||wss==0)
  54.         {
  55.                 table1[3]=fen%10;         //分离分的个位与十位
  56.                table1[4]=fen/10;
  57.         }
  58.         else
  59.                   table1[3]=table1[4]=11;

  60.         if(cnt!=12||wss==0)
  61.         {
  62.                 table1[6]=shi%10;         //分离时的个位与十位
  63.                table1[7]=shi/10;
  64.         }
  65.         else
  66.                 table1[6]=table1[7]=11;

  67.     table1[2]=table1[5]=10;         //两个横线

  68.     j=0xfb;
  69.     for(i=0;i<=7;i++)          //从秒到时的扫描
  70.     {  
  71.                 P2=j;
  72.         P0=table[table1[i]];  //显示数值
  73.         delay(10);
  74.         j=_cror_(j,1);        //循环右移
  75.     }
  76. }
  77. //*******************显示子函数,用于显示定时1时间*****************
  78. void display1()
  79. {  
  80.         uchar i,j;
  81.            if(alm1==0)         //闹1=0
  82.     {
  83.                 if(cnt!=1||wss==0)
  84.             {
  85.                         table2[0]=miao1%10;        //以下含义同上
  86.                    table2[1]=miao1/10;
  87.                 }
  88.                    else
  89.                       table2[0]=table2[1]=11;

  90.                    if(cnt!=2||wss==0)
  91.             {
  92.                         table2[3]=fen1%10;
  93.                    table2[4]=fen1/10;
  94.                 }
  95.                    else
  96.                      table2[3]=table2[4]=11;

  97.                    if(cnt!=3||wss==0)
  98.             {
  99.                         table2[6]=shi1%10;
  100.                    table2[7]=shi1/10;
  101.                 }
  102.                    else
  103.                      table2[6]=table2[7]=11;
  104.         }

  105.         else
  106.                    table2[0]=table2[1]=table2[3]=table2[4]=table2[6]=table2[7]=10;

  107.            table2[2]=table2[5]=10;
  108.            j=0xfb;
  109.            for(i=0;i<=7;i++)
  110.            {  
  111.                 P2=j;
  112.         P0=table[table2[i]];
  113.                 delay(10);
  114.         j=_cror_(j,1);
  115.            }
  116. }
  117. //********************显示子函数,用于显示定时2时间******************
  118. void display2()
  119. {  
  120.         uchar i,j;
  121.            if(alm2==0)
  122.     {
  123.                 if(cnt!=4||wss==0)
  124.             {
  125.                         table3[0]=miao2%10;        //以下含义同上
  126.                    table3[1]=miao2/10;
  127.                    }
  128.                    else
  129.                       table3[0]=table3[1]=11;
  130.                    if(cnt!=5||wss==0)
  131.             {
  132.                         table3[3]=fen2%10;
  133.                    table3[4]=fen2/10;
  134.                 }
  135.                 else
  136.                           table3[3]=table3[4]=11;
  137.                 if(cnt!=6||wss==0)
  138.                 {
  139.                         table3[6]=shi2%10;
  140.                    table3[7]=shi2/10;
  141.                 }
  142.                 else
  143.                          table3[6]=table3[7]=11;
  144.         }
  145.         else
  146.            table3[0]=table3[1]=table3[3]=table3[4]=table3[6]=table3[7]=10;
  147.            table3[2]=table3[5]=10;
  148.            j=0xfb;
  149.            for(i=0;i<=7;i++)
  150.     {         
  151.                 P2=j;
  152.         P0=table[table3[i]];
  153.                 delay(10);
  154.         j=_cror_(j,1);
  155.     }
  156. }
  157. //***************显示子函数,用于显示定时3时间数值****************//
  158. void display3()  
  159. {
  160.          uchar i,j;
  161.            if(alm3==0)
  162.     {  
  163.                 if(cnt!=7||wss==0)
  164.             {
  165.                         table4[0]=miao3%10;        //分离秒的个位与十位
  166.             table4[1]=miao3/10;
  167.         }
  168.                 else
  169.                            table4[0]=table4[1]=11;
  170.                 if(cnt!=8||wss==0)
  171.                 {
  172.                         table4[3]=fen3%10;         //分离分的个位与十位
  173.             table4[4]=fen3/10;
  174.                 }
  175.                 else
  176.                           table4[3]=table4[4]=11;
  177.                 if(cnt!=9||wss==0)
  178.                 {
  179.                         table4[6]=shi3%10;         //分离时的个位与十位
  180.             table4[7]=shi3/10;
  181.                 }
  182.                 else
  183.                           table4[6]=table4[7]=11;
  184.     }
  185.     else
  186.             table4[0]=table4[1]=table4[3]=table4[4]=table4[6]=table4[7]=10;
  187.            table4[2]=table4[5]=10;
  188.            j=0xfb;                    //从秒到时的扫描
  189.            for(i=0;i<=7;i++)
  190.            {  
  191.                 P2=j;
  192.         P0=table[table4[i]];  //显示数值
  193.                 delay(10);
  194.         j=_cror_(j,1);        //循环右移
  195.            }
  196. }
  197. //***********************时间子函数 *****************************//
  198. void shijian()         
  199. {  
  200.         if(flag>=20)               //判断是否到一秒
  201.     {
  202.                 wss=~wss;
  203.                    flag=0;                                 //到了,则标志位清零
  204.                    if(cnt1!=0)
  205.             {
  206.                         miao4++;          //秒加1
  207.                    if(miao4>59)    //判断秒是否到60s
  208.             {
  209.                                 miao4=0;      //到了,则清零
  210.                 fen4++;       //分加1
  211.                 if(fen4>59)  //以下含义同上
  212.                 {
  213.                                         fen4=0;
  214.                     shi4++;
  215.                     if(shi4>23)
  216.                       shi4=0;                                                                                                                  
  217.                  }        
  218.              }
  219.         }
  220.                    else
  221.                {
  222.                         miao++;          //秒加1
  223.                  if(miao>59)    //判断秒是否到60s
  224.                    {
  225.                                 miao=0;      //到了,则清零
  226.                      fen++;       //分加1
  227.                      if(fen>59)  //以下含义同上
  228.                        {
  229.                                         fen=0;
  230.                          shi++;
  231.                          if(shi>23)
  232.                                    shi=0;                                                                                                                  
  233.                 }        
  234.             }
  235.         }
  236.     }
  237. }
  238. //**************************键盘扫描子函数************************//
  239. void key_scan()
  240. {  
  241.         if(key1==0)
  242.     {  
  243.                 while(!key1)          //防止掉显
  244.         {  
  245.                         if(cnt==1||cnt==2||cnt==3)
  246.                 display1();
  247.                         if(cnt==4||cnt==5||cnt==6)
  248.                 display2();
  249.                         if(cnt==7||cnt==8||cnt==9)
  250.                 display3();
  251.                         if(cnt==0||cnt==10||cnt==11||cnt==12||cnt==13)
  252.                     display();
  253.         }
  254.         cnt++;     //记下按键key1按下的次数                  
  255.                 if(cnt==10&&cnt1==0)
  256.                 {
  257.                         miao4=miao;
  258.                     fen4=fen;
  259.                         shi4=shi;
  260.                         cnt1++;
  261.                 }
  262.                 if(cnt==13)
  263.                 {
  264.                         cnt=0;
  265.                     if(cnt1==1)
  266.                         {
  267.                                 miao=miao4;
  268.                                 fen=fen4;
  269.                                 shi=shi4;
  270.                         }
  271.                     cnt1=0;
  272.                 }         
  273.         }
  274.            if(key2==0)              //判断key2是否按下
  275.            {  
  276.                 while(!key2)          //防止掉显
  277.         {  
  278.                         if(cnt==1||cnt==2||cnt==3)
  279.                 display1();
  280.                         if(cnt==4||cnt==5||cnt==6)
  281.                 display2();
  282.                         if(cnt==7||cnt==8||cnt==9)
  283.                 display3();
  284.                         if(cnt==0||cnt==10||cnt==11||cnt==12||cnt==13)
  285.                 display();
  286.         }
  287.         jia();
  288.            }
  289.            if(key3==0)              //判断key3是否按下
  290.            {  
  291.                 while(!key3)          //防止掉显
  292.         {  
  293.                         if(cnt==1||cnt==2||cnt==3)
  294.                 display1();
  295.                         if(cnt==4||cnt==5||cnt==6)
  296.                 display2();
  297.                         if(cnt==7||cnt==8||cnt==9)
  298.                 display3();  
  299.                         if(cnt==0||cnt==10||cnt==11||cnt==12||cnt==13)
  300.                 display();
  301.         }
  302.         jian();                    //调用减1子函数           
  303.            }
  304.         if(key4==0)                   //判断key4是否按下
  305.         {
  306.                 while(!key4)            //防止掉
  307.             {
  308.                         if(cnt==1||cnt==2||cnt==3)
  309.                     {  
  310.                                 alm1=~alm1;
  311.                             display1();
  312.                         }
  313.                     if(cnt==4||cnt==5||cnt==6)
  314.                     {  
  315.                                 alm2=~alm2;
  316.                             display2();
  317.                         }
  318.                     if(cnt==7||cnt==8||cnt==9)
  319.                     {  
  320.                                 alm3=~alm3;
  321.                             display3();
  322.                         }
  323.                         if(cnt==0||cnt==10||cnt==11||cnt==12||cnt==13)
  324.                        display();
  325.                  }
  326.         }      
  327. }
  328. //****************************加1子函数***************************
  329. void jia()            
  330. {
  331.         if(cnt==1)                         //判断key1按下的次数是否为1
  332.     {  
  333.                 miao1++;                 //是,则秒加1
  334.                if(miao1>59)         //判断秒是否大于60,是,则秒清零
  335.            miao1=0;
  336.     }
  337.          if(cnt==2)                         //以下含意同上
  338.     {  
  339.                 fen1++;
  340.         if(fen1>59)
  341.            fen1=0;
  342.     }
  343.           if(cnt==3)
  344.     {  
  345.                 shi1++;
  346.         if(shi1>23)
  347.            shi1=0;
  348.     }
  349.           if(cnt==4)
  350.     {  
  351.                 miao2++;
  352.         if(miao2>59)
  353.            miao2=0;
  354.     }
  355.           if(cnt==5)
  356.     {  
  357.                 fen2++;
  358.         if(fen2>59)
  359.            fen2=0;
  360.     }
  361.           if(cnt==6)
  362.     {  
  363.                 shi2++;
  364.         if(shi2>23)
  365.            shi2=0;
  366.     }
  367.           if(cnt==7)
  368.     {  
  369.                 miao3++;
  370.         if(miao3>59)
  371.            miao3=0;
  372.     }
  373.           if(cnt==8)
  374.     {  
  375.                 fen3++;
  376.         if(fen3>59)
  377.            fen3=0;
  378.     }
  379.           if(cnt==9)
  380.     {  
  381.                 shi3++;
  382.         if(shi3>23)
  383.            shi3=0;
  384.     }
  385.           if(cnt==10)         
  386.     {  
  387.                 miao++;         
  388.                if(miao>59)      
  389.           miao=0;
  390.                 cnt1++;
  391.     }
  392.           if(cnt==11)         
  393.     {  
  394.                 fen++;
  395.         if(fen>59)
  396.            fen=0;
  397.                 cnt1++;
  398.     }
  399.           if(cnt==12)
  400.     {  
  401.                 shi++;
  402.         if(shi>23)
  403.            shi=0;
  404.                 cnt1++;
  405.     }
  406. }
  407. //***************************减1子函数**************************//
  408. void jian()   
  409. {  
  410.         if(cnt==1)                        //判断key1按下的次数是否为1,是则秒减1
  411.     {  
  412.                 miao1--;
  413.         if(miao1==255)        //判断秒是否减到255,是,则秒置59
  414.            miao1=59;
  415.     }
  416.            if(cnt==2)                        //以下含意同上
  417.     {  
  418.                 fen1--;
  419.         if(fen1==255)
  420.            fen1=59;
  421.     }
  422.            if(cnt==3)
  423.     {  
  424.                 shi1--;
  425.                if(shi1==255)
  426.            shi1=23;
  427.     }
  428.            if(cnt==4)
  429.     {  
  430.                 miao2--;
  431.                if(miao2==255)
  432.            miao2=59;
  433.     }
  434.            if(cnt==5)
  435.     {  
  436.                 fen2--;
  437.         if(fen2==255)
  438.            fen2=59;
  439.     }
  440.            if(cnt==6)
  441.     {  
  442.                 shi2--;
  443.         if(shi2==255)
  444.            shi2=23;
  445.     }
  446.         if(cnt==7)
  447.     {  
  448.                 miao3--;
  449.         if(miao3==255)
  450.             miao3=59;
  451.     }
  452.     if(cnt==8)
  453.     {  
  454.                 fen3--;
  455.         if(fen3==255)
  456.             fen3=59;
  457.     }
  458.     if(cnt==9)
  459.     {  
  460.                 shi3--;
  461.         if(shi3==255)
  462.             shi3=23;
  463.     }
  464.     if(cnt==10)         
  465.     {  
  466.                 miao--;
  467.         if(miao==255)  
  468.             miao=59;
  469.                 cnt1++;
  470.     }
  471.     if(cnt==11)
  472.     {  
  473.                 fen--;
  474.         if(fen==255)
  475.             fen=59;
  476.                 cnt1++;
  477.     }
  478.     if(cnt==12)
  479.     {  
  480.                 shi--;
  481.         if(shi==255)
  482.             shi=23;
  483.                 cnt1++;
  484.     }
  485. }
  486. //***************************闹铃子函数***************************//
  487. void clock()           
  488. {  
  489.         //判断秒的数值是否相等
  490.         if(miao==miao1&&alm1==0||miao==miao2&&alm2==0||miao==miao3&&alm3==0)
  491.         //是,在判断分是否相等                                   
  492.             if(fen==fen1&&alm1==0||fen==fen2&&alm2==0||fen==fen3&&alm3==0)
  493.         //是,再判断时是否相等            
  494.                     if(shi==shi1&&alm1==0||shi==shi2&&alm2==0||shi==shi3&&alm3==0)
  495.                     {
  496.                                 flag2=0;                       //是,则标志位,flag2清零
  497.                         while(!(flag2==1200)&&(cnt==0)) //判断flag2是否到1200且不                          
  498.                         {
  499.                                         //             为调时状态
  500.                                         if(key4==0)                                 //没有,则继续驱动蜂鸣器响
  501.                                         {
  502.                                                 while(!key4)
  503.                                                     flag2=1200;
  504.                                         }
  505.                                         if(flag1>1)               //等时方波驱动蜂鸣器
  506.                                 {
  507.                                                 beep=~beep;
  508.                                                 flag1=0;
  509.                                         }
  510.                             shijian();         //调用时间子函数
  511.                             display();         //调用显示子函数  
  512.                         }
  513.                         beep=0;                  //关闭蜂鸣器
  514.                         }
  515. }
  516. //**************************主函数********************************//
  517. void main()
  518. {  
  519.         init();                         //调用初始化子函数
  520.            while(1)
  521.     {  
  522.                 //clock();                   //闹钟子函数                    
  523.         if(cnt==1||cnt==2||cnt==3) //显示子函数
  524.                    display1();
  525.                 if(cnt==4||cnt==5||cnt==6)
  526.                    display2();
  527.                 if(cnt==7||cnt==8||cnt==9)
  528.                    display3();
  529.                 if(cnt==0||cnt==10||cnt==11||cnt==12||cnt==13)
  530.                    display();
  531.             shijian();                 //调用时间子函数
  532.                 key_scan();                //调用键盘扫描子函数
  533.     }
  534. }
  535. //**************************定时中断******************************//
  536. void t0() interrupt 1         
  537. {   
  538.         TH0=0x3c;        //初值50ms (65536-50000)/256
  539.     TL0=0x0b0;                 // (65536-50000)%256
  540.     flag++;          //标志位
  541.     flag1++;
  542.         flag2++;                 }
复制代码


回复

使用道具 举报

ID:345662 发表于 2018-7-9 16:09 | 显示全部楼层
已解决
回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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