cancel
Showing results for 
Search instead for 
Did you mean: 

HAL_Delay in microseconds for NucleoF411RE

didos.nicky
Associate II
Posted on August 17, 2015 at 18:45

Hello everyone !! I have a

http://www.st.com/web/catalog/tools/FM116/SC959/SS1532/LN1847/PF260320

and use IAR with .

How can I implement

a

delay

with

HAL

in

us (microseconds)

?

Please give us some hints.

Best Regards

#microseconds #nucleo #hal
2 REPLIES 2
Posted on August 17, 2015 at 19:36

I think this question has been asked multiple times here.

You can't expect the processor to interrupt, and increment a variable at 1 MHz. What you need to do is use a free-running counter, either the TIM or DWT_CYCCNT one to mark brief amounts of time. One millisecond is SystemCoreClock / 1000, one microsecond is SystemCoreClock / 1000000

For delays that relate to the CPU clock speed, consider using DWT_CYCCNT

//******************************************************************************
volatile unsigned int *DWT_CYCCNT = (volatile unsigned int *)0xE0001004; //address of the register
volatile unsigned int *DWT_CONTROL = (volatile unsigned int *)0xE0001000; //address of the register
volatile unsigned int *SCB_DEMCR = (volatile unsigned int *)0xE000EDFC; //address of the register
//******************************************************************************
void EnableTiming(void)
{
*SCB_DEMCR = *SCB_DEMCR | 0x01000000;
*DWT_CYCCNT = 0; // reset the counter
*DWT_CONTROL = *DWT_CONTROL | 1 ; // enable the counter
}
//******************************************************************************
void TimingDelay(unsigned int tick)
{
unsigned int start, current;
start = *DWT_CYCCNT;
do
{
current = *DWT_CYCCNT;
} while((current - start) < tick);
}
//******************************************************************************

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
didos.nicky
Associate II
Posted on August 18, 2015 at 17:53

Fantastic

Clive, it works. 

Thanks for your help.