cancel
Showing results for 
Search instead for 
Did you mean: 

stm32f4 Timer

puneethj
Associate II
Posted on November 10, 2015 at 17:34

Hello every one! How can i check if the timer is already running.. which flag to use I am trying this

if (TIM2 -> CR1 == TIM_CR1_CEN)

{

// turn led on blue

else

{

// turn led red on

}

but it does not work, please help me

#!stm32f4-disco #discovery #stm32f4 #timers
6 REPLIES 6
Posted on November 10, 2015 at 18:56

I'd presume it's looking for a bit level mask, not a comparison.

Tips, buy me a coffee, or three.. PayPal Venmo Up vote any posts that you find helpful, it shows what's working..
puneethj
Associate II
Posted on November 10, 2015 at 19:11

I dont know, but my task is to check if the timer is enabled or disabled. Please tell me how can i do that

Posted on November 10, 2015 at 19:17

It's a bit, mask it and check if it's zero or not

if (TIM2->CR1 & TIM_CR1_CEN) // test the enable BIT
{
// turn led on blue
} 
else
{
// turn led red on
}

Tips, buy me a coffee, or three.. PayPal Venmo Up vote any posts that you find helpful, it shows what's working..
ftiti
Associate II
Posted on November 10, 2015 at 19:21

Hello,

I have a solution proposal:

You can set an Update event by Software juste before enabling the timer ( timer when enabled it will generate an UEV )

  /* Set th UG bit to enable UEV */

  TIM1->EGR |= TIM_EGR_UG;

Then, you should manage by reading the UIF (update interrupt flag)  and cheking if it's set :

if ( (TIM2 -> SR & TIM_SR_UIF) != RESET )

{

// turn led on blue

}

else

{

// turn led red on

}

Good luck

puneethj
Associate II
Posted on November 10, 2015 at 19:28

The problem with Update interrupt flag is it is always triggered even when the timer is not started. 

ftiti
Associate II
Posted on November 11, 2015 at 13:06

Yes, I see. Since UDIS and URS bit in TIM_CR1 are reset by defaut, by enabling the timer an UEV is automatically generated and thus the UIF bit is set .

So it beacomes more easy for your case and you can directly check if the timer is enabled or not through the UIF bit status:

if ( (TIM2 -> SR & TIM_SR_UIF) != RESET )

{

// turn led on blue

}

else

{

// turn led red on

}