AnsweredAssumed Answered

Delay function on Timer

Question asked by K. Suppakit on Jan 30, 2014
Latest reply on Jan 30, 2014 by francescato.diego
Hi, I need to implement acculate delay_us. Try to use timer like this.

01.void delay_us ( uint32_t usec )
02.{
03.    TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
04. 
05.    /* TIM2 clock enable */
06.    RCC_APB1PeriphClockCmd ( RCC_APB1Periph_TIM2, ENABLE );
07. 
08.    /* Time base configuration */
09.    TIM_TimeBaseStructure.TIM_Period = (SystemCoreClock / 1000000) - 1;
10.    TIM_TimeBaseStructure.TIM_Prescaler = usec - 1;
11.    TIM_TimeBaseStructure.TIM_ClockDivision = 0;
12.    TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
13.    TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
14. 
15.    /* TIM2 enable counter */
16.    TIM_Cmd(TIM2, ENABLE);
17. 
18.    while (TIM_GetFlagStatus(TIM2, TIM_FLAG_Update) == RESET );
19.    TIM_ClearFlag (TIM2, TIM_FLAG_Update);
20. 
21.    /* TIM2 disable counter */
22.    TIM_Cmd(TIM2, DISABLE);
23.    TIM_DeInit(TIM2);
24.}

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.

Outcomes