You are on page 1of 8

TB3014

Low-Power Capacitive Sensing with the Capacitive Sensing Module


The combination between the Capacitive Sensing
Author: Aurelian Floricica Module and Timer1 leads to a normal decision to use
Microchip Technology Inc. the Toggle mode of the Timer1 gate. This means that,
on the first overflow of the WDT, the counting mecha-
nism of Timer1 will be enabled and, on the next WDT
INTRODUCTION overflow, the same mechanism will be disabled, but an
This technical brief describes a small application that interrupt will be generated by the Timer1 gate. This
demonstrates the low-power capacitive touch potential interrupt shows that a gate event has taken place.
of the Capacitive Sensing Module (CSM). The main Table 1 provides a short description of the capacitive
benefit of this new part is the introduction of a special buttons.
module for capacitive sensing detection. A more
detailed description about this module can be found in
AN1171, “Using the Capacitive Sensing Module on the
TABLE 1: DESCRIPTION OF THE
PIC16F72X”. CAPACITIVE BUTTONS
Button Name Description
HARDWARE DESCRIPTION WAKE-UP Scanned for detection of the user’s
hand in proximity of the board.
A simple design was developed to create a board using
the PIC16F726 (the first family to integrate the RIGHT Scanned for the detection of the
Capacitive Sensing Module), a simple LED driver IC user’s intention to shift one position
Allegro 6276 and 16 surface-mounted LEDs, arranged to the right of the row of LEDs.
in a 4x4 matrix. The LED driver was introduced in order LEFT Scanned for the detection of the
to separate the current path of the LEDs from that of the user’s intention to shift one position
microcontroller, hence a more accurate current to the left of the row of LEDs.
consumption measurement on the device’s side. The UP Scanned for the detection of the
inputs to the CSM are represented by 8 capacitive user’s intention to shift one position
pads. The VDD line of the microcontroller is shunted by up the row of LEDs.
a small 100 Ohms resistor for measuring the current
DOWN Scanned for the detection of the
consumption by the device. The board schematics can
be found in Appendix A: Low-Power Capacitive user’s intention to shift one position
down the row of LEDs
Touch – PIC16F726.
RESET Scanned for the detection of the
The purpose of the board is to allow the user to move
user’s intention to reset the row of lit
a row of 3 LEDs in the matrix using the arrow buttons,
LEDs to its default value.
reset their position and vary the frequency at which
these LEDs are lit using the slider keys positioned on SLIDER This is a mixed button formed of two
the right side of the board. independent buttons connected in
anti-phase. It is scanned to detect
The frequency measurement for each button consists the user’s intention to modify the fre-
of Timer1 acting as a counter and WDT acting as a time
quency at which the LEDs are lit,
base. The counter will increment itself on every rising from a relative low frequency to an
edge coming from the Capacitive Sensing Module. The ultra-low frequency. This limitation is
time base represents the acquisition window for these
caused by the fact that the time base
pulses. Furthermore, the time base, in this case the is around 70 ms (WDT overflowing
WDT overflow, will determine the start and stop of the twice with the prescaler bits cleared).
counter.
The microcontroller scans only one key, trying to detect
a shift in capacitance of the WAKE-UP button. This pro-
cedure acts as a proximity detector. When such a prox-
imity event takes place, the microcontroller will scan all
the buttons to detect the user’s will to play with the
LEDs in the matrix.

© 2008 Microchip Technology Inc. DS93014A-page 1


