cancel
Showing results for 
Search instead for 
Did you mean: 

Help initiating Timer

sima
Associate II
Posted on November 27, 2009 at 08:26

Help initiating Timer

2 REPLIES 2
sima
Associate II
Posted on May 17, 2011 at 13:32

I need to make a delay of 5 seconds. I have tried to initialize TIM4 but it fails the first time, second time it works.

My test code:

Code:

<BR>// Initiate TIM4 <BR>TIM4->CR1 = 0; // Disable timer <BR>TIM4->ARR = 18000 - 1; // Count 18000 cycles = 5 seconds <BR>TIM4->PSC = 10000 - 1; // Divide TIM4CLK by 10000 <BR>TIM4->CNT = 0; // Clear counter register <BR>TIM4->SR = 0; // Clear status register <BR>TIM4->CR1 = TIM_CR1_OPM | TIM_CR1_CEN; // Enable timer <BR> <BR>while((TIM4->CR1 & TIM_CR1_CEN) == TIM_CR1_CEN) <BR>{ <BR> ; // wait <BR>} <BR>

What is wrong, why does it fail the first time but not the second time?

[ This message was edited by: sima1 on 27-11-2009 11:36 ]

sima
Associate II
Posted on May 17, 2011 at 13:32

Problem resolved :D

Code:

// Initiate TIM4

TIM4->CR1 = 0; // Disable timer

TIM4->ARR = 18000 - 1; // Count 18000 cycles = 5 seconds

TIM4->PSC = 10000 - 1; // Divide TIM2CLK by 10000

TIM4->EGR = TIM_EGR_UG; // **** ADDED THIS!!!!! Force reload of prescaler

TIM4->CNT = 0; // Clear counter register

TIM4->SR = 0; // Clear status register

TIM4->CR1 = TIM_CR1_OPM | TIM_CR1_CEN; // Enable timer

while((TIM4->CR1 & 0x0001) == 0x0001)

{

; // wait

}