Option to use 32768Hz watch crystal for timing. Code suggested by Georg von Zengen.
This commit is contained in:
parent
e51f03eeb1
commit
ab7f3bd8e3
2 changed files with 62 additions and 47 deletions
|
@ -37,7 +37,44 @@
|
||||||
TIMSK = _BV (OCIE0);
|
TIMSK = _BV (OCIE0);
|
||||||
|
|
||||||
#elif defined (__AVR_ATmega1284P__) || (__AVR_AT90USB1287__) || (__AVR_ATmega1281__)
|
#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() \
|
#define OCRSetup() \
|
||||||
/* Select internal clock */ \
|
/* Select internal clock */ \
|
||||||
ASSR = 0x00; \
|
ASSR = 0x00; \
|
||||||
|
@ -48,10 +85,10 @@
|
||||||
/* \
|
/* \
|
||||||
* Set comparison register: \
|
* Set comparison register: \
|
||||||
* Crystal freq. is 8000000,\
|
* Crystal freq. is 8000000,\
|
||||||
* pre-scale factor is 1024, i.e. we have 125 "ticks" / sec: \
|
* pre-scale factor is 1024, we want 125 ticks / sec: \
|
||||||
* 8000000 = 1024 * 125 * 62.5 \
|
* 8000000 = 1024 * 126.01 * 62, less 1 for CTC mode \
|
||||||
*/ \
|
*/ \
|
||||||
OCR0A = 62; \
|
OCR0A = 61; \
|
||||||
\
|
\
|
||||||
/* \
|
/* \
|
||||||
* Set timer control register: \
|
* Set timer control register: \
|
||||||
|
@ -69,6 +106,7 @@
|
||||||
* counter value in TCNT0 is cleared automatically. \
|
* counter value in TCNT0 is cleared automatically. \
|
||||||
*/ \
|
*/ \
|
||||||
TIMSK0 = _BV (OCIE0A);
|
TIMSK0 = _BV (OCIE0A);
|
||||||
|
#endif /* AVR_CONF_USE32KCRYSTAL */
|
||||||
|
|
||||||
#define AVR_OUTPUT_COMPARE_INT TIMER0_COMPA_vect
|
#define AVR_OUTPUT_COMPARE_INT TIMER0_COMPA_vect
|
||||||
|
|
||||||
|
|
|
@ -6,19 +6,32 @@
|
||||||
#include <avr/io.h>
|
#include <avr/io.h>
|
||||||
#include <avr/interrupt.h>
|
#include <avr/interrupt.h>
|
||||||
|
|
||||||
/* RADIOSTATS should be also be defined in the radio driver and webserver */
|
static volatile clock_time_t count;
|
||||||
|
static volatile uint8_t scount;
|
||||||
|
volatile unsigned long seconds;
|
||||||
|
|
||||||
|
/* Set RADIOSTATS to monitor radio on time (must also be set in the radio driver) */
|
||||||
#if RF230BB && WEBSERVER
|
#if RF230BB && WEBSERVER
|
||||||
#define RADIOSTATS 1
|
#define RADIOSTATS 1
|
||||||
#endif
|
#endif
|
||||||
#if RADIOSTATS
|
#if RADIOSTATS
|
||||||
static volatile clock_time_t count, scount, rcount;
|
static volatile uint8_t rcount;
|
||||||
volatile unsigned long seconds,radioontime;
|
volatile unsigned long radioontime;
|
||||||
extern uint8_t RF230_receive_on;
|
extern uint8_t RF230_receive_on;
|
||||||
#else
|
|
||||||
static volatile clock_time_t count, scount;
|
|
||||||
volatile unsigned long seconds;
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
/*
|
||||||
|
CLOCK_SECOND is the number of ticks per second.
|
||||||
|
It is defined through CONF_CLOCK_SECOND in the contiki-conf.h for each platform.
|
||||||
|
The usual AVR default is ~125 ticks per second, counting a prescaler the CPU clock
|
||||||
|
using the 8 bit timer0.
|
||||||
|
|
||||||
|
As clock_time_t is an unsigned 16 bit data type, intervals up to 524 seconds
|
||||||
|
can be measured with 8 millisecond precision.
|
||||||
|
For longer intervals a 32 bit global is incremented every second.
|
||||||
|
|
||||||
|
clock-avr.h contains the specific setup code for each mcu.
|
||||||
|
*/
|
||||||
/*---------------------------------------------------------------------------*/
|
/*---------------------------------------------------------------------------*/
|
||||||
//SIGNAL(SIG_OUTPUT_COMPARE0)
|
//SIGNAL(SIG_OUTPUT_COMPARE0)
|
||||||
ISR(AVR_OUTPUT_COMPARE_INT)
|
ISR(AVR_OUTPUT_COMPARE_INT)
|
||||||
|
@ -41,49 +54,13 @@ ISR(AVR_OUTPUT_COMPARE_INT)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* External clock source does not work ? */
|
|
||||||
#if 0
|
|
||||||
void
|
|
||||||
clock_init(void)
|
|
||||||
{
|
|
||||||
cli ();
|
|
||||||
TIMSK &= ((unsigned char)~(1 << (TOIE0)));
|
|
||||||
TIMSK &= ((unsigned char)~(1 << (OCIE0)));
|
|
||||||
/* Disable TC0 interrupt */
|
|
||||||
|
|
||||||
/**
|
|
||||||
* set Timer/Counter0 to be asynchronous
|
|
||||||
* from the CPU clock with a second external
|
|
||||||
* clock(32,768kHz)driving it
|
|
||||||
*/
|
|
||||||
ASSR |= (1 << (AS0));
|
|
||||||
TCCR0 = _BV (CS02) | _BV (CS01) | _BV (WGM1);
|
|
||||||
|
|
||||||
TCNT0 = 0;
|
|
||||||
OCR0 = 128;
|
|
||||||
|
|
||||||
TIMSK |= (1 << (OCIE0));
|
|
||||||
TIMSK |= (1 << (TOIE0));
|
|
||||||
sei ();
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
/*---------------------------------------------------------------------------*/
|
/*---------------------------------------------------------------------------*/
|
||||||
void
|
void
|
||||||
clock_init(void)
|
clock_init(void)
|
||||||
{
|
{
|
||||||
cli ();
|
cli ();
|
||||||
|
|
||||||
|
|
||||||
OCRSetup();
|
OCRSetup();
|
||||||
|
//scount = count = 0;
|
||||||
/*
|
|
||||||
* Counts the number of ticks. Since clock_time_t is an unsigned
|
|
||||||
* 16 bit data type, time intervals of up to 524 seconds can be
|
|
||||||
* measured.
|
|
||||||
*/
|
|
||||||
scount = count = 0;
|
|
||||||
|
|
||||||
sei ();
|
sei ();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue