Traffic light designed based on STC12C5A32S2 microcontroller

Publisher:太白山人Latest update time:2012-10-24 Source: 21ic Keywords:STC12C5A32S2 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

/*===Chip:STC12C5A32S2==================================*/
/*===Software:Keil 4 C51=======================================*/
/*===Author:Liang Peng==============================================*/
/*===Organization:College of Physics and Electronic Engineering,Guangxi University for Nationalities,07 Automation==========================*/
/*===Date:May 26, 2010==================================*/
/*===Version:1.0==========================================*/
/*===Statement: This program is an original work and is only for learning and communication. It may not be used for commercial purposes. If you need to
reprint it, please indicate the source. Thank you for your cooperation! ===============================*/
///*----------File name: Traffic_light.c-------------*/

#include
#include
#include<LCD1602.H>
#define  ON  0
#define  OFF  1
#define  yellow_time  3
//===========================
sbit MODE_KEY=P3^3; //Mode key for adjusting time
sbit TIME_ADD=P3^4; //button to increase the time by 5 seconds
sbit TIME_DEC=P3^5; //button to reduce the time by 5 seconds
sbit LOCK_KEY=P3^6; //Lock/Restore Traffic Button

sbit North_turn_R= P0^0; //Red light for north-south left turn
sbit North_turn_G= P0^1; //North-South left turn green light
sbit North_Y = P0^2; //North and south yellow lights
sbit North_R = P0^3; //Red light for north-south straight driving
sbit North_G = P1^0; //North-South straight green light

sbit East_turn_R = P1^1; //Red light for turning left from east to west
sbit East_turn_G = P1^2; //Green light for east-west left turn
sbit East_Y = P1^3; //East and West yellow lights
sbit East_R = P1^4; //Red light for east-west driving
sbit East_G = P1^5; //Green light for east-west straight ahead
//===========================
void delay_ms(uint); //delay sub-function
void init(); //Program timer interrupt external interrupt initialization
void system_on(); //The system is powered on, and the two yellow lights flash for 3 seconds.
void display_lcd(); //LCD display sub-function
void display_light(); //LED light display
void judge(); //judgment function for each state switching
void yellow_light(); //Sub-function that flashes the yellow light for 3 seconds
void GoStraight(); //Sub-function for the straight path
void TurnLeft(); //Sub-function of the left turn
//===========================
bit direction; //0->North-South direction; 1->East-West direction;
bit flash_flags; // flashing status variable
bit change_flags; //Timeout variable
bit Lock_flags; //0->Traffic is not blocked; 1->Traffic is blocked
uint ovf_cnt; //Timer interrupt count variable
uchar mode_flags; //key changes variables
uchar run_flags; //Runtime status variables
uchar time_temp; //timing time cache
uchar north_green_time=10; //initialize the north-south straight green light time
uchar east_green_time=10; //initialize the green light time for east-west straight travel

