Design of traffic light signal controller based on single chip microcomputer

Publisher:HappyExplorerLatest update time:2011-10-22 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

Design a traffic light signal controller based on a single-chip microcomputer. It is known that there are three lights in red, yellow and green in the four directions of east, west, south and north. There are two digital tubes in the east-west direction and two digital tubes in the north-south direction. The traffic light is required to display and switch according to Table 1, and the remaining time of each state in the east-west and north-south directions is required to be counted down on the digital tube.
Table 1 Traffic light state switching table

North-South Direction

East-West Direction

Serial number

state

Serial number

state

1

The green light is on for 25 seconds, and the red and yellow lights are off

1

The red light is on for 30 seconds, and the green and yellow lights are off

2

The yellow light is on for 5 seconds, and the red and green lights are off

3

The red light is on for 30 seconds, and the green and yellow lights are off

2

The green light is on for 25 seconds, and the red and yellow lights are off

3

The yellow light is on for 25 seconds, and the red and green lights are off

Return to state 1

Return to state 1

3.2.1 Module 1: System Design
(1) Task Analysis and Overall Design Ideas
The functions required to be implemented in the test mainly include timing function, dynamic scanning and state switching.
Timing function: To realize the timing function, you need to use a timer to count. By setting the initial value of the timer to control the time interval of the overflow interrupt, a variable is used to record the number of timer overflows to achieve the function of timing 1 second. When the timing reaches 1 second, the variable storing the remaining time of each state of the east-west and north-south signal lights is reduced by 1. When the variable storing the remaining time is reduced to 0, switch to the next state, and load the initial countdown value of the next state into the timing variable. Start the next state, and repeat the cycle.
Dynamic scanning: You need to use 4 digital tubes to display the countdown numbers of the east-west and north-south respectively. Extract the "tens" and "units" of the numbers storing the remaining time of each state from the variables and display them in the digital tubes using dynamic scanning. The
entire program counts according to the overflow number of the timer. Every 1S, the remaining time of the corresponding state is reduced by 1. When it is reduced to 0, the start of the next state is triggered.
(2) MCU model and required peripheral device models, MCU hardware circuit schematic diagram

Figure 3-5 Traffic light hardware circuit schematic diagram
The MCS51 series AT89S51 single-chip microcomputer is selected as the microcontroller, and two four-connected common cathode digital tubes are selected to form an 8-bit display module. Due to the limited driving capability of the AT89S51 single-chip microcomputer, two 74HC244s are used to realize the bus drive. One 74HC244 completes the control and drive of the common cathode digital tube bit control line, and the other 74HC244 completes the 7-segment code output of the digital tube. A 100-ohm resistor is connected in series to the 7-segment code output port to limit the current of the 7-segment digital tube. The P3.0-P3.5 of the P3 port is used to control the light-emitting diode to realize the display of the traffic light signal. Each light-emitting diode is connected in series with a 500-ohm resistor to limit the current. The hardware circuit schematic diagram is shown in Figure 3-5.
(3) Program design ideas, single-chip microcomputer resource allocation and program flow
① Single-chip microcomputer resource allocation
The P3.0-P3.1 pins of the single-chip microcomputer P3 port are used as outputs to control the display of the light-emitting diode. In the timing module, two array variables (init_sn[3], init_ew[3]) need to be defined to store the initial values ​​of the countdown in different states in the east-west and north-south directions. The traffic lights in each direction in the question have 3 display states, so the number of array elements is 3. Two variables (cnt_ sn, cnt_ ew) need to be defined to temporarily store the remaining countdown time in the east-west and north-south directions.
In the state switching, in order to clarify which state is currently in, a state variable (state_val_sn, state_val_ew) is set in the east-west and north-south directions. When the remaining countdown time reaches zero, the state variable increases by 1, indicating the start of the next state. When the variable increases to 3, it changes to 0 and returns to the state with sequence number 1.
② Program design ideas
In the design, since there is no keyboard function, only the timing counting and dynamic scanning functions are involved. After the main program initializes the variables
, it sets the initial values ​​of the microcontroller timer and interrupt special function registers, sets the working mode of timer T1 to 8-bit automatic
loading mode, and the timer overflows every 250us.
After initializing the variables and registers, the main program enters a loop structure, in which only dynamic scanning is performed, and dynamic scanning and display are performed according to the remaining time in the east-west and north-south directions.
Timing and state switching are realized through the interrupt service program of the timer. In the interrupt service program, every time the timing reaches one second, the remaining time of the current state in each direction is reduced by 1. When it is reduced to 0, the next state is triggered and the traffic light indication is changed.
③Program Flow

(4) Software and hardware debugging plan
Software debugging plan: In the WeiFu software, in "File New File", create a new C language source program file and write the corresponding program. In the "File New Project" menu, create a new project and include the C language source program file in the project file.
In the "Project Compile" menu, compile the C source file and check for syntax errors and logical errors. After successful compilation, target files with "*.hex" and "*.bin" suffixes are generated.
Hardware debugging plan: In the design platform, connect P3.0-P3.5 of the microcontroller to the corresponding bits of the stand-alone keyboard through plug wires.
After compiling the program file into a target file in WeiFu, run the "MCU Download Program", select the corresponding flash data file, and click the "Program" button to download the program file to the Flash of the microcontroller.
Then, power on and restart the microcontroller to check whether the written program meets the requirements of the question and whether it fully and completely completes the content of the test question.

