The general timer and advanced timer of STM32 not only support one-way up or down counting mode, but also support center aligned counting mode, that is, a counting cycle is composed of up counting and down counting. When the center aligned counting mode based on the counter is used as the comparison output function of the timer, three center aligned counting modes are further derived, which are 1 / 2 / 3 of the center aligned counting mode. Through register timx_ CMS [1:0] selection in CR1.
Since a complete counting cycle based on center aligned counting mode includes up and down counting processes, when the CCR value of the given comparison register is within the range of (0, ARR), two matching events will occur in a cycle, that is, two time points of CNT = CCR, and whether the two matching events finally complete the setting of the comparison event flag will trigger an interrupt Or DMA, you can choose different center alignment counting mode according to the application requirements.
In the STM32 series of reference manuals, the timer description section is similar to the following figure, which is a visual display of the setting of the comparison event flag after the matching event occurs in different center alignment counting modes.
Here CCR = 4, arr = 8, let’s take a look.
When the center alignment counting mode 1 is selected, the comparison event flag cc4if is set only when the matching action occurs during the down counting process; when the center alignment counting mode 2 is selected, the comparison event flag cc4if is set only when the matching action occurs during the up counting process; When center aligned counting mode 3 is selected, the event flag cc4if will be set when matching occurs during up / down counting. Of course, when the compare event flag is set, an interrupt or DMA request can be triggered.
Here, we might as well use CH4 of TIM1 of STM32 to do a verification test to see the number of interrupts in a center alignment counting cycle by selecting different center alignment modes. [this is the same no matter which STM32 series]
Based on the above configuration and processing code, we only adjust the center alignment counting mode to compare the interrupt response in a center alignment counting cycle. [counter is the number of times to enter the comparison interrupt, division is the counting direction when the comparison interrupt occurs, 0 is the current downward counting process, and 0xff is the upward counting process]
The test results are very clear in the figure above, so there is no need to repeat them.
So, what’s the use of this timer feature? It’s hard to say. After all, specific applications are often colorful and changeable. In a word, when books are used, we hate less. Understanding the characteristics of various peripherals is the basis and premise of our flexible use. For example, in motor applications, this feature may be useful [how to use it depends on the application scenario and the selected chip].
As shown in the figure above, the counter works in the center aligned counting mode. Sometimes we may need to start ADC conversion on the left or right side of the overflow point on the counter. At this time, we can select the appropriate center alignment mode and start ADC conversion at the appropriate time point.
Of course, just for the above-mentioned application, many series of STM32 family have opened up more flexible, convenient and efficient hardware measures, such as stm32f3, stm32g4, stm32f7, stm32h7 and so on. By configuring the rising / falling edge of the output signal of advanced timer channel 4 / 5 / 6 and different combinations, the ADC can be triggered synchronously to minimize software processing and CPU participation.
The figure below is the configuration diagram of trigger output signal from TIM1 of stm32f3 series. Trgo2 event can be used as trigger event of ADC, and single event or combined event can be selected. [stm32cubemx used by configuration tool]
The following screenshot comes from the timer introduction section of stm32f3 series reference manual. The following figure has a lot of information, but some of the information is related to the above content. It’s also a good idea to post here.
The purpose of this paper is to share the characteristics and differences of the three modes of STM32 timer center alignment counting. After knowing these characteristics, we can use them flexibly when we need them.