找回密码
 立即注册

QQ登录

只需一步,快速开始

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

为什么单片机计时器的计时功能一直都不准,而且偏差很大?

[复制链接]
跳转到指定楼层
楼主


单片机源程序如下:
  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)  
  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)       //??1y1ms′óμíμ??????¢D?ò???êy??1ü
  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.                 {  
  187.                                 KeySta[ i] = 0;
  188.                 }
  189.                 else if (keybuf[ i] == 0xFF)
  190.                 {  
  191.                                 KeySta[ i] = 1;
  192.                 }
  193.                 else
  194.                 {}
  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. }
复制代码
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享淘帖 顶 踩
回复

使用道具 举报

沙发
ID:332444 发表于 2020-6-2 16:28 | 只看该作者
中断里面放置太多其他执行东西影响了。
回复

使用道具 举报

板凳
ID:123289 发表于 2020-6-2 21:40 | 只看该作者
你如何证明你有的晶振是:11.0592,而不是11.0510呢?
回复

使用道具 举报

地板
ID:332444 发表于 2020-6-4 08:39 | 只看该作者
可参我这个回复的例子中的中断使用,计时和其它操作莫放在中断里面。https://ask.csdn.net/questions/1078609
回复

使用道具 举报

5#
ID:332444 发表于 2020-6-4 18:45 | 只看该作者


回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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