timerstm32stm32f4discoverystm32f4stm32-hal

STM32 - How to trigger interrupt after a certain PWM ON time?


I'm new to ARM MCUs (STM32F411), and I have been trying to find my way around the peripherals using STM's HAL library and STM32Cube.

I've already configured my board in order to use some peripherals:

Let us suppose, now, that the PWM's whole period is 100 ms and its duty cycle is 50% (50 ms PWM on and 50 ms PWM off).

I would like to trigger an interrupt after a certain time of the PWM on level, let us say 50% of it.

Hence, I would like to run an interrupt at 25 ms in order to use the ADC for sampling it's analog inputs.

Do you have any suggestion on how could I implement such a kind of interrupt?

Thank you in advance for your help!


Solution

  • Since the ADC of the STM32F411 is used in Regular mode (not Injected mode) and only three channels out of four are used to generate PWM on Timer 3, the fourth channel can be used to trigger the ADC.

    Hence Timer 3 is configured as follows:

    Therefore TIM3->CCR1 is loaded to a value that gives 25% of duty, then it will generate TIM3_CH1 events that can be used to trigger ADC start-of-conversion at 25% of your TIM3 timebase.