cancel
Showing results for 
Search instead for 
Did you mean: 

Generating a delay of 5 microseconds using STM32F407VG Discovery board?

GSING.19
Associate II

Dear All,

I need to a get a delay of 5 microseconds using STM32F407VG Discovery board.

Is using a timer a good option for this or there is other ways available to solve the problem without using any external library?

10 REPLIES 10
MNapi
Senior III

I looked at this before there seems to be only one function HAL_Delay(); which gives you only milliseconds.

The only way I see is to use

for (int i=1; i<1000; i++)

{}

but you need to find a way to measure how many you need. They way I did is. I connected oscilocope

and I do turn a pin high and low before and after the loop to measure fequency.

HAL_GPIO_WritePin and you use SET and RESET to turn it on and off

Is using timer here a good idea?

I do not know how you want to implement the times as interrupt ? you could use PWM to get 5 microseconds

then you could use to jump on falling or rising to: and do whatever you need inside.

HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)

{}

I was using a timer and polling to generate a delay of 5 microseconds.

I do not think that there is anything in C++ or in HAL drivers

But if you go deeper into assembly languages you could do much more start one time 5 microseconds later etc.

But not something I could help you with. Actually I have sensor which have to be driven with different timers, one delayed 3 microseconds before the other kicks in etc.

I did it with various PWM and combination of external NOR and NAND gates. It works pretty well.

S.Ma
Principal

I guess you just want 5+ usec delay?

Is this delay triggering HW activity or not?

If it's pure SW, if you are just Waiting for 5+ usec delays you can use a SW delay with NOPs.

The number of loops depends on SYSCLK frequency.

Peter Mather
Associate III
#define SetupTime 25
void uSec(int us) {
  us=us*(PeripheralBusSpeed/1000000)-SetupTime;
  __HAL_TIM_SET_COUNTER(&htim2, 0);
  while (__HAL_TIM_GET_COUNTER(&htim2)<us);
}

In most of my applications I set up a 32-bit timer (TIM2) freerunning at bus speed. Then I can use it for any short delays

/* TIM2 init function */
static void MX_TIM2_Init(void)
{
 
  TIM_ClockConfigTypeDef sClockSourceConfig;
  TIM_MasterConfigTypeDef sMasterConfig;
 
  htim2.Instance = TIM2;
  htim2.Init.Prescaler = 0;
  htim2.Init.CounterMode = TIM_COUNTERMODE_UP;
  htim2.Init.Period = 0xFFFFFFFF;
  htim2.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
  htim2.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_ENABLE;
  if (HAL_TIM_Base_Init(&htim2) != HAL_OK)
  {
    _Error_Handler(__FILE__, __LINE__);
  }
 
  sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
  if (HAL_TIM_ConfigClockSource(&htim2, &sClockSourceConfig) != HAL_OK)
  {
    _Error_Handler(__FILE__, __LINE__);
  }
 
  sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
  sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
  if (HAL_TIMEx_MasterConfigSynchronization(&htim2, &sMasterConfig) != HAL_OK)
  {
    _Error_Handler(__FILE__, __LINE__);
  }
 
}
unsigned long readusclock(void){
	return __HAL_TIM_GET_COUNTER(&htim2)/(PeripheralBusSpeed/1000000);
}
void initusclock(void){
	__HAL_TIM_SET_COUNTER(&htim2, 0);
}

See also examples of DWT_CYCCNT usage, this provide nano-second level granularity without using a TIM

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..

I have sensor in order to activate that sensor I need to held line low for 5 microseconds.