Microcontroller › 8051 › 8051 Traffic Controller
- This topic has 0 replies, 1 voice, and was last updated 11 years, 11 months ago by David Barahona.
-
AuthorPosts
-
May 22, 2012 at 4:01 am #3821David BarahonaParticipant
Design and implement a simple traffic light control system to control an intersection. The intersection consists of two streets, one running north-south (called NS) and another running east-west (called EW). The system need to control the traffic lights on both directions of the NS street and the EW street. Each traffic-light consists of a red, yellow, and green light. Each signal will cycle through red, green, yellow and back to red. When one signal is green, the other is red; when yellow, the other will be red; and when red, the other will be green. To simplify the design, and to shorten the experimentation time, we will set the green, yellow, and red periods to 6, 2, and 8 seconds, respectively (sixteen seconds for a complete cycle).
The system uses three LEDs for each of the two directions (north-bound and south bound) of the NS lights and another three for each of the two directions (east-bound and west bound) of the EW lights. The system also has two “pedestrian buttons”, one for the NS and one for EW.
In addition, a LCD display panel will be used for the following purpose:
1) When the pedestrian light is not activated, the LCD is used to display warning or alert information, such as “Drive safely and save lives!”, “10 minutes to 110, 15 minutes to 10”, etc. Basic requirement for this display is: scroll the message from right to left until the whole message is shown on the LCD, then hold the message for 3 second and start over again.
2) When pedestrian cross indicator light is activated (a pedestrian light is activated when a pedestrian button has been pressed and the corresponding direction has green lights), the LCD will be used to display number of seconds left (6, 5 … 2, 1) for “green” with an indication for direction, NS or EW. Here is a display example: “Seconds left for NS: 6”. When the traffic light for one direction is green, turn on the pedestrian cross indicator LED for 3 seconds and flash for another 3 seconds before turn it off. For the last 3 seconds, the system also flashes the LCD display.If a “pedestrian button” for a direction (NS or EW) is pressed while there is a “green” light for the opposite direction (EW or NS), the system will only allow one more second of green before going to the “yellow” cycles. Ignore the pedestrian button for the current “green/yellow” cycle.
#include <reg51.h>
#define LED P1 // Traffic controller LED's are displayed on Port 1#define DATA_BITS_LCD P2 // Data bits DB0-DB7 of LCD#define COMMAND 0#define LCD_DATA 1// sbit busy = P2^7; // connected to DB7 on the LCD// RW -> 0 = Write to LCD module, 1 = Read from LCD modulesbit RW = P3^5; // Read/Write// RS -> 0 = Instruction input, 1 = Data inputsbit RS = P3^6; // Register Select// EN: L-to-H for read, H-to-L for writesbit EN = P3^7; // Enable – Starts data read or write// North-South Intersection LEDssbit ns_green = P0^7;sbit ns_yellow = P0^6;sbit ns_red = P0^5;sbit ns_pedestrian = P0^4;bit ns_ped_btn_flag = 0;// East-West Intersection LEDssbit ew_green = P0^0;sbit ew_yellow = P0^1;sbit ew_red = P0^2;sbit ew_pedestrian = P0^3;bit ew_ped_btn_flag = 0;void initial_setup();void MS_Delay(unsigned int itime);void initial_LCD();void lcd_ready(void);void write_to_LCD(unsigned char value, bit mode);void NS_Intersection_Check(int d);void EW_Intersection_Check(int b);void LED_Lights_Delay(unsigned char num);unsigned char d, b;unsigned char one_second_delay = 20;unsigned char NS = 0;unsigned char EW = 0;/////////////////////////////////////////////////////////////////////// This sets up the LCD for the required display pattern ///////////////////////////////////////////////////////////////////////void initial_LCD(){// Function set format: 001 DL N F * *// Function set value: 00111000// DL=1, use 8-bit data bus, N=0,1/16 duty(1 lines),// F=0,5×7 dot character font, we can make the font// larger when displaying only one line.write_to_LCD(0x38, COMMAND);// Display On/Off Control format: 00001 D C B// Display On/Off Control value: 00001100// D=1, display o; C=0, cursor off; B=0, cursor blink offwrite_to_LCD(0x0C, COMMAND);// Entry mode set format: 000001 I/D S// Entry mode set value: 00000111// I/D=1, Increment cursor position; S=1, display shiftwrite_to_LCD(0x07, COMMAND);// Cursor/display shift format: 0001 S/C R/L * *// Entry mode set value: 00011000// S/C=1, shift display; R/L=0, shift leftwrite_to_LCD(0x18, COMMAND);// Clear display and returns cursor to the home position(address 0)write_to_LCD(0x01, COMMAND);} // end void initial_LCD///////////////////////////////////////////////////////////////////////////////////////////////////////// //// The initial setup function initializes both the interrupt and the timer at the beginning of the //// program. The startup function also initializes the Port #0 switch and the external dip switch //// which is connected to Port #2 for use in the program. //// /////////////////////////////////////////////////////////////////////////////////////////////////////////void initial_setup(void){P0 = 0xFF;IT0 = 1; // This sets INT0 to be edge-triggeredIT1 = 1; // This sets INT1 to be edge-triggeredEX0 = 1; // This enables the INT0EX1 = 1; // This enables the INT1EA = 1; // Using Timer 0 overflow interruptTMOD = 0x01; //Timer settings of timer 0, mode 1(16-bit)}void main(void){unsigned char code first_line_msg[]=" DRIVERS ";unsigned char code second_line_msg[]= "SLOW DOWN! ";unsigned char i;initial_LCD();initial_setup();while (1){i = 0;write_to_LCD(0x01, COMMAND); // Clears displaywrite_to_LCD(0x06, COMMAND);write_to_LCD(0x8F, COMMAND);while(first_line_msg != '