Skip to main content
UMilo.1
Associate III
March 4, 2022
Question

How to set up TIM1 and TIM1 Interrupt using registers

  • March 4, 2022
  • 6 replies
  • 4982 views

Hi guys,

I have trouble with setting up TIM1, I am already using TIM2 and TIM3 to generate PWM signal and ADC conversion. I need TIM1 to generate and handle interrupt so I can display some messages on LCD.

For some reason, I can't do that. I am basically using the same code as for other timers but this one just won't work.

Here is my current code:

void TIM1_UP_IRQHandler(void){
	
	/*code*/
	
	TIM1 -> SR &= ~TIM_SR_UIF; //Update Interrupt Flag
 
}
 
 
void timerispisa1(void){
 
		RCC->APB2ENR |= RCC_APB2ENR_IOPAEN; 
 
 
		RCC->APB2ENR |= RCC_APB2ENR_TIM1EN;
 
 
		TIM1->PSC = 799;
 
		TIM1->ARR = 10;
 
		TIM1->CCR1 = 100;
 
	
 
		TIM1->CCMR1 &= ~TIM_CCMR1_OC2M; 
 
		TIM1->CCMR1 |= (0b110 << TIM_CCMR1_OC2M_Pos); 
 
 
		TIM1->CCER &= ~TIM_CCER_CC2P; 
 
		TIM1->CCER |= TIM_CCER_CC2P;
 
 
		TIM1->CCER |= TIM_CCER_CC1E; 
 
 
		TIM1->CR1 |= TIM_CR1_CEN;
		TIM1->DIER |=TIM_DIER_UIE; //Update Interrupt Enable
		TIM1 -> SR &= ~TIM_SR_UIF; //Update Interrupt Flag
		NVIC_EnableIRQ(TIM1_UP_IRQn); //Interrupt Set-Enable Register
		__enable_irq(); //Enable Interrupt
}

It seems like no matter what GPIO I select it keeps turning on PB11 very fast like ARR and PSC does not have any effect on it. This very similar setup works for me for other timers so I am very confused.

Does anybody can help me, thank you!

This topic has been closed for replies.

6 replies

TDK
March 5, 2022

> It seems like no matter what GPIO I select it keeps turning on PB11 very fast like ARR and PSC does not have any effect on it

Nothing in the code you've presented modifies PB11, so hard to track that one down. Perhaps something else in your code is driving PB11.

> TIM1 -> SR &= ~TIM_SR_UIF;

You should always check the flag in the interrupt before acting upon it. There are times when the IRQ may be called with no active flags due to delays in the pipeline.

To clear the UIF flag, write zero to only that bit. No need for a read-modify-write, although in this case it doesn't affect things.

TIM1->SR = ~TIM_SR_UIF;

It's unclear what you're trying to achieve with CCR1. No need for CCR if you just want periodic interrupts. In any case, having CCRx > ARR is unlikely to be useful.

> TIM1_UP_IRQHandler

You should also verify this is the correct name for the handler as it appears in the vector table. You don't mention what chip you're using.

"If you feel a post has answered your question, please click ""Accept as Solution""."
UMilo.1
UMilo.1Author
Associate III
March 6, 2022

Thanks for your help. I am using ST32F103R6 with Proteus and Eclipse. Simulation is a bit tricky so I was digging through header files and found a couple of TIM1 Handles, no standard handlers are there like I used for TIM2 and TIM3.

I will go through your suggestions and post a new situation soon.

waclawek.jan
Super User
March 5, 2022

Which STM32?

+1 to everything TDK wrote above, plus TIM1 is Advanced timer and for output needs TIMx_BDTR.MOE to be set.

JW

UMilo.1
UMilo.1Author
Associate III
March 6, 2022

Thanks for helping me. I will check this out and post a new situation soon.

UMilo.1
UMilo.1Author
Associate III
March 6, 2022

I posted a new situation in reply to TDK's answer