TB3014
SOFTWARE DESCRIPTION be taken into consideration, and so the WDT overflow
period cannot be extended over a limit that no longer
The PIC16F726 CSM allows the user to select between allows an acceptable scan rate of the buttons.
three time bases for the frequency measurement.
Consequently, until there is a proximity detection, the
Because the idea behind this small application was to do
microcontroller will execute the detection algorithm of a
the detection while in Sleep mode, WDT was selected to
single key, the WAKE-UP button, once every ~560 ms.
create the time window. The firmware flowchart is shown
If a proximity detection has been performed, the
in Appendix B: Firmware Flowchart.
microcontroller will clear the prescaler selection bits,
The code begins with the initialization of the microcon- and thus scanning every ~70 ms. This means that the
troller. The only peripherals needed for the low-power user’s hand is in close proximity to the board. The
capacity sensing are Timer1, WDT and the CSM. All firmware detects it and allows all the keys to be
other peripherals must be inactive, with the corre- scanned.
sponding interrupt enable bits disabled. There are
Once the user’s hand is detected close to the board,
some peripheral interrupt events that can wake the
the last state, or the default state of the LEDs will be
device from Sleep and disrupt the WDT time window,
flashed for a couple of seconds. This is an indication of
because the WDT is cleared upon such an event, no
the fact that the average values of the frequency read-
matter its source. The user should avoid setting any
ings of the buttons are revalued. Once this process is
peripheral interrupt enable bit other than TMR1 Gate
finished, the display will show the last state of the
Interrupt Enable bit (TMR1GIE), Peripheral Interrupt
LEDs, or the default state – if this is the first proximity
Enable bit (PEIE) and Global Interrupt Enable bit (GIE).
detection.
The only special microcontroller feature needed in this
kind of application is the precision internal oscillator After 4 seconds of inactivity, the MCU shifts back the
which can be accessed via the OSCCON register. A prescaler ratio and goes back to scan only one key.
convenient internal clock source can be obtained by Thus, the user has about four seconds at his disposal
enabling the PLL and selecting a frequency of 2 MHz to start playing with the keyboard, otherwise a time out
or by having the PLL disabled and selecting a fre- is considered.
quency of 500 kHz. The difference between the two fre- Pressing the WAKE-UP button again during this time
quencies is that the first one is higher and will allow the interval of four seconds has no effect. Only when this
device to execute the code faster when awake, but will interval expires and the microcontroller starts scanning
draw more current due to the enabled PLL; while the again just this button, at a much slower rate, can a
second one will trigger the device to execute the same proximity detection be performed again for the
code slower, but with a lower current consumption. The WAKE-UP button.
average current drawn by the device using either of the
two frequencies is the same. The buttons are considered press and release, mean-
ing that only the release of a button will generate an
After the device is initialized, it is put to Sleep until the action. The only exception is the WAKE-UP button,
first WDT overflow. On the first overflow there will be no which is scanned at a more sensitive threshold for
trigger of the Interrupt Service Routine (ISR), because detecting proximity.
there hasn’t been any Timer1 gate event. However, the
second WDT overflow will determine the
microcontroller to start executing the code from the CURRENT MEASUREMENT
ISR. A brief look at the schematic in Appendix A:
The algorithm used allows the microcontroller to scan Low-Power Capacitive Touch – PIC16F726 shows
for a single key only, thus detecting the proximity of the that the current consumption by the microcontroller can
user’s hand to the board. This reason, plus the fact be measured via the shunt R1 resistor on the VDD line.
that the device has to consume as little current as Because the voltage drop on the shunt resistor is a DC
possible, leads to the configuration of the WDT signal with a varying amplitude, the current resulted is
prescaler in a 1:8 rate. This means that, if the time not constant. Thus, we can have a couple of
window created by the WDT overflowing twice measurement techniques, which could be:
measures approximately 70 ms, using the prescaler
select bits will increase this window to approximately • the average DC voltage measurement of a
560 ms. TrueRMS multimeter
• plotting the voltage drop on a oscilloscope
The user can play with the prescaler rate select bits,
PS<2:0>, in the OPTION register, to increase the time
taken by the WDT timer to overflow, hence a longer
period spent by the device in Sleep mode. On the other
hand, the accuracy of capacitance shift detection must

DS93014A-page 2 © 2008 Microchip Technology Inc.


