I need to implement a delay function with a hardware timer. The timer value is incremented every millisecond.
The usual approach is to use the width of the timer register and use modulo the behavior corresponding to
volatile int TimerReg;
void Delay(int amount)
{
int start = TimerReg;
int elapsed;
do
{
eleapsed = TimerReg - start;
} while (elapsed < amount);
}
This works when TimerReg has an int width. The difference now - startin this case is steadily increasing.
But when the width of the TimerReg is less than the width of the int or (as in my case), the timer only counts from 0..1000, there is a problem when the timer wraps from 999 to 1000 by 0.
What is a good approach to using such a timer? I would like to avoid the operation with the module, because it costs a lot on the microcontroller.
Edit: The separation module is not yet included in the microcontroller code.
source
share