EngineersGarage

  • Engineers Garage Main Site
  • Visit our active EE Forums
    • EDABoard.com
    • Electro-Tech-Online
  • Projects & Tutorials
    • Circuits
    • Electronic Projects
    • Tutorials
    • Components
  • Digi-Key Store
    • Cables, Wires
    • Connectors, Interconnect
    • Discrete
    • Electromechanical
    • Embedded Computers
    • Enclosures, Hardware, Office
    • Integrated Circuits (ICs)
    • Isolators
    • LED/Optoelectronics
    • Passive
    • Power, Circuit Protection
    • Programmers
    • RF, Wireless
    • Semiconductors
    • Sensors, Transducers
    • Test Products
    • Tools
  • Advertise
You are here: Home / Topics / 8051 Traffic Controller

8051 Traffic Controller

|

Microcontroller › 8051 › 8051 Traffic Controller

  • This topic has 0 replies, 1 voice, and was last updated 13 years, 1 month ago by David Barahona.
Viewing 1 post (of 1 total)
  • Author
    Posts
  • May 22, 2012 at 4:01 am #3821
    David Barahona
    Participant

    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 module
    sbit RW = P3^5; // Read/Write
    // RS -> 0 = Instruction input, 1 = Data input
    sbit RS = P3^6; // Register Select
    // EN: L-to-H for read, H-to-L for write
    sbit EN = P3^7; // Enable – Starts data read or write
     
    // North-South Intersection LEDs
    sbit 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 LEDs
    sbit 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 off
    write_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 shift
    write_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 left
    write_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-triggered
    IT1 = 1; // This sets INT1 to be edge-triggered
    EX0 = 1; // This enables the INT0
    EX1 = 1; // This enables the INT1
    EA = 1; // Using Timer 0 overflow interrupt
     
        TMOD = 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 display
      write_to_LCD(0x06, COMMAND);
      write_to_LCD(0x8F, COMMAND);
      while(first_line_msg != '')
      {
    write_to_LCD(first_line_msg[i++], LCD_DATA);
      } // end while
      write_to_LCD(0xCF, COMMAND);
      i = 0;
      while(second_line_msg != '')
      {
      write_to_LCD(second_line_msg[i++], LCD_DATA);
    write_to_LCD(0x07, COMMAND);
      } // end while
     
     
    if(!ns_ped_btn_flag)
    {
    b = 8;
     
    while(!ns_ped_btn_flag && b > 2)
    {
    ns_green = 0;
    ns_yellow = 1;
    ns_red = 1;
    ew_green = 1;
    ew_yellow = 1;
    ew_red = 0;
    LED_Lights_Delay(1);
    b–;
     
    if(ew_ped_btn_flag)
    {
    b = 1;
    } // end if
    } // end while
     
    if(ns_ped_btn_flag)
    {
    NS_Intersection_Check(b++);
    ns_ped_btn_flag = 0;
    } // end if
    else
    {
    MS_Delay(200); // This should generate a 1 sec delay to hold before going to yellow 
    while(b > 0)
    {
    ns_green = 1;
    ns_yellow = 0;
    ns_red = 1;
    ew_green = 1;
    ew_yellow = 1;
    ew_red = 0;
    LED_Lights_Delay(1);
    b–;
    } // end while
    } // end else
    } // end if
    else if(ns_ped_btn_flag)
    {
    b = 8;
    NS_Intersection_Check(b);
    ns_ped_btn_flag = 0;
    } // end if else
     
      if(!ew_ped_btn_flag)
      {
    b = 8;
    while(!ew_ped_btn_flag && b > 2)
    {
    ns_green = 1;
    ns_yellow = 1;
    ns_red = 0;
    ew_green = 0;
    ew_yellow = 1;
    ew_red = 1;
    LED_Lights_Delay(1);
    b–;
     
    if(ns_ped_btn_flag)
    {
    b = 1;
    }
    } // end while
     
    if(ew_ped_btn_flag)
    {
    EW_Intersection_Check(b++);
    ew_ped_btn_flag = 0;
    }
     
    else
    {
    MS_Delay(200); // This should generate a 1 sec delay to hold before going to yellow
    while(b > 0)
    {
    ns_green = 1;
    ns_yellow = 1;
    ns_red = 0;
    ew_green = 1;
    ew_yellow = 0;
    ew_red = 1;
    LED_Lights_Delay(1);
    b–;
    } // end while
    } // end else
    } // end if
     
    else if(ew_ped_btn_flag)
    {
    b = 8;
    EW_Intersection_Check(b);
    ew_ped_btn_flag = 0;
    } // end else if
    } // end super loop
    }// end main
     
    void NS_Intersection_Check(int d)
    {
    int a;
    unsigned char code warn_NSped[] = "Time left NS: ";
     
    write_to_LCD(0x06, COMMAND);
    write_to_LCD(0x80, COMMAND);
    write_to_LCD(0x01, COMMAND);
    a = 0;
    while(warn_NSped[a] != '')
    write_to_LCD(warn_NSped[a++], LCD_DATA);
     
    while(d > 5)
    {
    ns_green = 0;
    ns_yellow = 1;
    ns_red = 1;
    ns_pedestrian = 0; // Pedestrian light for NS is off
    ew_green = 1;
    ew_yellow = 1;
    ew_red = 0;
    LED_Lights_Delay(1);
    write_to_LCD(0x8E, COMMAND);
    write_to_LCD((d + 0x30), LCD_DATA); // 0x30 is added in order to convert the BCD value to ASCII
    d–;
    } // end while
     
    while(d > 1)
    {
    while(ns_pedestrian == 0)
    {
    ns_pedestrian = ~ns_pedestrian;
    MS_Delay(75);
    }// end while
    ns_green = 0;
    ns_yellow = 1;
    ns_red = 1;
    ns_pedestrian = 0; // Pedestrian light for NS is off
    ew_green = 1;
    ew_yellow = 1;
    ew_red = 0;
    LED_Lights_Delay(1);
    a = 0;
    write_to_LCD(0x01, COMMAND);
    while(warn_NSped[a] != '')
    write_to_LCD(warn_NSped[a++], LCD_DATA);
    write_to_LCD((d + 0x30), LCD_DATA); // 0x30 is added in order to convert the BCD value to ASCII
    d–;
    }// end while
     
    while(d > 0)
    {
    ns_green = 1;
    ns_yellow = 0;
    ns_red = 1;
    ns_pedestrian = 1; // Pedestrian light for NS is on
    ew_green = 1;
    ew_yellow = 1;
    ew_red = 0;
    LED_Lights_Delay(1);
    a = 0;
    write_to_LCD(0x01, COMMAND);
    while(warn_NSped[a] != '')
    write_to_LCD(warn_NSped[a++], LCD_DATA);
    write_to_LCD((d + 0x30), LCD_DATA); // 0x30 is added in order to convert the BCD value to ASCII
    d–;
    }// end while
    LED_Lights_Delay(1);
    write_to_LCD(0x01, COMMAND);
    } // end NS_Intersection_Check
     
    void EW_Intersection_Check(int b)
    {
    int c;
    unsigned char code warn_EWped[] = "Time left EW: ";
     
    write_to_LCD(0x06, COMMAND);
    write_to_LCD(0x80, COMMAND);
    write_to_LCD(0x01, COMMAND);
    c = 0;
    while(warn_EWped[c] != '')
    write_to_LCD(warn_EWped[c++], LCD_DATA);
     
    while(b > 5)
    {
    ns_green = 1;
    ns_yellow = 1;
    ns_red = 0;
    ew_green = 0;
    ew_yellow = 1;
    ew_red = 1;
    ew_pedestrian = 0; // Pedestrian light for EW is off
    LED_Lights_Delay(1);
    write_to_LCD(0x8E, COMMAND);
    write_to_LCD((b + 0x30), LCD_DATA); // 0x30 is added in order to convert the BCD value to ASCII
    b–;
    } // end while
     
    while(b > 1)
    {
    while(ew_pedestrian == 0)
    {
    ew_pedestrian = ~ew_pedestrian;
    MS_Delay(75);
    }// end while
    ns_green = 1;
    ns_yellow = 1;
    ns_red = 0;
    ew_green = 0;
    ew_yellow = 1;
    ew_red = 1;
    ew_pedestrian = 0; // Pedestrian light for NS is off
    LED_Lights_Delay(1);
    c = 0;
    write_to_LCD(0x01, COMMAND);
    while(warn_EWped[c] != '')
    write_to_LCD(warn_EWped[c++], LCD_DATA);
    write_to_LCD((b + 0x30), LCD_DATA); // 0x30 is added in order to convert the BCD value to ASCII
    b–;
    }// end while
     
    while(b > 0)
    {
    ns_green = 1;
    ns_yellow = 1;
    ns_red = 0;
    ew_green = 1;
    ew_yellow = 0;
    ew_red = 1;
    ew_pedestrian = 1; // Pedestrian light for NS is on
    LED_Lights_Delay(1);
    c = 0;
    write_to_LCD(0x01, COMMAND);
    while(warn_EWped[c] != '')
    write_to_LCD(warn_EWped[c++],LCD_DATA);
    write_to_LCD((b + 0x30), LCD_DATA); // 0x30 is added in order to convert the BCD value to ASCII
    b–;
    }// end while
    LED_Lights_Delay(1);
    write_to_LCD(0x01, COMMAND);
    } // end EW_Intersection_Check
     
    void write_to_LCD(unsigned char value, bit mode)
    {
    lcd_ready();
    DATA_BITS_LCD = value;
    RS = mode; // Set for data, Reset for command
    RW = 0;
    EN = 1;
    MS_Delay(1);
    EN = 0;
    }// end write to lcd
     
    /////////////////////////////////////////////////////////////////////
    // This wait for the LCD to become ready   //
    /////////////////////////////////////////////////////////////////////
    void lcd_ready(void)
    {
    EN = 1;
    RS = 0; // This is a command
    RW = 1; // This is a read command
    EN = 0;
    MS_Delay(1);
    EN = 1;
    EN = 0;
    RW = 0;
    } // end lcd_ready
     
     
    void MS_Delay(unsigned int itime)
    {
    unsigned int i, j;
     
    for (i = 0; i < itime; i++)
    for (j = 0; j < 1275; j++); //20000 best for scrolling
    }// end MS_Delay
     
    ///////////////////////////////////////////////////////////////////////////////////////////////////////
    // //
    // The LED_Lights_Delay function contains a timer that calculates a 5ms delay. Timer 0, Mode 1     //
    // are the setting used. //
    // //
    ///////////////////////////////////////////////////////////////////////////////////////////////////////
    // Calculate the initial counting value x:
    // In AT89C51RD2 1 machine cycle equals 12 crystal cycles:
    // (12/11.0592MHz)(2^16-x)=25ms
    // solve the equation for x, we obtain:
    // x = [2^16 – 25ms * 11.0592MHz]/12 = 60928=EE00H
     
    void LED_Lights_Delay(unsigned char num)
    {
      unsigned char i, one_second_delay = num * 20;
      for(i = 0; i < one_second_delay; i++)
      {
     TL0 = 0xFD;   // load initial counting value
     TH0 = 0x4B;
     TR0 = 1;      // turn on T0
     while (TF0 == 0);  // wait for timer 0 to overflow
     TR0 = 0;  // turn off timer 0
     TF0 = 0;  // clear TF0 as interrupt is not used, 
             // it will not be cleared by hardware.
      } // end for
    } // end void LED_Lights_Delay
     
    ///////////////////////////////////////////////////////////////////////////////////////////////////////
    // Interupt Service Routine 0       //
    ///////////////////////////////////////////////////////////////////////////////////////////////////////
    void ew_ped_btn(void) interrupt 0 // Port 3.2
    {
      ew_ped_btn_flag = 1;
    }
     
    ///////////////////////////////////////////////////////////////////////////////////////////////////////
    // Interupt Service Routine 1     //
    ///////////////////////////////////////////////////////////////////////////////////////////////////////
    void ns_ped_btn(void) interrupt 2 // Port 3.3
    {
      ns_ped_btn_flag = 1;
    }
     
  • Author
    Posts
