I've got an application up and running on a MC9S08SH8 that blinks an LED once per second (as a test) using an interrupt triggered by the real time counter. I'm initializing it as follows, and this works:
// Use the 1kHz low power oscillator
RTCMOD = 0x00;
RTCSC = 0x1F;
I've read that the low power oscillator has up to a 30% error rate depending on voltage and temperature, and I need better accuracy than that, so I'd like to use the higher powered internal clock, or an external 32.768kHz crystal. However, neither of the following produce any results (no interrupts are generated):
// Use the higher accuracy 32kHz internal clock, based on table 13-6
RTCMOD = 0b01111101; // 125 (125*8ms=1s)
RTCSC = 0b01010101; // CS=IRCLK, RTIE, PS=0101 (8ms)
or
// Use the external clock (32.768kHz crystal), based on table 13-3
RTCMOD = 0b00100000; // 32 (32*31.25ms=1s)
RTCSC = 0b00110111; // CS=ERCLK, RTIE, PS=0111 (/1024=31.25ms)
The tables I'm referring to come from the data sheet at http://www.nxp.com/assets/documents/data/en/data-sheets/MC9S08SH8.pdf
If my values for RTCMOD and RTCSC are correct, then is there any extra initialization I need to do in software to use the internal/external clock? Thanks in advance.
Here's the entire code. We really want to get the external oscillator going.
#include <hidef.h> /* for EnableInterrupts macro */
#include "derivative.h" /* include peripheral declarations */
// Clock source constants
#define LPO 0
#define IRCLK 1
#define ERCLK 2
#define CLK_SRC LPO
#define LED_DIR PTADD_PTADD2
#define LED_OUT PTAD_PTAD2
// Interrupt service routine for real time counter
interrupt VectorNumber_Vrtc void RTC_ISR(void)
{
// Clear interrupt flag
RTCSC |= 0x80;
LED_OUT = !LED_OUT; // toggle the LED every second
}
void main(void) {
// Board has an LED connected to PDB7; make it output
LED_DIR = 1;
#if CLK_SRC == IRCLK
// Use the higher accuracy 32kHz internal clock
RTCMOD = 0b01111101; // 125 (125*8ms=1s)
RTCSC = 0b01010101; // CS=IRCLK, RTIE, PS=0101 (8ms)
#elif CLK_SRC == ERCLK
// Use the external clock (32.768kHz xtal)
RTCMOD = 0b00100000; // 32 (32*31.25ms=1s)
RTCSC = 0b00110111; // CS=ERCLK, RTIE, PS=0111 (/1024=31.25ms)
#else
// Use the 1kHz low power oscillator (30% margin for error)
RTCMOD = 0x00; // 0b00000000
RTCSC = 0x1F; // 0b00011111
#endif
EnableInterrupts;
/* include your code here */
for( ; ; {
__RESET_WATCHDOG(); /* feeds the dog */
} /* loop forever */
/* please make sure that you never leave main */
}
Thanks!