elapsedTime
Overview
Section titled “Overview”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 precisionFeatures
Section titled “Features”- Lightweight, header-only implementation.
- Uses your configured
Timerinstance 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.
Basic Usage
Section titled “Basic Usage”-
Create a
Timerobject and callbegin()before using the elapsed classes. -
For each task you want to time, create a separate
elapsedMicros,elapsedMillis, orelapsedSecondsobject, depending on the required precision. -
In your main loop, check if the elapsed time exceeds your threshold
(for example,if (emillis >= 1000)). -
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; } }}Classes Summary
Section titled “Classes Summary”elapsedMicros
Section titled “elapsedMicros”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;}elapsedMillis
Section titled “elapsedMillis”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;}elapsedSeconds
Section titled “elapsedSeconds”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;}Operator Support
Section titled “Operator Support”Each class supports these operations:
| Operation | Description |
|---|---|
= | Resets timer offset to specific value. |
+=, -= | Adjusts internal offset (forward/backward). |
+, - | Returns a new adjusted copy. |
Implicit conversion to uint64_t | Returns elapsed time. |
Example: LED Heartbeat + Sensor Update
Section titled “Example: LED Heartbeat + Sensor Update”#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; } }}Integration Notes
Section titled “Integration Notes”- Always create at least one
Timerinstance and callbegin()before using elapsed classes. - Each elapsed instance internally holds a pointer to a
Timerbackend. - 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).
Dependencies
Section titled “Dependencies”timerLib.hdriverlib/sysctl.hdriverlib/timer.h
License and Credits
Section titled “License and Credits”Developed for ECE3849 — Real-Time Embedded Systems. Based on PJRC’s open elapsedMillis API, rewritten for TivaWare.
Author: Edwin R. Version 1.0