找回密码
 立即注册

QQ登录

只需一步,快速开始

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

51单片机RTX51交通灯智能管理控制操作系统Proteus仿真程序设计

[复制链接]
跳转到指定楼层
楼主
仿真原理图如下(proteus仿真工程文件可到本帖附件中下载)


单片机源程序如下:
  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. extern void getline (char idata *, char);  /* external function:  input line       */
  25. extern void serial_init (void);            /* external function:  init serial UART */

  26. #define INIT      0                   /* task number of task:  init           */
  27. #define COMMAND   1                   /* task number of task:  command        */
  28. #define CLOCK     2                   /* task number of task:  clock          */
  29. #define BLINKING  3                   /* task number of task:  blinking       */
  30. #define LIGHTS    4                   /* task number of task:  signal         */
  31. #define KEYREAD   5                   /* task number of task:  keyread        */
  32. #define GET_ESC   6                   /* task number of task:  get_escape     */

  33. struct time  {                        /* structure of the time record         */
  34.   unsigned char hour;                 /* hour                                 */
  35.   unsigned char min;                  /* minute                               */
  36.   unsigned char sec;                  /* second                               */
  37. };

  38. #define   LAMP_ON                0
  39. #define   LAMP_OFF                1

  40. struct time ctime = { 12,  0,  0 };   /* storage for clock time values        */
  41. struct time start = {  7, 30,  0 };   /* storage for start time values        */
  42. struct time end   = { 18, 30,  0 };   /* storage for end   time values        */

  43. sbit  red    = P1^0;                  /* I/O Pin:  red    lamp output         */
  44. sbit  yellow = P1^1;                  /* I/O Pin:  yellow lamp output         */
  45. sbit  green  = P1^2;                  /* I/O Pin:  green  lamp output         */
  46. sbit  stop   = P1^3;                  /* I/O Pin:  stop   lamp output         */
  47. sbit  walk   = P1^4;                  /* I/O Pin:  walk   lamp output         */
  48. sbit  key    = P1^5;                  /* I/O Pin:  self-service key input     */

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


  50. /******************************************************************************/
  51. /*        Task 0 'init': Initialize                                           */
  52. /******************************************************************************/
  53. void init (void) _task_ INIT  {       /* program execution starts here        */
  54.   serial_init ();                     /* initialize the serial interface      */
  55.   os_create_task (CLOCK);             /* start clock task                     */
  56.   os_create_task (COMMAND);           /* start command task                   */
  57.   os_create_task (LIGHTS);            /* start lights task                    */
  58.   os_create_task (KEYREAD);           /* start keyread task                   */
  59.   os_delete_task (INIT);              /* stop init task (no longer needed)    */
  60. }


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

  62. /******************************************************************************/
  63. /*        Task 2 'clock'                                                      */
  64. /******************************************************************************/
  65. void clock (void)  _task_ CLOCK  {
  66.   while (1)  {                        /* clock is an endless loop             */
  67.     if (++ctime.sec == 60)  {         /* calculate the second                 */
  68.       ctime.sec = 0;
  69.       if (++ctime.min == 60)  {       /* calculate the minute                 */
  70.         ctime.min = 0;
  71.         if (++ctime.hour == 24)  {    /* calculate the hour                   */
  72.           ctime.hour = 0;
  73.         }
  74.       }
  75.     }
  76.     if (display_time)  {              /* if command_status == display_time    */
  77.       os_send_signal (COMMAND);       /* signal to task command: time changed */
  78.     }
  79.     os_wait (K_IVL, 100, 0);          /* wait interval:  1 second             */
  80.   }
  81. }


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

  83. /******************************************************************************/
  84. /*        readtime: convert line input to time values & store in rtime        */
  85. /******************************************************************************/
  86. bit readtime (char idata *buffer)  {
  87.   unsigned char args;                          /* number of arguments         */

  88.   rtime.sec = 0;                               /* preset second               */
  89.   args = sscanf (buffer, "%bd:%bd:%bd",        /* scan input line for         */
  90.                  &rtime.hour,                  /* hour, minute and second     */
  91.                  &rtime.min,
  92.                  &rtime.sec);
  93.   
  94.   if (rtime.hour > 23  ||  rtime.min > 59  ||  /* check for valid inputs      */
  95.       rtime.sec > 59   ||  args < 2        ||  args == EOF)  {
  96.     printf ("\n*** ERROR: INVALID TIME FORMAT\n");
  97.     return (0);
  98.   }
  99.   return (1);
  100. }



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

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

  103. /******************************************************************************/
  104. /*        Task 6 'get_escape': check if ESC (escape character) was entered    */
  105. /******************************************************************************/
  106. void get_escape (void) _task_ GET_ESC  {
  107.   while (1)  {                                 /* endless loop                */
  108.     if (_getkey () == ESC)  escape = 1;        /* set flag if ESC entered     */
  109.     if (escape)  {                             /* if escape flag send signal  */
  110.       os_send_signal (COMMAND);                /* to task 'command'           */
  111.     }
  112.   }
  113. }


  114. /******************************************************************************/
  115. /*        Task 1 'command': command processor */
  116. /******************************************************************************/
  117. void command (void) _task_ COMMAND  {
  118.   unsigned char i;

  119.   printf (menu);                               /* display command menu        */
  120.   while (1)  {                                 /* endless loop                */
  121.     printf ("\nCommand: ");                    /* display prompt              */
  122.     getline (&inline, sizeof (inline));        /* get command line input      */

  123.     for (i = 0; inline[i] != 0; i++)  {        /* convert to uppercase        */
  124.       inline[i] = toupper(inline[i]);
  125.     }

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

  127.     switch (inline[i])  {                      /* proceed to command function */
  128.       case 'D':                                /* Display Time Command        */
  129.         printf ("Start Time: %02bd:%02bd:%02bd    "
  130.                 "End Time: %02bd:%02bd:%02bd\n",
  131.                  start.hour, start.min, start.sec,
  132.                  end.hour,   end.min,   end.sec);
  133.         printf ("                        type ESC to abort\r");

  134.         os_create_task (GET_ESC);              /* ESC check in display loop   */
  135.         escape = 0;                            /* clear escape flag           */
  136.         display_time = 1;                      /* set display time flag       */
  137.         os_clear_signal (COMMAND);             /* clear pending signals       */

  138.         while (!escape)  {                     /* while no ESC entered        */
  139.           printf ("Clock Time: %02bd:%02bd:%02bd\x0d",      /* display time     */
  140.                    ctime.hour, ctime.min, ctime.sec);
  141.           os_wait (K_SIG, 0, 0);               /* wait for time change or ESC */
  142.         }

  143.         os_delete_task (GET_ESC);              /* ESC check not longer needed */
  144.         display_time = 0;                      /* clear display time flag     */
  145.         printf ("\n\n");
  146.         break;

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

  154.       case 'E':                                /* Set End Time Command        */
  155.         if (readtime (&inline[i+1]))  {        /* read time input and         */
  156.           end.hour = rtime.hour;               /* store in 'end'              */
  157.           end.min  = rtime.min;
  158.           end.sec  = rtime.sec;
  159.         }
  160.         break;

  161.       case 'S':                                /* Set Start Time Command */
  162.         if (readtime (&inline[i+1]))  {        /* read time input and         */
  163.           start.hour = rtime.hour;             /* store in 'start'            */
  164.           start.min  = rtime.min;
  165.           start.sec  = rtime.sec;
  166.         }
  167.         break;

  168.       default:                                 /* Error Handling              */
  169.         printf (menu);                         /* display command menu        */
  170.         break;
  171.     }   
  172.   }
  173. }


  174. /******************************************************************************/
  175. /*        signalon: check if clock time is between start and end              */
  176. /******************************************************************************/
  177. static bit signalon (void)   {
  178.   if (memcmp (&start, &end, sizeof (struct time)) < 0)  {
  179.     if (memcmp (&start, &ctime, sizeof (struct time)) < 0  &&
  180.         memcmp (&ctime, &end,   sizeof (struct time)) < 0)  return (1);
  181.   }
  182.                                                 
  183.   else  {
  184.     if (memcmp (&end,   &ctime, sizeof (start)) > 0  &&
  185.         memcmp (&ctime, &start, sizeof (start)) > 0)  return (1);
  186.   }
  187.   return (0);                                  /* signal off, blinking on     */
  188. }


  189. /******************************************************************************/
  190. /*        Task 3 'blinking': runs if current time is outside start & end time */
  191. /******************************************************************************/
  192. void blinking (void) _task_ BLINKING  {        /* blink yellow light          */
  193.   red    = LAMP_OFF;                                  /* all lights off              */
  194.   yellow = LAMP_OFF;
  195.   green  = LAMP_OFF;
  196.   stop   = LAMP_OFF;
  197.   walk   = LAMP_OFF;

  198.   while (1)  {                                 /* endless loop                */
  199.     yellow = LAMP_OFF;                                /* yellow light on             */
  200.     os_wait (K_TMO, 30, 0);                   /* wait for timeout: 30 ticks  */
  201.     yellow = LAMP_ON;                                /* yellow light off            */
  202.     os_wait (K_TMO, 30, 0);                   /* wait for timeout: 30 ticks  */
  203.     if (signalon ())  {                        /* if blinking time over       */
  204.       os_create_task (LIGHTS);                 /* start lights                */
  205.       os_delete_task (BLINKING);               /* and stop blinking           */
  206.     }
  207.   }
  208. }


  209. /******************************************************************************/
  210. /*      Task 4 'lights': executes if current time is between start & end time */
  211. /******************************************************************************/
  212. void lights (void) _task_ LIGHTS  {            /* traffic light operation     */
  213.   red    = LAMP_ON;                                  /* red & stop lights on        */
  214.   yellow = LAMP_OFF;
  215.   green  = LAMP_OFF;
  216.   stop   = LAMP_ON;
  217.   walk   = LAMP_OFF;

  218.   while (1)  {                                 /* endless loop                */
  219. //    os_wait (K_TMO, 30, 0);                   /* wait for timeout: 30 ticks */
  220.     if (!signalon ())  {                       /* if traffic signal time over */
  221.       os_create_task (BLINKING);               /* start blinking              */
  222.       os_delete_task (LIGHTS);                 /* stop lights                 */
  223.     }
  224.     red    = LAMP_OFF;  
  225.         yellow = LAMP_ON;
  226.     os_wait (K_TMO, 50, 0);                   /* wait for timeout: 30 ticks */
  227.     red    = LAMP_OFF;                                /* green light for cars        */
  228.     yellow = LAMP_OFF;
  229.     green  = LAMP_ON;
  230.     os_clear_signal (LIGHTS);
  231.     os_wait (K_TMO + K_SIG, 250, 0);           /* wait for timeout & signal   */
  232.     os_wait (K_TMO, 50, 0);                   /* wait for timeout: 200 ticks */
  233.     yellow = LAMP_ON;
  234.     green  = LAMP_OFF;
  235.     os_wait (K_TMO, 50, 0);                   /* wait for timeout: 30 ticks */
  236.     red    = LAMP_ON;                                /* red light for cars          */
  237.     yellow = LAMP_OFF;
  238.     os_wait (K_TMO, 50, 0);                   /* wait for timeout: 30 ticks */
  239.     stop   = LAMP_OFF;                                /* green light for walkers     */   
  240.     walk   = LAMP_ON;
  241.     os_wait (K_TMO, 150, 0);                   /* wait for timeout: 100 ticks */
  242.     stop   = LAMP_ON;                                /* red light for walkers       */        
  243.     walk   = LAMP_OFF;

  244. //    stop   = LAMP_ON;                                /* red light for walkers       */        
  245. //    walk   = LAMP_OFF;
  246. //    red    = LAMP_OFF;                                /* green light for cars        */
  247. //    yellow = LAMP_OFF;
  248. //    green  = LAMP_ON;
  249. ////    os_clear_signal (LIGHTS);
  250. //    os_wait1 (K_SIG);           /* wait for timeout & signal   */
  251. //    os_wait (K_TMO, 250, 0);                   /* wait for timeout: 200 ticks */
  252. //    yellow = LAMP_ON;
  253. //    green  = LAMP_OFF;
  254. //    os_wait (K_TMO, 100, 0);                   /* wait for timeout: 30 ticks */
  255. //    red    = LAMP_ON;                                /* red light for cars          */
  256. //    yellow = LAMP_OFF;
  257. ////    os_wait (K_TMO, 30, 0);                   /* wait for timeout: 30 ticks */
  258. //    stop   = LAMP_OFF;                                /* green light for walkers     */   
  259. //    walk   = LAMP_ON;
  260. //    os_wait (K_TMO, 250, 0);                   /* wait for timeout: 100 ticks */
  261.   }
  262. }


  263. /******************************************************************************/
  264. /*        Task 5 'keyread': process key stroke from pedestrian push button    */
  265. /******************************************************************************/
  266. void keyread (void) _task_ KEYREAD  {
  267. static  bit keybuf;
  268.   while (1)  {                                 /* endless loop                */
  269.     if (keybuf==1 && key==0)  {                                /* if key pressed              */
  270.       os_send_signal (LIGHTS);                 /* send signal to task lights  */
  271.     }
  272.     keybuf = key;
  273.     os_wait (K_TMO, 2, 0);                     /* wait for timeout: 2 ticks   */
  274.   }
  275. }
复制代码

所有资料51hei提供下载:
RTX51操作系统.rar (94.18 KB, 下载次数: 28)


评分

参与人数 1黑币 +50 收起 理由
admin + 50 共享资料的黑币奖励!

查看全部评分

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

使用道具 举报

沙发
ID:207571 发表于 2020-7-11 15:06 | 只看该作者
下载下来了,有点没看懂,仿真时串口没有输入
回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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