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

QQ登錄

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

搜索
查看: 2674|回復(fù): 4
收起左側(cè)

說(shuō)說(shuō)KEIL自帶RTX-51單片機(jī)操作系統(tǒng)例程 Traffic

[復(fù)制鏈接]
ID:140644 發(fā)表于 2022-3-15 06:09 | 顯示全部樓層 |閱讀模式
51單片機(jī)的容量越來(lái)越大,感覺(jué)RTX-51在小型工程上還是比較適用的,
特別是現(xiàn)在STC新出了51版32位芯片,內(nèi)部FLASH更大了,
懂了RTX-51再進(jìn)一步學(xué)習(xí)FreeRTOS就有基礎(chǔ)了。
但是好像網(wǎng)上很少有RTX-51例程和交流的。
無(wú)意發(fā)現(xiàn)一個(gè)KEIL 自帶RTX-51操作系統(tǒng)例程。
感覺(jué)可以試試。
編譯結(jié)果顯示
Build target 'Demo - Simulator'
compiling TRAFFIC.C...
compiling SERIAL.C...
compiling GETLINE.C...
assembling Conf_tny.A51...
assembling START900.A51...
linking...
Program Size: data=124.1 xdata=0 code=5469
creating hex file from "traffic"...
"traffic" - 0 Error(s), 0 Warning(s).

那么問(wèn)題來(lái)了,編譯的代碼長(zhǎng)度   Size: data=124.1 xdata=0 code=5469
打開(kāi)STC的ISP下載軟件,說(shuō)文件大小超出程序區(qū)范圍,超出部分自動(dòng)移到EEPROM區(qū)。
是什么意思呢?

我把代碼燒寫(xiě)到STC芯片,板子可以運(yùn)行,有興趣的一起聊聊唄

