找回密码
 立即注册

QQ登录

只需一步,快速开始

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

有关单片机定时器中断问题求助

[复制链接]
跳转到指定楼层
楼主
ID:537770 发表于 2019-5-14 11:01 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
写了一个程序需要51单片机检测I/O口的上升沿,然后打开定时器,最后将计时的结果显示在LCD12864的程序,然后经过除1000000的Ctime2一直为0。经过测试,检测I/O口,12864显示,以及浮点数转换为字符数组(直接给一个数赋值,可以正常转换)的程序均可以正常运行,现在就排查到应该是定时器中断的问题,然后用中间变量time2和time1显示,发现结果均不超过65536,就想可能是中断的问题,希望大神能看看

单片机源程序如下:
  1. #include<reg52.h>
  2. #include<math.h>
  3. #include <string.h>
  4. #include<stdlib.h>
  5. #include<intrins.h>
  6. #include<stdio.h>
  7. #define uchar unsigned char
  8. #define uint unsigned int
  9. //输入I/O口
  10. sbit input1=P1^0
  11. sbit input2=P1^1
  12. sbit input3=P1^2
  13. sbit input4=P1^3
  14. sbit test1=P1^4;
  15. sbit res=P3^2;//S4
  16. //之前电平
  17. bit oldBit1;
  18. bit oldBit2;
  19. bit oldBit3;
  20. bit oldBit4;
  21. bit newBit1;
  22. bit newBit2;
  23. bit newBit3;
  24. bit newBit4;
  25. //偏左,偏右
  26. bit bias12;
  27. bit bias34;
  28. //定时器123的值
  29. unsigned int time1;
  30. unsigned int time2;
  31. unsigned int time3;
  32. //溢出次数
  33. unsigned int overFlow1;
  34. unsigned int overFlow2;
  35. unsigned int overFlow3;
  36. //数据
  37. float velocity=0;
  38. float enterAngle=0;
  39. float exitAngle=0;
  40. unsigned int length=8;
  41. float width=5.2;
  42. float Ctime1=0;
  43. float Ctime2=0;
  44. float Ctime3=0;
  45. float c=1000000;
  46. unsigned char idata enterAngle1[6];
  47. unsigned char idata exitAngle1[6];
  48. unsigned char idata velocity1[6];
  49. unsigned char idata testT[6];
  50. //声明
  51. void initTimer(void);
  52. void Delay(uint MS);
  53. void LcmInit(void);
  54. void LcmClearTXT(void);
  55. void PutStr(uchar row,uchar col,uchar*puts);//LCD显示字符串
  56. void PutFloat(uchar row,uchar col,uchar*puts);//LCD显示浮点数
  57. void float_to_str(char *str,double num);



  58. void main(void)
  59. {
  60.         //定时器初始化
  61.         initTimer();
  62.         EA=1;
  63.         ET0=1;
  64.         ET1=1;
  65.         input1=1;
  66.         input2=1;
  67.         input3=1;
  68.         input4=1;
  69.         overFlow1=0;
  70.         overFlow2=0;
  71.         overFlow3=0;
  72.         //检测
  73.         oldBit1=input1;
  74.         oldBit2=input2;
  75.         oldBit3=input3;
  76.         oldBit4=input4;
  77.         test1=0;
  78.         //LCD
  79.         Delay(100);
  80.         LcmInit();
  81.         LcmClearTXT();
  82.         /*
  83.         PutStr(0,0,"èë½Ç£o");
  84.         PutStr(1,0,"3ö½Ç£o");
  85.         PutStr(2,0,"Ëù¶è£o");
  86.         PutStr(0,7,"¡ã");
  87.         PutStr(1,7,"¡ã");
  88.         PutStr(2,6,"cm/s");
  89.         */
  90.         //检测1
  91. while(1)
  92.         {
  93.                 newBit1=input1;
  94.                 newBit2=input2;
  95.                 if((newBit1==1)&&(oldBit1==0))//左偏
  96.                 {
  97.                         Delay(1);
  98.                         TR0=1;
  99.                         Delay(1);
  100.                         TR1=1;
  101.                         bias12=1;
  102.                         break;
  103.                 }
  104.                 if((newBit2==1)&&(oldBit2==0))//右偏
  105.                         {
  106.                                 Delay(1);
  107.                                 TR0=1;
  108.                                 Delay(1);
  109.                                 TR1=1;
  110.                                 bias12=0;
  111.                                 break;
  112.                         }
  113.                 oldBit1=newBit1;
  114.                 oldBit2=newBit2;
  115.         }

  116. if(bias12==1)
  117.         {
  118.                 while(1)
  119.                 {
  120.                         newBit2=input2;
  121.                         if((newBit2==1)&&(oldBit2==0))
  122.                         {
  123.                                 Delay(1);
  124.                                 TR0=0;
  125.                                 break;
  126.                         }
  127.                         oldBit2=newBit2;
  128.                 }
  129.         }
  130.         
  131. if(bias12==0)
  132.         {
  133.                 while(1)
  134.                 {
  135.                         newBit1=input1;
  136.                         if((newBit1==1)&&(oldBit1==0))
  137.                         {
  138.                                 Delay(1);
  139.                                 TR0=0;
  140.                                 break;
  141.                         }
  142.                         oldBit1=newBit1;
  143.                 }
  144.         }
  145. //检测2
  146. time1=(TH0<<8)+TL0+(overFlow1<<16);
  147. overFlow3=0;
  148. TH0=0;
  149. TL0=0;        
  150. while(1)
  151.         {
  152.                 newBit3=input3;
  153.                 newBit4=input4;
  154.                 if((newBit3==1)&&(oldBit3==0))//右偏
  155.                 {
  156.                         Delay(1);
  157.                         TR0=1;
  158.                         Delay(1);
  159.                         TR1=0;
  160.                         bias34=1;
  161.                         break;
  162.                 }                          //óòÆ«
  163.                 if((newBit4==1)&&(oldBit4==0))
  164.                         {
  165.                                 Delay(1);
  166.                                 TR0=1;
  167.                                 Delay(1);
  168.                                 TR1=0;
  169.                                 bias34=0;
  170.                                 break;
  171.                         }
  172.                 oldBit3=newBit3;
  173.                 oldBit4=newBit4;
  174.         }
  175.         
  176. time2=20+(TH1*256)+TL1+overFlow2*(65536);
  177.         
  178. if(bias34==1)
  179.         {
  180.                 while(1)
  181.                 {
  182.                         newBit4=input4;
  183.                         if((newBit4==1)&&(oldBit4==0))
  184.                         {
  185.                                 Delay(1);
  186.                                 TR0=0;
  187.                                 break;
  188.                         }
  189.                         oldBit4=newBit4;
  190.                 }
  191.         }
  192.         
  193. if(bias34==0)
  194.         {
  195.                 while(1)
  196.                 {
  197.                         newBit3=input3;
  198.                         if((newBit3==1)&&(oldBit3==0))
  199.                         {
  200.                                 Delay(1);
  201.                                 TR0=0;
  202.                                 break;
  203.                         }
  204.                         oldBit3=newBit3;
  205.                 }
  206.         }
  207.         time3=TH0<<8+TL0+overFlow3<<16;
  208. //转换为秒
  209.         Ctime1=time1/c;//c为1000000
  210.         Ctime2=time2/c;
  211.         Ctime3=time3/c;
  212. /*        velocity=length/Ctime2;
  213.         enterAngle=atan((length*Ctime1)/(Ctime2*width));
  214.         exitAngle=atan((length*Ctime3)/(Ctime2*width));//3¤¶èμ¥λcm,ê±¼äμ¥λs
  215.         */
  216.         //LCD2
  217.         if(bias12==0)//óòÆ«
  218.         {
  219.                 PutStr(3,0,"èë¸ËóòÆ«");
  220.         }
  221.         else if(bias12==1)//×óÆ«
  222.         {
  223.                 PutStr(3,0,"èë¸Ë×óÆ«");
  224.         }
  225.         
  226.         if(bias34==0)//óòÆ«
  227.         {
  228.                 PutStr(3,4,"3ö¸ËóòÆ«");
  229.         }
  230.         else if(bias34==1)//×óÆ«
  231.         {
  232.                 PutStr(3,4,"3ö¸Ë×óÆ«");
  233.         }
  234. //×a»»
  235. /* float_to_str(enterAngle1,enterAngle);
  236.         float_to_str(exitAngle1,exitAngle);
  237.         float_to_str(velocity1,velocity);
  238.   PutStr(0,0,enterAngle1);
  239.         PutStr(1,0,exitAngle1);
  240.         PutStr(2,0,velocity1); */
  241.         float_to_str(testT,Ctime2);//测试中间变量
  242.         PutStr(0,0,testT);
  243.         while(res==1);
  244. }

  245. //oˉêy
  246. void initTimer(void)
  247.         {
  248.                 TMOD=0x11;
  249.                 TH0=0;
  250.                 TL0=0;
  251.                 TH1=0;
  252.                 TL1=20;
  253.         }
  254. //ÖD¶Ï
  255. void Timer0Int() interrupt 1
  256. {
  257.         overFlow1++;
  258.         overFlow3++;
  259.         TH0=0;
  260.         TL0=0;
  261. }
  262. void Timer1Int() interrupt 3
  263. {
  264.         overFlow2++;
  265.         TH1=0;
  266.         TL1=0;
  267. }
