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

Read_adc() in two 8 bits variables for a 10 bit ADC conv

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



Joined: 14 Nov 2015
Posts: 8

View user's profile Send private message

Read_adc() in two 8 bits variables for a 10 bit ADC conv
PostPosted: Mon Nov 16, 2015 5:29 am     Reply with quote

Im doing a 10bit adc conversion using PIC18F4550, and trying to send the data by USB to PC, so since USB admits 8bit data packets, i want to divide the value = read_adc() variable into two, the low and high bits. Problem is i don't know how to

the code i have is something like this for 2 channels

#DEVICE ADC=10

int8 out[4]

setup_adc_ports etc
.....
...
SET_ADC_CHANNEL(0)
output[0]=read_adc('i want here first 8 bits)
output[1]=read_adc('the other 2 bits')

Same for channel(1)

//usb things



I'm not sure if this is the best way to approach the problem so any help would be great, thx
Ttelmah



Joined: 11 Mar 2010
Posts: 19552

View user's profile Send private message

PostPosted: Mon Nov 16, 2015 6:10 am     Reply with quote

You don't read it into two byte, you just output the bytes from the int16. A lot of different ways to do this:
Code:

#device ADC=10

//Then in the code
    int16 val;

    val=read_adc();

    output[0]= make8(val,0); //LSB of val
    output[1]=make8(val,1); //MSB of val
//This is 'CCS only' code

    output[0] = val & 0xFF;
    output[1] = val/256;
//Generic, and the compiler is efficient, and will realise it can do
///256 by just taking the high byte

Or you can use a 'union' to allow you to access the bytes or the int16 at the same location in memory. This can be _very_ efficient, and again 'generic'. A search here will find this. Or you can be sneaky:
Code:


    *(int16 *)output=read_adc();

This tells the compiler to treat the array address (remember an array's name is it's address), as if it pointed to an int16. Then to write the 16bit return from read_adc, directly into the memory pointed to by this address. This puts the LSB into output[0], and the MSB into output[1], without any further copying.

There are probably at least a dozen other ways of doing this.

The union is as efficient as 'make8', and is also generic.
Even better you could make the union access the same memory as 'output', to avoid the copying as well. However the compiler's optimiser is quite good, and should automatically do the pointer version as efficiently.
dgg91



Joined: 14 Nov 2015
Posts: 8

View user's profile Send private message

PostPosted: Mon Nov 16, 2015 8:58 am     Reply with quote

Thx for your help, i found a solution using make8 function as you said, but i will take into account the generic & efficient ways as well.
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