找回密码
 立即注册

QQ登录

只需一步,快速开始

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

基于51单片机的秒表记次+数码管+按键 Proteus仿真程序

[复制链接]
跳转到指定楼层
楼主
仿真原理图如下(proteus仿真工程文件可到本帖附件中下载)


单片机源程序如下:
  1. #include<reg52.h>

  2. sbit KEY1 = P3^0;  //计次
  3. sbit KEY2 = P3^1;  //从快到慢显示计次数
  4. sbit KEY3 = P3^2;  //复位
  5. sbit KEY4 = P3^3;  //启停秒表
  6. unsigned char code LedChar[] = {  //数码管显示字符转换表
  7.     0xC0, 0xF9, 0xA4, 0xB0, 0x99, 0x92, 0x82, 0xF8,
  8.     0x80, 0x90, 0x88, 0x83, 0xC6, 0xA1, 0x86, 0x8E
  9. };
  10. unsigned char LedBuff[6] = {  //数码管显示缓冲区
  11.     0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF
  12. };
  13. unsigned char KeySta[4] = {1,1,1,1};  //当前按键状态
  14. unsigned char T0RH = 0;
  15. unsigned char T0RL = 0;
  16. //        static unsigned char Dec[10] = 0;
  17. //  static unsigned int  Int[10] = 0;
  18. bit kd = 0;
  19. bit lk = 1;
  20. bit stopwatchRefresh = 1;
  21. bit stopwatchRunning = 0;
  22. unsigned char DecimalPart = 0;
  23. unsigned int  IntegerPart = 0;

  24. void ConfigTimer0(unsigned int ms);
  25. void stopwatchDisplay();
  26. void KeyDriver();

  27. void main()
  28. {
  29.         EA = 1;
  30.         ConfigTimer0(2);
  31.         while(1)
  32.         {
  33.                 if(stopwatchRefresh)
  34.                 {
  35.                         stopwatchRefresh = 0;
  36.                         stopwatchDisplay();
  37.                 }
  38.                 KeyDriver();
  39.         }
  40. }




  41. void ConfigTimer0(unsigned int ms)
  42. {
  43.         unsigned long tmp;
  44.         
  45.         tmp = 11059200/12;
  46.         tmp = (tmp*ms)/1000;
  47.         tmp = 65536 - tmp;
  48.         tmp +=18;      //定时误差补偿
  49.         
  50.         T0RH = (unsigned char)(tmp>>8);
  51.         T0RL = (unsigned char)tmp;
  52.         TMOD &=0xf0;
  53.   TMOD |=0X01;
  54.         TH0 = T0RH;
  55.         TL0 = T0RL;
  56.         ET0 = 1;
  57.         TR0 = 1;
  58. }
  59. void stopwatchDisplay()
  60. {
  61.         signed char i;
  62.         unsigned char buf[4];
  63.         LedBuff[0] = LedChar[DecimalPart%10];
  64.         LedBuff[1] = LedChar[DecimalPart/10];
  65.         buf[0] = IntegerPart%10;
  66.         buf[1] = IntegerPart/10%10;
  67.         buf[2] = IntegerPart/100%10;
  68.         buf[3] = IntegerPart/1000%10;
  69.         for(i=3;i>=1;i--)
  70.         {
  71.                 if(buf[i]==0)
  72.                 {
  73.                         LedBuff[i+2]=0xFF;
  74.                 }else break;
  75.         }
  76.         for(;i>=0;i--)
  77.         {
  78.                 LedBuff[i+2] = LedChar[buf[i]];
  79.         }
  80.          LedBuff[2] &=0x7f;
  81. }

  82. void Stopwatchjc()
  83. {

  84.         signed char i;
  85.         unsigned char buf[4];
  86.         static unsigned char Dec[10] = 0;
  87.   static unsigned int  Int[10] = 0;
  88.         static char cnt = 0;
  89.         static char cnt1 = 0;
  90.         Dec[cnt] = DecimalPart;
  91.         Int[cnt] = IntegerPart;
  92.         cnt++;
  93.         if(kd == 1)
  94.         {        
  95.                 LedBuff[0] = LedChar[Dec[cnt1]%10];
  96.                 LedBuff[1] = LedChar[Dec[cnt1]/10];
  97.                 buf[0] = Int[cnt1]%10;
  98.                 buf[1] = Int[cnt1]/10%10;
  99.                 buf[2] = Int[cnt1]/100%10;
  100.                 buf[3] = Int[cnt1]/1000%10;
  101.                 for(i=3;i>=1;i--)
  102.                 {
  103.                         if(buf[i]==0)
  104.                         {
  105.                                 LedBuff[i+2]=0xFF;
  106.                         }else break;
  107.                 }
  108.                 for(;i>=0;i--)
  109.                 {
  110.                         LedBuff[i+2] = LedChar[buf[i]];
  111.                 }
  112.                  LedBuff[2] &=0x7f;
  113.                
  114.                 cnt1++;
  115.         
  116.         
  117.         }               

  118.         
  119. }
  120. void Stopwatchsy()
  121. {
  122.         stopwatchRunning = 0;
  123.         kd = 1;
  124.         stopwatchRefresh = 1;        
  125. }
  126. void StopwatchAction()
  127. {
  128.         stopwatchRunning = ~stopwatchRunning;
  129. }
  130. void StopwatchReset()
  131. {

  132.         stopwatchRunning = 0;
  133.         lk = 0;
  134.         
  135.         DecimalPart = 0;
  136.         IntegerPart = 0;
  137.         stopwatchRefresh = 1;        


  138. }

  139. void KeyDriver()     //按键驱动函数
  140. {
  141.           static unsigned char backup[4] = {1,1,1,1}; //按键值备份,保存前一次的扫描值
  142.     unsigned char i;
  143.             for(i=0;i<4;i++)
  144.                         {
  145.         if (KeySta[i] != backup[i])  //当前值与前次值不相等说明此时按键有动作
  146.         {
  147.             if (backup[i] == 0)   //如果前次值为0,则说明当前是弹起动作
  148.             {
  149.                                                           if(i==0)
  150.                                                                   Stopwatchjc();      //计次
  151.                                                                 else if(i==1)
  152.                                                                   Stopwatchsy();      //从快到慢
  153.                                                           else if(i==2)         //复位
  154.                                                                         StopwatchReset();
  155.                                                                 else if(i==3)                                        //启停秒表
  156.                                                                         StopwatchAction();
  157.                                                                
  158.             }
  159.             backup[i] = KeySta[i];   //更新备份为当前值,以备进行下次比较
  160.         }
  161.                         }
  162. }
  163. void LedScan()
  164. {
  165.           static unsigned char i=0;
  166.           P0 = 0xFF;
  167.                 switch(i)       //每过1ms从低到高位刷新一个数码管
  168.                 {
  169.                         case 0:P2=0x01;i++; P0=        LedBuff[0]; break;
  170.                         case 1:P2=0x02;i++; P0=        LedBuff[1]; break;
  171.                         case 2:P2=0x04;i++; P0=        LedBuff[2]; break;
  172.                         case 3:P2=0x08;i++; P0=        LedBuff[3]; break;
  173.                         case 4:P2=0x10;i++; P0=        LedBuff[4]; break;
  174.                         case 5:P2=0x20;i=0; P0=        LedBuff[5]; break;
  175.                         default :break;
  176.                 }
  177. }
  178. void KeyScan()
  179. {
  180.         static unsigned char keybuf[4] ={ 0xFF,0xFF,0xFF,0xFF};  //扫描缓冲区,保存一段时间内的扫描值
  181.   static unsigned char i=0;
  182.         keybuf[0] = (keybuf[0]<<1) | KEY1;  //缓冲区左移一位,并将当前扫描值移入最低位
  183.         keybuf[1] = (keybuf[1]<<1) | KEY2;  //缓冲区左移一位,并将当前扫描值移入最低位
  184.         keybuf[2] = (keybuf[2]<<1) | KEY3;  //缓冲区左移一位,并将当前扫描值移入最低位
  185.         keybuf[3] = (keybuf[3]<<1) | KEY4;  //缓冲区左移一位,并将当前扫描值移入最低位
  186.         for(i=0;i<4;i++)
  187.         {
  188.                 if (keybuf [i] == 0x00)
  189.                 {   //连续8次扫描值都为0,即16ms内都只检测到按下状态时,可认为按键已按下
  190.                                 KeySta[i] = 0;
  191.                 }
  192.                 else if (keybuf[i] == 0xFF)
  193.                 {   //连续8次扫描值都为1,即16ms内都只检测到弹起状态时,可认为按键已弹起
  194.                                 KeySta[i] = 1;
  195.                 }
  196.                 else
  197.                 {}  //其它情况则说明按键状态尚未稳定,则不对KeySta变量值进行更新
  198.         }
  199. }
  200. void StowatchCount()
  201. {
  202.         if(stopwatchRunning)
  203.         {
  204.                 DecimalPart++;
  205.                 if(DecimalPart>=100)
  206.                 {
  207.                         DecimalPart=0;
  208.                         IntegerPart++;
  209.                         if(IntegerPart>=100)  //  10000
  210.                         {
  211.                                 IntegerPart=0;
  212.                         }
  213.                 }
  214.                 stopwatchRefresh=1;
  215.         }
  216. }
  217. void InterruptTimer0() interrupt 1
  218. {
  219.         static unsigned char ter10ms = 0;
  220.         TH0 = T0RH;  //重新加载重载值
  221.         TL0 = T0RL;
  222.         LedScan();   //数码管扫描显示
  223.         KeyScan();   //按键扫描
  224.         ter10ms++;
  225.         if (ter10ms>=5)
  226.         {
  227.                 ter10ms = 0;
  228.                 StowatchCount();
  229.         }
  230. }
复制代码


仿真程序: 秒表记次+数码管+51+按键.7z (63.29 KB, 下载次数: 21)

评分

参与人数 1黑币 +50 收起 理由
admin + 50

查看全部评分

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

使用道具 举报

沙发
ID:1107746 发表于 2024-1-3 12:54 | 只看该作者
博主,有仿真图的完整原图吗?
回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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