From 34ada87a50cf4f689fa3eb5e7cf71b8e5112a6e6 Mon Sep 17 00:00:00 2001
From: olivier gillet
Date: Fri, 24 Feb 2012 18:06:08 +0100
Subject: [PATCH] Added function for modifying timer value
---
avrlib.h | 7 +++++++
timer.h | 16 ++++++++++------
2 files changed, 17 insertions(+), 6 deletions(-)
diff --git a/avrlib.h b/avrlib.h
index d2e6965..fb3de40 100755
--- a/avrlib.h
+++ b/avrlib.h
@@ -60,6 +60,13 @@ enum DigitalValue {
uint8_t operator()(const uint8_t& value) { return *ptr(); } \
};
+#define SpecialFunctionRegister16(reg) struct reg##Register { \
+ static volatile uint16_t* ptr() { return &_SFR_WORD(reg); } \
+ reg##Register& operator=(const uint16_t& value) { *ptr() = value; } \
+ uint16_t operator()(const uint16_t& value) { return *ptr(); } \
+};
+
+
// Represents a bit in an i/o port register.
template
struct BitInRegister {
diff --git a/timer.h b/timer.h
index 7fccff2..7c04b64 100755
--- a/timer.h
+++ b/timer.h
@@ -40,7 +40,7 @@ SpecialFunctionRegister(TIMSK0);
SpecialFunctionRegister(TIMSK1);
SpecialFunctionRegister(TIMSK2);
SpecialFunctionRegister(TCNT0);
-SpecialFunctionRegister(TCNT1);
+SpecialFunctionRegister16(TCNT1);
SpecialFunctionRegister(TCNT2);
SpecialFunctionRegister(OCR0A);
SpecialFunctionRegister(OCR0B);
@@ -87,17 +87,21 @@ struct TimerImpl {
// Sets the mode registers.
*StatusRegisterA::ptr() = (*StatusRegisterA::ptr() & 0xfc) | mode;
}
+
+ static inline void set_value(uint16_t value) {
+ *ValueRegister::ptr() = value;
+ }
// These are the values for MCUs clocked at 20 MHz
//
// Timer speed
// value | fast | accurate
// --------------------------------------
- // 1 | 78.125 kHz | 39.062 kHz
- // 2 | 9.765 kHz | 4.882 kHz
- // 3 | 1220.7 Hz | 610.3 Hz
- // 4 | 305.2 Hz | 152.6 Hz
- // 5 | 76.3 Hz | 38.1 Hz
+ // 1 | 78.125 kHz | 39.215 kHz
+ // 2 | 9.765 kHz | 4.901 kHz
+ // 3 | 1220.7 Hz | 612.7 Hz
+ // 4 | 305.2 Hz | 153.2 Hz
+ // 5 | 76.3 Hz | 38.3 Hz
static inline void set_prescaler(uint8_t prescaler) {
*StatusRegisterB::ptr() = (*StatusRegisterB::ptr() & 0xf8) | prescaler;
}