AI writing code is no longer a novelty, and the number of people who can write quickly and in large quantities is increasing. However, behind the myth of “efficient development,” the reality for teams is that code reviews are becoming increasingly difficult, collaboration is becoming more chaotic, and quality control is becoming more challenging. Therefore, the author of this article shares this technical article from the perspective of what can be practically demonstrated and realized. Below, this design will be highlighted.
This design is an intelligent traffic light control system based on the 51 microcontroller. It achieves automatic switching of traffic lights for east-west and north-south directions and displays the remaining time using timer interrupts and dynamic display technology. The system adopts a modular design, characterized by simplicity, high reliability, and strong scalability, making it a fundamental unit of intelligent traffic systems.
1. System Hardware Design
Core Control Unit: The 8051 series microcontroller is used as the main controller, with a 12MHz crystal oscillator providing a stable clock.
Peripheral Interfaces:
• Traffic Light Control: East-west red, yellow, and green lights (P2.0-P2.2)
• North-south red, yellow, and green lights (P2.5-P2.7)
• Four-digit display module (P1.0-P1.3 for digit selection, P0 for segment selection)
2. System Software Design
Core Function Modules:
• Timer Interrupt Module: Timer 0 (16-bit mode) is used to achieve a 10ms reference timing, generating a 1-second timer by accumulating 100 interrupts.
• Traffic Light State Machine: Implements a four-stage cyclic control, including east-west green light, yellow light, north-south green light, and yellow light, with each stage lasting a certain time before switching to the next stage.
• Dynamic Display of the Digital Tube: Time-division multiplexing technology is used to switch display positions every 10ms, allowing the four-digit display to show different content simultaneously.
Emergency Mode: When an emergency situation is detected, the system will immediately switch to emergency mode, turning on red lights in all directions to prohibit all vehicles from passing until the emergency is resolved.
Night Mode: During nighttime or low traffic flow periods, the system can switch to night mode, where the yellow light flashes to remind drivers to pay attention to safety.
3. Code Example
#include <reg51.h> // Define traffic light pins<br/>sbit RED_EW = P2^0;<br/>sbit YELLOW_EW = P2^1;<br/>sbit GREEN_EW = P2^2;<br/>sbit RED_NS = P2^5;<br/>sbit YELLOW_NS = P2^6;<br/>sbit GREEN_NS = P2^7;<br/>// Define digital tube pins<br/>sbit DIGIT1 = P1^0;<br/>sbit DIGIT2 = P1^1;<br/>sbit DIGIT3 = P1^2;<br/>sbit DIGIT4 = P1^3;<br/>// Digital tube segment code table<br/>unsigned char code SEGMENT[] = {0x3F, 0x06, 0x5B, 0x4F, 0x66, 0x6D, 0x7D, 0x07, 0x7F, 0x6F};<br/>// Global variable<br/>unsigned int time_left;<br/>void Timer0_Init() {<br/> TMOD |= 0x01; // Timer 0 mode 1<br/> TH0 = 0xFC; // Timer initial value, controls timing<br/> TL0 = 0x18; ET0 = 1; // Enable Timer 0 interrupt<br/> EA = 1; // Enable global interrupt<br/> TR0 = 1; // Start Timer 0<br/>}<br/>void TrafficLightControl() {<br/> while (1) {<br/> // East-west green light<br/> RED_EW = 0; YELLOW_EW = 0; GREEN_EW = 1;<br/> RED_NS = 1; YELLOW_NS = 0; GREEN_NS = 0;<br/> time_left = 20; // Green light duration is 20 seconds<br/> while (time_left > 0) {<br/> // Update digital tube display<br/> // Handle key input<br/> // Wait 1 second<br/> }<br/> // East-west yellow light<br/> RED_EW = 0; YELLOW_EW = 1; GREEN_EW = 0;<br/> RED_NS = 1; YELLOW_NS = 0; GREEN_NS = 0;<br/> time_left = 3; // Yellow light duration is 3 seconds<br/> while (time_left > 0) {<br/> // Update digital tube display<br/> // Handle key input<br/> // Wait 1 second<br/> }<br/> // North-south green light<br/> RED_EW = 1; YELLOW_EW = 0; GREEN_EW = 0;<br/> RED_NS = 0; YELLOW_NS = 0; GREEN_NS = 1;<br/> time_left = 15; // Green light duration is 15 seconds<br/> while (time_left > 0) {<br/> // Update digital tube display<br/> // Handle key input<br/> // Wait 1 second<br/> }<br/> // North-south yellow light<br/> RED_EW = 1; YELLOW_EW = 0; GREEN_EW = 0;<br/> RED_NS = 0; YELLOW_NS = 1; GREEN_NS = 0;<br/> time_left = 3; // Yellow light duration is 3 seconds<br/> while (time_left > 0) {<br/> // Update digital tube display<br/> // Handle key input<br/> // Wait 1 second<br/> }<br/> }}<br/>void main() {<br/> Timer0_Init();<br/> TrafficLightControl();<br/>}