Skip to content

Commit

Permalink
Since the prescalers are determined at compile time, make them const
Browse files Browse the repository at this point in the history
variables.
  • Loading branch information
root committed Aug 30, 2013
1 parent 6dd8c97 commit 7ee75f9
Showing 1 changed file with 28 additions and 34 deletions.
62 changes: 28 additions & 34 deletions lib/Charliplexing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,34 @@ typedef struct prescalerInfo {
uint8_t tccr2;
};

prescalerInfo slowPrescaler, fastPrescaler;
const prescalerInfo
#if defined (__AVR_ATmega168__) || defined (__AVR_ATmega48__) || defined (__AVR_ATmega88__) || defined (__AVR_ATmega328P__) || defined (__AVR_ATmega1280__) || defined (__AVR_ATmega2560__) || defined (__AVR_ATmega8__)
# if F_CPU < 8000000UL
fastPrescaler = { 8, _BV(CS20) }, // 1
slowPrescaler = { 1, _BV(CS21) }; // 8
# else
fastPrescaler = { 4, _BV(CS21) }, // 8
slowPrescaler = { 1, _BV(CS21) | _BV(CS20) }; // 32
# endif
#elif defined (__AVR_ATmega128__)
# if F_CPU < 8000000UL
fastPrescaler = { 8, _BV(CS20) }, // 1
slowPrescaler = { 1, _BV(CS21) }; // 8
# else
fastPrescaler = { 8, _BV(CS21) }, // 8
slowPrescaler = { 1, _BV(CS21) | _BV(CS20) }; // 64
# endif
#elif defined (__AVR_ATmega32U4__)
# if F_CPU < 8000000UL
fastPrescaler = { 8, _BV(WGM12) | _BV(CS10) }, // 1
slowPrescaler = { 1, _BV(WGM12) | _BV(CS11) }; // 8
# else
fastPrescaler = { 8, _BV(WGM12) | _BV(CS11) }, // 8
slowPrescaler = { 1, _BV(WGM12) | _BV(CS11) | _BV(CS10) }; // 64
# endif
#else
# error no support for this chip
#endif

static bool initialized = false;

Expand Down Expand Up @@ -225,39 +252,6 @@ void LedSign::Init(uint8_t mode)
OCR1A = 256;
#endif

#if defined (__AVR_ATmega168__) || defined (__AVR_ATmega48__) || defined (__AVR_ATmega88__) || defined (__AVR_ATmega328P__) || defined (__AVR_ATmega1280__) || defined (__AVR_ATmega2560__) || defined (__AVR_ATmega8__)
if (F_CPU < 8000000UL) {
fastPrescaler.tccr2 = _BV(CS20); // 1
slowPrescaler.tccr2 = _BV(CS21); // 8
fastPrescaler.relativeSpeed = 8;
} else { // F_CPU >= 8Mhz, prescaler set to 8
fastPrescaler.tccr2 = _BV(CS21); // 8
slowPrescaler.tccr2 = _BV(CS21) | _BV(CS20); // 32
fastPrescaler.relativeSpeed = 4;
}
#elif defined (__AVR_ATmega128__)
if (F_CPU < 8000000UL) { // prescaler set to 8
fastPrescaler.tccr2 = _BV(CS20); // 1
slowPrescaler.tccr2 = _BV(CS21); // 8
fastPrescaler.relativeSpeed = 8;
} else { // F_CPU >= 8Mhz, prescaler set to 8
fastPrescaler.tccr2 = _BV(CS21); // 8
slowPrescaler.tccr2 = _BV(CS21) | _BV(CS20); // 64
fastPrescaler.relativeSpeed = 8;
}
#elif defined (__AVR_ATmega32U4__)
if (F_CPU < 8000000UL) { // prescaler set to 8
fastPrescaler.tccr2 = _BV(WGM12) | _BV(CS10); // 1
slowPrescaler.tccr2 = _BV(WGM12) | _BV(CS11); // 8
fastPrescaler.relativeSpeed = 8;
} else { // F_CPU >= 8Mhz, prescaler set to 8
fastPrescaler.tccr2 = _BV(WGM12) | _BV(CS11); // 8
slowPrescaler.tccr2 = _BV(WGM12) | _BV(CS11) | _BV(CS10); // 64
fastPrescaler.relativeSpeed = 8;
}
#endif
slowPrescaler.relativeSpeed = 1;

// Record whether we are in single or double buffer mode
displayMode = mode;
#ifdef DOUBLE_BUFFER
Expand Down

0 comments on commit 7ee75f9

Please sign in to comment.