/* Name: LED simulated traffic light
Note: The east-west direction green light is on for a few seconds, the yellow
The light flashes 5 times and then turns red.
The northbound traffic light changes from red to green, and after a few seconds the northbound
The yellow light flashes 5 times and then turns red.
Green light, repeat.
*/
#include
#define uchar unsigned char
#define uint unsigned int
sbit RED_A=P0^0; //East-West direction light
sbit YELLOW_A=P0^1;
sbit GREEN_A=P0^2;
sbit RED_B=P0^3; //North-South lights
sbit YELLOW_B=P0^4;
sbit GREEN_B=P0^5;
uchar Flash_Count=0,Operation_Type=1; //Number of flashes, operation type variable
//Delay
void DelayMS(uint x)
{
uchar i;
}
while(x--) for(i=0;i<120;i++);
//Traffic light switching
void Traffic_Light()
{
switch(Operation_Type)
{
case 1: //The east-west direction is green and the north-south direction is red
RED_A=1;YELLOW_A=1;GREEN_A=0;
RED_B=0;YELLOW_B=1;GREEN_B=1;
DelayMS(2000);
Operation_Type=2;
break;
case 2: //The yellow light flashes in the east-west direction, and the green light is off
DelayMS(300);
YELLOW_A=~YELLOW_A;GREEN_A=1;
if(++Flash_Count!=10) return; // Flash 5 times
Flash_Count=0;
Operation_Type=3;
break; [page]
case 3: //The east-west direction is red, and the north-south direction is green
RED_A=0;YELLOW_A=1;GREEN_A=1;
RED_B=1;YELLOW_B=1;GREEN_B=0;
DelayMS(2000);
Operation_Type=4;
break;
Case 4: //The yellow light flashes 5 times in the north-south direction
DelayMS(300);
YELLOW_B=~YELLOW_B;GREEN_B=1;
if(++Flash_Count!=10) return;
Flash_Count=0;
Operation_Type=1;
}
}
//Main program
void main()
{
while(1)Traffic_Light();
}
Previous article:Single-chip C language programming: a single digital tube cyclically displays 0~9
Next article:Common delay and interrupt problems of single chip microcomputer and their solutions
- Popular Resources
- Popular amplifiers
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- 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
- [Mill Edge AI Computing Box FZ5 Review] Development Board Target Classification Demo
- [Domestic RISC-V Linux Board Fang·Starlight VisionFive Trial Report] GPIO Development Basics: From Principle to Practice
- Please ask senior programmers to help look at this problem
- How to use MSP430 to implement PWM signal
- Internal circuits of CH551, CH552, CH554, CH559
- Application issues of optocouplers in switch acquisition
- Noise Countermeasure Solution for Quick Charging Circuit
- MicroPython adds self-power and power consumption parameters to USB
- When do you use the RF interface? When do you use the Bluetooth interface?
- DONT_TOUCH Constraint