Combining STM8 to talk about the basic idea of ​​low-power MCU programming

1 Introduction

After working, it came into contact with "low power consumption" and began searching for a variety of low-power MCUs. The network is a very free place, and you can always see a variety of debates on which MCU is using less power. In these debates I looked at the features of almost all low-power MCUs, such as MSP430, EFM32, and STM32L. The core of network debate is which MCU's sleep current is lower. Looking at more information, I discovered how these low-power MCUs are used, what is the difference between low-power MCU programming and general MCU programming.

With these questions I continued to taste the "controversy" about low-power MCUs until I purchased the "MSP430 Series Microcontroller System Engineering Design and Practice." In the book mentioned the basic idea of ​​low-power MCU programming, can be summarized as:

1. Make good use of beats

2. Remove the blockage

Let's talk about how to practice the above two points in combination with STM8. (Although STM8 is not a low-power MCU in the strict sense, any MCU that has a low-power instruction can use the above two points to exert its low-power characteristics.)

2. Make good use of beats

Timing sleep is a commonly used method for low-power MCUs. The timed sleep is to allow the MCU to sleep first and then work, and keep switching between sleep and work. The STM8 has several low-power modes. For example, the standby mode is one of them. In the standby mode, only the MCU stops working, and other peripherals can work normally. Using the wait mode ensures that the timer or UART can normally enter the interrupt. Of course, you can also use the stop mode to further reduce power consumption. Here, you use wait mode to make a "methodological" description.

Combining STM8 to talk about the basic idea of ​​low-power MCU programming

Figure 1 Low-power beats in front and back systems

It can be understood that entering the low-power mode at regular time can also be understood as a task. This “task” will block the CPU and make other tasks unable to run, but this is not really blocking the CPU, but makes the CPU stop only by timing. The interrupt can only exit the "task". The implementation part of the code is as follows:

Combining STM8 to talk about the basic idea of ​​low-power MCU programming

The WaitFlag flag appears in the above code. This flag appears in the Enter_WaitMode function and the TIM4_UPD_OVF_IRQHandler interrupt. The purpose of this flag bit is to ensure that the timer 4 overflow interrupt is used to wake up the CPU. Other interrupts such as UART receive interrupts cannot be used. The CPU reruns. The STM8 wait mode and timer 4 overflow interrupt form a low-power beat “task.”

3. Remove obstruction

From the analysis above, we can see that in the entire operation cycle, we always hope that the proportion of low-power tasks is significant (from the perspective of time), and other tasks occupy a small proportion. Then other tasks should minimize the CPU usage. For example, to achieve LED interval flash, you can use the following code:

Combining STM8 to talk about the basic idea of ​​low-power MCU programming

Since LED_Process runs after a low-power task, and the CPU time occupied by the low-power task is a fixed time - Timer 4 overflow cycle, which means that the LED task's call cycle is 1ms, and this feature can be used to combine a count. The variable LEDCounter implements the LED blinking function. Of course, more complex timing can be achieved by a finite state machine.

4. Summary

Beat operation and elimination of blocking is the basic idea of ​​low-power MCU programming, understanding the basic idea of ​​low-power MCU programming, then not low-power MCU can also be used to develop low-power applications. Some time ago TB to see the MCU price, STM8's price really surprised me, he also purchased the STM8 development board, combined with low-power methods, then summed up the article.

Portable Inverter Generator

Wuxi Doton Power , http://www.dotonpower.com