1、RTX51 Tiny 应用程序实例 - MCS-51单片机在线教程 实例名称:TRAFFICTRAFFIC 是一个定时控制的红绿灯控制器。 基本控制要求在一个用户定义的时间段内,红绿灯正常运行;超过正常运行时段时,黄色信号灯闪烁;如果一个行人按下请求按钮,红绿灯立即变成“walk” 状态,以便行人通过;此后,红绿灯继续正常工作。 红绿灯控制命令红绿灯控制器通过8051 的串口进行通讯,其通讯命令见表6-5 。这些命令由 ASCII 字符组成,所有命令必须用回车结束。表6-5 红绿灯控制命令 应用程序组成TRAFFIC 应用程序由三个文件组成,这三个文件在C51V4RTX_TINYTRAFFIC
2、 或CDEMO51RTX_TINYTRAFFIC 目录中可以找到。TRAFFIC.C包括红绿灯控制程序,由下面几个任务组成: 任务 0 : 初始化初始化串行接口,并启动其他任务,初始化仅需执行一次,执行完以后,任务0自己删除。 任务 1 : 命令处理这是红绿灯控制器的命令处理程序,用于控制和处理接收到的串口命令。 任务 2 : 定时器 任务 3 : 黄灯闪烁控制在非正常运行时段,使黄色灯光闪烁。 任务 4 :红绿灯控制在正常运行时段(在开始和结束时间之间) ,控制红绿灯。 任务 5 : 按钮控制读取行人操作的按钮状态,并发送信号给任务4。 任务 6 : 退出在串口字符流中检查 ESC 字符,如
3、果发现 ESC 字符,就终止前一个显示命令。 SERIAL.C实现串行接口的中断程序。其中包括函数 putchar()和 getkey() ,其它高层的输入输出函数 printf()和 getline()将调用这两个基本的输入输出子程序。 GETLINE.C用于对来自串口的字符命令行进行处理。 参考源程序如下: TRAFFIC.C/*/* */* TRAFFIC.C: Traffic Light Controller using the C51 Compiler */* */*/code char menu = “n“+* TRAFFIC LIGHT CONTROLLER using C51
4、and RTX-51 tiny *+n“| This program is a simple Traffic Light Controller. Between |n“| start time and end time the system controls a traffic light |n“| with pedestrian self-service. Outside of this time range |n“| the yellow caution lamp is blinking. |n“+ command -+ syntax -+ function -+n“| Display |
5、 D | display times |n“| Time | T hh:mm:ss | set clock time |n“| Start | S hh:mm:ss | set start time |n“| End | E hh:mm:ss | set end time |n“+-+-+-+n“;#include /* special function registers 8052*/#include /* RTX-51 tiny functions /* external function: input line*/extern serial_init (); /* external fu
6、nction: init serial UART*/#define INIT 0 /* task number of task: init*/#define COMMAND 1 /* task number of task: command*/#define CLOCK 2 /* task number of task: clock*/#define BLINKING 3 /* task number of task: blinking*/#define LIGHTS 4 /* task number of task: signal*/#define KEYREAD 5 /* task num
7、ber of task: keyread*/#define GET_ESC 6 /* task number of task: get_escape*/struct time /* structure of the time record*/unsigned char hour; /* hour*/unsigned char min; /* minute*/unsigned char sec; /* second*/;struct time ctime = 12, 0, 0 ; /* storage for clock time values*/struct time start = 7, 3
8、0, 0 ; /* storage for start time values*/struct time end = 18, 30, 0 ; /* storage for end time values*/sbit red = P12; /* I/O Pin: red lamp output*/sbit yellow = P11; /* I/O Pin: yellow lamp output*/sbit green = P10; /* I/O Pin: green lamp output*/sbit stop = P13; /* I/O Pin: stop lamp output*/sbit
9、walk = P14; /* I/O Pin: walk lamp output*/sbit key = P15; /* I/O Pin: self-service key input*/idata char inline16; /* storage for command input line*/*/* Task 0 init: Initialize */*/init () _task_ INIT /* program execution starts here*/serial_init (); /* initialize the serial interface*/os_create_ta
10、sk (CLOCK); /* start clock task*/os_create_task (COMMAND); /* start command task*/os_create_task (LIGHTS); /* start lights task*/os_create_task (KEYREAD); /* start keyread task*/os_delete_task (INIT); /* stop init task (no longer needed)*/bit display_time = 0; /* flag: signal cmd state display_time*
11、/*/* Task 2 clock*/*/clock () _task_ CLOCK while (1) /* clock is an endless loop*/if (+ctime.sec = 60) /* calculate the second*/ctime.sec = 0;if (+ctime.min = 60) /* calculate the minute*/ctime.min = 0;if (+ctime.hour = 24) /* calculate the hour*/ctime.hour = 0;if (display_time) /* if command_status
12、 = display_time*/os_send_signal (COMMAND); /* signal to task command: time changed*/os_wait (K_IVL, 100, 0); /* wait interval: 1 second*/struct time rtime; /* temporary storage for entry time*/*/* readtime: convert line input to time values /* number of arguments*/rtime.sec = 0; /* preset second*/ar
13、gs = sscanf (buffer, “%bd:%bd:%bd“, /* scan input line for*/if (rtime.hour 23 | rtime.min 59 | /* check for valid inputs*/rtime.sec 59 | args 0 return (0); /* signal off, blinking on*/*/* Task 3 blinking: runs if current time is outside start /* all lights off*/yellow = 0;green = 0;stop = 0;walk = 0
14、;while (1) /* endless loop*/yellow = 1; /* yellow light on*/os_wait (K_TMO, 30, 0); /* wait for timeout: 30 ticks*/yellow = 0; /* yellow light off*/os_wait (K_TMO, 30, 0); /* wait for timeout: 30 ticks*/if (signalon () /* if blinking time over*/os_create_task (LIGHTS); /* start lights*/os_delete_tas
15、k (BLINKING); /* and stop blinking*/*/* Task 4 lights: executes if current time is between start /* red green = 0;stop = 1;walk = 0;while (1) /* endless loop*/os_wait (K_TMO, 30, 0); /* wait for timeout: 30 ticks*/if (!signalon () /* if traffic signal time over*/os_create_task (BLINKING); /* start b
16、linking*/os_delete_task (LIGHTS); /* stop lights*/yellow = 1;os_wait (K_TMO, 30, 0); /* wait for timeout: 30 ticks*/red = 0; /* green light for cars*/yellow = 0;green = 1;os_clear_signal (LIGHTS);os_wait (K_TMO, 30, 0); /* wait for timeout: 30 ticks*/os_wait (K_TMO + K_SIG, 250, 0); /* wait for time
17、out green = 0;os_wait (K_TMO, 30, 0); /* wait for timeout: 30 ticks*/red = 1; /* red light for cars*/yellow = 0;os_wait (K_TMO, 30, 0); /* wait for timeout: 30 ticks*/stop = 0; /* green light for walkers*/walk = 1;os_wait (K_TMO, 100, 0); /* wait for timeout: 100 ticks*/stop = 1; /* red light for walkers*/walk = 0;