Greetings all,
I am attempting to use a PIC 18F2431 to read more than one analogue input voltage. I would like pin AN0 to read 2.5V and pin AN1 to read 5V. I have pin AN0 reading and storing the 2.5V value perfect however when I try to read AN1 it returns the same value of 2.5V that is on pin AN0. I do not understand why. I have debugged the code and it seems to select pin AN1 in the code but no matter what I cannot get the 5V reading even if I only try to read pin AN1 by itself, I still get the 2.5V on pin AN0.
Here is the code I am using to test this:
#include <p18f2431.h>
#include <stdlib.h>
#include <adc.h>
#define ADC_CH0 0x00 //Select Channel 0
#define ADC_CH1 0x01 //Select Channel 0
int var0;
int var1;
void measure_0(void);
void measure_1(void);
void oscillator_setup()
{
OSCCON = 0xf0; // 8MHz internal clock frequency
TRISA = 1;
TRISB = 0x00;
PORTB = 0;
}
void Setup_ADC()
{
OpenADC ( ADC_FOSC_RC &
ADC_RIGHT_JUST &
ADC_12_TAD,
ADC_CH0 &
ADC_INT_OFF &
ADC_VREFPLUS_VDD, 0);
}
void measure_0()
{
SelChanConvADC(ADC_CH0);
Delay10TCYx( 5 ); // Delay for 50TCY
ConvertADC(); // Start conversion
BusyADC(); // Wait for completion
var0 = ReadADC();
}
void measure_1()
{
SelChanConvADC(ADC_CH1);
Delay10TCYx( 5 ); // Delay for 50TCY
ConvertADC(); // Start conversion
BusyADC(); // Wait for completion
var1 = ReadADC();
}
void main()
{
var0 = 0;
var1 = 0;
oscillator_setup();
Setup_ADC();
while(1) {
measure_0();
measure_1();
//variable = variable;
//CloseADC();
}
}
Any help will be greatly appreciated. I hope I am overlooking something really basic.
Thanks,
Athosza