Option to use 32768Hz watch crystal for timing. Code suggested by Georg von Zengen.

This commit is contained in:
dak664 2010-06-18 17:30:30 +00:00
parent e51f03eeb1
commit ab7f3bd8e3
2 changed files with 62 additions and 47 deletions

View file

@ -37,7 +37,44 @@
TIMSK = _BV (OCIE0);
#elif defined (__AVR_ATmega1284P__) || (__AVR_AT90USB1287__) || (__AVR_ATmega1281__)
/*
The Raven has a 32768Hz watch crystal that can be used to clock the timer
while the 1284p is sleeping. The Jackdaw has pads for a crystal. The crystal
can be used to clock the 8 bit timer2.
*/
#if AVR_CONF_USE32KCRYSTAL
#define OCRSetup() \
/* Clock from crystal on TOSC0-1 */ \
ASSR = _BV(AS2); \
\
/* Set counter to zero */ \
TCNT2 = 0; \
\
/* \
* Set comparison register: \
* Crystal freq. is 32768,\
* pre-scale factor is 8, we want 125 ticks / sec: \
* 32768 = 8 * 124.1 * 33, less 1 for CTC mode\
*/ \
OCR2A = 32; \
\
/* \
* Set timer control register: \
* - prescale: 8 (CS21) \
* - counter reset via comparison register (WGM21) \
*/ \
TCCR2A = _BV(WGM21); \
TCCR2B = _BV(CS21); \
\
/* Clear interrupt flag register */ \
TIFR2 = TIFR2; \
\
/* \
* Raise interrupt when value in OCR2 is reached. Note that the \
* counter value in TCNT2 is cleared automatically. \
*/ \
TIMSK2 = _BV (OCIE2A);
#else
#define OCRSetup() \
/* Select internal clock */ \
ASSR = 0x00; \
@ -48,10 +85,10 @@
/* \
* Set comparison register: \
* Crystal freq. is 8000000,\
* pre-scale factor is 1024, i.e. we have 125 "ticks" / sec: \
* 8000000 = 1024 * 125 * 62.5 \
* pre-scale factor is 1024, we want 125 ticks / sec: \
* 8000000 = 1024 * 126.01 * 62, less 1 for CTC mode \
*/ \
OCR0A = 62; \
OCR0A = 61; \
\
/* \
* Set timer control register: \
@ -69,6 +106,7 @@
* counter value in TCNT0 is cleared automatically. \
*/ \
TIMSK0 = _BV (OCIE0A);
#endif /* AVR_CONF_USE32KCRYSTAL */
#define AVR_OUTPUT_COMPARE_INT TIMER0_COMPA_vect