|
PIC18F45K80-I Datasheet, PDF (243/622 Pages) Microchip Technology – 28/40/44/64-Pin, Enhanced Flash Microcontrollers with ECAN and nanoWatt XLP Technology | |||
|
◁ |
PIC18F66K80 FAMILY
EXAMPLE 18-2: CURRENT CALIBRATION ROUTINE
#include "p18cxxx.h"
#define COUNT 500
#define DELAY for(i=0;i<COUNT;i++)
#define RCAL .027
#define ADSCALE 1023
#define ADREF 3.3
//@ 8MHz = 125uS.
//R value is 4200000 (4.2M)
//scaled so that result is in
//1/100th of uA
//for unsigned conversion 10 sig bits
//Vdd connected to A/D Vr+
int main(void)
{
int i;
int j = 0; //index for loop
unsigned int Vread = 0;
double VTot = 0;
float Vavg=0, Vcal=0, CTMUISrc = 0;
//float values stored for calcs
//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
}
ï£ 2010-2012 Microchip Technology Inc.
DS39977F-page 243
|
▷ |