cancel
Showing results for 
Search instead for 
Did you mean: 

How to select pwm interrupt rising or falling edge and perform action in Handler

NT.1
Associate

I am trying to achieve gpio pin toggle in TIM_Handler() on pwm interrupt. Currently gpio is toggling after some delay of falling edge of pwm. But I am expecting to toggle gpio at each rising edge of pwm. please help me to achieve it.

My TIM Config for STM32F030R8 - MCU is as follows

>TIM16 Enabled in CubeMx

>Selected PWM Generation CH1 and CH1N

>Pulse Width 50%

>Selected TIM16 global interrupt

>Code Generated by CubeMX

>In main.c

>Added HAL_TIM_PWM_Start_IT(&htim16, TIM_CHANNEL_1);

>Added HAL_TIMEx_PWMN_Start_IT(&htim16, TIM_CHANNEL_1);

>In stm32f0xx_it.c under timer handler what I wrote is

void TIM16_IRQHandler(void)

{

GPIOA->BSRR = (uint32_t)GPIO_PIN_10;

HAL_TIM_IRQHandler(&htim16);

GPIOA->BRR = (uint32_t)GPIO_PIN_10;

}

Yellow is PWM and green is gpio toggle. You can see gpio toggles after PWM rising edge and some delay.

0693W000000TsbEQAS.jpg

3 REPLIES 3

Any of following, depending on what exactly do you want to achieve:

- use the other PWM mode i.e. PWM2 instead of PWM1 out vice versa

- change the output signal polarity be setting TIMx_CCER.CCxP

- use interrupt on CC, not on Update, see TIMx_DIER

JW

I had tried TIM1 and TIM1_CC_IRQHandler at first. Since it is also not giving the expected output I had config TIM16 and wrote gpio toggle code in TIM16_IRQHandler. So i think interrupt CC is not going to work.

berendi
Principal

Timer interrupts can be generated on two kinds of events (and some more which are not relevant now), timer update (overflow) or channel compare. Which one corresponds to the rising edge of the PWM output depends on a couple of settings, PWM mode, counter direction, output polarity, maybe some more.

Setting bits in TIMx->DIER selects whether the update or the compare event triggers the interrupt. (You can request an interrupt on both events as well). TIM16 has a common interrupt handler for both events, so setting either update or compare in DIER will trigger the same interrupt, but possibly on the wrong edge. Fix the setting of DIER (compare instead of update or the other way round) to have an interrupt on the other edge.

TIM1 has separate interrupt lines (and vectors and handlers) for compare and update, so a wrong setting in DIER causes the wrong handler called at the wrong time.

> So i think interrupt CC is not going to work.

It is working in countless projects when used as documented in the reference manual.

What is not going to work is using a barely documented library instead to set up the hardware, and expecting it to work the way one thinks it should work.