compling.png
單片機(jī)源程序如下:
  1. /******************************************************************************/
  2. /*                                                                            */
  3. /*   TRAFFIC.C:  Traffic Light Controller using the C-51 COMPILER             */
  4. /*                                                                            */
  5. /******************************************************************************/

  6. char code menu[] =
  7.    "\n"
  8.    "+***** TRAFFIC LIGHT CONTROLLER using C51 and RTX-51 tiny *****+\n"
  9.    "| This program is a simple Traffic Light Controller.  Between  |\n"
  10.    "| start time and end time the system controls a traffic light  |\n"
  11.    "| with pedestrian self-service.  Outside of this time range    |\n"
  12.    "| the yellow caution lamp is blinking.                         |\n"
  13.    "+ command -+ syntax -----+ function ---------------------------+\n"
  14.    "| Display  | D           | display times                       |\n"
  15.    "| Time     | T hh:mm:ss  | set clock time                      |\n"
  16.    "| Start    | S hh:mm:ss  | set start time                      |\n"
  17.    "| End      | E hh:mm:ss  | set end time                        |\n"
  18.    "+----------+-------------+-------------------------------------+\n";

  19. #include <REG52.H>                   /* special function registers 8052      */
  20. #include <rtx51tny.h>                 /* RTX-51 tiny functions & defines      */
  21. #include <stdio.h>                    /* standard I/O .h-file                 */
  22. #include <ctype.h>                    /* character functions                  */
  23. #include <string.h>                   /* string and memory functions          */

  24. #include "traffic.h"                  /* project specific header file         */

  25. struct time ctime = { 12,  0,  0 };   /* storage for clock time values        */
  26. struct time start = {  7, 30,  0 };   /* storage for start time values        */
  27. struct time end   = { 18, 30,  0 };   /* storage for end   time values        */

  28. sbit  red    = P2^6;                  /* I/O Pin:  red    lamp output         */
  29. sbit  yellow = P2^7;                  /* I/O Pin:  yellow lamp output         */
  30. sbit  green  = P1^0;                  /* I/O Pin:  green  lamp output         */
  31. sbit  stop   = P1^1;                  /* I/O Pin:  stop   lamp output         */
  32. sbit  walk   = P1^2;                  /* I/O Pin:  walk   lamp output         */
  33. sbit  key    = P3^2;                  /* I/O Pin:  self-service key input     */

  34. char idata inline[16];                /* storage for command input line       */


  35. /******************************************************************************/
  36. /*        Task 0 'init': Initialize                                           */
  37. /******************************************************************************/
  38. void init (void) _task_ INIT  {       /* program execution starts here        */
  39.   serial_init ();                     /* initialize the serial interface      */
  40.                                       /* configure I/O port                   */
  41.   P2M1 = 0x1F;                        /* P2.0-P.2.4 output, P2.5-P2.7 input   */

  42.   os_create_task (CLOCK);             /* start clock task                     */
  43.   os_create_task (COMMAND);           /* start command task                   */
  44.   os_create_task (LIGHTS);            /* start lights task                    */
  45.   os_create_task (KEYREAD);           /* start keyread task                   */
  46.   os_delete_task (INIT);              /* stop init task (no longer needed)    */
  47. }


  48. bit display_time = 0;                 /* flag:  signal cmd state display_time */

  49. /******************************************************************************/
  50. /*        Task 2 'clock'                                                      */
  51. /******************************************************************************/
  52. void clock (void)  _task_ CLOCK  {
  53.   while (1)  {                        /* clock is an endless loop             */
  54.     if (++ctime.sec == 60)  {         /* calculate the second                 */
  55.       ctime.sec = 0;
  56.       if (++ctime.min == 60)  {       /* calculate the minute                 */
  57.         ctime.min = 0;
  58.         if (++ctime.hour == 24)  {    /* calculate the hour                   */
  59.           ctime.hour = 0;
  60.         }
  61.       }
  62.     }
  63.     if (display_time)  {              /* if command_status == display_time    */
  64.       os_send_signal (COMMAND);       /* signal to task command: time changed */
  65.     }
  66.     os_wait (K_IVL, 100, 0);          /* wait interval:  1 second             */
  67.   }
  68. }


  69. struct time rtime;                    /* temporary storage for entry time     */

  70. /******************************************************************************/
  71. /*        readtime: convert line input to time values & store in rtime        */
  72. /******************************************************************************/
  73. bit readtime (char idata *buffer)  {
  74.   unsigned char args;                          /* number of arguments         */

  75.   rtime.sec = 0;                               /* preset second               */
  76.   args = sscanf (buffer, "%bd:%bd:%bd",        /* scan input line for         */
  77.                  &rtime.hour,                  /* hour, minute and second     */
  78.                  &rtime.min,
  79.                  &rtime.sec);
  80.   
  81.   if (rtime.hour > 23  ||  rtime.min > 59  ||  /* check for valid inputs      */
  82.       rtime.sec > 59   ||  args < 2        ||  args == EOF)  {
  83.     printf ("\n*** ERROR: INVALID TIME FORMAT\n");
  84.     return (0);
  85.   }
  86.   return (1);
  87. }



  88. #define ESC  0x1B                     /* ESCAPE character code                */

  89. static bit   escape;                  /* flag: mark ESCAPE character entered  */

  90. /******************************************************************************/
  91. /*        Task 6 'get_escape': check if ESC (escape character) was entered    */
  92. /******************************************************************************/
  93. void get_escape (void) _task_ GET_ESC  {
  94.   while (1)  {                                 /* endless loop                */
  95.     if (_getkey () == ESC)  escape = 1;        /* set flag if ESC entered     */
  96.     if (escape)  {                             /* if escape flag send signal  */
  97.       os_send_signal (COMMAND);                /* to task 'command'           */
  98.     }
  99.   }
  100. }


  101. /******************************************************************************/
  102. /*        Task 1 'command': command processor */
  103. /******************************************************************************/
  104. void command (void) _task_ COMMAND  {
  105.   unsigned char i;

  106.   printf (menu);                               /* display command menu        */
  107.   while (1)  {                                 /* endless loop                */
  108.     printf ("\nCommand: ");                    /* display prompt              */
  109.     getline (&inline, sizeof (inline));        /* get command line input      */

  110.     for (i = 0; inline[i] != 0; i++)  {        /* convert to uppercase        */
  111.       inline[i] = toupper(inline[i]);
  112.     }

  113.     for (i = 0; inline[i] == ' '; i++);        /* skip blanks                 */

  114.     switch (inline[i])  {                      /* proceed to command function */
  115.       case 'D':                                /* Display Time Command        */
  116.         printf ("Start Time: %02bd:%02bd:%02bd    "
  117.                 "End Time: %02bd:%02bd:%02bd\n",
  118.                  start.hour, start.min, start.sec,
  119.                  end.hour,   end.min,   end.sec);
  120.         printf ("                        type ESC to abort\r");

  121.         os_create_task (GET_ESC);              /* ESC check in display loop   */
  122.         escape = 0;                            /* clear escape flag           */
  123.         display_time = 1;                      /* set display time flag       */
  124.         os_clear_signal (COMMAND);             /* clear pending signals       */

  125.         while (!escape)  {                     /* while no ESC entered        */
  126.           printf ("Clock Time: %02bd:%02bd:%02bd\r",      /* display time     */
  127.                    ctime.hour, ctime.min, ctime.sec);
  128.           os_wait (K_SIG, 0, 0);               /* wait for time change or ESC */
  129.         }

  130.         os_delete_task (GET_ESC);              /* ESC check not longer needed */
  131.         display_time = 0;                      /* clear display time flag     */
  132.         printf ("\n\n");
  133.         break;

  134.       case 'T':                                /* Set Time Command            */
  135.         if (readtime (&inline[i+1]))  {        /* read time input and         */
  136.           ctime.hour = rtime.hour;             /* store in 'ctime'            */
  137.           ctime.min  = rtime.min;
  138.           ctime.sec  = rtime.sec;
  139.         }
  140.         break;

  141.       case 'E':                                /* Set End Time Command        */
  142.         if (readtime (&inline[i+1]))  {        /* read time input and         */
  143.           end.hour = rtime.hour;               /* store in 'end'              */
  144.           end.min  = rtime.min;
  145.           end.sec  = rtime.sec;
  146.         }
  147.         break;

  148.       case 'S':                                /* Set Start Time Command */
  149.         if (readtime (&inline[i+1]))  {        /* read time input and         */
  150.           start.hour = rtime.hour;             /* store in 'start'            */
  151.           start.min  = rtime.min;
  152.           start.sec  = rtime.sec;
  153.         }
  154.         break;

  155.       default:                                 /* Error Handling              */
  156.         printf (menu);                         /* display command menu        */
  157.         break;
  158.     }   
  159.   }
  160. }


  161. /******************************************************************************/
  162. /*        signalon: check if clock time is between start and end              */
  163. /******************************************************************************/
  164. static bit signalon (void)   {
  165.   if (memcmp (&start, &end, sizeof (struct time)) < 0)  {
  166.     if (memcmp (&start, &ctime, sizeof (struct time)) < 0  &&
  167.         memcmp (&ctime, &end,   sizeof (struct time)) < 0)  return (1);
  168.   }
  169.                                                 
  170.   else  {
  171.     if (memcmp (&end,   &ctime, sizeof (start)) > 0  &&
  172.         memcmp (&ctime, &start, sizeof (start)) > 0)  return (1);
  173.   }
  174.   return (0);                                  /* signal off, blinking on     */
  175. }


  176. /******************************************************************************/
  177. /*        Task 3 'blinking': runs if current time is outside start & end time */
  178. /******************************************************************************/
  179. void blinking (void) _task_ BLINKING  {        /* blink yellow light          */
  180.   red    = 0;                                  /* all lights off              */
  181.   yellow = 0;
  182.   green  = 0;
  183.   stop   = 0;
  184.   walk   = 0;

  185.   while (1)  {                                 /* endless loop                */
  186.     yellow = 1;                                /* yellow light on             */
  187.     os_wait (K_TMO, 150, 0);                   /* wait for timeout: 150 ticks  */
  188.     yellow = 0;                                /* yellow light off            */
  189.     os_wait (K_TMO, 150, 0);                   /* wait for timeout: 150 ticks  */
  190.     if (signalon ())  {                        /* if blinking time over       */
  191.       os_create_task (LIGHTS);                 /* start lights                */
  192.       os_delete_task (BLINKING);               /* and stop blinking           */
  193.     }
  194.   }
  195. }


  196. /******************************************************************************/
  197. /*      Task 4 'lights': executes if current time is between start & end time */
  198. /******************************************************************************/
  199. void lights (void) _task_ LIGHTS  {            /* traffic light operation     */
  200.   red    = 1;                                  /* red & stop lights on        */
  201.   yellow = 0;
  202.   green  = 0;
  203.   stop   = 1;
  204.   walk   = 0;
  205.   while (1)  {                                 /* endless loop                */
  206.     os_wait (K_TMO, 150, 0);                   /* wait for timeout: 150 ticks */
  207.     if (!signalon ())  {                       /* if traffic signal time over */
  208.       os_create_task (BLINKING);               /* start blinking              */
  209.       os_delete_task (LIGHTS);                 /* stop lights                 */
  210.     }
  211.     yellow = 1;
  212.     os_wait (K_TMO, 150, 0);                   /* wait for timeout: 150 ticks */
  213.     red    = 0;                                /* green light for cars        */
  214.     yellow = 0;
  215.     green  = 1;
  216.     os_clear_signal (LIGHTS);
  217.     os_wait (K_TMO, 200, 0);                   /* wait for timeout: 200 ticks */
  218.     os_wait (K_TMO + K_SIG, 250, 0);           /* wait for timeout & signal   */
  219.     yellow = 1;
  220.     green  = 0;
  221.     os_wait (K_TMO, 150, 0);                   /* wait for timeout: 150 ticks */
  222.     red    = 1;                                /* red light for cars          */
  223.     yellow = 0;
  224.     os_wait (K_TMO, 150, 0);                   /* wait for timeout: 150 ticks */
  225.     stop   = 0;                                /* green light for walkers     */   
  226.     walk   = 1;
  227.     os_wait (K_TMO, 250, 0);                   /* wait for timeout: 250 ticks */
  228.     os_wait (K_TMO, 250, 0);                   /* wait for timeout: 250 ticks */
  229.     stop   = 1;                                /* red light for walkers       */        
  230.     walk   = 0;
  231.   }
  232. }


  233. /******************************************************************************/
  234. /*        Task 5 'keyread': process key stroke from pedestrian push button    */
  235. /******************************************************************************/
  236. void keyread (void) _task_ KEYREAD  {
  237.   while (1)  {                                 /* endless loop                */
  238.     if (key)  {                                /* if key pressed              */
  239.       os_send_signal (LIGHTS);                 /* send signal to task lights  */
  240.     }
  241.     os_wait (K_TMO, 2, 0);                     /* wait for timeout: 2 ticks   */
  242.   }
  243. }
復(fù)制代碼
51hei.png

Keil代碼下載: Traffic.rar (104.48 KB, 下載次數(shù): 12)

評(píng)分

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

查看全部評(píng)分

回復(fù)

使用道具 舉報(bào)

ID:301191 發(fā)表于 2022-3-15 23:49 | 顯示全部樓層
頂一下
回復(fù)

使用道具 舉報(bào)

ID:301191 發(fā)表于 2022-3-17 17:47 | 顯示全部樓層
頂一下
回復(fù)

使用道具 舉報(bào)

ID:409831 發(fā)表于 2022-4-7 09:02 | 顯示全部樓層
8位機(jī)也能跑系統(tǒng)那確實(shí)美了
回復(fù)

使用道具 舉報(bào)

ID:879348 發(fā)表于 2022-4-7 11:40 | 顯示全部樓層
但是核心沒(méi)有開(kāi)源,一旦不用keil就用不了
回復(fù)

使用道具 舉報(bào)

本版積分規(guī)則

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

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

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