AN1250
Microchip CTMU for Capacitive Touch Applications
Author:
Bruce Bohn
Microchip Technology Inc.
GENERAL FEATURES OF THE CTMU
The CTMU is made up of a constant current source and
several logic blocks for it to operate:
• The constant current source is connected to the
A/D converter of the microcontroller.
• A switch connected to the CTMU constant current
source (and the A/D converter) is provided to
allow accumulated charge to be drained.
• Two external pins are provided to trigger the
constant current source.
• An output pin is also available for CTMU use.
For the purposes of capacitive touch, the external
pins for triggering the CTMU and the CTMU output
pin are not used.
The connection of the CTMU to the A/D converter is all
that is needed for capacitive touch applications. For a
more in-depth discussion on the CTMU module and
how it is used to measure charge or time, refer to the
CTMU chapter of the
“PIC24F Family Reference Man-
ual”
for PIC24F microcontrollers, or the appropriate
data sheet for PIC18F microcontrollers on the
Microchip web site (http://www.microchip.com).
See Figure 1 for a block diagram of the CTMU hardware.
Note that there are a few slight differences in the CTMU
module from the PIC18F and the PIC24F. The PIC18F
CTMU module does not have an automatic ADC
conversion trigger. This is of no consequence for
capacitive touch applications. For clarity, the automatic
ADC trigger present on the PIC24F has not been used
in the sample code so that it more closely matches the
PIC18F code.
Also note that the CTMU on the PIC18F has different
internal trigger sources. (ECCP1 and ECCP2 for
PIC18F and Timer1 and OC1 for PIC24F). This is
mentioned purely for reference, and again, makes no
difference for capacitive touch applications. The sample
code listings in this application note manually set/clear
the EDG1STAT1 and/or EDG2STAT bits in the CTMU
control register for starting and stopping the CTMU
current source.
INTRODUCTION
This application note describes the use of Microchip’s
Charge Time Measurement Unit (CTMU) for capacitive
touch applications. The CTMU is an excellent periph-
eral for use in touch sensing applications. The following
are some of the benefits of using the CTMU for touch
sensing applications:
• Easy hardware setup
• Simple to use software algorithms, available free
of charge
• High speed allows for greater scan rate of
capacitive touch switches and for many other
processor tasks
• Low cost and low component count; no external
hardware other than a connection to the copper
sensor pad
The purpose of this application note is to inform
prospective users of the CTMU for capacitive touch on
general usage guidelines, CTMU setup and software
algorithms, hardware and layout considerations, and
advanced capacitive touch applications, such as matrix
keys, sliders and multiple press keys.
The CTMU is currently available on selected PIC24
and PIC18F parts. Wherever possible, sample code for
both has been included.
All hardware examples used in this application note
directly pertain to the PICDEM™ Touch Sense 2
Demonstration Board (Part Number DM164128). The
PICDEM Touch Sense 2 Demo Board uses a PIC24F
device-based microcontroller (PIC24FJ256GB110).
This demo board is not necessary for understanding this
application note.
©
2009 Microchip Technology Inc.
DS01250A-page 1
AN1250
FIGURE 1:
CTMU BLOCK DIAGRAM
CTMUCON
(CTMUCONH and CTMUCONL for PIC18F)
CTMUICON
Current Source
External Edge
Trigger Pins
Edge
Control
Logic
Current
Control
Timer1
(PIC24F only)
OC1
(PIC24F only)
ECCP1/ECCP2
(PIC18F only)
Pulse
Generation
Logic
A/D Converter
Comparator 2 Input
Comparator 2
Output
CTMU
Control
Logic
A/D Conversion
Trigger
(PIC24F only)
Pulse Output
Pin
DS01250A-page 2
©
2009 Microchip Technology Inc.
AN1250
OVERVIEW OF CAPACITIVE TOUCH
APPLICATIONS
The CTMU is used in capacitive touch applications by
applying the constant current source of the CTMU to
the capacitive touch pad using the following equation:
IxT=CxV
Where:
•
I
is the constant current source of the CTMU
•
T
is a fixed period that the CTMU charges the
capacitive touch circuit
•
C
is the capacitance of the touch circuit
•
V
is the voltage read by the A/D converter after
the capacitive touch circuit is finished charging
It is possible to sense a relative shift in capacitance by
observing a change in voltage. The above equation can
be rearranged to:
V = (I x T)/C
Since the CTMU current source is constant (I), the
voltage present on the capacitive touch sensor (V)
relies on two variables: the amount of time the touch
circuit is charged (T) and the capacitive size of the
touch circuit (C). If the amount of time the touch circuit
is charged is held constant, then changes in the capac-
itance of the touch circuit will ultimately affect the
voltage that the circuit charges to in the fixed period.
The A/D converter is used to read the voltage that the
touch circuit is charged to with the CTMU.
When the capacitance of a human finger is added to
the touch sensor pad, the capacitance increases and
the result is a lowering of the voltage seen by the A/D
converter (since
I
and
T
are held constant).
Setting Up of the CTMU
For a capacitive touch application, each sensor must
be connected directly to a channel of the A/D converter.
See Figure 2 for a block diagram of a Microchip
microcontroller with a CTMU peripheral. This diagram
illustrates that the CTMU is internally connected to the
A/D converter and allows for selection of any of the A/D
channels. With this configuration, a single CTMU unit
can measure many capacitive touch sensors. Note that
the CTMU has been simplified in order to show only the
elements required for capacitive touch, namely the cur-
rent source, the current source control and the current
drain mechanism. These will be explained in detail in
later sections. The current source control, labeled
“Trigger” in Figure 2, is manually controlled in software
by manipulating bits in the CTMU Control register.
FIGURE 2:
CAPACITIVE TOUCH BLOCK DIAGRAM
Current Source
Sensor 0
Trigger
CTMU
Discharge
C A/D
A/D Converter
A/D MUX
©
2009 Microchip Technology Inc.
DS01250A-page 3
AN1250
The current source of the CTMU is available in three
ranges: 0.55
μA,
5.5
μA
and 55
μA.
The current range
selection is made in the CTMUICON register. The cur-
rent precision for each of the three ranges is ±20%. For
many capacitive touch applications, the highest current
range setting (55
μA)
works best. This allows for the
quickest charging of the capacitive touch circuit. The
CTMUICON register also has bits used to trim the
current source in ±2% increments up to ±62% for each
of the three current ranges.
The CTMU current source is enabled and disabled
using software. Two control bits, EDG1STAT and
EDG2STAT in the CTMU control register, determine if
the current source is enabled. These bits are exclu-
sively ORed. That is, if EDG1STAT and EDG2STAT are
both set or cleared, the current source is off. If either bit
is set while the other is cleared, the current source is
enabled and charging the circuit.
The IDISSEN bit is enabled to drain charge from the
A/D converter to insure the charging process begins at
zero potential. If the bit is set, the circuit is connected to
V
SS
(grounded). Note that the discharge of the entire
circuit is not accomplished with this feature. This is
because the A/D converter is not always connected to
the external circuitry (i.e., touch sensor pad).
The CTMU Configuration register (CTMUCON) is set
up so that the external triggers are not used (these pins
may be used for general purpose I/Os). The same is
true of the CTMU pulse output pin. The CTMU current
source is configured using the CTMUICON register.
For detailed information on the CTMU registers on the
PIC24F, refer to
Section 11. “Charge Time Measure-
ment Unit (CTMU)”
of the
“PIC24F Family Reference
Manual”.
The registers that control the CTMU on the PIC18F parts
are identical, with the exception that they are 8-bit
registers: CTMUCONH and CTMUCONL. The CTMU
also has different external trigger sources available,
namely the ECCP1 and ECCP2 Special Event Triggers.
For detailed information on the CTMU registers for
PIC18F, refer to the specific product data sheet.
See Example 1 for a typical setup of the required
CTMU and A/D converter registers. Note that the
CTMU has been configured so that the external pins
are not enabled. For the purposes of this application
note, all control of the CTMU is handled through
software. The A/D converter is set up to do manual
conversion.
For capacitive touch sensing, a relative change in
capacitance due to the presence of a finger is required.
The absolute measurement of capacitance is not
required.
Note:
All PIC18F code examples are written for
the PIC18F46J11 family. For other PIC18F
parts with the CTMU module, see the
device data sheet for specific CTMU
register configurations and CTMU usage.
EXAMPLE 1:
CTMU AND A/D CONVERTER SETUP FOR PIC24F
//setup CTMU
//CTMUCON
CTMUCONbits.CTMUEN =
CTMUCONbits.CTMUSIDL =
CTMUCONbits.TGEN =
CTMUCONbits.EDGEN =
CTMUCONbits.EDGSEQEN =
CTMUCONbits.IDISSEN =
CTMUCONbits.CTTRIG =
CTMUCONbits.EDG2POL =
CTMUCONbits.EDG2SEL =
CTMUCONbits.EDG1POL =
CTMUCONbits.EDG1SEL =
//CTMUICON
CTMUICON = 0x300;
CTMUICON.ITRIM = 0;
//setup A/D converter
0;
0;
0;
0;
0;
0;
0;
0;
0x3;
1;
0x3;
//make sure CTMU is disabled
//CTMU continues to run in idle mode
//disable edge delay generation mode of the CTMU
//edges are blocked
//edge sequence not needed
//Do not ground the current source
//Trigger Output is disabled
//Edge2 Src = OC1 (don’t care)
//Edge1 Src = Timer1 (don’t care)
//55uA
//Nominal - No Adjustment
AD1PCFGL = 0x0000;
AD1CON1 = 0x0000;
AD1CHS =
0x0000;
AD1CSSL=0x0000;
AD1CON1bits.FORM = 0x0;
AD1CON3 = 0x0000;
AD1CON2 = 0x0000;
AD1CON1bits.ADON = 1;
CTMUCONbits.CTMUEN = 1;
//select the analog channel 0
//Unsigned fractional format
//bits.ADRC=0;
//Turn On A/D
//Enable CTMU
DS01250A-page 4
©
2009 Microchip Technology Inc.
AN1250
EXAMPLE 2:
CTMU AND A/D CONVERTER SETUP FOR PIC18F
//setup CTMU
//CTMUCON
CTMUCONHbits.CTMUEN = 0;
CTMUCONHbits.CTMUSIDL = 0;
CTMUCONHbits.TGEN = 0;
CTMUCONHbits.EDGEN = 0;
CTMUCONHbits.EDGSEQEN = 0;
CTMUCONHbits.IDISSEN = 0;
CTMUCONHbits.CTTRIG = 0;
CTMUCONLbits.EDG2POL = 0;
CTMUCONLbits.EDG2SEL = 0x0;
CTMUCONLbits.EDG1POL = 1;
CTMUCONLbits.EDG1SEL = 0x1;
//CTMUICON
CTMUICON = 0x03;
CTMUICON.ITRIM = 0;
//setup A/D converter
ANCON0bits.PCFG = 0x00;
ANCON1 = 0x0000;
ADCON0bits.CHS = 0x00;
ADCON1bits.ADFM = 0x00;
ADCON1bits.ADON = 1;
CTMUCONHbits.CTMUEN = 1;
//make sure CTMU is disabled
//CTMU continues to run in idle mode
//disable edge delay generation mode of the CTMU
//edges are blocked
//edge sequence not needed
//Do not ground the current source
//Trigger Output is disabled
//Edge2 Src = ECCP2 (don’t care)
//Edge1 Src = ECCP1 pin (don’t care)
//55uA
//Nominal - No Adjustment
//select the analog channel 0
//right justified result
//Turn On A/D
//Enable CTMU
In the case of the PIDEM Touch Sense 2 Demo Board,
all 16 A/D channels are used for capacitive touch. Read-
ing of each of the sensor channels (A/D channels) is
controlled by Timer1. Timer1 is setup to fire at a 1 ms
interval. Each time that the Timer1 interrupt handler
runs, it increments the channel number of the A/D and
the capacitive sensor connected to that channel is read.
After all 16 touch sensors have been read, the Timer1
interrupt handler sets a flag called, “dataReadyCTMU”,
and the main routine uses this flag to know when to
process the new data just gathered from all 16 capacitive
touch sensors.
©
2009 Microchip Technology Inc.
DS01250A-page 5