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 receive int32 or float over SPI?

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



Joined: 06 Oct 2006
Posts: 44

View user's profile Send private message

How to receive int32 or float over SPI?
PostPosted: Sun Oct 07, 2007 8:22 pm     Reply with quote

I have searched for this and cannot find the exact answer I am looking for. As I understand it, when receiving/sending data over SPI (in my case from Slave to the Master) you can only send an 8 bit integer at a time. I need to be able to send an int32, or float, to the Master. For example I will have to send the output from a PID controller to the Master, so I will have signed numbers with decimals (ex: -5.34).

How can I do this? In regard to the int32, would I just shift the 32 bit integer and send 8 bits at a time, 4 times. Once I have received this on my Master, would I then just reconstruct the int32 by shifting them in the other way? Also this data will have to be signed.

If I were to use a float how would I do this? Is there any better way? I am trying to conserve time to maintain I high sampling rate for my application. I know that bit operations are not slow, but the less overhead the better.

Thanks for the help!
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Oct 08, 2007 12:24 am     Reply with quote

You can use this FAQ article as a guide and write some macros to
break up a float into 4 bytes, and then to re-assemble the 4 bytes back
into a float.
http://www.ccsinfo.com/faq.php?page=write_eeprom_not_byte

Example:
Code:

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

#define GetByte(x, offset)  *((int8 *)&x +offset)

#define MakeDWord(x, b3, b2, b1, b0)  \
*(int8 *)&x = b0;      \
*((int8 *)&x +1) = b1; \
*((int8 *)&x +2) = b2; \
*((int8 *)&x +3) = b3

//======================================

void main()
{
float  value, result;
int8 b0, b1, b2, b3;

value = 123.4567;
printf("value = %7.4f \r", value);

// Break the float into 4 bytes.
b0 = GetByte(value, 0);
b1 = GetByte(value, 1);
b2 = GetByte(value, 2);
b3 = GetByte(value, 3);

// Re-assemble the float from 4 bytes.
MakeDWord(result, b3, b2, b1, b0);

printf("result = %7.4f \n\r", result);

while(1);
}


Actually, if you want minimum overhead, you should probably do it
with loops as shown in the FAQ. The code above uses un-rolled loops.
Guest








Exactly what i needed to see!
PostPosted: Mon Oct 08, 2007 12:53 am     Reply with quote

Wow, that is exactly the information I needed. I appreciate the help greatly. You source code will be a perfect thing to work off of, as soon as I can assemble a circuit to test it. Thank you very much!

I searched the forum, and I should have searched the CCS FAQ also. Thanks for the heads up I will need to look through that more often.
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