io port programing chalange

Not rated 31,106

Circuit Image

This program is a simple Traffic Light Controller. Between the start time and end time, the system controls a traffic light with pedestrian self-service. Outside of this time range, the yellow caution lamp is blinking. The commands include displaying times, setting the clock time, start time, and end time.

The program includes several external functions for string and memory operations, initializing the serial UART, and managing various tasks such as initializing, handling commands, maintaining the clock, controlling the traffic lights, reading keys, and checking for escape characters. It employs a structure to record time, storing values for current time, start time, and end time. The traffic light system utilizes specific I/O pins for controlling the red, yellow, green, stop, and walk lamps, as well as a key input for pedestrian self-service.

The program initializes tasks for clock management, command processing, and light control, with a continuous loop for clock updates. The timing functions include checking for valid time inputs and handling errors in the input format.

The Traffic Light Controller is designed to operate within a defined schedule, managing the traffic lights and pedestrian signals appropriately. The system uses a time structure that includes hours, minutes, and seconds, allowing for precise control over the light sequences. The red, yellow, and green lamps are activated based on the current time in relation to the defined start and end times, while the yellow caution lamp operates outside of these hours.

The implementation of the clock task ensures that the current time is continuously updated, and any changes in time status are communicated to the command task. The program also includes mechanisms to read user input for setting times and managing the operation of the traffic lights. Error handling is incorporated to ensure that only valid time formats are accepted, enhancing the robustness of the system.

In summary, this Traffic Light Controller program effectively manages traffic signals and pedestrian requests based on a defined schedule, utilizing a structured approach to time management and user input. The system is designed for reliability and ease of use, ensuring safe and efficient traffic flow.This program is a simple Traffic Light Controller. Between start time and end time the system controls a traffic light with pedestrian self-service. Outside of this time range the yellow caution lamp is blinking. | " "+ command -+ syntax -+ function -+ " "| Display | D | display times Time | T hh:mm:ss | set clock time Start | S hh:mm:ss | set start time End | E hh:mm:ss | set end time | " "+-+-+-+ "; #include /* string and memory functions */ extern getline (char idata *, char); /* external function: input line */ extern serial_init (); /* 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 */ }; 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 = 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 walk = P14; /* I/O Pin: walk lamp output */ sbit key = P15; /* I/O Pin: self-service key input */ char idata inline[16]; /* storage for command input line */ / <7465> */ /* Task 0 `init`: Initialize */ / <7465> */ 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 */ / <7465> */ /* Task 2 `clock` */ / <7465> */ 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 */ / <7465> */ /* readtime: convert line input to time values & store in rtime */ / <7465> */ 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 (" * ERROR: INVALID TIME FORMAT "); return (0); } return (1); } #define ESC 0x1B /* ESCAPE character code */ bit escape; /* flag: mark ESCAPE character entered */ / <7465> */ /* Task 6 `get_escape`: check if ESC (escape character) was entered */ / <7465> */ void get_escape (void) _task_ GET_ESC { while (1) { /* endless loop */ if (_getkey () = ESC) escape = 1; /* set flag if ES 🔗 External reference