3.2.2 Program design (C language source program for reference only)
//Crystal oscillator: 11.0592M T1-250 microseconds overflow once
/*Variable definition:
show_val_sn,show_val_ew: displayed value 0-59
state_val_sn,state_val_ew: state value north-south direction 0-green light on; 1-yellow light on; 2-red light on
T1_cnt: timer count overflow number
cnt_sn,cnt_ew: countdown value
init_sn[3],init_ew[3] countdown
led_seg_code: digital tube 7-segment code
*/
#include "reg51.h"
sbit SN_green=P3^2 ;//North-South direction green light
sbit SN_yellow=P3^1 ;//North-South direction yellow light
sbit SN_red=P3^0 ;//North-South direction red light
sbit EW_green=P3^5 ;//East-West direction green light
sbit EW_yellow=P3^4 ;//East-West direction yellow light
sbit EW_red=P3^3 ;//East-West direction red light
unsigned char data cnt_sn,cnt_ew;
unsigned int data T1_cnt;
unsigned char data state_val_sn,state_val_ew;
char code led_seg_code[10]={0x3f,0x06,0x05b,0x04f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
char code init_sn[3]={24,4,29};
char code init_ew[3]={29,24,4};
//------------------------
void delay(unsigned int i)//delay
{ while(--i); }
//------------------------
void led_show(unsigned int u,unsigned int v)
{ unsigned char i;
i=u%10; //temporarily store the ones digit
P0=led_seg_code[i];
P2=0xbf;
delay(100); //delay
i=u%100/10; //temporarily store the tens digit
P0=led_seg_code[i];
P2=0x7f;
delay(100); //delay
i=v%10; //temporarily store the ones digit
P0=led_seg_code[i];
P2=0xfe;
delay(100); //delay
i=v%100/10; //temporarily store the tens digit
P0=led_seg_code[i];
P2=0xfd;
delay(100); //delay
}
//-------------------------
void timer1() interrupt 3 //T1 interrupt
{ T1_cnt++;
if(T1_cnt>3999) //If the count>3999, time 1s
{ T1_cnt=0;
if (cnt_sn!=0) //North-South timing
{ cnt_sn--; }
else
{ state_val_sn++;
if (state_val_sn>2) state_val_sn=0;
cnt_sn=init_sn[state_val_sn];
switch (state_val_sn) //Refresh the status of each signal light according to the state value
{ case 0: SN_green=0 ;//Green light in the north-south directionSN_yellow
=1 ;//Yellow light in the north-south directionSN_red
=1 ;//Red light in the north-south directionbreak
;
case 1: SN_green=1 ;//Green light in the north-south directionSN_yellow
=0 ;//Yellow light in the north-south directionSN_red
=1 ;//Red light in the north-south
directionbreak;
case 2:SN_green=1 ;//Green light in the north-south directionSN_yellow
=1 ;//Yellow light in the north-south directionSN_red
=0 ;//Red light
break in north-south direction;
}
}
if (cnt_ew!=0) //Timer in east-west direction
{ cnt_ew--; }
else
{ state_val_ew++;
if (state_val_ew>2) state_val_ew=0;
cnt_ew=init_ew[state_val_ew];
switch (state_val_ew) //Refresh the status of each signal light according to the state value
{ case 0: EW_green=1 ;//Green light in the east-west directionEW_yellow
=1;//Yellow light in the east-west
directionEW_red=0 ;//Red light in the east-west directionbreak
;
case 1: EW_green=0 ;//Green light in the east-
west directionEW_yellow=1 ;//Yellow light in the east-west
directionEW_red=1 ;//Red light in the east-west directionbreak
;
case 2: EW_green=1 ;//Green light in the east-west
directionEW_yellow=0 ;//Yellow light in the east-west
directionEW_red=1 ;//Red light in the east-west directionbreak
;
}
}
}
}
//-------------------------
main()
{//Initialize variables
cnt_sn=init_sn[0];
cnt_ew=init_ew[0];
T1_cnt=0;
state_val_sn=0; //After startup, the default state is 1
state_val_ew=0;
//Initialize the state of each light
SN_green=0 ;//Green light on in the north-south direction SN_yellow=
1 ;//Yellow light off in the north-south direction
SN_red=1 ;//Red light off in the north-south direction
EW_green=1 ;//Green light off in the east-west direction
EW_yellow=1;//Yellow light off in the east-west direction
EW_red=0 ;//Red light on in the east-west direction
//Initialize 51's register
TMOD=0x20;//Use T1 to time 8-bit automatic loading timing mode
TH1=0x19;//0x4b; //Overflow once every 500 microseconds; 250=(256-x)*12/11.0592 -> x= 230.4
TL1=0x19;
EA=1; //Open interrupt
ET1=1;
TR1=1; //Open timer T1
while(1)
{ led_show(cnt_sn,cnt_ew);}}
//End of main program

Reference address:Design of traffic light signal controller based on single chip microcomputer

Previous article:Design and production of taximeter
Next article:Design of Simple Timing Alarm Circuit Controlled by Single Chip Microcomputer

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号