STM32F303K8 - TIM16 does not triggers an interrupt - Page 1
Forum section: electronics / general

author: ppTRN
author_rank: Regular Contributor
timestamp: February 17, 2025, 01:01:22 pm
content: Hi everyone,I am trying to write a simple C code in keil that outputs two complementary waveforms with dead time. The dead time insertion is software programmed and I am not using the hardware featue of the timer.Once I configured the timer, I found out that it does not triggers any interrupt. What I found so far is:- TIM16 ISR is shared with TIM1 ISR, and goes by TIM1_UP_TIM16_IRQHandler- The timer starts, I can see it counting and I can see it setting both update and compare match interrupt flags (in debug mode)- I tried enabling TIM1, without any results- A breakpoint in the ISR is never reachedCode:#define START 5void timer16Init(){RCC->APB2ENR |= 1<<17;// Timer 16 clock enableRCC->AHBENR |= 1<<18;// GPIOB clock enabledGPIOB->MODER |= 1<<6;// Pin13 (PB3) is an outputGPIOB->MODER &= ~(1<<7);GPIOB->MODER |= 1<<8;// Pin12 (PB4) is an outputGPIOB->MODER &= ~(1<<9);GPIOB->BSRR = 1<<19;// NPN signal starts at 0GPIOB->BSRR = 1<<4;// PNP signal starts at 1TIM16->CR2 |= 0;// Unused: configures waveform generationTIM16->DIER |= 1<<1 | 1<<0;// Interrupt enabled on update event and compare match eventTIM16->SR |= 0;// Interrupt flag registerTIM16->EGR |= 0;// Unused: event generation registerTIM16->CCMR1 |= 0;// Capture/Compare channel 1 is by default configured as an output compare channelTIM16->CCER |= 1<<0;// Output compare module enableTIM16->CNT = 0;// Reset counter valueTIM16->PSC = 9;// Timer is fed with 6.4MHz for Ttick = 156.25nsTIM16->ARR = 960;// Timer overflows in 150us for Fovfl = 6.67kHzTIM16->RCR = 0;// No need to use the repetition counterTIM16->CCR1 = START;// Assignment of the output compare valueTIM16->BDTR |= 0;// Unused: break and dead time generation registerTIM16->DCR |= 0;// Unused: DMA control registerTIM16->DMAR |= 0;// Unused: DMA transfer addressTIM16->OR |= 0;// Unused: select the input TI1TIM16->CR1 |= 1<<0;// Timer enabled}void TIM1_UP_TIM16_IRQHandler(void){if(TIM16->SR && 1){/* ********** UPDATE EVENT ********** */TIM16->SR &= ~(1<<0);GPIOB->BSRR = 1<<19;// NPN goes to 0GPIOB->BSRR = 1<<4;// PNP goes to 1/* ********************************** */}if(TIM16->SR && 2){/* ******* COMPARE MATCH EVENT ******* */TIM16->SR &= ~(1<<1);switch(TIM16->CCR1){case 5:TIM16->CCR1 = 475;GPIOB->BSRR = 1<<3;// NPN goes to 1break;case 475:TIM16->CCR1 = 480;GPIOB->BSRR = 1<<19;// NPN goes to 0break;case 480:TIM16->CCR1 = 5;GPIOB->BSRR = 1<<20;// PNP goes to 1break;default:break;}/* *********************************** */}}I really cannot understand what is going on. I am also absolutely sure about the name of the IRQ handler since it is stated in that exact way on the startup .s file.Can anyone help me? Thanks!

author: wek
author_rank: Frequent Contributor
timestamp: February 17, 2025, 01:23:08 pm
content: I don't see interrupt being enable in NVIC, e.g.NVIC_EnableIRQ(TIM1_UP_TIM16_IRQn);Otherwise, a generic "interrupt does not fire" checklisthere.JWPS.Quoteno, you want bitwise AND, i.e. if(TIM16->SR & 1){QuoteDon't use RMWto clear status bits.

author: ppTRN
author_rank: Regular Contributor
timestamp: February 17, 2025, 01:28:33 pm
content: It works now!Thank you very much for providing the solution and for the extra tips.I tought that setting the bits that enabled the TIM16 interupt was enough.Thanks again!