Vault 7: Projects

This publication series is about specific projects related to the Vault 7 main publication.

© 2010 Microchip Technology Inc. DS39724B-page 11-23
Section 11. CTMU
CTMU
11
Example 11-8: Routine for Capacitive Touch Switch for Devices with CTMUCON1 and
CTMUCON2 Registers
#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();
CTMUCON1bits.CTMUEN = 1; //Enable the CTMU
AD1CON1bits.SAMP = 1; //Manual sampling start
CTMUCON1bits.IDISSEN = 1; //drain charge on the circuit
DELAY; //wait 125us
CTMUCON1bits.IDISSEN = 0; //end drain of circuit
CTMUCON2bits.EDG1STAT = 1; //Begin charging the circuit
//using CTMU current source
DELAY; //wait for 125us
CTMUCON2bits.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;
}
}

e-Highlighter

Click to send permalink to address bar, or right-click to copy permalink.

Un-highlight all Un-highlight selectionu Highlight selectionh