2023-03-28 12:18 AM
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.
2023-03-28 01:00 AM
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
2023-03-28 01:25 PM
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: I'm using DMA for ADC, I wonder if that can cause some incompatibilities.
Thanks, PJ