TB3014
Through the second choice, the user will detect the EQUATION 3:
periodicity of the voltage drop pulses, calculate the duty V DIFF _ AVG = V DIFF _ AMPLITUDE × V DIFF _ DC =
cycle of the wave, measure the amplitude and then
multiply the amplitude with the duty cycle. The result is = 118mV × 0.00421 = 0.049mV
the average voltage drop. Knowing the resistor’s value,
the current can be deducted. The voltage drop signal is Dividing this value by 100 Ohms, the resulted current is
an indication of the fact that the device wakes up from ~4.9 uA. This is the average current consumption by
Sleep and executes the code. the microcontroller while it does the proximity
detection.
Note that the voltage drop pulse width may vary in the
periodicity window. Also, because the microcontroller The current value consumption while doing the
wakes up to do the detection in two modes, once for the detection of the entire keyboard at a higher rate is
proximity detection and then for the whole keyboard obtained by replacing period T = 5 x 560 ms with T = 5
detection, there are two calculations, both pursued in x 70 ms. The resulted current in this example scenario
the same way, but with different data. is ~40 uA.
Here is an example: Note: This current consumption calculus is
TABLE 2: based on a WDT overflow of ~35 ms.
Users may base their calculations on the
Label Definition data sheet typical value of the WDT
VDIFF Voltage drop signal Time-out, but the most accurate result can
Ton VDIFF ON period be obtained only by measuring the actual
value of this time out.
T VDIFF period
Because the calculus resembles the
VDIFF_AVG Average value of VDIFF
approximation of a 1st order integral,
VDIFF_AMPLITUDE Amplitude of VDIFF meaning that one seeks for the surface
VDIFF_DC Duty cycle of VDIFF beneath the VDIFF signal, the current
drawn by the MCU while sleeping must be
Let’s presume that the VDIFF signal has a periodicity of
taken into consideration and added.
five pulses. Four of them have a width of 2 ms, while
the fifth has a width of 3.8 ms. Thus: A typical Sleep current measured on the
board designed for this application is
EQUATION 1: ~ 9uA. All the measurements are taken at
VDD = 5V.
Ton = 4*2ms + 3.8ms = 11.8ms

T, the period, equals 5 times the time frame at which the


microcontroller wakes up from Sleep to do the detec-
tion. In the case it does the proximity detection, this
time frame is about 560 ms (the double overflow period
of the WDT, which is ~70 ms multiplied by the prescaler
ratio, 8). If the device has detected the proximity and it
performs the scanning of the whole keyboard, the time
frame is only ~70 ms (WDT overflowing twice to cause
a TIMER1 gate event).
Thus, in the proximity case, T will be:

EQUATION 2:

T = 560ms × 5 = 2800ms

The DC of the VDIFF signal is Ton/T = 11.8 ms / 2800


ms = 0.00421. In a certain scenario (for instance, the
Capacitive Sensing Module oscillator in low range), the
VDIFF signal amplitude is, for example, 118 mV. Thus,
the average value of the VDIFF signal is:

© 2008 Microchip Technology Inc. DS93014A-page 3


TB3014
CONCLUSION
The Capacitive Sensing Module on the PIC16F72X
implements the hardware connection between the
WDT and Timer1 gate, and thus the possibility of using
the WDT wake from the Sleep event as a time resource
for counting pulses coming from the capacitive module
to the TMR1 gate. This introduces the power-down
mode (Sleep) to be used, which is essential for power
saving.
The most powerful method used to bring down the
current consumption of the device is to keep it as much
as possible in Sleep mode. For the lowest current
consumption in Sleep mode, all unused I/O pins should
be tied either at VDD or VSS.
In non-critical applications, where the concept of
enabling the user interface can be used, the proximity
detection technique gets the most out of the low-power
capacitive sensing concept by increasing the interval in
which the MCU stays in Sleep mode.
Below are some general ideas behind proximity
detection; the user should:
- create a state machine-based firmware that
can shift from scanning one button to scan-
ning the whole keyboard
- increase/decrease the pulse acquisition win-
dow by choosing the WDT prescaler value
according to the type of scan (proximity or
normal)
- keep the scan rate at a reasonable level to
maintain a fast capacitance shift detection
The firmware developed for the application presented
here to illustrate the low-power capacitance sensing
concept uses a total amount of 1,930 words of program
space and 130 bytes of data memory.
The core firmware that contains the code and data on
top of which users can build their application requires
approximately 512 words of Flash and 80 bytes of data
memory.

