Showing posts with label Timing Library. Show all posts
Showing posts with label Timing Library. Show all posts

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: https://www.programming-electronics-diy.xyz/2024/03/defining-sysclk-or-fcpu-in-simplicity.html

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.

Download

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

Saturday, January 9, 2021

millis and micros library for AVR microcontrollers - milliseconds and microseconds time tracking

Having precise timing in microcontrollers is important in many projects. For this you can use the two libraries presented in this article - millis & micros.

millis library triggers a timer interrupt every 1 millisecond and increments the milliseconds variable. The user can select the size of the milliseconds variable ranging from char (8 bits) to long long (64 bits) with an overflow from 255 milliseconds to 584.9 million years.

micros library is almost the same as millis but for microseconds. The overflow is between 255 microseconds and 584942 years.

For both libraries the user can select which timer to use: Timer0, Timer1, Timer2, Timer3 or Timer4.

In the case of millis library, it is recommended that other interrupts take less than 1 millisecond to complete otherwise the millis timer interrupt will be delayed. For the micros the ISRs must finish in less than 100 microseconds. The faster the CPU clock the better.

For the milliseconds and microseconds variable decide if you really need a 32 or 64 variable (long and long long) because the bigger the variable the longer it takes to increment it. For example on a 1MHz CPU it takes about 77 microseconds to increment a long long variable.

millis & micros library for AVR microcontrollers