I'm using dsPIC33CK64MC105 to read 3 analog sensors using the ADC channels AN3, AN4 and AN9 which corresponds to the pins RA3, RA4 and RA2, the idea is when any of the channels read a value higher than a certain threshold an LED will be turned on.
while testing the ADC channels by applying a voltage on each pin one by one and increasing this voltage to the threshold level I found that only channel AN3 read the voltage correctly while AN4 and AN9 make the LED turn on at lower voltage level (1.3V instead of 2V for AN4 and 1.4V instead of 2.8V for AN9).
the ADC configurations looks fine to me, but can anyone tell me what am I doing wrong please?
here is the code
#include "mcc_generated_files/system.h"
#include "mcc_generated_files/mcc.h"
#define FCY 50000000UL
#include <libpic30.h>
uint16_t AN3_RESULT;
uint16_t AN4_RESULT;
uint16_t AN9_RESULT;
int main(void) {
SYSTEM_Initialize();
int Th_High_1 = 3500; // 2.821V
int Th_Low_1 = 3000; // 2.418V
int Th_High_2 = 2500; // 2.015V
int Th_Low_2 = 2000; // 1.612V
int Th_High_3 = 1500; // 1.209V
int Th_Low_3 = 1000; // 0.806V
ADCON1L = 0;
ADCON1Lbits.ADON = 0;
ADCON1Lbits.ADSIDL = 0;
ADCON1H = 0;
ADCON1Hbits.FORM = 0; // Integer
ADCON1Hbits.SHRRES = 3; // 12bit
ADCON2L = 0;
ADCON2Lbits.SHRADCS = 0; //TADCORE = 2 * TCORESRC = 20ns > 14.3ns
ADCON2H = 0;
ADCON2Hbits.SHRSAMC = 0x3FF; //sample time 20.5us
ADCON3L = 0;
ADCON3H = 0;
ADCON3Hbits.CLKSEL = 1; //Fosc 100MHz
ADCON3Hbits.CLKDIV = 0; //TCORESRC 1/100MHz = 10ns
ADCON3Hbits.SHREN = 0;
ADCON5L = 0;
ADCON5H = 0;
ADCON5Hbits.SHRCIE = 0;
ADCON5Hbits.WARMTIME = 10; //1024 * TCORESRC = 10.24us
ADCON1Lbits.ADON = 1;
ADCON5Lbits.SHRPWR = 1;
while (ADCON5Lbits.SHRRDY == 0);
ADCON3Hbits.SHREN = 1;
while (1) {
ADCON3Lbits.CNVCHSEL = 4;
ADCON3Lbits.SHRSAMP = 1;
__delay_us(100);
ADCON3Lbits.SHRSAMP = 0;
ADCON3Lbits.CNVRTCH = 1;
while (ADSTATLbits.AN4RDY == 0);
AN4_RESULT = ADCBUF4;
ADCON3Lbits.CNVCHSEL = 9;
ADCON3Lbits.SHRSAMP = 1;
__delay_us(100);
ADCON3Lbits.SHRSAMP = 0;
ADCON3Lbits.CNVRTCH = 1;
while (ADSTATLbits.AN9RDY == 0);
AN9_RESULT = ADCBUF9;
ADCON3Lbits.CNVCHSEL = 3;
ADCON3Lbits.SHRSAMP = 1;
__delay_us(100);
ADCON3Lbits.SHRSAMP = 0;
ADCON3Lbits.CNVRTCH = 1;
while (ADSTATLbits.AN3RDY == 0);
AN3_RESULT = ADCBUF3;
if(AN3_RESULT > Th_High_3 || AN4_RESULT > Th_High_2 || AN9_RESULT > Th_High_1){
FAULT_SetHigh();
LED_SetLow();
}
if(AN3_RESULT < Th_Low_3 && AN4_RESULT < Th_Low_2 && AN9_RESULT < Th_Low_1){
FAULT_SetLow();
LED_SetHigh();
}
__delay_ms(1);
}
return 1;
}
I'm using a lab power supply connected directly to the ADC pins, I then change the voltage from 0V to the specific threshold of each channel, for example for channel AN3 I increase the voltage until it reaches 1.2V and then the LED turns on and when I lower the voltage to below 0.8V it turns off as expected, this behavior does not happen with AN4 and AN9 which has threshold levels of 2V and 2.8V respectively, the LED turns on at about 1.3V to 1.4V, I tried with different threshold levels and also tried to change the conversion sequence (AN4 first or AN3 first) and that didn't work either, also tried to increase the sample time but that didn't work too.