I've written a little routine to kick my clock down to a low power state for when it's on battery backup. I'm trying to get it to switch from the 20MHz main crystal to the 32.768kHz on Timer1 on loss of main power, and then switch back on power restore. But when power is restored the clock isn't running anymore. I've read the datasheet oscillator chapter four times and I don't see any "gotchas" I've missed. Here's the routine. RA5 senses main power. Oh ya, it's a 18F248:
Code:
void lo_power(void){
ccpr1l=0; //turn off LCD backlight
adcon0.ADON=0; //A/D off
osccon=1; //switch to timer1 osc (32.768kHz) - low power mode
while(!porta.5) //wait while no power
delay_ms(100);
osccon=0; //switch back to 20MHz crystal
adcon0.ADON=1; //turn A/D back on
ccpr1l=bl; //restore backlight
}
Really? I just don't read it that way in the datasheet. It says the whole thing is automatic and when switched over, program execution resumes (not in those words obviously ).
If I have to reset I'll have to save some variables in EEPROM first and restore them after. Probably more work than it's worth.
Oh, fer pete's sake! I just noticed it starting up on its own out of the corner of my eye. The problem is that the 32.768kHz osc is just SO slow that it appears to be dead while it's stuck in the delay loop. The interrupt is still blipping away though, because when it finally wakes back up the time is still correct.
So I (and the datasheet) was right. No reset is required. All timing things are handled automatically when switching between oscillators.
I need to pinch that delay WAY down. There's no reason for it to be so long anyway.
Oh, fer pete's sake! I just noticed it starting up on its own out of the corner of my eye. The problem is that the 32.768kHz osc is just SO slow that it appears to be dead while it's stuck in the delay loop. The interrupt is still blipping away though, because when it finally wakes back up the time is still correct.
Got that right. The delay is unnecessary. It could just spin in that while loop without one. I left it in anyway, but changed it to 5ms. So when power is restored, even at 32.768 it pops out of the loop in maybe a second at most.
The battery backup works great. I'm still going to work on getting it to consume even less power though. Do some Sleeping and things like that...