2022-03-04 03:55 PM
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!
2022-03-04 05:30 PM
> 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.
2022-03-05 02:55 AM
Which STM32?
+1 to everything TDK wrote above, plus TIM1 is Advanced timer and for output needs TIMx_BDTR.MOE to be set.
JW
2022-03-06 08:37 AM
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.
2022-03-06 08:38 AM
Thanks for helping me. I will check this out and post a new situation soon.
2022-03-06 09:10 AM
Here is new situation:
void TIM1_UP_IRQHandler(void){
/*code*/
TIM1 -> SR &= ~TIM_SR_UIF; //Update Interrupt Flag
}
void timerispisa1(void){
RCC->APB2ENR |= RCC_APB2ENR_IOPAEN;
GPIOA->CRH &= ~(GPIO_CRH_MODE8 | GPIO_CRH_CNF8); // PA7 ref. man. page 179
GPIOA->CRH |= (GPIO_CRH_MODE8_1 | GPIO_CRH_CNF8_0);
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->BDTR |= TIM_BDTR_MOE; // MOE enabled
TIM1->CR1 |= TIM_CR1_CEN;
TIM1->DIER |=TIM_DIER_UIE; //Update Interrupt Enable
TIM1 -> SR &= ~TIM_SR_UIF; //Update Interrupt Flag delete
NVIC_EnableIRQ(TIM1_UP_IRQn); //Interrupt Set-Enable Register
__enable_irq(); //Enable Interrupt
}
Still no much progress but interrupt does not fire all the time like before. I think MOE helped as @Community member suggested. I found mapping pin PA8 in the ref manual so I declared it but it still seems to prefer PB11, I made sure nothing else in the code used that pin. That is still a mystery.
I will attach my header file if you are willing to take a look at it, I cant find "usual" interrupt handler like for TIM2/3. Do you have any suggestion on what handler should I use or should I somehow do it on my own?
Thank you!
2022-03-06 09:11 AM
I posted a new situation in reply to TDK's answer