Use flags with caution (I am a novice in microcontrollers, experts please help me, welcome to give me advice!~)
What I am talking about here is to use flags reasonably and carefully, otherwise it is easy to make mistakes and difficult to debug.
When I was working today, I modified the timer timing function and gained experience. What I said here has nothing to do with the specific microcontroller, it is a general idea.
Take what I modified today as an example:
The two I/O pins of the PIC microcontroller control the on and off of two LED lights (blue and red lights, respectively, LEDs are used here to avoid involving company content).
The requirements are that
the blue light turns on once every 5s, for about 100ms;
the red light turns on once every 10s, for 100ms each time;
the red and blue lights are staggered as much as possible.
My idea is
to define a 10s time axis and use a timer to implement it, turning on the blue light at 2.5 seconds and 7.5 seconds, and turning on the red light at 10s.
There is a global variable cnt.
Define a global variable times = 0.
[cpp] view plain copy print ?
0 2.5s 5s 7.5s 10s
|-------------|===|-------------|-------------|=== |-------------|+++|
blue blue red
|----times=0------|----------times=1----------|----times=0---- -------|
I took it for granted that this would work, but I didn’t
know there was a
bug here, because this condition is also met between 7.5 and 10 s . . . The system that comes out like this is not what we want above. We should change it to if((cnt > 250)&&(times == 0)&&(cnt < 750)) so that it will work. . .
Previous article:Serial communication uses parity check to send data
Next article:PWM speed regulation principle
- 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
- AD19AD19, on a PCB that has already been copper-plated, I deleted the copper and changed the layout to redraw a PCB, but when I entered the 3D view, the original copper outline would be displayed. How can I delete this outline?
- STM32F4 hardware I2C driver 0.96 OLED screen
- Analog electronics course selection test + isolation course learning
- Why can't my INT function enter the interrupt?
- [RT-Thread Reading Notes] Part 2 (3) Semaphores and Mutexes
- What software do I use for schematic illustrations?
- Problems encountered in debugging B-U585I-IOT02A WIFI module
- 2021 National Undergraduate Electronic Design Competition Restart Notice and Schedule
- MicroPython 1.18 released
- Last day: Let's play together, AI development board based on Allwinner R329 chip