/* Name: Timer controlled traffic light
Note: After the east-west green light is on for 5 seconds, the yellow light flashes, and then the red light flashes 5 times.
After the red light comes on, the north-south direction turns from red to green, and after 5 seconds the north-south direction lights flash yellow.
After flashing 5 times, the red light turns on, and the east-west light turns on green, and so on.
*/
#include
#define uchar unsigned char
#define uint unsigned int
sbit RED_A=P0^0; //East-West direction indicator
sbit YELLOW_A=P0^1;
sbit GREEN_A=P0^2;
sbit RED_B=P0^3; //North-South direction indicator light
sbit YELLOW_B=P0^4;
sbit GREEN_B=P0^5;
//Delay multiple, number of flashes, operation type
variable
uchar Time_Count=0,Flash_Count=0,Operation_Type=1;
//Timer 0 interrupt function
void T0_INT() interrupt 1
{
TL0=-50000/256;
TH0=-50000%256;
switch(Operation_Type)
{
case 1: //The east-west green light and the north-south red light are on for 5 seconds
RED_A=0;YELLOW_A=0;GREEN_A=1;
RED_B=1;YELLOW_B=0;GREEN_B=0;
if(++Time_Count!=100) return; //5s (100*50ms) switch
Time_Count=0;
Operation_Type=2;
break; [page]
case 2: //The east-west yellow light starts flashing and the green light turns off
if(++Time_Count!=8) return;
Time_Count=0;
YELLOW_A=~YELLOW_A;GREEN_A=0;
if(++Flash_Count!=10) return; //Flash
Flash_Count=0;
Operation_Type=3;
break;
Case 3: //The east-west red light and the north-south green light are on for 5 seconds
RED_A=1;YELLOW_A=0;GREEN_A=0;
RED_B=0;YELLOW_B=0;GREEN_B=1;
if(++Time_Count!=100) return; //5s (100*50ms) switch
Time_Count=0;
Operation_Type=4;
break;
case 4: //The yellow light in the north-south direction starts flashing, and the green light turns off
if(++Time_Count!=8) return;
Time_Count=0;
YELLOW_B=~YELLOW_B;GREEN_A=0;
if(++Flash_Count!=10) return; //Flash
Flash_Count=0;
Operation_Type=1;
break;
}
}
//Main program
void main()
{
TMOD=0x01; //T0 mode 1
IE=0x82;
TR0=1;
while(1);
}
Previous article:Microcontroller C language programming: alarm and rotating light
Next article:New IC and topology break through automotive data network bottleneck
- Popular Resources
- Popular amplifiers
- Learn ARM development(16)
- Learn ARM development(17)
- Learn ARM development(18)
- Embedded system debugging simulation tool
- A small question that has been bothering me recently has finally been solved~~
- Learn ARM development (1)
- Learn ARM development (2)
- Learn ARM development (4)
- Learn ARM development (6)
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
- Detailed explanation of intelligent car body perception system
- How to solve the problem that the servo drive is not enabled
- Why does the servo drive not power on?
- What point should I connect to when the servo is turned on?
- How to turn on the internal enable of Panasonic servo drive?
- What is the rigidity setting of Panasonic servo drive?
- How to change the inertia ratio of Panasonic servo drive
- What is the inertia ratio of the servo motor?
- Is it better for the motor to have a large or small moment of inertia?
- What is the difference between low inertia and high inertia of servo motors?
- SimpleLink MCU code migration guide: CC1310 from VQFN48 (7×7) to VQFN32 (5×5) code migration process reference
- Welcome the new moderator of the "201420208012" eSports
- What is the difference between a servo motor and a brushless DC motor?
- [Raspberry Pi Pico Review] ADC External Benchmark vs. Onboard Benchmark
- Do DC powered devices have common mode interference?
- Allegro photo-painting problem
- The PCIE communication problem that has troubled me for three days - please help!
- Watch the live broadcast and win an oscilloscope | Today at 2 pm RIGOL live broadcast [Oscilloscope, programmable DC power supply, spectrum analyzer application...
- Typical Vacuum Cleaner/Robot Sweeper BMS Topology
- TI's crystal-free SimpleLink wireless MCU helps you easily achieve crystal-free