找回密碼
 立即注冊(cè)

QQ登錄

只需一步,快速開始

搜索
查看: 1310|回復(fù): 5
打印 上一主題 下一主題
收起左側(cè)

有關(guān)單片機(jī)定時(shí)器中斷問題求助

[復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
樓主
ID:537770 發(fā)表于 2019-5-14 11:01 | 只看該作者 回帖獎(jiǎng)勵(lì) |倒序?yàn)g覽 |閱讀模式
寫了一個(gè)程序需要51單片機(jī)檢測(cè)I/O口的上升沿,然后打開定時(shí)器,最后將計(jì)時(shí)的結(jié)果顯示在LCD12864的程序,然后經(jīng)過除1000000的Ctime2一直為0。經(jīng)過測(cè)試,檢測(cè)I/O口,12864顯示,以及浮點(diǎn)數(shù)轉(zhuǎn)換為字符數(shù)組(直接給一個(gè)數(shù)賦值,可以正常轉(zhuǎn)換)的程序均可以正常運(yùn)行,現(xiàn)在就排查到應(yīng)該是定時(shí)器中斷的問題,然后用中間變量time2和time1顯示,發(fā)現(xiàn)結(jié)果均不超過65536,就想可能是中斷的問題,希望大神能看看

單片機(jī)源程序如下:
  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. //定時(shí)器123的值
  29. unsigned int time1;
  30. unsigned int time2;
  31. unsigned int time3;
  32. //溢出次數(shù)
  33. unsigned int overFlow1;
  34. unsigned int overFlow2;
  35. unsigned int overFlow3;
  36. //數(shù)據(jù)
  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顯示浮點(diǎn)數(shù)
  57. void float_to_str(char *str,double num);



  58. void main(void)
  59. {
  60.         //定時(shí)器初始化
  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.         //檢測(cè)
  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.         //檢測(cè)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. //檢測(cè)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. //轉(zhuǎn)換為秒
  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);//測(cè)試中間變量
  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. }
復(fù)制代碼



分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏 分享淘帖 頂 踩
回復(fù)

使用道具 舉報(bào)

沙發(fā)
ID:158375 發(fā)表于 2019-5-14 12:52 | 只看該作者
先聲明:沒看你程序。
TH+TL本來就不可能>65535.
你想怎樣使用定時(shí)器0和定時(shí)器1中斷呢?
回復(fù)

使用道具 舉報(bào)

板凳
ID:537770 發(fā)表于 2019-5-14 14:07 | 只看該作者
笨笨兔 發(fā)表于 2019-5-14 12:52
先聲明:沒看你程序。
TH+TL本來就不可能>65535.
你想怎樣使用定時(shí)器0和定時(shí)器1中斷呢?

我是TH左移8位+TL+溢出次數(shù)*65535得到的time,所以加起來不超過65535,就感覺應(yīng)該是溢出次數(shù)為0,也就是沒有進(jìn)入中斷
使用中斷主要就是為了讓溢出次數(shù)overFloew++,
然后最后根據(jù)上面式子計(jì)算脈沖個(gè)數(shù),除以1000000,得到單位為s的時(shí)間。謝謝
回復(fù)

使用道具 舉報(bào)

地板
ID:158375 發(fā)表于 2019-5-14 17:01 | 只看該作者
請(qǐng)仔細(xì)看51的定時(shí)器部分,溢出了,就中斷了;
你其實(shí)是想計(jì)時(shí),溢出次數(shù)x你對(duì)定時(shí)器的定時(shí)時(shí)間=總時(shí)間;
定時(shí)時(shí)間=65535-你的定時(shí)時(shí)間(根據(jù)晶振計(jì)算);
中斷中,溢出次數(shù)++,改為你的定時(shí)時(shí)間即可.
回復(fù)

使用道具 舉報(bào)

5#
ID:213173 發(fā)表于 2019-5-14 17:50 | 只看該作者
只是粗略的看了樓主的程序,基本斷定不可能正常運(yùn)行。問題也不在中斷,而是在主程序結(jié)構(gòu)缺陷。main程序中沒有主循環(huán),多處不恰當(dāng)使用while(1)死循環(huán),當(dāng)強(qiáng)制跳出后又進(jìn)入下一個(gè)死循環(huán),最終由P3.2的低電位跳出main程序。然后再次進(jìn)入main程序;再次初始化.......。
回復(fù)

使用道具 舉報(bào)

6#
ID:537770 發(fā)表于 2019-5-14 18:46 | 只看該作者
笨笨兔 發(fā)表于 2019-5-14 17:01
請(qǐng)仔細(xì)看51的定時(shí)器部分,溢出了,就中斷了;
你其實(shí)是想計(jì)時(shí),溢出次數(shù)x你對(duì)定時(shí)器的定時(shí)時(shí)間=總時(shí)間;
定時(shí) ...

之前為了省事,就沒這個(gè)做,現(xiàn)在重裝填為
TH0=(65535-1000)/256;
TL0=(65535-1000)%256;
就可以正確計(jì)時(shí)了...感謝!
回復(fù)

使用道具 舉報(bào)

本版積分規(guī)則

小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機(jī)教程網(wǎng)

快速回復(fù) 返回頂部 返回列表