Microchip Technology MA160014 数据表

下载
页码 560
PIC18(L)F2X/4XK22
DS41412F-page 330
 2010-2012 Microchip Technology Inc.
EXAMPLE 19-4:
ROUTINE FOR CAPACITIVE TOUCH SWITCH
#include "p18cxxx.h"
#define COUNT 500                       //@ 8MHz = 125uS.
#define DELAY for(i=0;i<COUNT;i++)
#define OPENSW 1000                     //Un-pressed switch value
#define TRIP 300                        //Difference between pressed
                                        //and un-pressed switch
#define HYST 65                         //amount to change
                                        //from pressed to un-pressed
#define PRESSED 1
#define UNPRESSED 0
int main(void)
{
    unsigned int Vread;                 //storage for reading
    unsigned int switchState;
    int i;
    
    //assume CTMU and A/D have been set up correctly
    //see Example 25-1 for CTMU & A/D setup
    setup();
    
    CTMUCONHbits.CTMUEN = 1;            // Enable the CTMU
    CTMUCONLbits.EDG1STAT = 0;          // Set Edge status bits to zero
    CTMUCONLbits.EDG2STAT = 0;    
    CTMUCONHbits.IDISSEN = 1;           //drain charge on the circuit  
    DELAY;                              //wait 125us
    CTMUCONHbits.IDISSEN = 0;           //end drain of circuit
    
    CTMUCONLbits.EDG1STAT = 1;          //Begin charging the circuit
                                        //using CTMU current source
    DELAY;                              //wait for 125us
    CTMUCONLbits.EDG1STAT = 0;          //Stop charging circuit
    
    PIR1bits.ADIF = 0;                  //make sure A/D Int not set
    ADCON0bits.GO=1;                    //and begin A/D conv.
    while(!PIR1bits.ADIF);              //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;
    }
}