Skip to content

elapsedTime

The elapsedTime utilities (elapsedMicros, elapsedMillis, elapsedSeconds) provide convenient wrappers to measure time intervals using the Timer class as a backend. They replicate the familiar API from PJRC’s Arduino elapsedMillis/elapsedMicros, adapted for TM4C1294XL and TivaWare.

These classes allow precise tracking of microseconds, milliseconds, and seconds without using interrupts or blocking loops.

Header-only library Micro / Milli / Second precision
  • Lightweight, header-only implementation.
  • Uses your configured Timer instance for accurate hardware timing.
  • Compatible with both 32-bit and 64-bit timers.
  • Implicit conversion to numeric types for easy comparison or arithmetic.
  • Supports addition, subtraction, and assignment operations for intuitive usage.

  1. Create a Timer object and call begin() before using the elapsed classes.

  2. For each task you want to time, create a separate elapsedMicros, elapsedMillis, or elapsedSeconds object, depending on the required precision.

  3. In your main loop, check if the elapsed time exceeds your threshold
    (for example, if (emillis >= 1000)).

  4. If the condition is met, perform the task and reset the counter by assigning zero (emillis = 0;).

#include "timerLib.h"
#include "elapsedTime.h"
#define LED_PORT GPIO_PORTN_BASE
#define LED_PIN GPIO_PIN_1
Timer timer;
int main(void)
{
uint32_t sysclk = SysCtlClockFreqSet(
SYSCTL_XTAL_25MHZ | SYSCTL_OSC_MAIN |
SYSCTL_USE_PLL | SYSCTL_CFG_VCO_480, 120000000);
timer.begin(sysclk, TIMER0_BASE);
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPION);
while(!SysCtlPeripheralReady(SYSCTL_PERIPH_GPION));
GPIOPinTypeGPIOOutput(LED_PORT, LED_PIN);
elapsedMillis blink(timer);
elapsedSeconds log(timer);
bool led = false;
while (1) {
if (blink >= 500) { // Toggle LED every 500 ms
led = !led;
GPIOPinWrite(LED_PORT, LED_PIN, led ? LED_PIN : 0);
blink = 0;
}
if (log >= 2) { // Print every 2 seconds
printf("%llu us, %llu ms, %llu s\n",
(uint64_t)elapsedMicros(timer),
(uint64_t)elapsedMillis(timer),
(uint64_t)elapsedSeconds(timer));
log = 0;
}
}
}

Tracks elapsed microseconds since creation or last reset.

explicit elapsedMicros(Timer& timer);
operator uint64_t() const;

Example:

elapsedMicros mu(timer);
if (mu >= 100) { // 100 microseconds
mu = 0;
}

Tracks elapsed milliseconds since creation or last reset.

explicit elapsedMillis(Timer& timer);
operator uint64_t() const;

Example:

elapsedMillis ms(timer);
if (ms >= 500) { // 0.5 second
toggleLED();
ms = 0;
}

Tracks elapsed seconds since creation or last reset.

explicit elapsedSeconds(Timer& timer);
operator uint64_t() const;

Example:

elapsedSeconds sec(timer);
if (sec >= 5) {
printf("5 seconds passed!\n");
sec = 0;
}

Each class supports these operations:

OperationDescription
=Resets timer offset to specific value.
+=, -=Adjusts internal offset (forward/backward).
+, -Returns a new adjusted copy.
Implicit conversion to uint64_tReturns elapsed time.

#include "timerLib.h"
#include "elapsedTime.h"
#define LED_PORT GPIO_PORTN_BASE
#define LED_PIN GPIO_PIN_1
Timer timer;
int main(void)
{
uint32_t sysclk = SysCtlClockFreqSet(
SYSCTL_XTAL_25MHZ | SYSCTL_OSC_MAIN |
SYSCTL_USE_PLL | SYSCTL_CFG_VCO_480, 120000000);
timer.begin(sysclk, TIMER0_BASE);
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPION);
while(!SysCtlPeripheralReady(SYSCTL_PERIPH_GPION));
GPIOPinTypeGPIOOutput(LED_PORT, LED_PIN);
elapsedMillis blink(timer);
elapsedSeconds log(timer);
bool led = false;
while (1) {
if (blink >= 500) { // Toggle LED every 500 ms
led = !led;
GPIOPinWrite(LED_PORT, LED_PIN, led ? LED_PIN : 0);
blink = 0;
}
if (log >= 2) { // Print every 2 seconds
printf("%llu us, %llu ms, %llu s\n",
(uint64_t)elapsedMicros(timer),
(uint64_t)elapsedMillis(timer),
(uint64_t)elapsedSeconds(timer));
log = 0;
}
}
}

  • Always create at least one Timer instance and call begin() before using elapsed classes.
  • Each elapsed instance internally holds a pointer to a Timer backend.
  • Multiple elapsed instances can share the same timer for synchronized measurements.
  • Precision depends on timer resolution (microseconds for 120 MHz clock ≈ 1 µs per tick).

  • timerLib.h
  • driverlib/sysctl.h
  • driverlib/timer.h

Developed for ECE3849 — Real-Time Embedded Systems. Based on PJRC’s open elapsedMillis API, rewritten for TivaWare.

Author: Edwin R. Version 1.0