Viewing 1 post (of 1 total)
  • You must be logged in to reply to this topic.
Log In

RSS Recent Posts

  • Can I make two inputs from one?? June 21, 2025
  • Beats Solo 4 June 21, 2025
  • Behlke swich June 21, 2025
  • Is AI making embedded software developers more productive? June 21, 2025
  • Simple LED Analog Clock Idea June 21, 2025

Stay Up To Date

Newsletter Signup
EngineersGarage

Copyright © 2025 WTWH Media LLC. All Rights Reserved. The material on this site may not be reproduced, distributed, transmitted, cached or otherwise used, except with the prior written permission of WTWH Media
Privacy Policy | Advertising | About Us

Search Engineers Garage

  • Engineers Garage Main Site
  • Visit our active EE Forums
    • EDABoard.com
    • Electro-Tech-Online
  • Projects & Tutorials
    • Circuits
    • Electronic Projects
    • Tutorials
    • Components
  • Digi-Key Store
    • Cables, Wires
    • Connectors, Interconnect
    • Discrete
    • Electromechanical
    • Embedded Computers
    • Enclosures, Hardware, Office
    • Integrated Circuits (ICs)
    • Isolators
    • LED/Optoelectronics
    • Passive
    • Power, Circuit Protection
    • Programmers
    • RF, Wireless
    • Semiconductors
    • Sensors, Transducers
    • Test Products
    • Tools
  • Advertise