[Solved] Timers not working in sleep mode
Hello all,
I have a PIC24FJ128GA306 on which I have a magnetic reed switch connected to an input assigned to T2CLK. The timer is essentially operating as a counter. However, it refuse to count when sleeping. If I replace the sleep instruction with a delay sequence then it will count fine. I am using this pic in a low power situation and would like to be able to go into sleep mode (having delays is not really a viable option). Sleep uses the watchdog timer to wake up.
The data sheet clearly states all timers will run in sleep mode either from LPRC or external clocking source (ie reed switch). Clearly I'm doing something wrong but I just can't figure it out. Extensive searching on the net reveals nothing. The pin shares functionality with RTCC out and an LCD controller pin but I have disabled both features (RTCC is running but output is disabled)
Here's the relevant code:
Initialisation:
void init_pulse(){
RPINR3bits.T2CKR = 2; //RD8
T2CONbits.T32 = 1; //32 bit mode
T2CONbits.TCS = 1; //External source
PR2 = 0xff; PR3 = 0xff;
T2CONbits.TON = 1; //on
TMR2 = 0;
TMR3 = 0;
}
Reading value:
pulse_count = TMR3HLD;
pulse_count = pulse_count << 16;
pulse_count += TMR2;
TMR2 = 0;
TMR3HLD = 0;
volume = pulse_count * pulse_scalingf;
Sleep and/or delay:
for(i=0;i<sleep_cycles;i++)
{
asm volatile ( "CLRWDT\n"
"BSET RCON, #5\n" //wdt en
"NOP\n"
"NOP\n"
"NOP\n"
"PWRSAV #0");
}
// for(i=0;i<35;i++)
// __delay_ms(250);
Any help/hints would be greatly appreciated
post edited by stu1232 - 2012/10/28 18:10:03