#include "p33FJ256GP710a.h"
// Start with one pin being used to read a single battery
// Next read all four batteries from battery circuit.
// initialize the ADC for single conversion, select Analog input pins
void initADC1( int amask)
{
AD1PCFGH = amask; // select analog input pins
AD1CON1 = 0x04E0; // data output format-integer, auto convert after end of sampling.12bit Operation mode. No need to include a delay loop to provide time for completion of sampling
AD1CSSL = 0; // no scanning required
AD1CON3 = 0x1FFF; // Use internal clock, max sample time avail is 31Tad, conversion time Tad = 128*Tcy = 128*(2/Fosc) = 256/8MHz = 32us which is > (required)75ns ); 31 Tad = 31*32us = 992us is the sample time. About 100samples/sec
//Possibly 1FFFF Check Both
AD1CON2 = 0; // use MUXA, AVss and AVdd are used as Vref+/-
AD1CON1bits.ADON = 1; // turn on the ADC
} //initADC
int readADC1( int ch)
{
AD1CHS0 = ch; // select analog input channel
ADC1BUF0 = 0x0000; // Reset Buffer
AD1CON1bits.SAMP = 1; // start sampling, automatic conversion will follow at end of sampling
while (!AD1CON1bits.DONE); // wait to complete the conversion. DONE will be set as soon as conversion ends
return ADC1BUF0; // read the conversion result
} // readADC