DS93014A-page 4 © 2008 Microchip Technology Inc.


APPENDIX A:

SLIDER
PICkit™ 2

4
2
3
5
6

1
S2

© 2008 Microchip Technology Inc.


R1

100

C3 C2

10 uF 100 nF
D1 VPP/MCLR/RE3 RB7/ICSPSDAT
GND VDD VCAP/AN0/RA0 RB6/ICSPCLK
D2 SDI REXT R2 AN1/RA1
CLK SDO RB5/AN13/CPS5/T1G
D3 GND AN2/RA2 RB4/AN11/CPS4
LE A6276 OE GND 1K VREF/AN3/RA3 RB3/AN9/CPS3/CCP2
OUT0 OUT15 T0CKI/CPS6/RA4
D4 RB2/AN8/CPS2
OUT1 OUT14 VCAP/CPS7/AN4/RA5 RB1/AN10/CPS1
D5 OUT2 OUT13 VSS RB0/AN12/CPS0/INT
D6 OUT3 OUT12 CLKIN/OSC1/RA7
OUT4 VDD UP
OUT11 VCAP/CLKOUT/OSC2/RA6 VSS
D7 OUT5 OUT10
C1

D8 T1CKI/T1OSO/RC0 RC7/TX/DT
OUT6 OUT9
100 nF

CCP2/T1OSI/RC1 RC6/TX/CX WAKE-UP


OUT7 OUT8 LEFT RIGHT
CCP1/RC2 RC5/SDO
D9 SCL/SCK/RC3 RC4/SDI/SDA
D10
PIC16F726
D11
D12
D13 DOWN
D14
RESET
D15
D16
LOW-POWER CAPACITIVE TOUCH – PIC16F726

DS93014A-page 5
TB3014
TB3014
APPENDIX B: FIRMWARE FLOWCHART

START
SLEEP() ISR()

Init MCU
Configure for SLEEP YES
NO TMR1 Gate
Proximity Detection
Event?
NOP

Main SLEEP
Loop
NO YES
Prox. Enabled?
ISR()

YES Prox. Enabled NO Decode only


and Detected? Decode all
Buttons except WAKE-UP
NOP Button
WAKE-UP
Configure for all NO LEDs YES
Buttons Scanning Enable?
Main
(except WAKE-UP) Loop
YES Slider NO
Active?
Rearrange
Matrix of LEDs
Enable LEDs Do Slider Routine
Disable Prox.
Detection
YES 4 sec NO
Time Out? Average Buttons
Set Next Channel
Restart TMR1
Config. for Prox.
Detection Only

Return

SLEEP()

DS93014A-page 6 © 2008 Microchip Technology Inc.


Note the following details of the code protection feature on Microchip devices:
• Microchip products meet the specification contained in their particular Microchip Data Sheet.

• Microchip believes that its family of products is one of the most secure families of its kind on the market today, when used in the
intended manner and under normal conditions.

• There are dishonest and possibly illegal methods used to breach the code protection feature. All of these methods, to our
knowledge, require using the Microchip products in a manner outside the operating specifications contained in Microchip’s Data
Sheets. Most likely, the person doing so is engaged in theft of intellectual property.

• Microchip is willing to work with the customer who is concerned about the integrity of their code.

• Neither Microchip nor any other semiconductor manufacturer can guarantee the security of their code. Code protection does not
mean that we are guaranteeing the product as “unbreakable.”

Code protection is constantly evolving. We at Microchip are committed to continuously improving the code protection features of our
products. Attempts to break Microchip’s code protection feature may be a violation of the Digital Millennium Copyright Act. If such acts
allow unauthorized access to your software or other copyrighted work, you may have a right to sue for relief under that Act.

