Skip to main content
hdemi.1
Associate III
Jan 15, 2023 9:15 AM UTC
Question

CNT value interrupt

I want to generete interrupt for specific value of TIM->CNT

TIM1->DIER |= TIM_IT_CC1;

TIM1->CCR1 = myVal;

I use only above.

if(TIM_GetITStatus(TIM1,TIM_IT_CC1) != RESET){

//when interrupt generete here ,CCR1 is not equal my value

}

 I'm so confused why am I doing wrong?

This topic has been closed for replies.

6 replies

gbm
Super User
Jan 15, 2023 10:05 AM UTC

The bit in DIER is called TIM_DIER_CC1IE, and the flag TIM_SR_CC1IF, so:

TIM1->DIER |= TIM_DIER_CC1IE;

if (TIM1->SR & TIM_SR_CC1IF) ...

My STM32 stuff on github - compact USB device stack and more: https://github.com/gbm-ii/gbmUSBdevice
hdemi.1
hdemi.1Author
Associate III
Jan 15, 2023 12:57 PM UTC

I write code like that. It always generete interrupt despite clear the flag and interrupt source.

if(Counter == .DelayloopCount){

TIM1->CCR1 = DelayOverFlow ; //this code not always running

TIM1->DIER |= TIM_DIER_CC1IE;

}

if(TIM1->SR & TIM_SR_CC1IF){

//doing something

TIM1->DIER &= (uint16_t)~TIM_DIER_CC1IE;

hdemi.1
hdemi.1Author
Associate III
Jan 15, 2023 12:56 PM UTC

I write code like that. It always generete interrupt despite clear the flag and interrupt source.

if(Counter == .DelayloopCount){

TIM1->CCR1 = DelayOverFlow ; //this code not always running

TIM1->DIER |= TIM_DIER_CC1IE;

}

if(TIM1->SR & TIM_SR_CC1IF){

//doing something

TIM1->DIER &= (uint16_t)~TIM_DIER_CC1IE;

TIM1->SR = (uint16_t)~TIM_SR_CC1IF; //clear flag }

gbm
Super User
Jan 15, 2023 1:27 PM UTC

If enabled, the timer should generate an interrupt during every period, so I assume it's normal.

What other TIM1 interrupts are enabled? Use the debugger to check the values of DIER and SR registers while in ISR.

In the ISR, clear the CC1IF flag immediately after checking for it, NOT at the end of ISR.

My STM32 stuff on github - compact USB device stack and more: https://github.com/gbm-ii/gbmUSBdevice
hdemi.1
hdemi.1Author
Associate III
Jan 15, 2023 2:04 PM UTC

SR value is 0x025F when works TIM1->CR1 |= TIM_CR1_CEN I can not clean this flag

with this way :

TIM->SR &= (uint16_t)(~(TIM_SR_CC1IF | TIM_SR_CC2IF | TIM_SR_CC3IF |TIM_SR_CC4IF));

gbm
Super User
Jan 15, 2023 4:01 PM UTC

Do not use &= to clear the flags in SR - this leads to errors. The flags are cleared but if the timer is running, it sets the flags again.

My STM32 stuff on github - compact USB device stack and more: https://github.com/gbm-ii/gbmUSBdevice