找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 3859|回复: 3
收起左侧

单片机可调时钟程序与仿真图

[复制链接]
ID:254549 发表于 2017-11-28 11:02 | 显示全部楼层 |阅读模式
仿真原理图如下(proteus仿真工程文件可到本帖附件中下载)
QQ截图20171128105955.png

功能键,时或分+1,选择调节时或分或秒,调节清零键.
0.png

单片机源程序如下:
  1. #include"reg52.h"
  2. #define uchar unsigned char
  3. #define uint unsigned int
  4. uchar code DuanMa[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};  
  5. uchar code DuanMa1[]={0x40,0x79,0x24,0x30,0x19,0x12,0x02,0x78,0x00,0x10};
  6. uchar hours=00,minutes=00,seconds=00;
  7. uchar Set_Time=0;                                
  8. uchar key_mode=0;
  9. bit impulse;                        
  10. uchar Time0_bit=0;
  11. bit flash;
  12. sbit LED=P1^0;
  13. sbit k1=P3^2;                        
  14. sbit k2=P3^3;                        
  15. sbit k3=P3^4;                        
  16. sbit k4=P3^5;
  17. uchar shi,fen,miao;        
  18. void delay(uint n)                                                                                                         
  19. {
  20.   uint i;
  21.   while(n--)
  22.   {
  23.     for(i=0;i<120;i++);
  24.   }
  25. }
  26. void Mode_Choose()                                                                                   
  27. {
  28.         if(k1==0)
  29.                 {
  30.                         delay(10);
  31.                                 if(k1==0)
  32.                                         {
  33.                                                 key_mode++;
  34.                                                 if(key_mode==2)
  35.                                                         key_mode=0;
  36.                                                 while(!k1);                                       
  37.                                         }
  38.                 }
  39. }
  40. void key_Time_Set()                                                               
  41. {
  42.         if(key_mode==1)
  43.                 {
  44.                         if(k2==0)
  45.                                 {
  46.                                         delay(10);
  47.                                                 if(k2==0)
  48.                                                         {
  49.                                                                 if(Set_Time==0)
  50.                                                                         seconds=0;
  51.                                                                 else if(Set_Time==1)
  52.                                                                         {
  53.                                                                         minutes++;
  54.                                                                         if(minutes==60)
  55.                                                                         minutes=0;
  56.                                                                         }
  57.                                                                 else
  58.                                                                         {
  59.                                                                         hours++;
  60.                                                                         if(hours==24)
  61.                                                                         hours=0;
  62.                                                                         }
  63.                                                                 while(!k2);
  64.                                                         }
  65.                                 }
  66.                         if(k3==0)
  67.                                 {
  68.                                         delay(10);
  69.                                                 if(k3==0)
  70.                                                         {
  71.                                                                 Set_Time++;
  72.                                                                 if(Set_Time==3)
  73.                                                                         Set_Time=0;
  74.                                                                 while(!k3);
  75.                                                         }
  76.                                 }
  77.                         if(k4==0)
  78.                                 {
  79.                                         delay(10);
  80.                                                 if(k4==0)
  81.                                                         {
  82.                                                                 if(Set_Time==0)
  83.                                                                 seconds=0;
  84.                                                                 else if(Set_Time==1)
  85.                                                                 minutes=0 ;
  86.                                                                 else
  87.                                                                 hours=0;
  88.                                                                 while(!k4);
  89.                                                         }
  90.                                 }
  91.                 } }


  92. void Display_ShiZhon()                                                  
  93. {
  94.         P0=DuanMa[seconds%10];        
  95.         P2=0x80;                                       
  96.         delay(1);                                       
  97.         P0=0xff;                                       

  98.         P0=DuanMa[seconds/10];        
  99.         P2=0x40;                                       
  100.         delay(1);                                
  101.         P0=0xff;                                       

  102.         if(flash)                                
  103.         {
  104.                 P0=0xbf;
  105.                 P2=0x20;
  106.             delay(1);
  107.                 P0=0xff;
  108.         }
  109.         else
  110.         {
  111.                 P0=0xff;
  112.                 P2=0x20;
  113.             delay(1);
  114.                 P0=0xff;
  115.         }

  116.         P0=DuanMa[minutes%10];               
  117.         P2=0x10;                                       
  118.         delay(1);                                       
  119.         P0=0xff;                                

  120.         P0=DuanMa[minutes/10];               
  121.         P2=0x08;                                
  122.         delay(1);                                
  123.         P0=0xff;                                       

  124.         P0=0xbf;                                
  125.         P2=0x04;
  126.     delay(1);
  127.         P0=0xff;

  128.         P0=DuanMa[hours%10];               
  129.         P2=0x02;                                       
  130.         delay(1);                                       
  131.         P0=0xff;                                

  132.         P0=DuanMa[hours/10];               
  133.         P2=0x01;                                       
  134.         delay(1);                                       
  135.         P0=0xff;                                       

  136. }
  137. void Display_Time_Set()                                                                  
  138. {
  139.         if(Set_Time==0)
  140.                 {
  141.                         if(impulse)
  142.                         {
  143.                                 P0=DuanMa[seconds%10];               
  144.                                 P2=0x80;                                       
  145.                                 delay(1);                                
  146.                                 P0=0xff;                                
  147.                         
  148.                                 P0=DuanMa[seconds/10];               
  149.                                 P2=0x40;                                       
  150.                                 delay(1);                                       
  151.                                 P0=0xff;                                       
  152.                         }
  153.                         else
  154.                         {
  155.                                 P0=0xff;               
  156.                                 P2=0x80;                                       
  157.                                 delay(1);                                       
  158.                                 P0=0xff;                                
  159.                         
  160.                                 P0=0xff;               
  161.                                 P2=0x40;                                
  162.                                 delay(1);                                
  163.                                 P0=0xff;                                       
  164.                         }
  165.                
  166.                         if(flash)                                       
  167.                         {
  168.                                 P0=0xbf;
  169.                                 P2=0x20;
  170.                             delay(1);
  171.                                 P0=0xff;
  172.                         }
  173.                         else
  174.                         {
  175.                                 P0=0xff;
  176.                                 P2=0x20;
  177.                             delay(1);
  178.                                 P0=0xff;
  179.                         }
  180.                
  181.                         P0=DuanMa[minutes%10];               
  182.                         P2=0x10;                                       
  183.                         delay(1);                                
  184.                         P0=0xff;                                       
  185.                
  186.                         P0=DuanMa[minutes/10];               
  187.                         P2=0x08;                                
  188.                         delay(1);                                
  189.                         P0=0xff;                                
  190.                
  191.                         P0=0xbf;                                       
  192.                         P2=0x04;
  193.                     delay(1);
  194.                         P0=0xff;
  195.                
  196.                         P0=DuanMa[hours%10];                        
  197.                         P2=0x02;                                
  198.                         delay(1);                                       
  199.                         P0=0xff;                                
  200.                
  201.                         P0=DuanMa[hours/10];                        
  202.                         P2=0x01;                                
  203.                         delay(1);                                
  204.                         P0=0xff;                                       
  205.                 }
  206.         else if(Set_Time==1)
  207.                 {
  208.                         P0=DuanMa[seconds%10];               
  209.                         P2=0x80;                                       
  210.                         delay(1);                                       
  211.                         P0=0xff;                                
  212.                
  213.                         P0=DuanMa[seconds/10];               
  214.                         P2=0x40;                                       
  215.                         delay(1);                                       
  216.                         P0=0xff;                                
  217.                
  218.                         if(flash)                                
  219.                         {
  220.                                 P0=0xbf;
  221.                                 P2=0x20;
  222.                             delay(1);
  223.                                 P0=0xff;
  224.                         }
  225.                         else
  226.                         {
  227.                                 P0=0xff;
  228.                                 P2=0x20;
  229.                             delay(1);
  230.                                 P0=0xff;
  231.                         }
  232.                
  233.                         if(impulse)
  234.                         {
  235.                                 P0=DuanMa[minutes%10];               
  236.                                 P2=0x10;                                
  237.                                 delay(1);                                       
  238.                                 P0=0xff;                                       
  239.                         
  240.                                 P0=DuanMa[minutes/10];        
  241.                                 P2=0x08;                                       
  242.                                 delay(1);                                       
  243.                                 P0=0xff;                                
  244.                         }
  245.                         else
  246.                         {
  247.                                 P0=0xff;        
  248.                                 P2=0x10;                                       
  249.                                 delay(1);                                
  250.                                 P0=0xff;                                
  251.                         
  252.                                 P0=0xff;               
  253.                                 P2=0x08;                                       
  254.                                 delay(1);                                       
  255.                                 P0=0xff;                                
  256.                         }
  257.                
  258.                         P0=0xbf;                                
  259.                         P2=0x04;
  260.                     delay(1);
  261.                         P0=0xff;
  262.                
  263.                         P0=DuanMa[hours%10];                        
  264.                         P2=0x02;                                       
  265.                         delay(1);                                       
  266.                         P0=0xff;                                
  267.                
  268.                         P0=DuanMa[hours/10];                        
  269.                         P2=0x01;                                
  270.                         delay(1);                                
  271.                         P0=0xff;                                       
  272.                 }
  273.         else
  274.                 {
  275.                         P0=DuanMa[seconds%10];               
  276.                         P2=0x80;                                
  277.                         delay(1);                                
  278.                         P0=0xff;                                       
  279.                
  280.                         P0=DuanMa[seconds/10];        
  281.                         P2=0x40;                                       
  282.                         delay(1);                                
  283.                         P0=0xff;                                
  284.                
  285.                         if(flash)                                
  286.                         {
  287.                                 P0=0xbf;
  288.                                 P2=0x20;
  289.                             delay(1);
  290.                                 P0=0xff;
  291.                         }
  292.                         else
  293.                         {
  294.                                 P0=0xff;
  295.                                 P2=0x20;
  296.                             delay(1);
  297.                                 P0=0xff;
  298.                         }
  299.                
  300.                         P0=DuanMa[minutes%10];        
  301.                         P2=0x10;                                
  302.                         delay(1);                                
  303.                         P0=0xff;                                
  304.                
  305.                         P0=DuanMa[minutes/10];               
  306.                         P2=0x08;                                       
  307.                         delay(1);                                       
  308.                         P0=0xff;                                
  309.                
  310.                         P0=0xbf;                                       
  311.                         P2=0x04;
  312.                     delay(1);
  313.                         P0=0xff;
  314.                
  315.                         if(impulse)
  316.                         {
  317.                                 P0=DuanMa[hours%10];               
  318.                                 P2=0x02;                                       
  319.                                 delay(1);                                       
  320.                                 P0=0xff;                                       
  321.                         
  322.                                 P0=DuanMa[hours/10];                        
  323.                                 P2=0x01;                                
  324.                                 delay(1);                                       
  325.                                 P0=0xff;                                
  326.                         }
  327.                         else
  328.                         {
  329.                                 P0=0xff;               
  330.                                 P2=0x02;                                
  331.                                 delay(1);                                       
  332.                                 P0=0xff;                                       
  333.                         
  334.                                 P0=0xff;                        
  335.                                 P2=0x01;                                       
  336.                                 delay(1);                                       
  337.                                 P0=0xff;                                       
  338.                         }
  339.                 }
  340. }

  341. void Time_Init()                        
  342. {
  343.         TMOD=0x11;                                       
  344.         TH0=(65536-50000)/256;               
  345.         TL0=(65536-50000)%256;
  346.         ET0=1;                                                
  347.         TR0=1;                                                
  348.         TH1=(65536-50000)/256;               
  349.         TL1=(65536-50000)%256;
  350.         ET1=1;                                                
  351.         TR1=0;                                       

  352.         EA=1;                                       
  353. }
  354. void Time0_Interrupt()interrupt 1                                 
  355. {
  356.         TH0=(65536-50000)/256;                                                
  357.         TL0=(65536-50000)%256;
  358.         Time0_bit++;                                                                  
  359.         if((Time0_bit%10)==0)                                                  
  360.         impulse=~impulse;                                                
  361.                 if(Time0_bit==20)                                       
  362. ……………………

  363. …………限于本文篇幅 余下代码请从51黑下载附件…………
复制代码
0.png
所有资料51hei提供下载:
可调时钟.zip (82.33 KB, 下载次数: 21)
回复

使用道具 举报

ID:88606 发表于 2017-11-29 07:07 | 显示全部楼层
看看按键思路!
回复

使用道具 举报

ID:269319 发表于 2017-12-30 21:55 | 显示全部楼层
看看按键思路!
回复

使用道具 举报

ID:261637 发表于 2018-1-5 23:23 | 显示全部楼层
看看按键思路!
回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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