Hi Robert,
Firstly, you don't use the WDT to make the pulses....the CSM makes the pulses (clock)....and they feed into timer1. You use the WDT simply as the timeframe. The flow of things goes something like this...
CSM Module pulses out-> Timer1 (which obviously counts those pulses)
WDT wakes PIC up (sample period set by the WDTCON register)
Store the present timer1 count
compare the present timer count vs the last stored timer1 count
if it has deviated by 'x' percent, then point your code back into the main loop (out of the 'sleep/WDT loop')
if it hasn't deviated by 'x' percent, then
clear timer1 down
put the pic back to sleep
WDT wakes PIC up
Stores the present timer1 count
compares the present count vs the last count
rinse repeat.
(btw, yes I know Microchip recommend using averages, but it's not necessary...the deviation is massive - like 90% when my sensor is touched - it's easy to trap. Also 'averaging' when your sample period is in the order of seconds is not ideal.)
I've actually got it all working in sleep mode ...the CSM does run in sleeps & Timer1 counts ...all you have to do is wake the pic up with the WDT & check the present count vs the last count.
The gotcha, is that when you're sleeping your PIC - which is obviously to save battery power - you only want the PIC to wake up couple of seconds....but if you don't change the timer1 prescaler & adjust the CSM oscillator output frequency down before sending the PIC off to sleep, then timer1 overflows (but interrupts are disabled before going to sleep- since we don't want the timer to actually overflow here) So, before going to sleep I use the CPSCON1 register to drop the CSM oscillator frequency as well as the T1CON register to changed the pre/post scaler so that during your chosen WDT sample period, it's well under the number that TMR1 overflows.
Got to do a bit more tweaking but like I say it works...& the current draw while the PIC is asleep is sub 100uA! (in fact below what my meter can measure). This is great beause what it means in reality, is that you don't have to mess about implementing a power switch....ie with a couple of AA batteries you'll get a few years of sleep.
Bookmarks