View previous topic :: View next topic |
Author |
Message |
semmoor
Joined: 09 May 2012 Posts: 46 Location: KSA
|
How to send 10bit by spi ? |
Posted: Thu Jul 05, 2012 7:38 am |
|
|
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
|
|
Posted: Thu Jul 05, 2012 7:49 am |
|
|
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
|
|
Posted: Thu Jul 05, 2012 9:01 am |
|
|
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
|
|
Posted: Fri Jul 06, 2012 11:24 am |
|
|
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
|
|
Posted: Sun Jul 08, 2012 12:44 pm |
|
|
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
|
Thanks |
Posted: Tue Jul 10, 2012 4:07 am |
|
|
you're right i did it and it worked as you said.
thank you ckielstra and Gabriel for your help |
|
|
|