How to use STM32 timers in interrupt mode?
Need help understanding DMA for use with SPI
STM32 Not waking up from Stop-Mode
STM32: Interrupt from timer
Videos
I have the following code:
#include "stm32f407xx.h"
#define TIM2_IRQNO 28
void TIM2_IRQHandler(void) {
printf("tick\n");
}
int main() {
TIM_PeriClockControl(TIM2, ENABLE); //enable system clock on timer
TIM2->PSC = 168; //set prescaler
TIM2->ARR = 1000; //set frequency to 1kHz
TIM2->CR1 |= (1 << TIM_CR1_URS); //update enable (after overflow)
TIM2->EGR |= (1 << 0); //update generation
TIM2->DIER |= (1 << 0); //update interrupt enabled
TIM2->CR1 |= 1 << TIM_CR1_CEN; //enable timer
uint32_t *pISER1 = (uint32_t *)0xE000E100; //NVIC_ISER base addr (or ..104?)
*pISER1 |= ( 1 << (TIM2_IRQNO % 32) ); //enable NVIC for TIM2
for(;;); //main loop
}I have an STM32F407VG microcontroller. It features a 168MHz clock. Whith the prescaler I bring down it's speed to 1MHz on the peripherals and I further decrease the timers speed to 1kHz with the help of the ARR Auto Reload register.
Not sure about EGR and DIER registers, but have seen others use them in their implementations.
After enabling the timer I try to enable the interrupt on the timer using the base address of the ISER register of the NVIC. It should probably end in 104, not 100, but with 104 I managed to print nothing, while using 100 I could print the "tick" text, but uncontrollably fast.
What I would expect form this code is to call the TIM2_IRQHandler after every millisecond (1 tick of the timer with the given PSC and ARR configuration). What am I doing wrong?