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

QQ登錄

只需一步,快速開(kāi)始

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

KEIL RTX51_TINY仿真無(wú)法使用單片機(jī)T2定時(shí)器的解決方法

[復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
樓主
ID:57512 發(fā)表于 2021-12-21 11:38 | 只看該作者 |只看大圖 回帖獎(jiǎng)勵(lì) |倒序?yàn)g覽 |閱讀模式
最近做板子采用新唐N79E814,由于功能多而繁雜,我決定采用RTX51_TINY (2.02版)作為RTOS系統(tǒng)
由于仿真調(diào)試確實(shí)方便,所以沒(méi)有下載到板子上觀察,使用T1定時(shí)器時(shí),仿真沒(méi)有問(wèn)題(T0被RTOS使用)
當(dāng)我使用定時(shí)器2時(shí),發(fā)現(xiàn)定時(shí)器2的中斷,無(wú)論怎么都進(jìn)不去,接下來(lái)花了一天排查各種可能的人為錯(cuò)誤
經(jīng)過(guò)排查,我堅(jiān)信,除了給老婆認(rèn)錯(cuò)之外(調(diào)代碼把老婆丟一邊),我是不可能向代碼認(rèn)錯(cuò)的
發(fā)現(xiàn)使用KEIL仿真邏輯分析儀波形顯示如下

