2009-11-26 11:26 PM
Help initiating Timer
2011-05-17 04:32 AM
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 ]2011-05-17 04:32 AM
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 }