cancel
Showing results for 
Search instead for 
Did you mean: 

trigger ADC every "X" IRQs from TIM

PJano.1
Associate II

Hi, I'm building an LLC converter using STM32F301C8. The converter consists of MOSFET half-bridge. The output voltage is controlled by changing the switching frequency with a constant duty cycle of 50%. I use TIM2 for PWM and adjust the frequency using macros (40kHz - 400 kHz switching). Center-aligned mode 1 in CUBE IDE.

I use ADC to measure the output voltage and PI controller for control. When switching the MOSFETs, there is a lot of noise. Therefore I would like to trigger the ADC measurement between the switching when no noise is created by switching MOSFETs.

The issue is when I use TIM2 update interrupt, the MCU gets overloaded and stops responding due to too many IRQs (400 kHz worst case) - which makes sense.

*Is there a way to trigger the ADC once per every, let's say ten TIM2 updates, please?

I tried having a second timer with a ten times higher ARR value than TIM2 to trigger the IRQ, but that seemed unreliable due to frequent frequency adjustments (when changing frequency for TIM2 and TIM1 I had to use a different line of code causing a shift between the TIMs).

Thank you!

PJ.

2 REPLIES 2

Use the master-slave link between timers. Use TIM2 as master, set TIM2_CR2.MMS=0b010 to select Update event as source of TRGO signal.

Select a timer TIMx which will be slave and in its TIMx_SMCR set TS field to ITRx according to the "internal trigger connection" table at the end of TIMx_SMCR description, to input TRGI from TIM2; and set TIMx_SMCR.SMS=0b111 to set External clock mode 1. Now TIMx will count up one every time TIM2 Update happens (i.e. when TIM2 runs freely, every TIM2 period).

Then set appropriate signal from TIMx to trigger ADC.

The above requirements may/will limit the selection of TIMx.

JW

PJano.1
Associate II

Thank you very much, JW. Your solution seems to work gret. I set to toggle GPIO PIN in IRQ elapsed callback (IRQ from TIM1 - slave, every 1k of PWM periods) and measured both PWM from TIM2 and GPIO using an oscilloscope. I noticed a significant delay between the middle of the pulse and the PIN toggle (the delay is over one PWM period long, at certain frequencies toggling right when the MOSFETs are switched). The IRQ, perhaps, takes too long to do all the necessary instructions.

I tried to set the ADC to be triggered by an event on TIM1, hoping for improved speed. But I struggle to make it work. Any idea what am I doing wrong, please? Screen from Cube IDE attached:0693W00000aJsx7QAC.png I'm using DMA for ADC, I wonder if that can cause some incompatibilities.

Thanks, PJ