定時(shí)器T2的EIE寄存器的ET2(開(kāi)T2中斷)這個(gè)位,置1了,但是邏輯分析儀顯示為0,所以,應(yīng)該是不支持T2的仿真好吧,下載到板子上試一下~~單片機(jī)代碼如下:
  1. #include <stdio.h>
  2. #include <rtx51tny.h>
  3. #include "N79E81x.h"
  4. #include "Typedef.h"
  5. #include "Define.h"
  6. #include "LOGIC.h"

  7. /*重要說(shuō)明**本程序使用N79E814A內(nèi)部11.0592M*/
  8. /*重要說(shuō)明**本程序使用N79E814A內(nèi)部11.0592M*/
  9. //Conf_tny.A51中INT_CLOCK        設(shè)置為        9216 正好是 100HZ滴答 時(shí)間片是10ms
  10. //Conf_tny.A51中TIMESHARING 設(shè)置為0時(shí),輪換執(zhí)行各任務(wù)
  11. //Conf_tny.A51中TIMESHARING 設(shè)置為1時(shí),每個(gè)任務(wù)分得10ms
  12. //Conf_tny.A51中TIMESHARING 設(shè)置為2時(shí),每個(gè)任務(wù)分得20ms
  13. /*因?yàn)槲易约菏褂眯盘?hào)來(lái)驅(qū)動(dòng)事件,所以TIMESHARING我設(shè)置為0*/
  14. /**********************************************************************************/
  15. /*                    Struct init & I/O init:                                     */
  16. /**********************************************************************************/
  17. struct time ctime = { 12,  0,  0 };        /* storage for clock values            */

  18. /***********************************************************************************/
  19. /*                   Task 0 'init': Initialize                                     */
  20. /***********************************************************************************/
  21. void init (void) _task_ INIT  {            /* program execution starts here        */
  22.   ET2 = 1;                                 //開(kāi)T2中斷使能,EA受到RTOS控制,不用寫(xiě)EA = 1
  23.   os_create_task (CLOCK);                  /* create clock  1  task                */
  24.   os_create_task (KEYREAD);                /* create key    2  task                */
  25.   //os_create_task (BUZZOUT);              /* create key    2  task                */
  26.         os_delete_task (INIT);                   /* del init task (no longer needed)     */
  27. }


  28. /***********************************************************************************/
  29. /*                   Task 1 'clock'  @1HZ                                          */
  30. /***********************************************************************************/
  31. void clock (void)  _task_ CLOCK  {
  32.   while (1)  {                             /*  loop          */
  33.     if (++ctime.sec == 60)  {              /* calculate the second                 */
  34.       ctime.sec = 0;
  35.       if (++ctime.min == 60)  {            /* calculate the minute                 */
  36.         ctime.min = 0;
  37.         if (++ctime.hour == 24)  {         /* calculate the hour                   */
  38.           ctime.hour = 0;
  39.         }
  40.       }
  41.     }
  42.     os_wait (K_IVL, 100, 0);               /* wait interval:  1 second             */
  43.     P06 = ~P06;                            //每秒反轉(zhuǎn)一次
  44.     TF2 = 1;                               //每秒置位一次T2中斷標(biāo)志,進(jìn)入T2中斷
  45.   }
  46. }

  47. /***********************************************************************************/
  48. /*          Task 2 'keyread': process key   from  push button or EC12              */
  49. /***********************************************************************************/
  50. struct PressedReg PressedReg = {0,0,0,0,0};                    /*  按鍵被按下的次數(shù)  */
  51. void keyread (void) _task_ KEYREAD  {
  52.   UINT8 Count;
  53.   BIT   Keys_in;
  54.   while (1)  {                                             /* loop         */
  55.   Count = 0;                                            /*  10 ms interrupt count  */
  56.   do
  57.   {
  58.     os_wait(K_TMO, 1, 0);                               //os_wait()用于把CPU交給其它任務(wù)使用
  59.   } while (!Any_KeyPressed);
  60.   os_wait(K_TMO,1,0);
  61.   do
  62.   {
  63.     if(Any_KeyPressed&&Keys_in == UnLock)
  64.     {
  65.       Count = 0;
  66.       Keys_in = Locked;
  67.       //os_send_signal (BUZZOUT);                /* signal to task BUZZOUT: Any_KeyPressed */
  68.       os_wait (K_TMO, 1, 0);                                     /*  10ms 消抖  */
  69.       if(key1_pressed)
  70.       {
  71.         PressedReg.key1 ++;
  72.       }
  73.       if (key2_pressed)
  74.       {
  75.         PressedReg.key2 ++;
  76.       }
  77.       if (key3_pressed)
  78.       {
  79.         PressedReg.key3 ++;
  80.       }
  81.       if (EC12_key_pressed)
  82.       {
  83.         PressedReg.EC12_key ++;
  84.       }

  85.     }
  86.     else
  87.     {
  88.       os_wait (K_TMO, 1, 0);
  89.       Count ++;
  90.     }
  91.     if (All_KeyIdle)
  92.     {
  93.       Keys_in = UnLock;
  94.       Count ++;
  95.       os_wait (K_TMO, 1, 0);
  96.     }
  97.                                              //你可以更改 100為150,表示1500ms內(nèi)檢測(cè)按下次數(shù)
  98.   } while (Count<=100);                      //檢測(cè)1000ms內(nèi)按下多少次,可以響應(yīng)單擊,雙擊,三擊,四擊,五擊。。。等

  99.   if(key1_pressed&&PressedReg.key1==SingleClick)   /* 單擊且1秒后還沒(méi)放手就是長(zhǎng)按*/
  100.   {
  101.     PressedReg.key1 = LongPressed;
  102.     //os_send_signal (BUZZOUT);         
  103.   }
  104.   if (key2_pressed&&PressedReg.key2==SingleClick)
  105.   {
  106.     PressedReg.key2 = LongPressed;
  107.     //os_send_signal (BUZZOUT);      
  108.   }
  109.   if (key3_pressed&&PressedReg.key3==SingleClick)
  110.   {
  111.     PressedReg.key3 = LongPressed;
  112.     //os_send_signal (BUZZOUT);         
  113.   }
  114.   if (EC12_key_pressed&&PressedReg.EC12_key==SingleClick)
  115.   {
  116.     PressedReg.EC12_key = LongPressed;
  117.     //os_send_signal (BUZZOUT);                /* signal to task BUZZOUT: Any_KeylongPressed 按下1秒又響了就是長(zhǎng)按 */
  118.   }

  119.   if(
  120.     PressedReg.key1 != 0 ||  \
  121.     PressedReg.key2 != 0 ||  \
  122.     PressedReg.key3 != 0 ||  \
  123.     PressedReg.EC12_key != 0 \
  124.   )
  125.   {
  126.     //如果按鍵單擊,雙擊,三擊,四擊,長(zhǎng)按,執(zhí)行此處代碼,或者有按鍵動(dòng)作,就發(fā)送信號(hào)給your_TASK
  127.     //os_send_signal (your_TASK);                /* signal to task Logic: Any_KeyPressed */
  128.   }

  129.     PressedReg.key1 = 0;
  130.     PressedReg.key2 = 0;
  131.     PressedReg.key3 = 0;
  132.     PressedReg.EC12_key = 0;
  133.   }
  134.   
  135. }

  136. /***********************************************************************************/
  137. /*          T2  中斷函數(shù)            */
  138. /***********************************************************************************/
  139. void Timer2_ISR(void) interrupt 5        // Vecotr @  0x2B
  140. {
  141.   P07 = ~P07;           //反轉(zhuǎn)LED,使得LED閃爍
  142.   TF2  = 0;             //清除T2中斷標(biāo)志
  143. }

  144. /***********************************************************************************/
  145. /*          Task 4 'buzzout'蜂鳴器: process buzz from  push button or warning            */
  146. /***********************************************************************************/
  147. void buzzout (void) _task_ BUZZOUT  {
  148.   while (1)  {                                     /* endless loop                 */
  149.     os_wait(K_SIG,0,0);                            /* 等待按鍵任務(wù)發(fā)來(lái)信號(hào)*/
  150.     /*  your code  PWM開(kāi)      */
  151.     //os_wait(K_TMO,10,0);                          //讓PWM開(kāi)100ms,os_wait()不會(huì)像delay()函數(shù)死死的占用CPU時(shí)間,
  152.     /*  your code  PWM關(guān)      */

  153.     os_switch_task();                              /* 把時(shí)間交給其它任務(wù)   */
  154.   
  155.   }
  156. }
復(fù)制代碼

結(jié)果就是,仿真不行,但是實(shí)際燒錄運(yùn)行后,能進(jìn)入Timer 2 中斷:P06每秒反轉(zhuǎn)表示正在運(yùn)行,P07發(fā)生翻轉(zhuǎn)表明進(jìn)入了中斷
本試驗(yàn)只用到了task CLOCK和中斷,按鍵部分的代碼只是分享出來(lái),萬(wàn)一你用得著呢~~~
完整代碼請(qǐng)下載 N79E814.zip (79.13 KB, 下載次數(shù): 12)

評(píng)分

參與人數(shù) 1黑幣 +100 收起 理由
admin + 100 共享資料的黑幣獎(jiǎng)勵(lì)!

查看全部評(píng)分

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

使用道具 舉報(bào)

本版積分規(guī)則

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

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

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