I decided to start using MPLAB with XC8 after I started reaching the 2k code size limit in MikroC. The problem is that since all the Mikroc libraries are proprietary and there are no libraries available for the PIC16F series in MPLAB, I had to write my own ADC functions.
These are the two functions I wrote to set up my ADC using the datasheet:
void ADC_Init(){
PIR1bits.ADIF = 0; // Clear interrupt flag
PIE1bits.ADIE = 0; // Disable ADC interrupt
INTCONbits.PEIE = 0; // Disable perriferal interrupts
ADCON2 = 0; // No ADC Trigger
ADCON1bits.ADCS = 0b111; // Select internal RC clock source
ADCON1bits.ADPREF = 0b00; // Select VDD as Vref
ADCON1bits.ADFM = 0; // Left justify, upper 8 MSB in ADRESH
ADCON0bits.ADON = 1; // Turn on ADC Module
}
int ADC_Read(char channel){
ADCON0bits.CHS = channel; // Set channel, must be between 0 and 11
__delay_us(5); // Wait some arbitrary acquisiton time
PIR1bits.ADIF = 0; // Clear interrupt flag
ADCON0bits.GO_nDONE = 1; // Start acquisioin
while(ADCON0bits.GO_nDONE) {} // Wait for the conversion to finish
return (ADRESH << 2) | ADRESL;
}
The input signal that I'm reading in is pretty noisy to begin with. In my MikroC program with the library ADC functions, the noise floor on the samples ranged between 60 and 100 (10-bit ADC). By noise floor, I mean the range of values the ADC was reading with no input. Noise from the rest of the system I suppose.
In MPLAB with the above functions, the noise floor has shot up 200 to 300. On a 10-bit ADC, this much noise is unacceptable. I run the ADC_Init() function once outside my main program loop, then use the ADC_Read(..) function in my program loop.
Any ideas on why the noise is so much more? Or any ideas on how MikroC get this performance? I suspect it has something to do with the way I configured the ADC.
EDIT
In response to Olin's answer, I believe the 5us "acquisition time" might be a misnomer, that delay is there to allow the holding capacitor to charge after changing ADC channels. The datasheet shows an example for a 10kohm signal impedance needing 7.4us charge time. My signal impedance is only 700ohms, so I figured 5us should be more than enough. As for the circuit, it has not changed, only the software has. And the signal I'm measuring is a slightly noisy digital signal coming out of a MSGEQ7 filter IC.