Information contained in this publication regarding device Trademarks


applications and the like is provided only for your convenience
The Microchip name and logo, the Microchip logo, Accuron,
and may be superseded by updates. It is your responsibility to
dsPIC, KEELOQ, KEELOQ logo, MPLAB, PIC, PICmicro,
ensure that your application meets with your specifications.
PICSTART, rfPIC, SmartShunt and UNI/O are registered
MICROCHIP MAKES NO REPRESENTATIONS OR
trademarks of Microchip Technology Incorporated in the
WARRANTIES OF ANY KIND WHETHER EXPRESS OR
U.S.A. and other countries.
IMPLIED, WRITTEN OR ORAL, STATUTORY OR
OTHERWISE, RELATED TO THE INFORMATION, FilterLab, Linear Active Thermistor, MXDEV, MXLAB,
INCLUDING BUT NOT LIMITED TO ITS CONDITION, SEEVAL, SmartSensor and The Embedded Control Solutions
QUALITY, PERFORMANCE, MERCHANTABILITY OR Company are registered trademarks of Microchip Technology
FITNESS FOR PURPOSE. Microchip disclaims all liability Incorporated in the U.S.A.
arising from this information and its use. Use of Microchip Analog-for-the-Digital Age, Application Maestro, CodeGuard,
devices in life support and/or safety applications is entirely at dsPICDEM, dsPICDEM.net, dsPICworks, dsSPEAK, ECAN,
the buyer’s risk, and the buyer agrees to defend, indemnify and ECONOMONITOR, FanSense, In-Circuit Serial
hold harmless Microchip from any and all damages, claims, Programming, ICSP, ICEPIC, Mindi, MiWi, MPASM, MPLAB
suits, or expenses resulting from such use. No licenses are Certified logo, MPLIB, MPLINK, mTouch, PICkit, PICDEM,
conveyed, implicitly or otherwise, under any Microchip PICDEM.net, PICtail, PIC32 logo, PowerCal, PowerInfo,
intellectual property rights. PowerMate, PowerTool, REAL ICE, rfLAB, Select Mode, Total
Endurance, WiperLock and ZENA are trademarks of
Microchip Technology Incorporated in the U.S.A. and other
countries.
SQTP is a service mark of Microchip Technology Incorporated
in the U.S.A.
All other trademarks mentioned herein are property of their
respective companies.
© 2008, Microchip Technology Incorporated, Printed in the
U.S.A., All Rights Reserved.
Printed on recycled paper.

Microchip received ISO/TS-16949:2002 certification for its worldwide


headquarters, design and wafer fabrication facilities in Chandler and
Tempe, Arizona; Gresham, Oregon and design centers in California
and India. The Company’s quality system processes and procedures
are for its PIC® MCUs and dsPIC® DSCs, KEELOQ® code hopping
devices, Serial EEPROMs, microperipherals, nonvolatile memory and
analog products. In addition, Microchip’s quality system for the design
and manufacture of development systems is ISO 9001:2000 certified.

© 2008 Microchip Technology Inc. DS93014A-page 7


