Skip to content

Commit

Permalink
Added function for modifying timer value
Browse files Browse the repository at this point in the history
  • Loading branch information
olivier gillet committed Feb 24, 2012
1 parent 596e8e8 commit 34ada87
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
7 changes: 7 additions & 0 deletions avrlib.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<typename Register, uint8_t bit>
struct BitInRegister {
Expand Down
16 changes: 10 additions & 6 deletions timer.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ SpecialFunctionRegister(TIMSK0);
SpecialFunctionRegister(TIMSK1);
SpecialFunctionRegister(TIMSK2);
SpecialFunctionRegister(TCNT0);
SpecialFunctionRegister(TCNT1);
SpecialFunctionRegister16(TCNT1);
SpecialFunctionRegister(TCNT2);
SpecialFunctionRegister(OCR0A);
SpecialFunctionRegister(OCR0B);
Expand Down Expand Up @@ -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;
}
Expand Down

0 comments on commit 34ada87

Please sign in to comment.