English
Language : 

PIC18F23K22 Datasheet, PDF (326/492 Pages) Microchip Technology – 28/40/44-Pin, Low-Power, High-Performance Microcontrollers with nanoWatt XLP Technology
PIC18(L)F2X/4XK22
EXAMPLE 19-4: ROUTINE FOR CAPACITIVE TOUCH SWITCH
#include "p18cxxx.h"
#define COUNT 500
#define DELAY for(i=0;i<COUNT;i++)
#define OPENSW 1000
#define TRIP 300
#define HYST 65
#define PRESSED 1
#define UNPRESSED 0
//@ 8MHz = 125uS.
//Un-pressed switch value
//Difference between pressed
//and un-pressed switch
//amount to change
//from pressed to un-pressed
int main(void)
{
unsigned int Vread;
unsigned int switchState;
int i;
//storage for reading
//assume CTMU and A/D have been setup correctly
//see Example 25-1 for CTMU & A/D setup
setup();
CTMUCONHbits.CTMUEN = 1;
CTMUCONLbits.EDG1STAT = 0;
CTMUCONLbits.EDG2STAT = 0;
CTMUCONHbits.IDISSEN = 1;
DELAY;
CTMUCONHbits.IDISSEN = 0;
// Enable the CTMU
// Set Edge status bits to zero
//drain charge on the circuit
//wait 125us
//end drain of circuit
CTMUCONLbits.EDG1STAT = 1;
DELAY;
CTMUCONLbits.EDG1STAT = 0;
//Begin charging the circuit
//using CTMU current source
//wait for 125us
//Stop charging circuit
PIR1bits.ADIF = 0;
ADCON0bits.GO=1;
while(!PIR1bits.ADIF);
//make sure A/D Int not set
//and begin A/D conv.
//Wait for A/D convert complete
Vread = ADRES;
//Get the value from the A/D
if(Vread < OPENSW - TRIP)
{
switchState = PRESSED;
}
else if(Vread > OPENSW - TRIP + HYST)
{
switchState = UNPRESSED;
}
}
DS41412B-page 326
Preliminary
 2010 Microchip Technology Inc.