Introduction to using STM32 timer to periodically trigger ADC
Preface
The conversion start of the ADC of the STM32 chip is generally divided into software start or external trigger event start. The external trigger event start can be a timer trigger event or an EXTI pin signal. In many applications, such as motors, power supplies, inverters, etc., the ADC sampling point may have very strict time requirements. If the sampling point is selected incorrectly, it may cause serious adverse consequences to the entire control system. Here is a brief introduction to the implementation method of the STM32 timer to periodically trigger ADC sampling.
Here we take the conversion of the ADC injection channel triggered by the TIMER1 of STM32F4 as an example [of course, regular channels can also be triggered by timers]. We can use the timer update event or the comparison output signal as the trigger enable signal of the ADC. According to the STM32F4 reference manual, we can use the TRGO event of TIM1 or the capture event of channel CH4 to trigger the ADC conversion of the injection channel.
1. Use TIM1 TRGO to trigger ADC
// Select Tim1 update event as TRGO
TIM_SelectOutputTrigger(TIM1,TIM_TRGOSource_Update);
// Set T1_TRGO as ADC trigger source
ADC_InitStructure.ADC_ExternalTrigConv =ADC_ExternalTrigConv_T1_TRGO;
Special reminder : People often forget to configure the red statement above, which is equivalent to using the default TRGO signal. But for a timer, there are many signals that can be used as TRGO, and sometimes the default signal is not what you want .
2. Use the comparison event of TIM1 CH4 to trigger ADC
Here we select OC4REF signal as TRGO output to trigger ADC.
// Select OC4REF as TRGO;
TIM_SelectOutputTrigger(TIM1,TIM_TRGOSource_OC4Ref);
// Set T1_TRGO as ADC trigger source
ADC_InitStructure.ADC_ExternalTrigConv =ADC_ExternalTrigConv_T1_TRGO;
Compared with the first method above, the ADC sampling time point is more flexible and adjustable, and is not limited to the timer update event.
Figure 1: Interval-triggered
ADC
sampling points
Sometimes, we may need multiple cycles to perform an AD trigger sampling and related calculation. If the time is based on the update moment and is a multiple of the timing cycle, a more convenient way is to use the repeat counter in the timer and use the update event as TRGO. The software setting is to add a sentence to set the repeat counter TIMx_RCR based on the first method above. The blue arrow in Figure 1 is the trigger point setting for this situation. The basic configuration is as follows:
//Configure the repeat counter to 2 times
TIM_TimeBaseStructure.TIM_RepetitionCounter = 1;
// Select Tim1 update event as TRGO
TIM_SelectOutputTrigger (TIM1,TIM_TRGOSource_Update);
// Set T1_TRGO as ADC trigger source
ADC_InitStructure.ADC_ExternalTrigConv =ADC_ExternalTrigConv_T1_TRGO;
If we want to implement sampling and calculation at non-updated time points across cycles, which is the situation indicated by the green arrow in the figure above, we can consider using multiple sampling with interrupts for counting or introducing other timers for frequency division for flexible processing.
========================
Links to previous topics:
1. A topic about the inability to start RTOS in IAP+APP mode
2. An analysis of anomalies when using the STM32F4 chip CCM RAM
3. Topic of STM32 chip startup mode with air check function
4. Recommended reading of STM32 timer application note AN4776
5. A video database that STM32 fans must know