C language reference source code:
/************************************************************
Crossroad traffic light control C program
**********************************************************/
#define uchar unsigned char
#define uint unsigned int
#include /Define control bits*****************/ sbit Time_Show_LED2=P2^5; //Time_Show_LED2 control bit sbit Time_Show_LED1=P2^4; //Time_Show_LED1 control bit sbit EW_LED2=P2^3; //EW_LED2 control bit sbit EW_LED1=P2^2; //EW_LED1 control bit sbit SN_LED2=P2^1; //SN_LED2 control bit sbit SN_LED1=P2^0; //SN_LED1 control bit sbit SN_Yellow=P1^6; //SN yellow light sbit EW_Yellow=P1^2; //EW yellow light sbit EW_Red=P1^3; //EW red light sbit SN_Red=P1^7; //SN red light sbit EW_ManGreen=P3^0; //EW sidewalk green light sbit SN_ManGreen=P3^1; //SN pedestrian green light sbit Special_LED=P2^6; //Normal traffic indicator light sbit Busy_LED=P2^7; //Busy traffic indicator light sbit Nomor_Button=P3^5; // Normal traffic button sbit Busy_Btton=P3^6; //Busy traffic button sbit Special_Btton=P3^7; // Traffic special button sbit Add_Button=P3^3; //Time addition sbit Reduces_Button=P3^4; //Time reduction bit Flag_SN_Yellow; //SN yellow light flag bit bit Flag_EW_Yellow; //EW yellow light flag char Time_EW; //East-West direction countdown unit char Time_SN; //Countdown unit in north-south direction uchar EW=60,SN=40,EWL=19,SNL=19; //Program initialization assignment, normal mode uchar EW1=60,SN1=40,EWL1=19,SNL1=19; //Variables used to store modified values uchar code table[10]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F}; //1~~~~9 segment code selection uchar code S[8]={0X28,0X48,0X18,0X48,0X82,0X84,0X81,0X84}; //Traffic light control code /Delay subroutine**/ void Delay(uchar a) { uchar i; i=a; while(i–){;} } /Display subfunction*********/ void Display(void) { char h,l; h=Time_EW/10; l=Time_EW%10; P0=table[l]; EW_LED2=1; Delay(2); EW_LED2=0; P0=table[h]; EW_LED1=1; Delay(2); EW_LED1=0; h=Time_SN/10; l=Time_SN%10; P0=table[l]; SN_LED2=1; Delay(2); SN_LED2=0; P0=table[h]; SN_LED1=1; Delay(2); SN_LED1=0; h = EW1/10; l = EW1%10; P0=table[l]; Time_Show_LED1=1; Delay(2); Time_Show_LED1=0; P0=table[h]; Time_Show_LED2=1; Delay(2); Time_Show_LED2=0; } /External 0 interrupt service routine**/ void EXINT0(void)interrupt 0 using 1 { EX0=0; //Disable interrupt if(Add_Button0) //Time addition { EW1+=5; SN1+=5; if(EW1>=100) { EW1=99; SN1=79; } } if(Reduces_Button0) //Time reduction { EW1-=5; SN1-=5; if(EW1<=40) { EW1=40; SN1=20; } } 1 if(Nomor_Button0) //Test whether the button is pressed, if pressed it is normal { EW1=60; SN1=40; EWL1=19; SNL1=19; Busy_LED=0; //Turn off the busy signal light Special_LED =0; //Turn off the special signal light } if(Busy_Btton0) //Test whether the button is pressed, if pressed it is busy { EW1=45; SN1=30; EWL1=14; SNL1=14; Special_LED=0; //Turn off the special signal light Busy_LED=1; //Turn on the busy signal light } if(Special_Btton==0)//Test whether the button is pressed, pressing is a special state { EW1=75; SN1=55; EWL1=19; SNL1=19; Busy_LED=0; //Turn off the busy signal light Special_LED =1; //Turn on the special signal light } EX0=1; //Open interrupt } /***T0 interrupt service routine/ void timer0(void)interrupt 1 using 1 { static uchar count; TH0=(65536-50000)/256; TL0=(65536-50000)%256; count++; if(count==10) { 1 2 if(Flag_SN_Yellow1) //Test the north and south yellow light flag {SN_Yellow=~SN_Yellow;} if(Flag_EW_Yellow1) //Test the east-west yellow light flag {EW_Yellow=~EW_Yellow;} } if(count20) { Time_EW–; Time_SN–; if(Flag_SN_Yellow1) //Test the north and south yellow light flag {SN_Yellow=~SN_Yellow;} if(Flag_EW_Yellow==1)//Test the east-west yellow light flag {EW_Yellow=~EW_Yellow;} count=0; } } /Main program starts*/ void main(void) { Busy_LED=0; Special_LED=0; IT0=1; //INT0 negative transition trigger TMOD=0x01; //Timer works in mode 1 TH0=(65536-50000)/256; //Timer initial value TL0=(65536-50000)%256; EA=1; //CPU interrupt is always allowed ET0=1; //Open timing interrupt EX0=1; //Open external INTO interrupt TR0=1; //Start timing while(1) { /S0 status***/ EW_ManGreen=0; //EW sidewalk prohibited SN_ManGreen=1; //SN pedestrian traffic Flag_EW_Yellow=0; //EW turns off the yellow light to display the signal Time_EW=EW; Time_SN=SN; while(Time_SN>=5) {P1=S[0]; //SN is allowed, EW is red light Display();} /S1 status***/ P1=0x00; while(Time_SN>=0) {Flag_SN_Yellow=1; //SN turns on the yellow light signal position EW_Red=1; //SN yellow light is on, waiting for left turn signal, EW red light Display(); } /S2 status***/ Flag_SN_Yellow=0; //SN turns off the yellow light to display the signal Time_SN=SNL; while(Time_SN>=5) {P1=S[2]; //SN left turn green light on, EW red light Display();} /S3 Status***/ P1=0x00; while(Time_SN>=0) {Flag_SN_Yellow=1; //SN turns on the yellow light signal position EW_Red=1; //SN yellow light is on, waiting for stop signal, EW red light Display();} /*Assignment/ EW=EW1; SN=SN1; EWL=EWL1; SNL=SNL1; /S4 status***/ EW_ManGreen=~EW_ManGreen; //EW sidewalk access SN_ManGreen=~SN_ManGreen; //SN sidewalk prohibited Flag_SN_Yellow=0; //SN turns off the yellow light to display the signal Time_EW=SN; Time_SN=EW; while(Time_EW>=5) {P1=S[4]; //EW, SN red light Display();} /S5 status***/ P1=0X00; while(Time_EW>=0) {Flag_EW_Yellow=1; //EW yellow light signal position SN_Red=1; //EW yellow light is on, waiting for the left turn signal, SN red light Display();} /S6 status***/ Flag_EW_Yellow=0; //EW turns off the yellow light to display the signal Time_EW=EWL; while(Time_EW>=5) {P1=S[6]; //EW left turn green light on, SN red light Display();} /S7 Status***/ P1=0X00; while(Time_EW>=0) {Flag_EW_Yellow=1; //EN turns on the yellow light signal SN_Red=1; //EW yellow light is on, waiting for stop signal, SN red light Display();} /*Assignment/ EW=EW1; SN=SN1; EWL=EWL1; SNL=SNL1; }
Previous article:Single chip microcomputer water tower control system
Next article:Digital thermometer controlled by 51 single chip microcomputer
- 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
- CGD and Qorvo to jointly revolutionize motor control solutions
- CGD and Qorvo to jointly revolutionize motor control solutions
- Keysight Technologies FieldFox handheld analyzer with VDI spread spectrum module to achieve millimeter wave analysis function
- Infineon's PASCO2V15 XENSIV PAS CO2 5V Sensor Now Available at Mouser for Accurate CO2 Level Measurement
- Advanced gameplay, Harting takes your PCB board connection to a new level!
- Advanced gameplay, Harting takes your PCB board connection to a new level!
- A new chapter in Great Wall Motors R&D: solid-state battery technology leads the future
- Naxin Micro provides full-scenario GaN driver IC solutions
- Interpreting Huawei’s new solid-state battery patent, will it challenge CATL in 2030?
- Are pure electric/plug-in hybrid vehicles going crazy? A Chinese company has launched the world's first -40℃ dischargeable hybrid battery that is not afraid of cold
- How to view the future development of Linux
- PYBv1.0 vs PYBv1.1
- What MCUs are commonly used in portable medical applications?
- What is the voltage input range of ADS1230?
- TI Wireless Products RF Hardware FAQ
- Useful Information | Special components in switching power supplies, do you know their types and uses?
- Some dry goods, RVB2601 development board OLED basic driver
- A website that can customize the modules contained in the nodemcu firmware (seemingly lua language version)
- Detailed Explanation of Current Sensing Methods for Switch-Mode Power Supplies
- LIS25BA configuration on STM32F4