cancel
Showing results for 
Search instead for 
Did you mean: 

Is it possible to generate delay in microseconds in STM32L053C6?

Huzaifah Limbada
Associate II
Posted on March 29, 2018 at 14:52

I'm using STM32L053C6 for the project and i'm using it on the 16 MHz clock frequency,

i want to generate the delay in the microseconds but i can't found any solution regurding this,

if anyone knows please share it,

Thank you!

#stm32l053 #stm32l053-discovery-board
9 REPLIES 9
Huzaifah Limbada
Associate II
Posted on March 29, 2018 at 14:54

it may not C6 it, so you can share it for the STM32L053 also.

Uwe Bonnes
Principal II
Posted on March 29, 2018 at 16:30

With some timer running, get the present value of the timer, calculate the value of the timer when the delay should end and wait until the timer reaches the expected value. Do expensive calculation when starting up. Care for timer roll-over.

Posted on March 29, 2018 at 18:41

Would agree, you could free run a TIM at 16 MHz or 1 MHz depending on the granularity you want

uint16_t start, current;

start = TIM2->CNT;

do

{

  current = TIM2->CNT;

} while((current - start) < 32); // 2us at 16 MHz tick

Tips, buy me a coffee, or three.. PayPal Venmo Up vote any posts that you find helpful, it shows what's working..
Posted on March 30, 2018 at 11:45

Cam you please explain it in briefe.

Posted on March 30, 2018 at 14:42

Pick a TIM and configure the timebase to maximal (ie 0xFFFF)

The HAL software trees have a lot of examples, you should review those.

Tips, buy me a coffee, or three.. PayPal Venmo Up vote any posts that you find helpful, it shows what's working..
henry.dick
Senior II
Posted on March 30, 2018 at 17:03

I generally have a free running timer in my application. SysTick often but others do as well.

Time stamp a variable and wait for a set duration to expire.

Posted on March 30, 2018 at 17:16

The OP wants micro-second precision, not milli-second, and the CM0 doesn't provide access to 32-bit wide DWT_CYCCNT, and the SYSTICK counter goes backward, and is 24-bit making the wrapping math more tedious for tight loops.

Tips, buy me a coffee, or three.. PayPal Venmo Up vote any posts that you find helpful, it shows what's working..
Posted on March 30, 2018 at 17:25

STM32Cube_FW_L0_V1.10.0\Projects\STM32L053C8-Discovery\Examples\TIM\TIM_TimeBase

  /*♯♯-1- Configure the TIM peripheral ♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯*/

  /* Set TIMx instance */

  TimHandle.Instance = TIMx;

  TimHandle.Init.Period = 0xFFFF; // 16-bit maximal count

  TimHandle.Init.Prescaler = 0; // At CPU clock, or for 1 MHz (1us ticks) CoreMHz-1

  TimHandle.Init.ClockDivision = 0;

  TimHandle.Init.CounterMode = TIM_COUNTERMODE_UP;

  if(HAL_TIM_Base_Init(&TimHandle) != HAL_OK)

  {

    /* Initialization Error */

    ErrorHandler();

  }

Current count readable via TIMx->CNT, elapsed time computed via subtracting start count from current count, basic math, wrapping handled if math done with uint16_t

Tips, buy me a coffee, or three.. PayPal Venmo Up vote any posts that you find helpful, it shows what's working..
henry.dick
Senior II
Posted on March 30, 2018 at 19:32

'

the SYSTICK counter goes backward, and is 24-bit making the wrapping math more tedious for tight loops.'

so what? sounds like a non-issue.