Skip to main content
puneethj
Associate III
November 10, 2015
Question

stm32f4 Timer

  • November 10, 2015
  • 6 replies
  • 901 views
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
This topic has been closed for replies.

6 replies

Tesla DeLorean
Guru
November 10, 2015
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 VenmoUp vote any posts that you find helpful, it shows what's working..
puneethj
puneethjAuthor
Associate III
November 10, 2015
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

Tesla DeLorean
Guru
November 10, 2015
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 VenmoUp vote any posts that you find helpful, it shows what's working..
ftiti
Visitor II
November 10, 2015
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
puneethjAuthor
Associate III
November 10, 2015
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
Visitor II
November 11, 2015
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

}