Problem enabling timer in STM32F103
Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2015-07-17 3:50 AM
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.
This discussion is locked. Please start a new topic to ask your question.
4 REPLIES 4
Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2015-07-17 4:33 AM
Posted on July 17, 2015 at 13:33
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.
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Up vote any posts that you find helpful, it shows what's working..
Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2015-07-17 7:14 AM
Posted on July 17, 2015 at 16:14 Sorry, but the code is too large to be copied in the forum, but I will try to be more specific. Thanks for the tip. Anyway, the problem is solved now. The problem was that I did not call the function ''TIM_ITConfig'' right before ''TIM_Cmd''. The function is called now and it is working correctly.
Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2015-07-17 7:31 AM
Posted on July 17, 2015 at 16:31
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.
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Up vote any posts that you find helpful, it shows what's working..
Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2015-07-21 2:00 AM
Posted on July 21, 2015 at 11:00 Sure.
