Friday, March 15, 2024

Delay functions for Silicon Labs microcontrollers

Sometimes you need to delay the program execution, for example when waiting for another device to startup or respond and the software cannot continue. An interrupt driven delay is better in most cases but not in all cases. For this purpose I made two functions one for milliseconds and one for microseconds, that uses NOPs to delay the program execution for a certain amount of time. The milliseconds delay is quite accurate, the microseconds one, not so much.

For this to work, the SYSCLK must be defined first. It is recommended to be defined in the project settings. You can find a short tutorial here on how to add a global define in Simplicity Studio:

Tested on:

- C8051F330

For other microcontrollers the following SFR declarations include file should be replaced accordingly:

#include <SI_C8051F330_Register_Enums.h>

Library usage


Milliseconds delay

_delay_ms(uint32_t ms)

Microseconds delay

_delay_us(uint32_t us)

I couldn't figure a way to make millis delay more accurate since the code used to create the delay, adds delay and cannot be easily subtracted because it depends of the delay time. I think it should be implemented using assembly but i don't know the language. If you need just a certain delay time, you could tweak the function for that particular delay, using a logic analyzer.


v1.0 delay Contains delay.h and delay.c

No comments:

Post a Comment