Skip to main content
sajeed
Associate II
August 28, 2014
Question

Delay with timers on stm32F107

  • August 28, 2014
  • 4 replies
  • 1096 views
Posted on August 28, 2014 at 11:52

Hi

I am working on STM32F107 evaluation board, There is a need for me to implement a delay in ms using H/W timers with polling method. I looked for other examples but they are not working for me can anyone suggest me how to go about.

#stm32f107-timers
This topic has been closed for replies.

4 replies

Tesla DeLorean
Guru
August 28, 2014
Posted on August 28, 2014 at 14:23

You could perhaps use One-Shot mode.

The easiest would perhaps be to put the timer in maximal mode where it free-runs across the 16-bit range. You then read TIMx->CNT and observe it tick for the desired number of time units. Set the prescaler so the timer ticks in 1us units, or whatever is convenient.

void delay(uint16_t ticks)
{
uint16_t start = TIMx->CNT;
do { } while((TIMx->CNT - start) < ticks);
}

Tips, Buy me a coffee, or three.. PayPal VenmoUp vote any posts that you find helpful, it shows what's working..
sajeed
sajeedAuthor
Associate II
September 15, 2014
Posted on September 15, 2014 at 12:44

Hi Clive

Thanks for the response.I was into some other project for some time. I tried implementing as you suggested but I feel I am missing something important as the timer doesnt start at all, it get stuck in the loop. Here is my implementation void Timer_Init(void) { /* Time base configuration */ TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; TIM_TimeBaseStructInit(&TIM_TimeBaseStructure); TIM_TimeBaseStructure.TIM_Prescaler = 0; TIM_TimeBaseStructure.TIM_Period = 65535; TIM_TimeBaseStructure.TIM_ClockDivision = 0; TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure); /* Enable counter */ TIM_Cmd(TIM4, ENABLE); }

void delay(uint16_t ms)
{
uint16_t start = TIM4->CNT;
do { } while((TIM4->CNT - start) < ms);
}

and the call /*Initialize the timer for delay operations*/ Timer_Init(); Delay_ms(10); right now it is not for 1 ms but it is not working with this delay. Please suggest what mistake is done here. Thanks and Regards Sajeed
Tesla DeLorean
Guru
September 15, 2014
Posted on September 15, 2014 at 14:38

Make sure the TIM4 clock is enabled on the APB/AHB bus as appropriate.

If you have a system with a 72 MHz clock, a Prescaler value of 72-1 will get you to 1 us ticks. You won't get 1 ms ticks as 72000-1 won't fit in 16-bit, you could get 500 us ticks with 36000-1
Tips, Buy me a coffee, or three.. PayPal VenmoUp vote any posts that you find helpful, it shows what's working..
sajeed
sajeedAuthor
Associate II
September 16, 2014
Posted on September 16, 2014 at 07:55

Hi Clive

Thanks for pointing out the problem and for the explanation. It worked.

Thanks and regards

Sajeed