久久久久久久999_99精品久久精品一区二区爱城_成人欧美一区二区三区在线播放_国产精品日本一区二区不卡视频_国产午夜视频_欧美精品在线观看免费

 找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索
查看: 1324|回復: 5
打印 上一主題 下一主題
收起左側

有關單片機定時器中斷問題求助

[復制鏈接]
跳轉到指定樓層
樓主
ID:537770 發(fā)表于 2019-5-14 11:01 | 只看該作者 回帖獎勵 |倒序瀏覽 |閱讀模式
寫了一個程序需要51單片機檢測I/O口的上升沿,然后打開定時器,最后將計時的結果顯示在LCD12864的程序,然后經(jīng)過除1000000的Ctime2一直為0。經(jīng)過測試,檢測I/O口,12864顯示,以及浮點數(shù)轉換為字符數(shù)組(直接給一個數(shù)賦值,可以正常轉換)的程序均可以正常運行,現(xiàn)在就排查到應該是定時器中斷的問題,然后用中間變量time2和time1顯示,發(fā)現(xiàn)結果均不超過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. //溢出次數(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顯示浮點數(shù)
  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空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏 分享淘帖 頂 踩
回復

使用道具 舉報

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

使用道具 舉報

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

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

使用道具 舉報

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

使用道具 舉報

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

使用道具 舉報

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

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

使用道具 舉報

您需要登錄后才可以回帖 登錄 | 立即注冊

本版積分規(guī)則

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

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

快速回復 返回頂部 返回列表
主站蜘蛛池模板: 精品在线99 | 久草热在线 | 亚洲乱码一区二区三区在线观看 | 一区二区精品在线 | 精品国产一区二区国模嫣然 | 男人天堂手机在线视频 | 日本在线免费观看 | 精品福利在线 | 日韩毛片网 | 亚洲一区中文字幕在线观看 | 欧美二级 | 色婷婷综合网站 | 久久久久久看片 | 狠狠躁夜夜躁人人爽天天高潮 | 亚洲精品一区二区三区在线观看 | 欧美精品网站 | 欧美久久久久久久久中文字幕 | 久久国产精品-国产精品 | www.免费看片.com | 日韩久久久久久久久久久 | 99精品国产一区二区三区 | 国产精品欧美一区二区三区不卡 | 999精品视频在线观看 | 久久久女女女女999久久 | 欧美国产精品一区二区三区 | 黄色一级毛片 | 国产精品区一区二区三区 | 欧美成人免费在线视频 | 免费视频一区二区 | 日韩欧美不卡 | 久久精品一区二区三区四区 | 国产毛片毛片 | www.亚洲国产精品 | 99视频网| 91伊人 | 成人免费看电影 | 日本久草 | 日本三级电影在线观看视频 | 午夜在线视频 | 午夜av电影 | 日韩 欧美 综合 |