WORLDWIDE SALES AND SERVICE
AMERICAS ASIA/PACIFIC ASIA/PACIFIC EUROPE
Corporate Office Asia Pacific Office India - Bangalore Austria - Wels
2355 West Chandler Blvd. Suites 3707-14, 37th Floor Tel: 91-80-4182-8400 Tel: 43-7242-2244-39
Chandler, AZ 85224-6199 Tower 6, The Gateway Fax: 91-80-4182-8422 Fax: 43-7242-2244-393
Tel: 480-792-7200 Harbour City, Kowloon Denmark - Copenhagen
India - New Delhi
Fax: 480-792-7277 Hong Kong Tel: 45-4450-2828
Tel: 91-11-4160-8631
Technical Support: Tel: 852-2401-1200 Fax: 45-4485-2829
Fax: 91-11-4160-8632
http://support.microchip.com Fax: 852-2401-3431
India - Pune France - Paris
Web Address:
Australia - Sydney Tel: 91-20-2566-1512 Tel: 33-1-69-53-63-20
www.microchip.com
Tel: 61-2-9868-6733 Fax: 91-20-2566-1513 Fax: 33-1-69-30-90-79
Atlanta Fax: 61-2-9868-6755
Japan - Yokohama Germany - Munich
Duluth, GA
China - Beijing Tel: 49-89-627-144-0
Tel: 678-957-9614 Tel: 81-45-471- 6166
Tel: 86-10-8528-2100 Fax: 49-89-627-144-44
Fax: 678-957-1455 Fax: 81-45-471-6122
Fax: 86-10-8528-2104 Italy - Milan
Boston Korea - Daegu
China - Chengdu Tel: 39-0331-742611
Westborough, MA Tel: 82-53-744-4301
Tel: 86-28-8665-5511 Fax: 39-0331-466781
Tel: 774-760-0087 Fax: 82-53-744-4302
Fax: 86-28-8665-7889 Netherlands - Drunen
Fax: 774-760-0088 Korea - Seoul
China - Hong Kong SAR Tel: 82-2-554-7200 Tel: 31-416-690399
Chicago
Tel: 852-2401-1200 Fax: 82-2-558-5932 or Fax: 31-416-690340
Itasca, IL
Tel: 630-285-0071 Fax: 852-2401-3431 82-2-558-5934 Spain - Madrid
Fax: 630-285-0075 China - Nanjing Tel: 34-91-708-08-90
Malaysia - Kuala Lumpur
Tel: 86-25-8473-2460 Tel: 60-3-6201-9857 Fax: 34-91-708-08-91
Dallas
Addison, TX Fax: 86-25-8473-2470 Fax: 60-3-6201-9859 UK - Wokingham
Tel: 972-818-7423 China - Qingdao Tel: 44-118-921-5869
Malaysia - Penang
Fax: 972-818-2924 Tel: 86-532-8502-7355 Fax: 44-118-921-5820
Tel: 60-4-227-8870
Detroit Fax: 86-532-8502-7205 Fax: 60-4-227-4068
Farmington Hills, MI China - Shanghai Philippines - Manila
Tel: 248-538-2250 Tel: 86-21-5407-5533 Tel: 63-2-634-9065
Fax: 248-538-2260 Fax: 86-21-5407-5066 Fax: 63-2-634-9069
Kokomo China - Shenyang Singapore
Kokomo, IN Tel: 86-24-2334-2829 Tel: 65-6334-8870
Tel: 765-864-8360 Fax: 86-24-2334-2393 Fax: 65-6334-8850
Fax: 765-864-8387
China - Shenzhen Taiwan - Hsin Chu
Los Angeles Tel: 86-755-8203-2660 Tel: 886-3-572-9526
Mission Viejo, CA Fax: 86-755-8203-1760 Fax: 886-3-572-6459
Tel: 949-462-9523
China - Wuhan Taiwan - Kaohsiung
Fax: 949-462-9608
Tel: 86-27-5980-5300 Tel: 886-7-536-4818
Santa Clara Fax: 86-27-5980-5118 Fax: 886-7-536-4803
Santa Clara, CA
China - Xiamen Taiwan - Taipei
Tel: 408-961-6444
Tel: 86-592-2388138 Tel: 886-2-2500-6610
Fax: 408-961-6445
Fax: 86-592-2388130 Fax: 886-2-2508-0102
Toronto
China - Xian Thailand - Bangkok
Mississauga, Ontario,
Tel: 86-29-8833-7252 Tel: 66-2-694-1351
Canada
Fax: 86-29-8833-7256 Fax: 66-2-694-1350
Tel: 905-673-0699
Fax: 905-673-6509 China - Zhuhai
Tel: 86-756-3210040
Fax: 86-756-3210049

01/02/08

DS93014A-page 8 © 2008 Microchip Technology Inc.

You might also like