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
Previous article:Design and production of taximeter
Next article:Design of Simple Timing Alarm Circuit Controlled by Single Chip Microcomputer
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Innolux's intelligent steer-by-wire solution makes cars smarter and safer
- 8051 MCU - Parity Check
- How to efficiently balance the sensitivity of tactile sensing interfaces
- What should I do if the servo motor shakes? What causes the servo motor to shake quickly?
- 【Brushless Motor】Analysis of three-phase BLDC motor and sharing of two popular development boards
- Midea Industrial Technology's subsidiaries Clou Electronics and Hekang New Energy jointly appeared at the Munich Battery Energy Storage Exhibition and Solar Energy Exhibition
- Guoxin Sichen | Application of ferroelectric memory PB85RS2MC in power battery management, with a capacity of 2M
- Analysis of common faults of frequency converter
- In a head-on competition with Qualcomm, what kind of cockpit products has Intel come up with?
- Dalian Rongke's all-vanadium liquid flow battery energy storage equipment industrialization project has entered the sprint stage before production
- Allegro MicroSystems Introduces Advanced Magnetic and Inductive Position Sensing Solutions at Electronica 2024
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- New real-time microcontroller system from Texas Instruments enables smarter processing in automotive and industrial applications
- TMS320C2X/C5X Memory Mode
- Seeking the test standard for constant pressure audio broadcasting
- Analysis of the whole process of IAR microcontroller programming software from project establishment to program burning
- Atom development board module for sale
- EEWORLD University Hall----Live Replay: Smaller, faster, more reliable connectors drive new developments in IoT applications
- How to achieve aperture tuning
- Black Gold's FPGA is really bad
- This amplifier is a bit figurative, isn't it?
- ad19.0.10 3D display problem
- LPC1114 temperature sensor DS18B20 program