cancel
Showing results for 
Search instead for 
Did you mean: 

Delay function on Timer

suppakit
Associate II
Posted on January 30, 2014 at 07:43

Hi, I need to implement acculate delay_us. Try to use timer like this.


void
delay_us ( uint32_t usec )

{

TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;


/* TIM2 clock enable */

RCC_APB1PeriphClockCmd ( RCC_APB1Periph_TIM2, ENABLE );


/* Time base configuration */

TIM_TimeBaseStructure.TIM_Period = (SystemCoreClock / 1000000) - 1;

TIM_TimeBaseStructure.TIM_Prescaler = usec - 1;

TIM_TimeBaseStructure.TIM_ClockDivision = 0;

TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;

TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);


/* TIM2 enable counter */

TIM_Cmd(TIM2, ENABLE);


while
(TIM_GetFlagStatus(TIM2, TIM_FLAG_Update) == RESET );

TIM_ClearFlag (TIM2, TIM_FLAG_Update);


/* TIM2 disable counter */

TIM_Cmd(TIM2, DISABLE);

TIM_DeInit(TIM2);

}

But it doesn't work. it struct on while loop. I have to run this delay on the FreeRTOS. What is the best way to do? I used XTAL for CPU 25 MHz and set SystemCoreClock with 150000000. #timer-delay
3 REPLIES 3
francescatodiego
Associate II
Posted on January 30, 2014 at 08:33

Check the TIM_Period and TIM_Prescaler assignement in  your code

suppakit
Associate II
Posted on January 30, 2014 at 08:51

You mean the value of 

 TIM_Period and TIM_Prescaler, right?

Or check the value is already kept on the register. If this, how we check it?
francescatodiego
Associate II
Posted on January 30, 2014 at 10:05

Sorry for unclear answer

T

he

prescaler

must contain

the

frequency divider

for

the timer

tick

you want (

The calculation

depends on

how are configured

the

AHBx

APBx

clocks)

The

Period

field must contain the

timer

delay

in multiples of the

timer

tics