1. Shift blocking
#include #define uint unsigned int #define uchar unsigned char void delayms(uint xms) { uint i,j; for(i=xms;i>0;i--) for(j=110;j>0;j--); } void main() { fly Su8Data=0x01,Su8Cnt=0; while(1) { P1=Su8Data; //8 pins of P1 port connect to 8 LED lights delayms(500); //delay 0.5S, blocking delay Su8Data=Su8Data<<1;//Shift left one bit Su8Cnt++; if(Su8Cnt>=8)//8th reassignment to continue from the first position { Su8Cnt=0; Su8Data=0x01; } } } Because of the "blocking delay", the whole program seems mechanical and rigid, lacking a multi-task parallel framework. 2. Non-blocking shift #include #define BLINK_TIME 500 unsigned char timeFlag=0,Su8Data=0x01,Su8Cnt=0; unsigned int count=0; void main() { TMOD=0x01; TH0=0xfc; //initial value of timing 1ms TL0=0x66; EA=1; ET0=1; TR0=1; while(1) { if(count==0) //non-blocking { timeFlag=0;//mutex count=BLINK_TIME; //500ms delay timeFlag=1; P1=Su8Data; Su8Data=Su8Data<<1; Su8Cnt++; if(Su8Cnt>=8)//Reassign value for next cycle { Su8Cnt=0; Su8Data=0x01; } } } } void T0timer() interrupt 1 { TH0=0xfc; TL0=0x66; if(timeFlag==1 && count>0) // can generate multiple software timers for parallel processing { count--; } } Although the basic element of multi-tasking parallel processing, "software timer", is used, it still remains at the shifting stage and is just a marquee, and has not gone beyond the marquee itself. 3. State switching is non-blocking Two core frameworks: (four zones and one line; switch plus timed interrupt) The "four zones and one line" structure below is mainly for understanding the approximate "spatial partitioning" of the microcontroller program. "Zone 1" is a system initialization function, which is specifically used to initialize the microcontroller's own registers and some peripheral output devices that require fast response speed, to prevent the peripheral devices from malfunctioning due to the uncertain level state of the output IO port after power-on, such as malfunction of the driving relay. "One line" is a delay function, which is mainly prepared for the peripheral initialization function, because the peripheral initialization function is specially used to initialize peripheral chips and modules that do not require immediate processing after power-on. For example, LCD modules, AT24C02 memory chips, etc., these chips need a little time to reset themselves internally at the moment of power-on, and it also takes a little time for the external voltage to stabilize. Only after this time, these chips are in working state, and the microcontroller can communicate with them normally. The "second area" is the peripheral initialization function, which is specifically used to initialize peripheral chips and modules that do not require immediate processing upon power-up. "The third area" is the task function that is continuously scanned in the main function. "Area 4", timing interrupt function. Provides the system's beat time, and processes and scans some functions related to IO port debounce and buzzer driver. #include #define BLINK_TIME 500 void SystemInit(); void delayms(unsigned int xms); void PeripheralInitial(); void LedTask(); sbit led1=P1^0; sbit led2=P1^1; sbit led3=P1^2; sbit led4=P1^3; sbit led5=P1^4; sbit led6=P1^5; sbit led7=P1^6; sbit led8=P1^7; unsigned char Gu8Step=0,timeFlag=0; unsigned int count=0; void main() { SystemInit(); //"First District" of "Four Districts and One Line", system initialization function delayms(1000); //"One line" of "four zones and one line", the dividing line between "system initialization function" and "peripheral initialization function", delay function PeripheralInitial(); // "Second zone" of "four zones and one line", peripheral initialization function while(1) { LedTask(); //The "third zone" of "four zones and one line", a task function that is continuously scanned in the main function } } void T0timer() interrupt 1 //The fourth zone of "four zones and one line", the timer interrupt function, provides the system's beat time { TH0=0xfc; TL0=0x66; if(1==timeFlag && count>0)//Software timer { count--; } } void SystemInit() { TMOD=0x01; TH0=0xfc; TL0=0x66; EA=1; ET0=1; TR0=1; } void delayms(unsigned int xms) { unsigned int i,j; for(i=xms;i>0;i--) for(j=110;j>0;j--); } void PeripheralInitial() {} void LedTask() { switch(Gu8Step) { case 0: if(0==count) //500ms time to execute command { led1=0; //The first light is on, the others are off led2=1; led3=1; led4=1; led5=1; led6=1; led7=1; led8=1; timeFlag=0; count=BLINK_TIME; //Reload time timeFlag=1; Gu8Step=1; //Switch steps to achieve non-blocking purpose } break; case 1: if(0==count) { led1=1; led2=0; led3=1; led4=1; led5=1; led6=1; led7=1; led8=1; timeFlag=0; count=BLINK_TIME; timeFlag=1; Gu8Step=2; } break; case 2: if(0==count) { led1=1; led2=1; led3=0; led4=1; led5=1; led6=1; led7=1; led8=1; timeFlag=0; count=BLINK_TIME; timeFlag=1; Gu8Step=3; } break; case 3: if(0==count) { led1=1; led2=1; led3=1; led4=0; led5=1; led6=1; led7=1; led8=1; timeFlag=0; count=BLINK_TIME; timeFlag=1; Gu8Step=4; } break; case 4: if(0==count) { led1=1; led2=1; led3=1; led4=1; led5=0; led6=1; led7=1; led8=1; timeFlag=0; count=BLINK_TIME; timeFlag=1; Gu8Step=5; } break; case 5: if(0==count) { led1=1; led2=1; led3=1; led4=1; led5=1; led6=0; led7=1; led8=1; timeFlag=0; count=BLINK_TIME; timeFlag=1; Gu8Step=6; } break; case 6: if(0==count) { led1=1; led2=1; led3=1; led4=1; led5=1; led6=1; led7=0; led8=1; timeFlag=0; count=BLINK_TIME; timeFlag=1; Gu8Step=7; } break; case 7: if(0==count) { led1=1; led2=1; led3=1; led4=1; led5=1; led6=1; led7=1; led8=0; timeFlag=0; count=BLINK_TIME; timeFlag=1; Gu8Step=0; } break; } } Although the code size of this program has increased, it does not affect the running efficiency. (If the 8 LED lights are on the P1 and P0 ports respectively or if the two marquees are processed in parallel by multi-tasking, the above two shifting methods will not work.) The 8 LED lights are separated and displayed separately because the 8 pins represent not only LED lights but 8 output signals. These 8 output signals may drive different relays, motors, cannons, missiles and various ever-changing combinational logics in the future. After the separation, the program framework has unlimited scalability.
Previous article:Single and double click with separate buttons like a mouse
Next article:One timer interrupt generates N software timers
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
- AD09 has problems loading libraries
- CB5654 Development Board Review 4
- One-week evaluation report delivered~~
- Testing methods and diagnostic analysis for DSP-containing circuit boards
- Get the relevant information required by LSM6DSOX+STEVAL-MKI109V3 contestants (including official information and moderator reviews)
- ZigBee terminal (low power device)
- [RVB2601 Creative Application Development] GPIO Simulation SPI Serial Port
- Embedded Qt-Table Usage Test
- I found a very useful Renesas RL78 offline programmer.
- Porting appweb