Vault 7: Projects
This publication series is about specific projects related to the Vault 7 main publication.
PIC24F Family Reference Manual
DS39724B-page 11-22 © 2010 Microchip Technology Inc.
Example 11-7: Routine for Capacitive Touch Switch for Devices with the
CTMUCON Register
#include “p24Fxxxx.h”
#define COUNT 500 //@ 8MHz = 125uS.
#define DELAY for(i=0;i<COUNT;i++)
#define OPENSW 1000 //Unpressed switch value
#define TRIP 300 //Difference between pressed
//and unpressed switch
#define HYST 65 //amount to change
//from pressed to unpressed
#define PRESSED 1
#define UNPRESSED0
int main(void)
{
unsigned int Vread; //storage for reading
unsigned int switchState;
int i;
//assume CTMU and A/D have been setup correctly
//see Example 11-1 for CTMU & A/D setup
setup();
CTMUCONbits.CTMUEN = 1; //Enable the CTMU
AD1CON1bits.SAMP = 1; //Manual sampling start
CTMUCONbits.IDISSEN = 1; //drain charge on the circuit
DELAY; //wait 125us
CTMUCONbits.IDISSEN = 0; //end drain of circuit
CTMUCONbits.EDG1STAT = 1; //Begin charging the circuit
//using CTMU current source
DELAY; //wait for 125us
CTMUCONbits.EDG1STAT = 0; //Stop charging circuit
IFS0bits.AD1IF = 0; //make sure A/D Int not set
AD1CON1bits.SAMP = 0; //and begin A/D conv.
while(!IFS0bits.AD1IF); //Wait for A/D convert complete
AD1CON1bits.DONE = 0;
Vread = ADC1BUF0; //Get the value from the A/D
if(Vread < OPENSW - TRIP)
{
switchState = PRESSED;
}
else if(Vread > OPENSW - TRIP + HYST)
{
switchState = UNPRESSED;
}
}
Protego_Release_01_05-Related-OEM-Documentation-PIC24FJ32MC10X-Reference_Manual-Section11-Charge_Time_Measurement_Unit.pdf