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 support@ccsinfo.com

How to send 10bit by spi ?

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



Joined: 09 May 2012
Posts: 46
Location: KSA

View user's profile Send private message Send e-mail MSN Messenger

How to send 10bit by spi ?
PostPosted: Thu Jul 05, 2012 7:38 am     Reply with quote

I need some help, I'm doing a project that has 2 pic connected by spi,

slave reads analog input and send result to master, i have done this and I'm getting the correct values but this is for ADC 8bit.

so my question is how can i send a 10 bit value to the master if i used ADC 10 bit???

i have tried many and many solutions but didn't manage to make it , to my knowledge spi can deal with a byte i guess!

can anybody give me a sample code? please guys i really need it .

thanks.
Gabriel



Joined: 03 Aug 2009
Posts: 1067
Location: Panama

View user's profile Send private message

PostPosted: Thu Jul 05, 2012 7:49 am     Reply with quote

there is no such thing as a "10bit" value/variable... per say...

if you are using 10bit _resolution_ on you ADC... you MUST be saving the value on a 16bit variable.

Code:
int16 ADC_Value = 0;
ADC_value=Read_ADC();

. . . something like that...

a 16 bit variable is basically 2 bytes toguether...of which you are only using 10 bits...

read the datasheet and you will see that the ADC writes the result to a "high-byte" and a "low-byte"....

you can split your 16bit variable into 2 bytes and then send them individually and then put them together...


G.
_________________
CCS PCM 5.078 & CCS PCH 5.093
semmoor



Joined: 09 May 2012
Posts: 46
Location: KSA

View user's profile Send private message Send e-mail MSN Messenger

PostPosted: Thu Jul 05, 2012 9:01 am     Reply with quote

Gabriel wrote:
there is no such thing as a "10bit" value/variable... per say...

if you are using 10bit _resolution_ on you ADC... you MUST be saving the value on a 16bit variable.

Code:
int16 ADC_Value = 0;
ADC_value=Read_ADC();

. . . somthing like that...

a 16 bit variable is basically 2 bytes toguether...of which you are only using 10 bits...

read the datasheet and you will se that the ADC writes the result to a "high-byte" and a "low-byte"....

you can split your 16bit variable into 2 bytes and then send them individually and then put them toghether...


G.

Thanks for your help:

So did you mean doing this:
Code:

int16 adc_value;

int8 get_value_high;

int8 get_value_low;

adc_value=adc_read();

get_value_high = make8(adc_value, 1);  //getting first byte
get_value_low = make8(adc_value, 2); //getting second byte

I'm not sure, tell me if there's an error.
Gabriel



Joined: 03 Aug 2009
Posts: 1067
Location: Panama

View user's profile Send private message

PostPosted: Fri Jul 06, 2012 11:24 am     Reply with quote

The right answer is: Test it!

... you seem to have understood the concept clearly...

you could set your ADC_Value temporarily to a fixed value say: 0XAA55


Code:
int16 adc_value;

int8 get_value_high;

int8 get_value_low;

//adc_value=adc_read();  // <-----Commented temporarly for testing
adc_value=0xAA55;        // <----- nice clear Fixed test value


get_value_high = make8(adc_value, 1);  //getting first byte
get_value_low = make8(adc_value, 2); //getting second byte


and then print to serial:

Code:
Printf("High: %X & Low: %X \n\r", get_value_high, get_value_low);


what do you get?

G.
_________________
CCS PCM 5.078 & CCS PCH 5.093
ckielstra



Joined: 18 Mar 2004
Posts: 3680
Location: The Netherlands

View user's profile Send private message

PostPosted: Sun Jul 08, 2012 12:44 pm     Reply with quote

Code:
get_value_high = make8(adc_value, 1);  //getting first byte
get_value_low = make8(adc_value, 2); //getting second byte

CCS manual on Make8() wrote:
Function: Extracts the byte at offset from var. Same as: i8 = (((var >> (offset*8)) & 0xff) except
it is done with a single byte move.
The value you specify in the make8 indicates the number of bytes that will be shifted, i.e. starting to count at 0, not 1 ! And another sequence:
Code:
get_value_high = make8(adc_value, 1);  //getting high order byte
get_value_low = make8(adc_value, 0); //getting low order byte
semmoor



Joined: 09 May 2012
Posts: 46
Location: KSA

View user's profile Send private message Send e-mail MSN Messenger

Thanks
PostPosted: Tue Jul 10, 2012 4:07 am     Reply with quote

you're right i did it and it worked as you said.

thank you ckielstra and Gabriel for your help
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