From f934e3dd03c8051ac3d467fef8f034547f31df1e Mon Sep 17 00:00:00 2001 From: Xoaquin Castrelo <xoaquin.cb@gmail.com> Date: Sat, 13 Nov 2021 21:04:37 +0000 Subject: [PATCH] Added time tracking. --- emb/time.cpp | 29 +++++++++++++++++++++++++++++ emb/time.hpp | 16 ++++++++++++++++ 2 files changed, 45 insertions(+) create mode 100644 emb/time.cpp create mode 100644 emb/time.hpp diff --git a/emb/time.cpp b/emb/time.cpp new file mode 100644 index 0000000..72da5a1 --- /dev/null +++ b/emb/time.cpp @@ -0,0 +1,29 @@ +#include "time.hpp" + +#include <avr/io.h> +#include <avr/interrupt.h> +#include <stdint.h> + +static uint32_t millis; + +void initTime() +{ + cli(); + + TCCR0A = (1 << WGM01); // CTC mode, TOP = OCRA + TCCR0B = (1 << CS01) | (1 << CS00); // 64th prescaler + OCR0A = 249; // set TOP for 1 kHz interrupts + TIMSK0 = (1 << OCIE0A); // enable compare A interrupt + + sei(); +} + +uint32_t getCurrentMillis() +{ + return millis; +} + +ISR(TIMER0_COMPA_vect) +{ + millis++; +} diff --git a/emb/time.hpp b/emb/time.hpp new file mode 100644 index 0000000..27a93e5 --- /dev/null +++ b/emb/time.hpp @@ -0,0 +1,16 @@ +#ifndef TIME_H +#define TIME_H + +#include <stdint.h> + +/** + * Initialises time. + */ +void initTime(); + +/** + * Gets the number of milliseconds since program start. + */ +uint32_t getCurrentMillis(); + +#endif /* TIME_H */ -- GitLab