multiple ADC using DSpic 30f3010
I am trying to convert two analogue signal using pin AN2 and AN3 by the dspic30f3010. However , I dont really understand how the result is stored. Therefore, I have written a code to test on LED first . If the input voltage in AN2 is above 2.5V, the LED in port E will light up . I assume for this AN2 conversion result is stored in ADCBUF0. SAme goes to AN3 where the LED in port D will light up from the ADCBUF1. However , my code doesnt work .
I think I do not understand how the result is stored. Below is my code , I wish to get some advice . Thanks for your help .
# include <p30f3010.h>
void SETUP_ADC (void);
/******MAIN BODY********************************************** **********************************************/
int main (void)
{
int input_1,input_2; /*Declare integer input 1 and 2*/
TRISD=0; /*Setting PORT D as output bit*/
TRISE=0; /*Setting PORT E as output bit*/
SETUP_ADC(); /*Calling the function where all the ADC setup is done*/
ADCON1bits.ADON=1; /*Turn ADC on*/
while(1) /*Repeat continously*/
{
ADCON1bits.SAMP=1; /*Start sampling*/
while (ADCON1bits.DONE!=1) /*Sampling is not done*/
{
}
ADCON1bits.SAMP=0; /*Sampling is holding*/
input_1= ADCBUF0;
if (input_1>=512)
{
PORTDbits.RD0=1;
}
else
{
PORTDbits.RD0=0;
}
input_2=ADCBUF1;
if (input_2>=512)
{
PORTEbits.RE0=1;
}
else
{
PORTEbits.RE0=0;
}
}
return(0);
}
/********************************SETUP_ADC********* ********************************************/
void SETUP_ADC (void)
{
/*SETTING UP ADCON1*/
ADCON1bits.ADSIDL=0; /*Continue operation in idle mode*/
ADCON1bits.FORM=0; /*Data output format is in integer*/
ADCON1bits.SSRC=7; /*Internal counter ends sampling and starts conversion ( Auto convert)*/
ADCON1bits.SIMSAM=1; /*Sample all channels simultanously*/
ADCON1bits.ASAM=0; /*Sampling begins when SAMP bit set*/
/*SETTING UP ADC0N2*/
ADCON2bits.VCFG=0; /*Configuring Voltage Reference where VrefH=AVdd & VrefL=AVss*/
ADCON2bits.CSCNA=1; /*Scan inputs*/
ADCON2bits.SMPI=0; /*Interrupt on each sample*/
ADCON2bits.BUFM=0; /*Buffer configured as one 16-word buffer*/
ADCON2bits.ALTS=0; /*ALways use MUX A input select*/
ADCON2bits.CHPS=2; /*Scan CH0, CH1, CH2 and CH3*/
/*SETTING UP ADCON3*/
ADCON3bits.SAMC=15; /*Auto sample time = 31TAD*/
ADCON3bits.ADRC=0; /*Conversion clock=system clock*/
ADCON3bits.ADCS=10; /*TAD=10*TCY*/
/*SETTING UP ADCHS- A/D INPUT SELECT REGISTER*/
ADCHS=0;
ADCHSbits.CH0NA=0; /*Channel 0 negative input is Vref-*/
ADCHSbits.CH0SA=2; /*Channel 0 positive input is AN2*/
ADCHSbits.CH123SA=1; /*Channel 1 positive input is AN3*/
ADCHSbits.CH123NA=0; /*Channel 1 negative input is Vref-*/
/*SETTING UP ADPCFG - A/D PORT CONFIGURATION REGISTER */
ADPCFG=0; /*Analog input pin is analogue mode*/
/*SETTING UP ADC BUFFER */
ADCBUF0=0;
ADCBUF1=0;
}