0
\$\begingroup\$

I'm working with STM32F411 Discovery board. I use Atollic TrueStudio and HAL Library. I'm trying to make a simple program that checks when Timer2 overflows and, when it happens, I toggle an LED.

I can configure Timer2 with no problem. The problem is that I can't find a function that returns true when the timer overflows. I have to use __HAL_TIM_GetCounter(&hTim2); to get the timer count, and I can use interrupt. But, what happens if I only want to know when the timer overflows without using interrupts. What should I use?

\$\endgroup\$
1
\$\begingroup\$

Check the UIF bit of the TIM2->SR register. Here is a complete working example (mostly) without HAL, but the loop should work with HAL too.

void tim2_test() {
    /* My LED is on port B. Change this if yours is different */
    RCC->AHBENR |= RCC_AHBENR_GPIOBEN;

    /* Enable TIM2 */
    RCC->APB1ENR |= RCC_APB1ENR_TIM2EN;

    /* Counter reload value. Must be a 16 bit integer, i.e. between 1 and 65535
    SystemCoreClock should be set by HAL */
    TIM2->ARR = SystemCoreClock / 10000 - 1;

    /* Prescaler value, pick it so that both this and the previous
    value fall between 1 and 65535 */
    TIM2->PSC = 9999;

    /* Start the timer with default settings, it'll count from 0 up to ARR */
    TIM2->CR1 = TIM_CR1_CEN;

    /* Port B pin 0 as output. Change this for your LED */
    GPIOB->MODER = (GPIOB->MODER & ~GPIO_MODER_MODER0) | GPIO_MODER_MODER0_0;

    while(1) {

        /* Check the update flag. It'll be set every time the timer overflows */
        if(TIM2->SR & TIM_SR_UIF) {

            /* Reset the flag, so that we can catch the next overflow */
            TIM2->SR &= ~TIM_SR_UIF;

            /* Toggle LED pin, change this for your board */
            GPIOB->ODR ^= 1;
        }
    }
}
\$\endgroup\$
  • \$\begingroup\$ I've tried your code but it does not work. I've found in internet your code but it's used when there is a Timer interrupt. \$\endgroup\$ – Gonzalo Cervetti Nov 7 '16 at 2:47
  • \$\begingroup\$ The code works, how is your timer setup? I've posted mine. \$\endgroup\$ – berendi Nov 7 '16 at 9:53
1
\$\begingroup\$

You can use the HAL_TIM_PeriodElapsedCallback . It's a very useful function which is called everytime a timer overflows.

For example:

 void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim) 
{

if(htim->Instance == TIM2)                    // check if TIM2 overflowed
{        
  // here you insert the code to blink the led
}
}

I would also advise you to download CubeMx in which you can find many useful example projects (also you can generate initialization code for various peripherals if you want to.)

Disclaimer: I know you mentioned that you need a solution without the need to use interrupts and while my solution may not be what you need I think it is a good alternative you should consider. In case you need to stick with the interrupt-free solution, Berendi's solution (which is more advanced in my opinion) is what you need.

Regardless of what solution you may end up using check that your timer is set up correctly, that you've loaded the appropriate auto reload value and that you started the timer.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.