English
Language : 

PIC18F45K80-I Datasheet, PDF (245/622 Pages) Microchip Technology – 28/40/44/64-Pin, Enhanced Flash Microcontrollers with ECAN and nanoWatt XLP Technology
PIC18F66K80 FAMILY
EXAMPLE 18-3: CAPACITANCE CALIBRATION ROUTINE
#include "p18cxxx.h"
#define COUNT 25
#define ETIME COUNT*2.5
#define DELAY for(i=0;i<COUNT;i++)
#define ADSCALE 1023
#define ADREF 3.3
#define RCAL .027
//@ 8MHz INTFRC = 62.5 us.
//time in uS
//for unsigned conversion 10 sig bits
//Vdd connected to A/D Vr+
//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;
for(j=0;j<10;j++)
{
CTMUCONHbits.IDISSEN = 1;
DELAY;
CTMUCONHbits.IDISSEN = 0;
//Enable the CTMU
//drain charge on the circuit
//wait 125us
//end drain of circuit
CTMUCONLbits.EDG1STAT = 1;
DELAY;
CTMUCONLbits.EDG1STAT = 0;
//Begin charging the circuit
//using CTMU current source
//wait for 125us
//Stop charging circuit
PIR1bits.ADIF = 0;
ADCON0bits.GO=1;
while(!PIR1bits.ADIF);
//make sure A/D Int not set
//and begin A/D conv.
//Wait for A/D convert complete
Vread = ADRES;
PIR1bits.ADIF = 0;
VTot += Vread;
}
//Get the value from the A/D
//Clear A/D Interrupt Flag
//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;
}
 2010-2012 Microchip Technology Inc.
DS39977F-page 245