Question
Problem enabling timer in STM32F103
Posted on July 17, 2015 at 12:50
Hi all,
I am using a STM32F103 MCU and I wanted to enable TIM2 timer and then perform some operations upon signals received by the timer, first by polling and then using interrupts. I have setup my timer i.e. TIM2 as below: TIM_TimeBaseInitTypeDef TIM_InitStruct; // Configure TIM2 internal timer. TIM_InitStruct.TIM_Prescaler = 40000; TIM_InitStruct.TIM_CounterMode = TIM_CounterMode_Up; TIM_InitStruct.TIM_Period = 500; TIM_InitStruct.TIM_ClockDivision = TIM_CKD_DIV1; TIM_InitStruct.TIM_RepetitionCounter = 0; TIM_TimeBaseInit(TIM2, &TIM_InitStruct); // Enable TIM2 internal timer. TIM_Cmd(TIM2, ENABLE); Than I tried to trigger an interrupt based on TIM2 timer using the following code: NVIC_InitTypeDef NVIC_InitStruct; NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0); // Enable TIM2 interrupt. NVIC_InitStruct.NVIC_IRQChannel = TIM2_IRQn; NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 0; NVIC_InitStruct.NVIC_IRQChannelSubPriority = 1; NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStruct); void TIM2_IRQHandler() { // Do something here by toggling bits for a LED. } My code never worked and the LED never turned on. So, I tried to perform polling within the infinite loop in the main function as below: while(1) { if (TIM_GetITStatus(TIM2, TIM_IT_Update) != RESET) { TIM_ClearITPendingBit(TIM2, TIM_IT_Update); // Do something here by toggling bits for a LED. } } And again, my code didn't work with polling approach neither. Am I doing something wrong in both cases i.e. interrupt- and polling-based solutions? It should be quite straightforward to use a timer in both ways (interrupt and polling´) so I am wondering what is wrong in my code? Can anybody help me with that? Regards, Dan.