Handy clock and timer functions. Platform independent.
Find a file
Repository files (latest commit first)
Filename Latest commit message Latest commit date
2026-11-06 22:42:14 +00:00
mw1cgg_timers.c Added missing return 2026-11-06 20:39:27 +00:00
mw1cgg_timers.h Changed count to int32 2026-11-06 22:42:14 +00:00
README.md Added URL 2026-11-04 17:32:45 +00:00

MW1CGG Timers

(c) MW1CGG / jamesb https://git.duckduckpigeon.co.uk/jamesb/MW1CGG_Timers

This module is to allow easily handling of timing periods and/or timers.

Clocks

Ideally you would set this up so you have at least one clock every 1ms, or time period of your choice. Additional clocks are then based from this timing period.

Call MW1CGG_HandleClock in your timer interupt handler for each clock. If it is the only clock then set main_clock to true.

Define each clock as below

MW1CGG_CLOCK_S Clock;

or

MW1CGG_CLOCK_S Clocks[3];

where 3 is the number of clocks you want.

In the interrupt handler call

MW1CGG_HandleClock(&Clock);

or

MW1CGG_HandleClock(&Clock[0]);
MW1CGG_HandleClock(&Clock[1]);
MW1CGG_HandleClock(&Clock[2]);

Initialise your clocks. I'm using an example of a 1ms clock for this - Clock[0] is 1ms, Clock[1] is 10ms, Clock[2] is 100ms.

Clock[0].count = 0;
Clock[0].reload = 0;

Clock[1].count = 0;
Clock[1].reload = 10;

Clock[2].count = 0;
Clock[2].reload = 100;

internal_count is just a helper so you can count within your timer periods

In the main code loop you would regularly run and check if a clock has been ticked, eg:

if (Clock[0].ticked)
{
    // 1ms
    // do stuff here
    Clock[0].ticked = false;
}

if (Clock[1].ticked)
{
    // 10ms
    // do stuff here
    Clock[1].ticked = false;
}

if (Clock[2].ticked)
{
    // 1ms
    // do stuff here
    Clock[2].ticked = false;
}

### Timers

Timers are for more arbitary timing - in this example it will be 30 seconds. Unlike the Clocks, the
timers count up rather than down.

Define your timer as

MW1CGG_TIMER_S Timer;


and initialise as below

Timer.enabled = true; Timer.count = 0; Timer.max_count = 30; Timer.stop_max_count = true;


In your clock handling, call MW1CGG_HandleTimer(&Timer) every second. In your main application code you would then
check if count >= max_count and then do whatever you wanted to do. You will need to reset the count manually when
you've handled it.

stop_max_count if enabled will stop the count at max_count. If disabled it will keep counting up until you either
reset count manually or it rolls over at UINT32_MAX. It allows you to see the time difference between when you wanted
it to happen and when you actually handled it.