Skip to main content
GSING.19
Associate II
February 20, 2019
Question

Generating a delay of 5 microseconds using STM32F407VG Discovery board?

  • February 20, 2019
  • 10 replies
  • 3076 views

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?

This topic has been closed for replies.

10 replies

MNapi
Senior II
February 20, 2019

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

GSING.19
GSING.19Author
Associate II
February 20, 2019

Is using timer here a good idea?

MNapi
Senior II
February 20, 2019

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)

{}

S.Ma
Principal
February 20, 2019

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.

GSING.19
GSING.19Author
Associate II
February 21, 2019

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

Peter Mather
Associate III
February 20, 2019
#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);
}

Tesla DeLorean
Guru
February 20, 2019

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 (See Profile) Up vote any posts that you find helpful, it shows what's working..
turboscrew
Senior III
February 21, 2019

I used timer to handle a DHT22. I first configured the timer to output compare for start pulse, and then to input capture to read the data.