CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

dual channel AD conversion in pic18F4331

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Hicham



Joined: 09 Feb 2010
Posts: 17

View user's profile Send private message

dual channel AD conversion in pic18F4331
PostPosted: Thu Apr 08, 2010 2:42 pm     Reply with quote

Hello,

I'm working with pic18F4331 and it has a multi-channel AD conversion.
Based on the datasheet, it has 4 places to save the value after conversion before putting them in the final registers (ADRESH,ADRESL),
in case of using dual or sequence conversion.
But I can't figure out how to get 2 values in the same time after a dual conversion in CCS.
Is there an example for this kind of conversion ?

Any examples code are much appreciated.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Apr 08, 2010 6:26 pm     Reply with quote

The code shown below works with vs. 4.106. I don't have a 18F4331, so
I used an 18F4431, which is very similar. I set AN0 to 2.0 volts, and
AN1 to 3.0 volts. I get the following output on the terminal window:
Quote:

Ch0 = 411, Ch1 = 627
Ch0 = 411, Ch1 = 626
Ch0 = 411, Ch1 = 626
Ch0 = 411, Ch1 = 626
Ch0 = 411, Ch1 = 627

This is basically correct, given an ADC output range from 0 to 1023
over a 0v to 5v range. 2.0v should be 409, and 3.0v should be 614.
My variable power supply can't put out exactly 3.00v. But close enough.

I don't guarantee that this program is exactly correct. I looked at the
data sheet and the .LST file, and keep playing with it until it worked and
until it looked like I was complying with the data sheet's instructions for
continuous dual sampling mode. But anyway, it's good enough to give
you a starting point. Remember, I don't want to do your project for you.
You must do any further work on it.
Code:

#include <18F4431.h>
#device adc=10
#fuses XT,NOWDT,PUT,BROWNOUT,NOLVP
#use delay(clock=4000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)

#byte ADCON1 = 0xFC1
#byte ADRESL = 0xFC3
#byte ADRESH = 0xFC4
#byte ADCHS  = 0xF99

#bit  ADC_FIFOEN = ADCON1.4
#bit  ADC_BFEMT  = ADCON1.3

//======================================
void main(void)
{
int16 result0, result1;

// Select pins AN0 and AN1, in Groups A and B, respectively.
setup_adc_ports(sAN0 | SAN1);

// Tell ADC to read continuously from input Groups A and B.
setup_adc(ADC_CLOCK_DIV_2 | ADC_CONT_AB);
delay_us(10);  // Wait 10us, per the 18F4431 data sheet.

// Tell the ADC to put the conversion results in the fifo.
ADC_FIFOEN = 1;

read_adc(ADC_START_ONLY);  // Set the GO bit in ADCON0

while(1)
  {
   // Wait for the first A/D result to become available in the fifo.
   // Then read it.
   while(ADC_BFEMT);
   result0 =  make16(ADRESH, ADRESL); 

   // Wait for the 2nd A/D result to become available in the fifo.
   // Then read it.
   while(ADC_BFEMT);
   result1 =  make16(ADRESH, ADRESL); 

   printf("Ch0 = %lu, Ch1 = %lu \n\r", result0, result1);

   delay_ms(1000);
  }

}
Hicham



Joined: 09 Feb 2010
Posts: 17

View user's profile Send private message

PostPosted: Sun Apr 11, 2010 8:19 am     Reply with quote

Thanks for this example, PCM programmer.
Now I understand how it works.

So, I have to create new bytes like "ADC_FIFOEN "
I thought they're already build-in CCS.

Anyway.. thanks for your help, and wait my new question soon.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group