標(biāo)題:
51單片機(jī)RTX51交通燈智能管理控制操作系統(tǒng)Proteus仿真程序設(shè)計(jì)
[打印本頁]
作者:
weras
時(shí)間:
2020-3-11 20:24
標(biāo)題:
51單片機(jī)RTX51交通燈智能管理控制操作系統(tǒng)Proteus仿真程序設(shè)計(jì)
仿真原理圖如下(proteus仿真工程文件可到本帖附件中下載)
無標(biāo)題.png
(18.51 KB, 下載次數(shù): 44)
下載附件
2020-3-11 20:22 上傳
單片機(jī)源程序如下:
/******************************************************************************/
/* */
/* TRAFFIC.C: Traffic Light Controller using the C-51 COMPILER */
/* */
/******************************************************************************/
char code menu[] =
"\n"
"+***** TRAFFIC LIGHT CONTROLLER using C51 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 | 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 <reg52.h> /* special function registers 8052 */
#include <rtx51tny.h> /* RTX-51 tiny functions & defines */
#include <stdio.h> /* standard I/O .h-file */
#include <ctype.h> /* character functions */
#include <string.h> /* string and memory functions */
extern void getline (char idata *, char); /* external function: input line */
extern void serial_init (void); /* external function: 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 number 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 */
};
#define LAMP_ON 0
#define LAMP_OFF 1
struct time ctime = { 12, 0, 0 }; /* storage for clock time values */
struct time start = { 7, 30, 0 }; /* storage for start time values */
struct time end = { 18, 30, 0 }; /* storage for end time values */
sbit red = P1^0; /* I/O Pin: red lamp output */
sbit yellow = P1^1; /* I/O Pin: yellow lamp output */
sbit green = P1^2; /* I/O Pin: green lamp output */
sbit stop = P1^3; /* I/O Pin: stop lamp output */
sbit walk = P1^4; /* I/O Pin: walk lamp output */
sbit key = P1^5; /* I/O Pin: self-service key input */
char idata inline[16]; /* storage for command input line */
/******************************************************************************/
/* Task 0 'init': Initialize */
/******************************************************************************/
void init (void) _task_ INIT { /* program execution starts here */
serial_init (); /* initialize the serial interface */
os_create_task (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 */
/******************************************************************************/
/* Task 2 'clock' */
/******************************************************************************/
void clock (void) _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 == 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 & store in rtime */
/******************************************************************************/
bit readtime (char idata *buffer) {
unsigned char args; /* number of arguments */
rtime.sec = 0; /* preset second */
args = sscanf (buffer, "%bd:%bd:%bd", /* scan input line for */
&rtime.hour, /* hour, minute and second */
&rtime.min,
&rtime.sec);
if (rtime.hour > 23 || rtime.min > 59 || /* check for valid inputs */
rtime.sec > 59 || args < 2 || args == EOF) {
printf ("\n*** ERROR: INVALID TIME FORMAT\n");
return (0);
}
return (1);
}
#define ESC 0x1B /* ESCAPE character code */
static bit escape; /* flag: mark ESCAPE character entered */
/******************************************************************************/
/* Task 6 'get_escape': check if ESC (escape character) was entered */
/******************************************************************************/
void get_escape (void) _task_ GET_ESC {
while (1) { /* endless loop */
if (_getkey () == ESC) escape = 1; /* set flag if ESC entered */
if (escape) { /* if escape flag send signal */
os_send_signal (COMMAND); /* to task 'command' */
}
}
}
/******************************************************************************/
/* Task 1 'command': command processor */
/******************************************************************************/
void command (void) _task_ COMMAND {
unsigned char i;
printf (menu); /* display command menu */
while (1) { /* endless loop */
printf ("\nCommand: "); /* display prompt */
getline (&inline, sizeof (inline)); /* get command line input */
for (i = 0; inline[i] != 0; i++) { /* convert to uppercase */
inline[i] = toupper(inline[i]);
}
for (i = 0; inline[i] == ' '; i++); /* skip blanks */
switch (inline[i]) { /* proceed to command function */
case 'D': /* Display Time Command */
printf ("Start Time: %02bd:%02bd:%02bd "
"End Time: %02bd:%02bd:%02bd\n",
start.hour, start.min, start.sec,
end.hour, end.min, end.sec);
printf (" type ESC to abort\r");
os_create_task (GET_ESC); /* ESC check in display loop */
escape = 0; /* clear escape flag */
display_time = 1; /* set display time flag */
os_clear_signal (COMMAND); /* clear pending signals */
while (!escape) { /* while no ESC entered */
printf ("Clock Time: %02bd:%02bd:%02bd\x0d", /* display time */
ctime.hour, ctime.min, ctime.sec);
os_wait (K_SIG, 0, 0); /* wait for time change or ESC */
}
os_delete_task (GET_ESC); /* ESC check not longer needed */
display_time = 0; /* clear display time flag */
printf ("\n\n");
break;
case 'T': /* Set Time Command */
if (readtime (&inline[i+1])) { /* read time input and */
ctime.hour = rtime.hour; /* store in 'ctime' */
ctime.min = rtime.min;
ctime.sec = rtime.sec;
}
break;
case 'E': /* Set End Time Command */
if (readtime (&inline[i+1])) { /* read time input and */
end.hour = rtime.hour; /* store in 'end' */
end.min = rtime.min;
end.sec = rtime.sec;
}
break;
case 'S': /* Set Start Time Command */
if (readtime (&inline[i+1])) { /* read time input and */
start.hour = rtime.hour; /* store in 'start' */
start.min = rtime.min;
start.sec = rtime.sec;
}
break;
default: /* Error Handling */
printf (menu); /* display command menu */
break;
}
}
}
/******************************************************************************/
/* signalon: check if clock time is between start and end */
/******************************************************************************/
static bit signalon (void) {
if (memcmp (&start, &end, sizeof (struct time)) < 0) {
if (memcmp (&start, &ctime, sizeof (struct time)) < 0 &&
memcmp (&ctime, &end, sizeof (struct time)) < 0) return (1);
}
else {
if (memcmp (&end, &ctime, sizeof (start)) > 0 &&
memcmp (&ctime, &start, sizeof (start)) > 0) return (1);
}
return (0); /* signal off, blinking on */
}
/******************************************************************************/
/* Task 3 'blinking': runs if current time is outside start & end time */
/******************************************************************************/
void blinking (void) _task_ BLINKING { /* blink yellow light */
red = LAMP_OFF; /* all lights off */
yellow = LAMP_OFF;
green = LAMP_OFF;
stop = LAMP_OFF;
walk = LAMP_OFF;
while (1) { /* endless loop */
yellow = LAMP_OFF; /* yellow light on */
os_wait (K_TMO, 30, 0); /* wait for timeout: 30 ticks */
yellow = LAMP_ON; /* 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_task (BLINKING); /* and stop blinking */
}
}
}
/******************************************************************************/
/* Task 4 'lights': executes if current time is between start & end time */
/******************************************************************************/
void lights (void) _task_ LIGHTS { /* traffic light operation */
red = LAMP_ON; /* red & stop lights on */
yellow = LAMP_OFF;
green = LAMP_OFF;
stop = LAMP_ON;
walk = LAMP_OFF;
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 blinking */
os_delete_task (LIGHTS); /* stop lights */
}
red = LAMP_OFF;
yellow = LAMP_ON;
os_wait (K_TMO, 50, 0); /* wait for timeout: 30 ticks */
red = LAMP_OFF; /* green light for cars */
yellow = LAMP_OFF;
green = LAMP_ON;
os_clear_signal (LIGHTS);
os_wait (K_TMO + K_SIG, 250, 0); /* wait for timeout & signal */
os_wait (K_TMO, 50, 0); /* wait for timeout: 200 ticks */
yellow = LAMP_ON;
green = LAMP_OFF;
os_wait (K_TMO, 50, 0); /* wait for timeout: 30 ticks */
red = LAMP_ON; /* red light for cars */
yellow = LAMP_OFF;
os_wait (K_TMO, 50, 0); /* wait for timeout: 30 ticks */
stop = LAMP_OFF; /* green light for walkers */
walk = LAMP_ON;
os_wait (K_TMO, 150, 0); /* wait for timeout: 100 ticks */
stop = LAMP_ON; /* red light for walkers */
walk = LAMP_OFF;
// stop = LAMP_ON; /* red light for walkers */
// walk = LAMP_OFF;
// red = LAMP_OFF; /* green light for cars */
// yellow = LAMP_OFF;
// green = LAMP_ON;
//// os_clear_signal (LIGHTS);
// os_wait1 (K_SIG); /* wait for timeout & signal */
// os_wait (K_TMO, 250, 0); /* wait for timeout: 200 ticks */
// yellow = LAMP_ON;
// green = LAMP_OFF;
// os_wait (K_TMO, 100, 0); /* wait for timeout: 30 ticks */
// red = LAMP_ON; /* red light for cars */
// yellow = LAMP_OFF;
//// os_wait (K_TMO, 30, 0); /* wait for timeout: 30 ticks */
// stop = LAMP_OFF; /* green light for walkers */
// walk = LAMP_ON;
// os_wait (K_TMO, 250, 0); /* wait for timeout: 100 ticks */
}
}
/******************************************************************************/
/* Task 5 'keyread': process key stroke from pedestrian push button */
/******************************************************************************/
void keyread (void) _task_ KEYREAD {
static bit keybuf;
while (1) { /* endless loop */
if (keybuf==1 && key==0) { /* if key pressed */
os_send_signal (LIGHTS); /* send signal to task lights */
}
keybuf = key;
os_wait (K_TMO, 2, 0); /* wait for timeout: 2 ticks */
}
}
復(fù)制代碼
所有資料51hei提供下載:
RTX51操作系統(tǒng).rar
(94.18 KB, 下載次數(shù): 30)
2020-3-11 20:23 上傳
點(diǎn)擊文件名下載附件
下載積分: 黑幣 -5
作者:
xiaoyong4000
時(shí)間:
2020-7-11 15:06
下載下來了,有點(diǎn)沒看懂,仿真時(shí)串口沒有輸入
歡迎光臨 (http://www.torrancerestoration.com/bbs/)
Powered by Discuz! X3.1