找回密码
 立即注册

QQ登录

只需一步,快速开始

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

使用单片机+数码管制作的计时器proteus仿真和源程序

[复制链接]
跳转到指定楼层
楼主
使用单片机+数码管制作的计时器proteus仿真和源程序


单片机源程序如下:
  1. /********************************************************************/
  2. /********************************************************************/
  3. /*****                                                          *****/
  4. /*****        L A B C E N T E R    E L E C T R O N I C S        *****/
  5. /*****                                                          *****/
  6. /*****       LABCENTER INTEGRATED SIMULATION ARCHITECTURE       *****/
  7. /*****                                                          *****/
  8. /*****                      Tasks Processes                     *****/
  9. /*****                                                          *****/
  10. /********************************************************************/
  11. /********************************************************************/

  12. #include <reg52.h>
  13. #include "cext.h"

  14. char num[ ] = {0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};   // 7-seg codes
  15. char add[ ] = {0x0fe,0x0fd,0x0fb,0x0f7};                             // Multiplexing address
  16. char dpn[ ] = {0,0,0,0};                                             // Dots matrix
  17. char buf[ ] = {0,0,0,0};                                             // Counter buffer
  18. char alm[ ] = {0,0,0,0};                                             // Alarm sets buffer
  19. char value, key_mask, new_key;

  20. static uchar d0, d1, d2, d3, d5;
  21. static uchar flag;
  22. static uchar digit_counter;

  23. static char set_flag;
  24. static char set_count;
  25. static char set_alm;
  26. static char stop_counter = true;

  27. char un, th;
  28. uint timer;
  29. uint alarm;
  30. uchar del;

  31. // P2.7. Alarm output
  32. sbit alm_out = P2^7;   

  33. // processor switch contest.
  34. #pragma registerbank (1)      

  35. // Multiplexing Task. Do it every 10ms
  36. void led_mux ()
  37. { if (!set_flag)
  38.     { // Normal counting. Display the counts.
  39.       value = buf[digit_counter];   
  40.       P1 = num[value] | dpn[digit_counter] ;
  41.     }
  42.    else
  43.     { // Display alarm values
  44.       value = alm[digit_counter];   
  45.       P1 = num[value] | dpn[digit_counter] ;
  46.     }     
  47.    
  48.    if (set_alm)
  49.     { // Timer and alarm value
  50.       timer = buf[0] * 1000 + buf[1] * 100 + buf[2] * 10 + buf[3];
  51.       alarm = alm[0] * 1000 + alm[1] * 100 + alm[2] * 10 + alm[3];
  52.       // Check whether the alarm condition is occured or not  
  53.       if ((alarm - timer) <= 0)
  54.        { alm_out = false;           // Make alarm out low.
  55.          stop_counter = true  ;     // Stop the counter.
  56.        }
  57.     }

  58.    // Drive the digit actually multiplexed.
  59.    P0 = add[digit_counter];
  60.    for (del=0; del<100; del++) ;
  61.    // Increase multiplexer address to the next digit
  62.    digit_counter++ ;

  63.    // Did you diplay all digit ?
  64.    if (digit_counter >= 4)
  65.       // Start again.
  66.       digit_counter = 0;      

  67.    // Pulse high P0.0, P0.1, P0.2 and P0.3
  68.    P0 = P0 | 0x0f;
  69. }


  70. // Blink on and off the dots when counting and alarm is on.  
  71. void blink_dp ()
  72. { if (!set_flag && !stop_counter)
  73.     { if (flag)
  74.          dpn[1] = 0x80;
  75.       else
  76.          dpn[1] = 0x0;
  77.       
  78.       flag = !flag;
  79.     }  
  80. }

  81. // Counter. Counts from 00.00 to 59.59 min.sec.
  82. // The Timer interrupt is driven every 1 sec.
  83. void led_counter ()
  84. { if (!stop_counter) buf[3]++;
  85.    
  86.    if (buf[3] > 9)
  87.     { buf[3] = 0;
  88.       d3 = true;
  89.     }
  90.    
  91.    if (d3)
  92.     { ++buf[2];
  93.       d3 = false;
  94.     }
  95.    
  96.    if (buf[2] > 5)
  97.     { buf[2] = 0;
  98.       d2 = true;
  99.     }

  100.    if (d2)
  101.     { ++buf[1];
  102.       d2 = false;
  103.     }
  104.    
  105.    if (buf[1] > 9)
  106.     { buf[1] = 0;
  107.       d1 = true;
  108.     }

  109.    if (d1)
  110.     { ++buf[0];
  111.       d1 = false;
  112.     }
  113.    
  114.    if (buf[0] > 5)
  115.     { buf[0] = 0;
  116.       d0 = true;
  117.     }
  118. }




  119. // Four keys Task manager.
  120. // Timer interrupt driven every 250ms
  121. void get_key ()
  122. { char valid_key;
  123.    char delay = 127;
  124.    key_mask = P0 & 0xf0;   
  125.    while (--delay >= 0) ;
  126.    valid_key =  P0 & 0xf0;
  127.    if (key_mask != valid_key) valid_key = 0;
  128.    while ((P0 & 0xf0) != (0 | (valid_key & 0x40))) ;

  129.    switch (valid_key)
  130.     { case 0x10:              // Reset Timer
  131.          buf[0] = 0;
  132.          buf[1] = 0;
  133.          buf[2] = 0;
  134.          buf[3] = 0;
  135.          alm_out = true;
  136.          stop_counter = !stop_counter;
  137.          break;      

  138.       case 0x20:              // Set alarm
  139.          ++set_count;
  140.          if (set_count == 1)
  141.           { // First time SET is pressed. Sets seconds
  142.             set_flag = true;           
  143.             dpn[0] = 0; dpn[1] = 0; dpn[2] = 0x80; dpn[3] = 0x80;
  144.           }
  145.          else if (set_count == 2)
  146.           { // Second time SET is pressed. Set minutes
  147.             set_flag = true;           
  148.             dpn[0] = 0x80; dpn[1] = 0x80; dpn[2] = 0; dpn[3] = 0;
  149.           }
  150.          else if (set_count == 3)
  151.           { // Latest time SET is pressed. Return to timer functions.
  152.             set_flag = false;         
  153.             set_count = 0;
  154.             dpn[0] = 0; dpn[1] = 0; dpn[2] = 0; dpn[3] = 0;
  155.           }
  156.          un = 0;
  157.          th = 0;
  158.          break;

  159.       case 0x40:              // Set seconds/minutes
  160.          if (set_count == 1)
  161.           { ++un;
  162.             if (un > 9)
  163.              { th++;
  164.                un = 0;
  165.                if(th > 5)
  166.                   th = 0;
  167.              }
  168.             alm[3] = un;
  169.             alm[2] = th;                  
  170.           }
  171. ……………………

  172. …………限于本文篇幅 余下代码请从51黑下载附件…………
复制代码

所有资料51hei提供下载:
8051 Timer with LCD.zip (54.22 KB, 下载次数: 23)



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

使用道具 举报

沙发
ID:35873 发表于 2017-6-16 02:04 | 只看该作者
不错啊   多谢分享
回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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