cancel
Showing results for 
Search instead for 
Did you mean: 

TIMERS used as DELAY Function For Beginners on STM32F3

projnikdroid
Associate II
Posted on December 19, 2013 at 13:28

Function works for TIM6 basic timer used as delay on STM32F303 , with suitable modifications can work on any STM8,32 variants

void TIMER_DELAY(uint16_t value)

{

RCC->APB1ENR |= RCC_APB1Periph;

//value is 0x10

        TIM6->ARR = value;       // delay in ms

TIM6->PSC = 36000-1;  // APB1 = 36Mhz so eqn :  CLK_CNT = (36000000)/36000,i.e. CLK_CNT = 1Khz

TIM6->CR1 = 0x01;         // ENaBle TIMER 

while(!((TIM6->SR & 0x01) == 0x01));  // Check for Update Event

TIM6->CR1 &= ~(0x01);                      // Disable timer

TIM6->SR &= ~(0x01);                        // Reset the update event

}

For eg. TIMER_DELAY(100) would provide me 100ms delay.

#old-school-delay-loops
5 REPLIES 5
Posted on December 19, 2013 at 16:06

TIM6->SR = ~(0x01);    // Not RMW

Personally I find DWT_CYCCNT to be far easier, and providing sub-microsecond resolution.
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
dthedens23
Associate II
Posted on December 19, 2013 at 17:59

I would get laughed out of the office if I were to submit some code for review which had a spin in place delay loop for some number of milliseconds.

The timer peripheral is better off being used as a system resource as a one shot timer where modules can register for a call back.  The module passes a time interval parameter when registering.

chen
Associate II
Posted on December 19, 2013 at 18:19

That is the difference between professional engineers and the amateur!

We should encourage the amateurs to learn better styles of coding.

projnikdroid
Associate II
Posted on December 20, 2013 at 14:07

Dear RocketDawg,

I guess it's very hard for u to read the title, so just to clarify , it was intended only for some basic knowledge.
projnikdroid
Associate II
Posted on December 20, 2013 at 14:09

Dear sung.chen_chung,

FYI coders are not born professionalists(some exceptions)...it always starts from amateurs. And instead of showing criticism ,,, enlighten others