#pragma interrupt high_handler // the TIMER is first in the sequence, then the CTMU and finally the ADC to read the data
void high_handler (void)
{
static unsigned char show_raw=DISPLAY_RAW, BASELINE=FALSE; // baseline calibration timer delay
static unsigned int i=0,delay_meas=0,delay_cal=0; // CTMU cap charge voltages
if ( INTCONbits.TMR0IF) { // check timer0, setup CTMU for charging
CTMUCONHbits.EDGEN=0; // edges are blocked
LEDR=1; // light link xmit is connected to LEDR (active low) //
CTMUCONHbits.CTMUEN = 1; // Enable the CTMU
CTMUCONHbits.IDISSEN = 0; // end drain of touch circuit
CTMUCONL = CTMU_REGL; // reset CTMU flags
CTMUCONHbits.EDGSEQEN=1; // edge 1 then 2 for interrupt
TIME_CHARGE=TRUE; // set charging flag
CTMU_WORKING=TRUE; // set working flag, doing
WriteTimer0 (TIMERCHARGE_BASE_X10); // set timer to charge rate time
CTMUCONHbits.EDGEN=1; // edges are not blocked
LEDR = 0; // Begin charging the touch circuit (xmit led on)
INTCONbits.TMR0IF = 0; // clear TIMER interrupt flag
}
if (PIR3bits.CTMUIF) { // the current source has been on and then turned off by the edge detect inputs
CTMUCONHbits.EDGEN=0; // edges are blocked
LEDR=1; // xmit is off
TIME_CHARGE=FALSE; // clear charging flag
CTMU_WORKING=TRUE; // set working flag, doing touch ADC conversion
// configure ADC for next reading
ADCON0bits.GO=1; // and begin A/D conv, will set adc int flag when done.
PIR3bits.CTMUIF=0; // clear CTMU interrupt flag
}
if (PIR1bits.ADIF) { // check ADC irq
if (!BASELINE) {
delay_cal=ADRES; // baseline voltage
if (++i > BASE_WAIT) {
BASELINE=TRUE;
LEDB=1; // calibration complete LED
}
} else { // get current measurement
if (ADRES>delay_cal) { // only show longer delays
delay_meas=ADRES-delay_cal; // find the diff from the 1M cable 5ns baseline measurement
}
C.raw=ADRES; // load the C structure with delay data
C.base=delay_cal;
C.result=delay_meas;
CTMU_ADC_UPDATED=TRUE; // New data set is in C structure, set to FALSE in main program flow
if (!show_raw) {
LATC=~delay_meas;
LATD=~delay_meas;
}
}
if (show_raw) {
LATC=~ADRES;
LATD=~ADRES;
};
CTMU_WORKING=FALSE; // clear working flag, ok to read timer3 counter.
LEDR = 1; // Set xmit off
// config CTMU for next reading
CTMUCONHbits.IDISSEN = 1; // drain charge on the circuit
WriteTimer0 ( TIMERDISCHARGE ); // set timer to discharge rate
PIR1bits.ADIF = 0; // clear ADC int flag
}
}