[SOLVED] Pic 18F2431 Multiple ADC conversion problem.

Status
Not open for further replies.

Athosza

Newbie level 5
Joined
Aug 3, 2010
Messages
8
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Location
South Africa
Activity points
1,363
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
 

SCS<1:0>: System Clock Select bits
1x = Internal oscillator block
01 = Secondary (Timer1) oscillator
00 = Primary oscillator

OSCCON = 0xf0; // 8MHz internal clock frequency. You set it to primary oscillator ( refer to page 36 of the datasheet)
 

Hey John,

Thanks for the reply. I see what you mean that was a bit of an oversight on my part thanks for pointing that out. I have now corrected it but the code is still not giving me a difference between AN0 and AN1.

Athosza
 

Hi Athos,

Try manually setting the ACMOD bits in the ADCON0 register (page 248 of the datasheet), I dont trust those built compiler functions.

Regards,
Cronus
 
Thanks CronusZA it worked perfectly! I had to set each of the ACMOD bits indivdually:

void measure_0()
{
SelChanConvADC(0);
Delay10TCYx( 5 ); // Delay for 50TCY
ADCON0bits.ACMOD0 = 0;
ConvertADC(); // Start conversion
while( BusyADC() ); // Wait for completion
result_0 = ReadADC(); // Read result
}

void measure_1()
{
SelChanConvADC(1);
Delay10TCYx( 5 ); // Delay for 50TCY
ADCON0bits.ACMOD0 = 1;
ConvertADC(); // Start conversion
while( BusyADC() ); // Wait for completion
result_1 = ReadADC(); // Read result

I hope this helps other people with the same problem.

Case closed South African style
 

Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…