复制代码



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

使用道具 举报

沙发
ID:158375 发表于 2019-5-14 12:52 | 只看该作者
先声明:没看你程序。
TH+TL本来就不可能>65535.
你想怎样使用定时器0和定时器1中断呢?
回复

使用道具 举报

板凳
ID:537770 发表于 2019-5-14 14:07 | 只看该作者
笨笨兔 发表于 2019-5-14 12:52
先声明:没看你程序。
TH+TL本来就不可能>65535.
你想怎样使用定时器0和定时器1中断呢?

我是TH左移8位+TL+溢出次数*65535得到的time,所以加起来不超过65535,就感觉应该是溢出次数为0,也就是没有进入中断
使用中断主要就是为了让溢出次数overFloew++,
然后最后根据上面式子计算脉冲个数,除以1000000,得到单位为s的时间。谢谢
回复

使用道具 举报

地板
ID:158375 发表于 2019-5-14 17:01 | 只看该作者
请仔细看51的定时器部分,溢出了,就中断了;
你其实是想计时,溢出次数x你对定时器的定时时间=总时间;
定时时间=65535-你的定时时间(根据晶振计算);
中断中,溢出次数++,改为你的定时时间即可.
回复

使用道具 举报

5#
ID:213173 发表于 2019-5-14 17:50 | 只看该作者
只是粗略的看了楼主的程序,基本断定不可能正常运行。问题也不在中断,而是在主程序结构缺陷。main程序中没有主循环,多处不恰当使用while(1)死循环,当强制跳出后又进入下一个死循环,最终由P3.2的低电位跳出main程序。然后再次进入main程序;再次初始化.......。
回复

使用道具 举报

6#
ID:537770 发表于 2019-5-14 18:46 | 只看该作者
笨笨兔 发表于 2019-5-14 17:01
请仔细看51的定时器部分,溢出了,就中断了;
你其实是想计时,溢出次数x你对定时器的定时时间=总时间;
定时 ...

之前为了省事,就没这个做,现在重装填为
TH0=(65535-1000)/256;
TL0=(65535-1000)%256;
就可以正确计时了...感谢!
回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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