Microchip Technology MCU PIC PIC18F87K22-I/PTRSL TQFP-80 MCP PIC18F87K22-I/PTRSL Data Sheet

Product codes
PIC18F87K22-I/PTRSL
Page of 550
 2009-2011 Microchip Technology Inc.
DS39960D-page 395
PIC18F87K22 FAMILY
EXAMPLE 27-3:
CAPACITANCE CALIBRATION ROUTINE
#include "p18cxxx.h"
#define COUNT 25
//@ 8MHz INTFRC = 62.5 us.
#define ETIME COUNT*2.5
//time in uS
#define DELAY for(i=0;i<COUNT;i++)
#define ADSCALE 1023
//for unsigned conversion 10 sig bits
#define ADREF 3.3
//Vdd connected to A/D Vr+
#define RCAL .027
//R value is 4200000 (4.2M) 
//scaled so that result is in
//1/100th of uA
int main(void)
{
    int i;
    int j = 0;
//index for loop
    unsigned int Vread = 0;
    float CTMUISrc, CTMUCap, Vavg, VTot, Vcal;
//assume CTMU and A/D have been setup correctly
//see Example 25-1 for CTMU & A/D setup
setup();
CTMUCONHbits.CTMUEN = 1;
//Enable the CTMU
    for(j=0;j<10;j++)
    {       
        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
        PIR1bits.ADIF = 0;
//Clear A/D Interrupt Flag
        VTot += Vread;
//Add the reading to the total
    }
    
    Vavg = (float)(VTot/10.000);
//Average of 10 readings
    Vcal = (float)(Vavg/ADSCALE*ADREF);
    CTMUISrc = Vcal/RCAL;
//CTMUISrc is in 1/100ths of uA
    CTMUCap = (CTMUISrc*ETIME/Vcal)/100;    
}