Home | Projects | Notes > ARM Cortex-M3/M4 Processor > Implementing delay()
delay()
delay()
without Using LibraryTo implement the delay()
function yourself without using the library, follow the following steps:
Write a simple for
loop and see how many assembly instructions each loop takes. In the following example, it is observed that the 7 instructions in the red box are repeated each loop.
xxxxxxxxxx
51void delay(void)
2{
3 for(uint32_t i = 0; i < 300000; i++); // at this point the upper limit can be
4 // chosen any value
5}
Check the frequency of the processor.
e.g., STM32F407 Discovery board running with 16Mhz of internal RC oscillator
Assume 1 instruction takes 1 clock cycle.
1 instruction
Then, 7 instructions
0.5 micro seconds for
loop.
1000 micro seconds (1 ms)
150 ms
for
loop introduces about 150 ms of delay.
This can be used to introduce the debouncing time when implementing the button press detection logic.
Nayak, K. (2022). Embedded Systems Programming on ARM Cortex-M3/M4 Processor [Video file]. Retrieved from https://www.udemy.com/course/embedded-system-programming-on-arm-cortex-m3m4/