View Full Version : Cap Sense ...getting a drowsy PIC to wake out of sleep!!
HankMcSpank
- 8th May 2011, 00:18
Oh dear, I've backed myself into a corner (at least a corner of my own knowledge!).
I'm running here a 16f1824...& have got the cap sense running fine...paid special attention to using the timer that still runs during sleep (timer1), also the cap sense module runs while asleep - soin my naive liuttle world, all is scoop'ndaddy, but then I hit this in the datasheet....
9.1 Wake-up from Sleep
The device can wake-up from Sleep through one of the
following events:
1. External Reset input on MCLR pin, if enabled
2. BOR Reset, if enabled
3. POR Reset
4. Watchdog Timer, if enabled
5. Any external interrupt
6. Interrupts by peripherals capable of running during
Sleep (see individual peripheral for more
information)
???
(that's my bold too - is a Timer interrupt deemed an 'external interrupt'?)
How on earth can I get the PIC to trap a finger being placed on a sensor while it's asleep?
Just for the record, here's what my program does...
timer 4 gets cleared
timer 4 starts running
the cap sense module output feeds into timer1 (timer1 is my cap sense oscillator 'count')
timer4 overflows (this is my timebase)
in a DT interrupt routine I compare the timer1 'count' vs the last timer1 'count' - if it has deviated down by more than 15% then a sensor has been touched - switch pressed!
timer 1 gets cleared
timer 4 gets cleared
rinse repeat.
So in the overall flow, a timer interrupt (for the timebase) is pretty key to cap sense working)
Like I say, it all works like a charm when awake, but I deffo need this circuit to go to sleep .....and then I really need the cap touch sensor to awake the PIC from its slumber.
So out of that overall 'flow' how can I keep tracking the timer 1 count while asleep? I'm figuring I can't and will have to come out of sleep say every few seconds to check the timer1 counts? Can theTimer4 overflow even wake the PIC up (even if it can. it's not eally the solution as obviously, timer4 is interrupting heavily...and the PIC would never get the chance to shut it's eyes)
ideas sought from those that are used to such conundrums?
HankMcSpank
- 8th May 2011, 01:15
See & thou shall find (though I had sought but hadn't found earlier!), & found this tucked away in an app note...
Use the Watchdog Timer (WDT) as the time base while in Sleep mode and the CSM is
able to continue oscillating during Sleep mode.
Timer1 will continue to count until Watchdog Timer overflows and wakes the MCU.
Once the device wakes from Sleep and detects a change in capacitance, due to a finger
or hand in close proximity to the sensor, then the program can be set to another time
base to detect the actual button pressed and perform the desired function.
guess I'll just have to see how long I can extend the WDT as timebase to without timer1 overflowing
rsocor01
- 9th May 2011, 00:34
in a DT interrupt routine I compare the timer1 'count' vs the last timer1 'count'
The CSM app notes recommend to compare the last count vs an average instead of comparing to the previous count.
I don't see how you can use CSM in sleep mode. If you use the WDT to make the pulses anyways, you need an interrupt to see how many pulses you have in a fixed amount of time. So, you really need two timers. Also, you need to make some calculation to know if the button was touched.
Robert
Demon
- 9th May 2011, 04:15
...
5. Any external interrupt
...
(that's my bold too - is a Timer interrupt deemed an 'external interrupt'?)
...
I'm just reading up on this now too, using the PIC 16F877 as example, check pin RB0/INT.
3.2 PORTB and the TRISB Register
...
RB0/INT is an external interrupt input pin and is configured using the INTEDG bit (OPTION_REG<6>).
...
p. 33/218
I wished I had more, like a code example, but it's all I found so far (quickie search).
(just one more feature on my list of things to play with)
Demon
- 9th May 2011, 15:41
A very well-written tutorial:
http://www.hobbyprojects.com/pic_tutorials/tutorial11.html
...The PIC has 4 sources of interrupt. They can be split into two groups. Two are sources of interrupts that can be applied externally to the PIC, while the other two are internal processes. We are going to explain the two external ones here.
...
And a step-by-step tutorial with code examples from Nuts & Volts:
http://www.precision.net.in/picbasic/PIC_INT.PDF
HankMcSpank
- 10th May 2011, 00:18
The CSM app notes recommend to compare the last count vs an average instead of comparing to the previous count.
I don't see how you can use CSM in sleep mode. If you use the WDT to make the pulses anyways, you need an interrupt to see how many pulses you have in a fixed amount of time. So, you really need two timers. Also, you need to make some calculation to know if the button was touched.
Robert
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.
rsocor01
- 10th May 2011, 05:19
Ooh, now I see what you are doing. I got this CSM working with a 16F727 and other chips but not in sleep mode. It is good to know that it works in sleep mode. Don't you have inaccurate value readings with the WDT as the timeframe interrupt? I read somewhere that the WDT is not too accurate.
Also, you said that you are using WDT intervals of two seconds. I didn't know you could go that high. I guess that a quick touch is not going to be detected by your device. I have used intervals in the order of a few milliseconds.
HankMcSpank
- 10th May 2011, 09:15
Ooh, now I see what you are doing. I got this CSM working with a 16F727 and other chips but not in sleep mode. It is good to know that it works in sleep mode. Don't you have inaccurate value readings with the WDT as the timeframe interrupt? I read somewhere that the WDT is not too accurate.
The readings only waiver by about 1%....a finger press see the deviation something like 80% ...plenty of headroom there!.
Also, you said that you are using WDT intervals of two seconds. I didn't know you could go that high. I guess that a quick touch is not going to be detected by your device. I have used intervals in the order of a few milliseconds.
According to the datasheet, the 16f1824 can have a WDT period of 256 seconds....but that'd be impossible to use with capacitive touch & sleep (timer1 would always overflow while asleep, as it's not possible to set the CSM output frequency low enough to avoid a timer1 overflow for 256 seconds!).
You are correct about quick tap not working when the WDT period is 2 seconds ...so, I'm using two sleep periods.
My first sleep period is only about 64ms ...that's very responsive to quick taps. Then after no finger taps detected for over 60 minutes, I actually then extend the sleep period out to about 2 seconds.......& yes, that requires you to hold you finger on the sensor for about two seconds... however once it comes out of sleep in this mode, I put the the WDT back to about 64ms.
the reasoning behind this, is that when a user first turns the circuit on, he'll have to hold his finger on the sensor for 2 seconds...but from thereon, it'll be responsive/snappy .....then if he stops using the circuit, it'll go into longer sleeps to conserve battery power.
bogdan
- 12th May 2011, 02:32
If you use the Timer1 to count the CSM pulses, how do you setup the "Timer1 Gate Source Selection"=T1GSS bits ?
Best Regards,
Bogdan
rsocor01
- 12th May 2011, 12:13
If you use the Timer1 to count the CSM pulses, how do you setup the "Timer1 Gate Source Selection"=T1GSS bits ?
Best Regards,
Bogdan
Good question.
I use T1GSS=01 because I use the "Timer0 Overflow Output". I have seen some people using T1GSS=10 which is for "TMR2 Match PR2 Output". HankMcSpank is probably using the last option T1GSS=11 which is "Watchdog Timer Scaler Overflow".
Robert
HankMcSpank
- 12th May 2011, 21:10
If you use the Timer1 to count the CSM pulses, how do you setup the "Timer1 Gate Source Selection"=T1GSS bits ?
Best Regards,
Bogdan
i don't touch those bits (so whatever they default to). to get Timer1 to count the CSM output, I simply set this...
T1CON = %11000101 'SOURCE TIMER1 CLOCK FROM CAPSENSE MODULE
bogdan
- 13th May 2011, 01:18
Do you guys use the:
a) Timer1 Gate Interrupt (triggered by the overflow of the timebase timer )
or
b) Timebase timer Interrupt (overflow)
...and when one of those interrupts is serviced checking the counts on the timer1
(microchip AN1171 http://www.google.com/url?sa=t&source=web&cd=1&ved=0CBkQFjAA&url=http%3A%2F%2Fww1.microchip.com%2Fdownloads%2Fe n%2FAppNotes%2F01171C.pdf&ei=FG7MTefQAcbd0QGUopDaBA&usg=AFQjCNH3b_c-HG_LTaOsTXqW29KQ80LTeA&sig2=wk5hQLY-eJmWE1g8kxdKZg say to use a).... why????)
Thank You
HankMcSpank
- 13th May 2011, 11:28
Do you guys use the:
a) Timer1 Gate Interrupt (triggered by the overflow of the timebase timer )
or
b) Timebase timer Interrupt (overflow)
...and when one of those interrupts is serviced checking the counts on the timer1
b) - I use timer2 interrupt (via DT's interrupts) as the timebase... & in the interrupt routine, I check the actual timer1 counts (ie as received from the cap sense module).
When I put the PIC to sleep, because timer2 can't be used while it's sleeping, I use the WDT as the necessary timebase.
bent10
- 30th July 2012, 21:11
Im also using the Pic16f1824 and would liek to capsense working for a SLEEP/ WAKEUP button. I understand the flow of how yo uare using the WDT to do this however I am having trouble getting the capsense to work in general. Would you be able to give light on config bits for using timer1 and capsense channel 2?
Thanks for all the help.
Powered by vBulletin® Version 4.1.7 Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.