I am trying to get an analog input working on my microcontroller. I am using the PIC 24FJ128GA204. The problem is that a specific input is not working, while other do work.
I want pin 26, AN9/C3INA/RP15/T3CK/T2CK/CTED6/CN11/RB15, to be the input. Other analog inputs seem to be working fine, but when I configure the input pin to be AN9 the results seem to be floating, even if I ground the input. The samples I get: 3987, 3987, 3988, 3989, 3989, 3987, etc. Below is the code I am using:
void initAD(){
//Pin used for ADC is pin 26 = RB15 = AN9
TRISBbits.TRISB15 = 1; //RB15 is input
ANSBbits.ANSB15 = 1; //analog mode
AD1CON1bits.MODE12 = 1; //set 12 bit mode operation
AD1CON1bits.SSRC = 0b0111; //auto-convert
AD1CON1bits.ASAM = 1; //SAMP bit is auto-set
AD1CON2 = 0; //AD1CON2 all 0
AD1CON2bits.PVCFG = 0b00; //00 = Vss, 01 = External VREF+
AD1CON2bits.NVCFG0 = 0; //AVSS
AD1CON3bits.SAMC = 0b11111; //Auto sample time = 31 TAD
AD1CON3bits.ADCS = 0b00000101; //AD conversion clock 5*Tcy = TAD
AD1CHSbits.CH0NA = 0; //negative is VREF-/AVSS
AD1CHSbits.CH0SA = 0b01001; //channel A AN9
AD1CHSbits.CH0NB = 0; //negative is VREF-/AVSS
AD1CHSbits.CH0SB = 0b01001; //channel B AN9
AD1CSSL = 0; //No scanned inputs
AD1CON1bits.ADON = 1; //enable AD module
}
and:
uint16_t ADread(){
while(!AD1CON1bits.DONE);
return(ADC1BUF0);
}
Thanks for reading. If you have any suggestions, that would be great.