include headers
volatile unsigned int channel_13;
volatile unsigned int channel_14;
int main()
{
uart_init(); // initialized uart for terminal program
ADC12_Init(); //12bit adc initialization
while(1)
{
IFS0bits.AD1IF = 0; //Clear the ADC1 Interrupt Flag
ADL0CONLbits.SAMP = 0;
while(IFS0bits.AD1IF==0 );
ADL0CONLbits.SAMP = 1; // Close the sample switch.
channel_13 = ADRES0; // Read result for the channel 13.
channel_14 = ADRES1; // Read result for the channel 14
printf("%f V \t" ,channel_13);
printf("%f V \n" ,channel_14);
}
}
void ADC12_Init(void)
{
TRISBbits.TRISB13 =1; //AN3 (RB13)
ANSBbits.ANSB13 =1;
TRISBbits.TRISB14 =1; //AN2 (RB14)
ANSBbits.ANSB14 =1;
ADCON1=0;
// ADCON2=0x0300;
ADCON3=0;
ADCON2bits.PVCFG = 0; // AVdd
ADCON2bits.NVCFG = 0; // AVss
ADCON3bits.ADRC = 1; // using FRC
ADCON3bits.ADCS = 8; // Tad
ADCON1bits.FORM = 0; // Output format is unsigned integer.
ADCON2bits.BUFORG = 1; // Result buffer is an indexed buffer.
ADCON2bits.BUFINT = 0; // No buffer interrupt.
ADCON1bits.PWRLVL = 0; // Low power, reduced frequency operation.
ADCON2bits.ADPWR = 3; // Module is always powered on.
ADL0CONL = 0;
ADL0CONH = 0;
ADL0CONLbits.SLSIZE = 2-1; // Sample list length: 2 channels.
ADL0CONHbits.ASEN = 1; // Enable auto-scan.
ADL0CONHbits.SLINT = 1; // Interrupt after auto-scan completion.
ADL0CONHbits.SAMC = 15; // Sample time is 15 TAD.
ADL0CONLbits.SLTSRC = 0; // Single trigger generated when SAMP is cleared.
ADL0PTR = 0; // Start from the first list entry.
ADL0CONHbits.CM = 0; // Disable threshold compare.
ADTBL0bits.ADCH = 13; //AN= Channel #13.
ADTBL1bits.ADCH = 14; //AN= Channel #14.
ADCON1bits.ADON = 1; // Enable A/D.
while(ADSTATHbits.ADREADY == 0);
ADL0CONLbits.SAMP = 1; // Close sample switch.
ADL0CONLbits.SLEN = 1; // Enable sample list.
printf("ADC12 Done\n");
}