2015-07-17 03:50 AM
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.2015-07-17 04:33 AM
Try not to editorialize code that isn't working, post a complete/concise example that shows all the code required to compile.
Do you enable the APB clock for the TIM ?If you enable the interrupt in the TIM, you need to service it in the IRQHandler, otherwise it will never leave the IRQ, and your main() loop will never see anything.2015-07-17 07:14 AM
2015-07-17 07:31 AM
Sorry, but the code is too large to be copied in the forum...
Ok, but in such situations you need to condense the code to that which demonstrates the problem. My problem with selective cut-n-paste presentations is that people often focus on the ''wrong'' code, which is why they can't see/solve the problem in the first place, and what's needed is a higher level view so I can see what's actually wrong/missing.2015-07-21 02:00 AM