# An Energy-Efficient Strategy for Microcontrollers
## 3 Analytical Models
### How Will Processor Clock Frequency Affect Energy Consumption? The processor frequency has a significant impact on both the working current and the code execution time. It is known that the power consumption will increase as the processor clock frequency increases, and for computationally intensive tasks, the execution time will be shortened by increasing the processor clock frequency. Except for programs that contain delays, almost all of the programs can speed up the execution by increasing the processor frequency, and in ideal situations, the execution time and processor clock frequency will show an inverse proportional relationship. The STM32F407 used as a target device has a wide range of processor clock frequency up to 168 MHz. The question of \"how [processor clock frequency will] affect energy consumption\" can be translated into whether Equation (2) holds, where \(\mathrm{E_{af}}\) represents the energy consumption at the adjusted frequency, and \(\mathrm{E_{of}}\) represents the energy consumption at the original frequency. Energy is the time integral of the power, or the product of power and time if the power is almost constant. We can expand Equation (2) into Equation (3), where \(\mathrm{P_{af}}\) represents the power consumption at the adjusted frequency, \(\mathrm{t_{af}}\) represents the execution time at the adjusted frequency, \(\mathrm{P_{of}}\) represents the power consumption at the original frequency, and \(\mathrm{t_{of}}\) represents the execution time at the original frequency. To make the comparison more intuitive, Equation (4) is derived. Figure 1 is a schematic diagram of the energy consumption of the application code before and after adjusting the processor clock frequency. \[\mathrm{E_{af}}<\mathrm{E_{of}} \tag{2}\]  
\[\mathrm{P_{af}}\times\mathrm{t_{af}}<\mathrm{P_{of}}\times\mathrm{t_{of}} \tag{3}\]  
\[\mathrm{P_{af}}\;/\;\mathrm{P_{of}}<\mathrm{t_{of}}\;/\;\mathrm{t_{af}} \tag{4}\]  
Figure 1: Schematic diagram of the energy consumption before and after adjusting the processor clock frequency. The area formed by the power consumed during the execution is the energy consumption of the program. The blue area is E\({}_{\text{of}}\), the energy consumption of the original program, and the orange area is E\({}_{\text{af}}\), the energy consumption of the optimized program. If the orange area is smaller than the blue area, then it will be possible to save energy by adjusting processor frequency. This criterion means that the increment in the power consumption must be less than the speedup of the execution time. However, even in a core board, there are many peripheral components that serve the system and program. These on-board peripheral components, other than the processor, also consume energy, while the whole system runs in active mode. Therefore, for Equation (3), in addition to the power consumption of the microcontroller itself, P\({}_{\text{af}}\) and P\({}_{\text{of}}\) also include the power consumed by other peripheral components. If the power consumption of the peripheral components is separated from the power consumption of the entire system, we can obtain the following:  
\[\text{P}_{\text{of}}=\text{P}_{\text{ofp}}+\text{P}_{\text{ofpc}} \tag{5}\]  
\[\text{P}_{\text{af}}=\text{P}_{\text{afp}}+\text{P}_{\text{afpc}} \tag{6}\]  
P\({}_{\text{ofp}}\) and P\({}_{\text{afp}}\) stand for the power consumption of the microcontroller itself at the original clock frequency and the adjusted clock frequency, respectively, and P\({}_{\text{ofpc}}\) and P\({}_{\text{afpc}}\) stand for the power consumption of other peripheral components at the original and adjusted processor clock frequency.