#define AINPUTS 0xE1C3
#define TTSENS1 TRISBbits.TRISB2 // Temperature Sensor
#define TCH1BAT12 TRISBbits.TRISB10 // +12V Channel 1
#define TCH2BAT12 TRISBbits.TRISB11 // +12V Channel 2
#define TCH1BAT24 TRISBbits.TRISB3 // +24V Channel 1
#define TCH2BAT24 TRISBbits.TRISB4 // +24V Channel 2
#define TCH3BAT24 TRISBbits.TRISB5 // +24V Channel 3
#define TCH1BAT48 TRISBbits.TRISB12 // +48V Channel 1
#define TCH2BAT48 TRISBbits.TRISB9 // +48V Channel 2
void InitADC(int amask)
{
InitADCPins();
AD1PCFGL = amask;
//ConfigPinsAsAnalog();
AD1CON1 = 0; // Clear it
AD1CON1bits.AD12B = 1; // 12-bit, 1-channel ADC operation
AD1CON1bits.FORM = 0; // Integer (DOUT = 0000 dddd dddd dddd)
AD1CON1bits.SSRC = 0; // SSRC bit = 111 implies internal counter ends sampling and starts converting.
AD1CON2bits.CSCNA = 0; // NO Scan required
AD1CON2bits.VCFG = 0; // AVss and AVdd are used as Vref+/-
AD1CON3 = 0x1F02; // max sample time = 31Tad, Tad = 2 x Tcy = 125ns >75ns
IFS0bits.AD1IF = 0; // Clear the A/D interrupt flag bit
IEC0bits.AD1IE = 0; // Do Not Enable A/D interrupt
AD1CON1bits.ADON = 1; // turn on the ADC
} //initADC
void InitADCPins(void)
{
// make pins input
TTSENS1 = 1; // Temperature Sensor
TCH1BAT24 = 1; // +24V Channel 1
TCH2BAT24 = 1; // +24V Channel 2
TCH3BAT24 = 1; // +24V Channel 3
TCH1BAT48 = 1; // +48V Channel 1
TCH2BAT48 = 1; // +48V Channel 2
TCH1BAT12 = 1; // +12V Channel 1
TCH2BAT12 = 1; // +12V Channel 2
}
void ConfigPinsAsAnalog(void)
{
AD1PCFGLbits.PCFG2 = 0;
AD1PCFGLbits.PCFG3 = 0;
AD1PCFGLbits.PCFG4 = 0;
AD1PCFGLbits.PCFG5 = 0;
AD1PCFGLbits.PCFG9 = 0;
AD1PCFGLbits.PCFG10 = 0;
AD1PCFGLbits.PCFG11 = 0;
AD1PCFGLbits.PCFG12 = 0;
}
int ReadADC(char Analogchannel)
{
AD1CHS0bits.CH0SA = Analogchannel; // Connect ANX to CH0 input
asm("nop"); // Some delay
asm("nop");
asm("nop");
AD1CON1bits.SAMP = 1; // start sampling, automatic conversion will follow
TDelayms(5); // Some delay
AD1CON1bits.SAMP = 0; // start sampling, automatic conversion will follow
while (!AD1CON1bits.DONE); // wait to complete the conversion
return ADC1BUF0; // read the conversion result
}