I am trying to read ADC channel 0 using Timer interrupt to trigger it. I am using dspic33ch128mp508 curiosity development board for anologue reading using potentiometer annologue channel 0. When I debug, I dont get any value on the Anlogue channel 0.
I am using MPLAB X IDE 5.10 for debugging. Here is my code. Any thoughts on this?
You should not need to set the GIE bit to 1 as that is its Power-on default setting.\
Look at the ADC FRM section for that MCU - it has a whole section of examples.
Suan
void adc_init(){//Configuration of the I/O pins to be used as analog inputs
TRISA = 0xFFFF;
ANSELA =0xFFFF;
ADCON1Lbits.ADSIDL=0;//ADC module operate in idle mode
ADCON3Hbits.CLKSEL0=1;//FOSC clock selection
ADCON3Hbits.CLKSEL1=0;
ADCON3Hbits.CLKDIV=0;//no clock divider
ADCON1Hbits.FORM=0;//Integer Output format
ADMOD0Lbits.SIGN0=0;//unsigned output format
ADCON3Lbits.REFSEL=0;//ADC Reference voltage selection
ADSTATL =0;// ADC Data ready status register
ADCON3Hbits.SHREN=1;//Shared ADC core is enabled
ADCON2Lbits.EIEN=0;//Early interrupt is disabled
ADCON1Lbits.ADON=1;// Turn on ADC
ADCON5Hbits.WARMTIME=15;
Enableandcalibrate();
ADIEL=0xFFFF;// ADC interrupt enable register low
IEC5=0xF100;// enable interrupts for AN0 -AN4
IEC6=0x07FF;//enable interrupts for AN4 -AN15
IFS5 =0x0000;//clear interrupts flags
IFS6 =0x0000;//clear interrupts flags}void Enableandcalibrate(){// Turn on analog power for shared core
ADCON5Lbits.SHRPWR=1;// Wait when the shared core is ready for operationwhile(ADCON5Lbits.SHRRDY==0);// Turn on digital power to enable triggers to the shared core
ADCON3Hbits.SHREN=1;}
unsigned int read_adc(int channel){int i =0;
ptr =&ADCBUF0;
ADCON3Lbits.CNVCHSEL= channel | 0x0100;// Channel selection
ADCON3Lbits.CNVRTCH=1;//Starts samplingwhile(ADSTATL &(1<<channel)){
conversionresults[i++]=*ptr++;}}