/*The left turn green light time is equal to the passing green light time*/
/*-Red light time is green light time plus yellow light time-*/
/*------The yellow light time is fixed at 3 seconds-----*/
//uchar north_red_time;
//uchar east_red_time;
//uchar north_turn_time;
//uchar east_turn_time;
//===========================
//When the red light switches to green, the yellow light does not need to flash
//When the green light switches to the red light, the yellow light needs to flash
//When the yellow light flashes, the green light does not go out
//Red light time = green light time + yellow light time
//Left turn time = straight green light time
//============================================
//********************************************
void main()
{
 init(); //Timed interrupt external interrupt initialization
 LCD_init(); //LCD1602 initialization
 /*When running for the first time, the yellow light flashes for three seconds*/
 LCD_str(1,1,"System on ...");
 time_temp=yellow_time;
 system_on(); //Two yellow lights flashing sub-function
 while(1)
 {
  judge();
  /*LED light display*/
  display_light();
  /*1602 display*/
  display_lcd();
 }
}
//*********************************************
//=============================================
void init()
{
 TMOD|=0x02; //Timer 0 mode 2, automatic reload
 TH0=5;TL0=5; //250us interruption
 ET0=1; //Enable timer 0 interrupt
 TCON|=0x01; //External interrupt 0 falling edge trigger
 EX0=1; //Enable external interrupt 0
 EA=1; //Enable global interrupt
 TR0=1; //Start timer 0
}
//===========================
void system_on() //After power on, the yellow light at the east, west, south, and north intersections flashes for 3 seconds
{
 do{
  if(flash_flags) {North_Y=OFF;East_Y=OFF;}
  else   {North_Y=ON; East_Y=ON; } 
 }while(!change_flags); //The first time is not completed and the status is not changed
}
//===========================
void display_lcd()
{ 
 if((!mode_flags)&&(!Lock_flags)){//Normal display status
  switch(run_flags){
  case 0:case 2:case 4:case 6:case 8: //yellow light flashing state
   if(!direction){ 
    if(flash_flags) {LCD_str(1,1,"                ");}
    else   {LCD_str(1,1,"Status Change...");}
   }else{
    if(flash_flags) {LCD_str(1,1,"                ");}
    else   {LCD_str(1,1,"Status Change...");}
   }
   break;
  case 1: case 5: //straight state
   if(!direction) {LCD_str(1,1,"North GoStraight");}
   else   {LCD_str(1,1,"East  GoStraight");}
   break;
  case 3: case 7: //Left turn state
   if(!direction) {LCD_str(1,1,"North Turn Left ");}
   else   {LCD_str(1,1,"East  Turn Left ");}
   break;
  }
  LCD_str(2,6," "); //Clear the second line
 }
 if((mode_flags==1)&&(!Lock_flags)){//Change the north-south direct time status
  LCD_str(1,1,"North Green Time");
  LCD_str(2,6,"To: ");
  LCD_int_(north_green_time);
  LCD_str_("S  ");
 }
 if((mode_flags==2)&&(!Lock_flags)){//Change the state of the east-west direct time
  LCD_str(1,1,"East  Green Time");
  LCD_str(2,6,"To: ");
  LCD_int_(east_green_time);
  LCD_str_("S  ");
 }
 if(!Lock_flags){/*Display the remaining time of the current state*/
  LCD_int(2,1,time_temp);
  LCD_char(' ');
  LCD_str(2,4,"S  ")
 }
 if(Lock_flags){ //Display when traffic is blocked
  if(flash_flags){
   LCD_str(1,1,"                ");
   LCD_str(2,1,"                ");
  }else{
   LCD_str(1,1," Please Wait... ");
   LCD_str(2,1,"   Locking...   ");
  }
 }
}
//===========================
void display_light()
{
 if(!Lock_flags){//When traffic is not blocked, the traffic lights are displayed according to the following procedures
  /*The following is determined by the run_flags status to determine whether the light is on or off*/
  switch(run_flags){
   case 0:case 2:case 4:case 6:case 8:
    yellow_light(); break;
   case 1:case 5:
    GoStraight(); break;
   case 3:case 7:
    TurnLeft();  break;
   default: run_flags=1;    
  }
  /*Red light on and off*/
  if(!direction){ //When traveling north and south, all green lights on the east and west sides are off and all red lights are on
   East_R=ON; East_turn_R=ON;
   East_G=OFF; East_turn_G=OFF;
   East_Y=OFF;
  }else{ //When traveling east-west, all green lights on the north and south sides are off and all red lights are on
   North_R=ON; North_turn_R=ON;
   North_G=OFF;North_turn_G=OFF;
   North_Y=OFF;
  }
 }else{//When traffic is blocked, all red lights are on
  North_R=ON; North_turn_R=ON;
  North_G=OFF;North_turn_G=OFF;
  North_Y=OFF;East_Y=OFF;
  East_R=ON; East_turn_R=ON;
  East_G=OFF; East_turn_G=OFF; 
 }
}
//===========================
void yellow_light() /*Sub-function of yellow light flashing*/
{
 if(!direction){
  //North_G=OFF; //Green light for north-south driving is off
  //North_turn_G=OFF;
  if(flash_flags) {North_Y=OFF;}
  else   {North_Y=ON; }
 }else{
  //East_G=OFF;
  //East_turn_G=OFF;
  if(flash_flags) {East_Y=OFF; }
  else   {East_Y=ON;  }
 } 
}
//===========================
void GoStraight(){ /*Subfunction of the straight path*/
 if(!direction){
  North_Y=OFF; //North and South yellow lights are off
  North_turn_R=ON; //Red light on for north-south left turn
  North_R=OFF; //Red light for north-south driving is off
  North_G=ON; //Green light for north-south straight driving is on     
 }else{
  East_Y=OFF;
  East_turn_R=ON;
  East_R=OFF;
  East_G=ON;   
 }
}
//===========================
void TurnLeft() /*Subfunction of the left turn*/
{
 if(!direction){
  North_G=OFF; //Green light for north-south driving is off
  North_R=ON; //Red light for north-south driving is on
  North_turn_R=OFF; //Red light turns off for north-south left turn
  North_turn_G=ON; //North-South left turn green light is on   
 }else{
  East_G=OFF;
  East_R=ON;
  East_turn_R=OFF;
  East_turn_G=ON;  
 }
}
//===========================
void judge() /*State switching judgment function*/
{
  if(change_flags){ //Status timing end flag
   change_flags=0; //Status flag clear
   run_flags++; //State switch
   if((run_flags==5)||(run_flags==9))direction=~direction;
   switch(run_flags){ //status time assignment
    case 0:case 2:case 4:case 6:case 8: //yellow light flashes
     time_temp=yellow_time;break;
    case 1:time_temp=north_green_time;break;//straight north and south
    case 5:time_temp=east_green_time;break; //Go east and west    
    case 3:time_temp=north_green_time;break;//Turn left from north to south
    case 7:time_temp=east_green_time;break; //Turn left     
    default:run_flags=1;time_temp=north_green_time;
   }
  }
}
//===========================
void keys_scan() interrupt 0 /*key scan subfunction*/
{
 delay_ms(20);
 if(!INT0){
  if(!MODE_KEY){
   if(++mode_flags>=3)mode_flags=0;
  }
  if(!TIME_ADD){
   if(mode_flags==1)north_green_time+=5;
   if(mode_flags==2)east_green_time+=5;
   if(north_green_time>90)north_green_time=5;
   if(east_green_time>90)east_green_time=5;
  }
  if(!TIME_DEC){
   if(mode_flags==1)north_green_time-=5;
   if(mode_flags==2)east_green_time-=5;
   if(north_green_time>90)north_green_time=5;
   if(east_green_time>90)east_green_time=5;   
  }
  /*You can also add a status here to change the left turn time*/
  if(!LOCK_KEY)Lock_flags=~Lock_flags; 
 }
 INT0=1;
}
//===========================
void T0_isr() interrupt 1
{
 if(ovf_cnt==1846)flash_flags=~flash_flags;
 if(++ovf_cnt>=3691){
  ovf_cnt=0;
  flash_flags=~flash_flags;
  if(!Lock_flags)if(time_temp--==1)change_flags=1;
 }
} 
//===========================
void delay_ms(uint xms)
{
 uint i,j;
 for(i=0;i
/*--------------File name: LCD1602.H----------------*/
#ifndef  _LCD1602_H_
#define  _LCD1602_H_
#define  uint unsigned int
#define  uchar unsigned char
#define LCM_P P2 //LCD control port
#define LCM_DATA P0 //LCD data port
#define  LCM_RS_0  LCM_P&=~(1<<5)
#define  LCM_RS_1  LCM_P|=  1<<5
#define  LCM_RW_0  LCM_P&=~(1<<6)
#define  LCM_RW_1  LCM_P|=  1<<6 
#define  LCM_EN_0  LCM_P&=~(1<<7)
#define  LCM_EN_1  LCM_P|=  1<<7
/*========================================*/
#define  LCD_str(x,y,s) Locate(x,y);LCD_str_(s);
#define  LCD_int(x,y,n) Locate(x,y);LCD_int_(n);
/*========================================*/
void LCD_init(); //LCM1602 initialization function, must be called before using 1602
void Locate(uchar,uchar); //Display positioning function
void LCD_half(uchar); //send half byte function
void LCD_char(uchar); //Write a character function
void LCD_cmd(uchar); //write command function
void LCD_int_(int); //Write integer data function
void LCD_str_(uchar str[]); //Write string data function
void LCD_delay(uint); //delay sub-function
/******************************************/
/*-Function: LCD usage initialization---------------*/
/*-Entry parameters: None*/
/******************************************/
void LCD_init()
{
 LCD_cmd(0x01);
 LCD_delay(10);
 LCD_cmd(0x28); //4-bit data, double-line display, 5*7 (0x38 is eight bits)
 LCM_EN_1;_nop_();_nop_();_nop_();
 LCM_EN_0; /*These two sentences must be added here*/
 LCD_delay(10);
 LCD_cmd(0x28);
 LCD_cmd(0x06);
 LCD_cmd(0x0c);
 LCD_cmd(0x01);
 LCD_delay(10);
}
/******************************************/
/*-Function: Display data positioning function-------------*/
/*-Entry parameters: row coordinate x, column coordinate y-------------*/
/******************************************/ 
void Locate(fly x,fly y)
{
 x&=0x01;
 LCD_cmd((x==0)?(y+0xbf):(y+0x7f));
}
/******************************************/
/*-Function: Send half byte function-----------------*/
/*- Entry parameter: To be written to the LCD instruction or data register -*/
/* The upper or lower four bits of the byte---------*/
/******************************************/
void LCD_half(uchar dataw_)
{
 LCM_DATA=(LCM_DATA&0x0f)|(dataw_);
 LCM_EN_1;_nop_();_nop_();_nop_();
 LCM_EN_0;
 LCD_delay(1); //Add 10ms delay in actual use
}
/******************************************/
/*-Function: Write one bit of data function---------------*/
/*-Entry parameters: data content---------------------*/
/******************************************/
void LCD_char(uchar dataw)
{
 LCM_RS_1;LCM_RW_0;LCM_EN_0;_nop_();
 LCD_half(dataw&0xf0);
 LCD_half(dataw<<4);  
}
/*========================================*/
void LCD_cmd(uchar cmd)
{ 
 LCM_RS_0;LCM_RW_0;LCM_EN_0;_nop_();
 LCD_half(cmd&0xf0);
 LCD_half(cmd<<4);
} 
/*========================================*/
void LCD_str_(uchar *str)
{
 while(*str)LCD_char(*str++);
}
/*========================================*/
void LCD_int_(int num)
{
 uchar counter=0,num_str[10],neg_flags=0;
 if(num<0){num=-num,neg_flags=1;}
 do{
  num_str[counter++]=num%10+0x30;
  num/=10;
 }while(num!=0);//After the loop ends, the value of counter is 1 more than the number  
 if(neg_flags){num_str[counter++]='-';}
 while(counter) 
 {   
  LCD_char(num_str[--counter]);
 }//The value of counter must be reduced by one first
}
/*========================================*/
void LCD_delay(uint xus)
{
 uint i=0,j=0;
 for(i=0;i
Keywords:STC12C5A32S2 Reference address:Traffic light designed based on STC12C5A32S2 microcontroller

Previous article:Multiplication formula simulation answering device based on 51 single chip microcomputer
Next article:Program and debugging of driving AD9954 with C51 single chip microcomputer

Recommended ReadingLatest update time:2024-11-16 22:35

AVR microcontroller proteus simulation lesson 3: single digital tube
Select 7SEG-MPX1-CC for the digital tube, a common cathode digital tube. That is, low level selection (rightmost pin), the 8 pins on the left side of the digital tube, from left to right, from low to high, are connected to PC0---PC7 respectively. The c program is as follows: #include avr/io.h #include util/delay.h
[Microcontroller]
AVR microcontroller proteus simulation lesson 3: single digital tube
Summary of MCU key learning
Basic key program structure analysis:  1 void key_scan(void)  2 {  3 if (!key) //Check if the key is pressed  4       {  5 delay_ms(20); //delay debounce, usually 20ms  6           if(!key)  7           {  8                ......  9           } 10 while (!key); //Wait for the key to be released 11      }        12 }
[Microcontroller]
The difference between CPU, MPU, MCU and SOC (concept)
 1. CPU (Central Processing Unit) is the computing core and control core of a computer. CPU is composed of arithmetic unit, controller and register and the data, control and status bus that realize the connection between them. The operation principle of almost all CPUs can be divided into four stages: fetch, decode, e
[Microcontroller]
STM8 MCU Engineer's Path 2 - AND or NOT Operation Bit
Problems with AND or NOT operations PE_ODR &= 0XFE; //Clear the 0th bit, output low level PD_ODR &= 0XF7; //Clear the 3rd bit, output low level I didn't know why it was written like this before! Now let's explain &=0XFE=1111 1110, that is, when using &= again, the upper 7 bits are to be determined, and the 0th
[Microcontroller]
Design of infrared counter based on 51 microcontroller
1. System Overview The modules used in the system include AT89C51 microcontroller + LCD1602 display screen + photoelectric switch + button + small light + buzzer. This design uses 51 microcontroller as the core control, and uses LCD1602 to display numerical values. The photoelectric switch of the system controls the
[Microcontroller]
Design of infrared counter based on 51 microcontroller
How to implement the upper control algorithm analysis solution of ARM9 microcontroller
introduction In many embedded control systems, the system must complete a large amount of information collection and complex algorithms, and also realize precise control functions. The ARM9 microcontroller running the embedded Linux operating system is used to complete signal collection and implement the upper-
[Microcontroller]
How to implement the upper control algorithm analysis solution of ARM9 microcontroller
51 single chip microcomputer + PWM control gradient colorful lights C51 program
1. Hardware Introduction: (Using 5050LED 2W) The RGB three-color LED control pins are P1.2, P1.1, and P1.0 of the microcontroller. The positive pole of the LED is connected to the positive pole of the main power supply (24V), and the negative pole is connected to the collector of the three transistors. The control pin
[Microcontroller]
51 single chip microcomputer + PWM control gradient colorful lights C51 program
PIC Microcontroller Quick Start
  PIC16F616 is a 14-pin, 8-bit CMOS microcontroller. It uses a reduced instruction set with only 35 instructions. Due to the use of the Harvard bus structure with separate data bus and instruction bus, most of the instructions are single-cycle instructions except for a few instructions that are not single-cycle. This
[Microcontroller]
Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号