找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 6363|回复: 7
收起左侧

51单片机秒表程序设计 有四个按键分别是计次,显示多次计次时间,复位,启停

  [复制链接]
ID:711823 发表于 2020-5-15 16:29 | 显示全部楼层 |阅读模式
功能:秒表精确到10ms,有四个按键分别是计次,显示多次计次时间,复位,启停。

仿真原理图如下(proteus仿真工程文件可到本帖附件中下载)
123.png

5.png

12.png

单片机源程序如下:
  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. bit kd = 0;
  17. bit lk = 1;
  18. bit stopwatchRefresh = 1;
  19. bit stopwatchRunning = 0;
  20. unsigned char DecimalPart = 0;
  21. unsigned int  IntegerPart = 0;

  22. void ConfigTimer0(unsigned int ms);
  23. void stopwatchDisplay();
  24. void KeyDriver();
  25. void main()
  26. {
  27.         EA = 1;
  28.         ConfigTimer0(2);
  29.         while(1)
  30.         {
  31.                 if(stopwatchRefresh)
  32.                 {
  33.                         stopwatchRefresh = 0;
  34.                         stopwatchDisplay();
  35.                 }
  36.                 KeyDriver();
  37.         }
  38. }
  39. void ConfigTimer0(unsigned int ms)
  40. {
  41.         unsigned long tmp;
  42.        
  43.         tmp = 11059200/12;
  44.         tmp = (tmp*ms)/1000;
  45.         tmp = 65536 - tmp;
  46.         tmp +=18;      //定时误差补偿
  47.        
  48.         T0RH = (unsigned char)(tmp>>8);
  49.         T0RL = (unsigned char)tmp;
  50.         TMOD &=0xf0;
  51.   TMOD |=0X01;
  52.         TH0 = T0RH;
  53.         TL0 = T0RL;
  54.         ET0 = 1;
  55.         TR0 = 1;
  56. }
  57. void stopwatchDisplay()
  58. {
  59.         signed char i;
  60.         unsigned char buf[4];
  61.         LedBuff[0] = LedChar[DecimalPart%10];
  62.         LedBuff[1] = LedChar[DecimalPart/10];
  63.         buf[0] = IntegerPart%10;
  64.         buf[1] = IntegerPart/10%10;
  65.         buf[2] = IntegerPart/100%10;
  66.         buf[3] = IntegerPart/1000%10;
  67.         for(i=3;i>=1;i--)
  68.         {
  69.                 if(buf[i]==0)
  70.                 {
  71.                         LedBuff[i+2]=0xFF;
  72.                 }else break;
  73.         }
  74.         for(;i>=0;i--)
  75.         {
  76.                 LedBuff[i+2] = LedChar[buf[i]];
  77.         }
  78.          LedBuff[2] &=0x7f;
  79. }
  80. void Stopwatchjc()
  81. {
  82.         signed char i;
  83.         unsigned char buf[4];
  84.         static unsigned char Dec[10] = 0;
  85.   static unsigned int  Int[10] = 0;
  86.         static char cnt = 0;
  87.         static char cnt1 = 0;
  88.         Dec[cnt] = DecimalPart;
  89.         Int[cnt] = IntegerPart;
  90.         cnt++;
  91.         if(kd == 1)
  92.         {
  93.                 LedBuff[0] = LedChar[Dec[cnt1]%10];
  94.                 LedBuff[1] = LedChar[Dec[cnt1]/10];
  95.                 buf[0] = Int[cnt1]%10;
  96.                 buf[1] = Int[cnt1]/10%10;
  97.                 buf[2] = Int[cnt1]/100%10;
  98.                 buf[3] = Int[cnt1]/1000%10;
  99.                 for(i=3;i>=1;i--)
  100.                 {
  101.                         if(buf[i]==0)
  102.                         {
  103.                                 LedBuff[i+2]=0xFF;
  104.                         }else break;
  105.                 }
  106.                 for(;i>=0;i--)
  107.                 {
  108.                         LedBuff[i+2] = LedChar[buf[i]];
  109.                 }
  110.                  LedBuff[2] &=0x7f;
  111.                
  112.                 cnt1++;
  113.                
  114.         }                       
  115.        
  116.        
  117. }
  118. void Stopwatchsy()
  119. {
  120.         stopwatchRunning = 0;
  121.         kd = 1;
  122.         stopwatchRefresh = 1;       
  123. }
  124. void StopwatchAction()
  125. {
  126.         stopwatchRunning = ~stopwatchRunning;
  127. }
  128. void StopwatchReset()
  129. {
  130.         stopwatchRunning = 0;
  131.         lk = 0;
  132.         DecimalPart = 0;
  133.         IntegerPart = 0;
  134.         stopwatchRefresh = 1;       
  135. }

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

所有资料51hei提供下载:
51秒表.rar (70.02 KB, 下载次数: 178)

评分

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

查看全部评分

回复

使用道具 举报

ID:749164 发表于 2020-5-17 19:42 | 显示全部楼层
仿真文件打不开呀 楼主你看看?
回复

使用道具 举报

ID:711823 发表于 2020-5-21 10:06 | 显示全部楼层
furuiabc 发表于 2020-5-17 19:42
仿真文件打不开呀 楼主你看看?

你是用的proteus 8.0吗
回复

使用道具 举报

ID:711823 发表于 2020-5-21 10:19 | 显示全部楼层
SAFASDF 发表于 2020-5-21 10:06
你是用的proteus 8.0吗

Proteus 8.0及以上版本生成的仿真电路图文件类型是Proteus Project(.pdsprj),是无法生成 .DSN 文件的
我传的是(.pdsprj)文件
回复

使用道具 举报

ID:362692 发表于 2020-5-21 10:59 来自手机 | 显示全部楼层
furuiabc 发表于 2020-5-17 19:42
仿真文件打不开呀 楼主你看看?

下载功能没了吗
回复

使用道具 举报

ID:446774 发表于 2020-6-10 09:58 | 显示全部楼层
你好楼主
回复

使用道具 举报

ID:316613 发表于 2020-11-4 18:02 | 显示全部楼层
大佬太牛逼了,解决了燃眉之急,也学习了代码看起来好复杂,没备注真不好看呢。但是大佬牛逼!
回复

使用道具 举报

ID:316613 发表于 2020-11-4 18:19 | 显示全部楼层
亲测有效!需要按下查询按键在按下记次按键就能看到记次的值了 5次是没问题的
回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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