📄 traffic.c
字号:
/*----------------------------------------------------------------------------
* R T L K e r n e l E x a m p l e
*----------------------------------------------------------------------------
* Name: TRAFFIC.C
* Purpose: Traffic Light Controller example program
* Rev.: V3.40
*----------------------------------------------------------------------------
* This code is part of the RealView Run-Time Library.
* Copyright (c) 2004-2008 KEIL - An ARM Company. All rights reserved.
*---------------------------------------------------------------------------*/
#include <stdio.h> /* standard I/O .h-file */
#include <ctype.h> /* character functions */
#include <string.h> /* string and memory functions */
#include <RTL.h> /* RTX kernel functions & defines */
#include <LPC21xx.H> /* LPC21xx definitions */
const char menu[] =
"\n"
"+***** TRAFFIC LIGHT CONTROLLER using MDK and RTX kernel *****+\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";
extern void getline (char *, int);
/* external function: input line */
extern void serial_init (void); /* external function: init serial UART*/
extern int getkey (void); /* external function: input character */
OS_TID t_command; /* assigned task id of task: command */
OS_TID t_clock; /* assigned task id of task: clock */
OS_TID t_lights; /* assigned task id of task: lights */
OS_TID t_keyread; /* assigned task id of task: keyread */
OS_TID t_getesc; /* assigned task id of task: get_esc */
struct time { /* structure of the time record */
U8 hour; /* hour */
U8 min; /* minute */
U8 sec; /* second */
};
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 */
#define red 0x010000 /* I/O Pin: red lamp output */
#define yellow 0x020000 /* I/O Pin: yellow lamp output */
#define green 0x040000 /* I/O Pin: green lamp output */
#define stop 0x100000 /* I/O Pin: stop lamp output */
#define walk 0x200000 /* I/O Pin: walk lamp output */
#define key 0x004000 /* I/O Pin: self-service key input */
#define SET(o,s) { \
if (s==0) IOCLR1 = o; \
else IOSET1 = o; \
};
char cmdline[16]; /* storage for command input line */
struct time rtime; /* temporary storage for entry time */
BIT display_time = __FALSE; /* flag signal cmd state display_time */
BIT escape; /* flag: mark ESCAPE character entered */
#define ESC 0x1B /* ESCAPE character code */
static U64 cmd_stack[800/8]; /* A bigger stack for command task */
__task void init (void); /* Function prototypes */
__task void clock (void);
__task void get_escape (void);
__task void command (void);
__task void blinking (void);
__task void lights (void);
__task void keyread (void);
/*----------------------------------------------------------------------------
* Main: Initialize and start RTX Kernel
*---------------------------------------------------------------------------*/
int main (void) { /* program execution starts here */
os_sys_init (init); /* init and start with task 'INIT' */
}
/*----------------------------------------------------------------------------
* Task 1 'init': Initialize
*---------------------------------------------------------------------------*/
__task void init (void) {
IODIR1 = 0xFF0000; /* P1.16..22 defined as Outputs */
serial_init (); /* initialize the serial interface */
t_clock = os_tsk_create (clock, 0); /* start clock task */
/* start command task */
t_command = os_tsk_create_user (command,0,&cmd_stack,sizeof(cmd_stack));
t_lights = os_tsk_create (lights, 0); /* start lights task */
t_keyread = os_tsk_create (keyread,0); /* start keyread task */
os_tsk_delete_self (); /* stop init task (no longer needed) */
}
/*----------------------------------------------------------------------------
* Task 3 'clock'
*---------------------------------------------------------------------------*/
__task void clock (void) {
os_itv_set (100); /* set wait interval: 1 second */
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_evt_set (0x0001,t_command); /* signal to task command time changed*/
}
os_itv_wait (); /* wait interval (already set to 1s ) */
}
}
/*----------------------------------------------------------------------------
* readtime: convert line input to time values & store in rtime
*---------------------------------------------------------------------------*/
char readtime (char *buffer) {
int args; /* number of arguments */
int hour,min,sec;
rtime.sec = 0; /* preset second */
args = sscanf (buffer, "%d:%d:%d", /* scan input line for */
&hour, &min, &sec); /* hour, minute and second */
if (hour > 23 || min > 59 || /* check for valid inputs */
sec > 59 || args < 2 ) {
printf ("\n*** ERROR: INVALID TIME FORMAT\n");
return (0);
}
rtime.hour = hour; /* setting a new time: hour */
rtime.min = min; /* setting a new time: min */
rtime.sec = sec; /* setting a new time: sec */
return (1);
}
/*----------------------------------------------------------------------------
* Task 7 'get_escape': check if ESC (escape character) was entered
*---------------------------------------------------------------------------*/
__task void get_escape (void) {
while (1) { /* endless loop */
if (getkey () == ESC) { /* If ESC entered, set flag */
escape = __TRUE; /* 'escape', set event flag of */
os_evt_set (0x0002, t_command); /* task 'command' and */
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -