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

sending floating numbers through spi

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



Joined: 05 Sep 2007
Posts: 252

View user's profile Send private message

sending floating numbers through spi
PostPosted: Sun Mar 02, 2008 6:37 am     Reply with quote

Hi everyone,
I am using ccs compiler(version 4.020).
I am communicating two pic16f877a through spi.i want to send floating numbers i-e(4.5,66.87 etc) from one controller to another through spi.
I know that the limitations is only we can send only integers.is there any way to send floating numbers.
my 2nd question is that i want to send binary data i-e(x=11000101) through pic to pc and want to see that data in binary format on hyper terminal.how can i do this?
Ttelmah
Guest







PostPosted: Sun Mar 02, 2008 9:50 am     Reply with quote

At heart, don't!.. Smile
Step 'away' from thinking about floating point numbers, and simply remember that _everything_ inside the processor works in bytes. At heart, the processor stores FP numbers, in four bytes. Simply send these four bytes, and at the other end, put them 'into' another varible also declared as an FP value.
Look at the compiler's 'make8' function, and make32 function or (my preferred route), use a union. So:
Code:

union {
   int8 b[4];
   float fpval;
} val_to_send;

Then if you put a floating point number 'into' val_to_send.fpval, you can access it's bytes as val_to_send.b[0] to b[3], and send these, while at the other end, if you then write the received bytes into val_received.b[0] to b[3] (having declared a similar union here), you can then work with the FP value here....

Best Wishes
hayee



Joined: 05 Sep 2007
Posts: 252

View user's profile Send private message

PostPosted: Mon Mar 03, 2008 12:01 pm     Reply with quote

thanks Ttelmah, but i am still confused about converting floating numbers in 4 bytes and then send and receive. Can you give me a proper example for master and slave both using pic16f877a.
kamputty
Guest







PostPosted: Mon Mar 03, 2008 12:25 pm     Reply with quote

Hi Hayee,

Look at it this way...

you have a floating point var, and internally to the compiler, it's "value" is stored in 4 bytes. So we can say that a float=4 bytes internally. How they are stored as 4 bytes does not matter right now.

Using a "union" contruct as Ttelmah showed us

Code:

union {
   int8 b[4];
   float fpval;
} val_to_send;


tells the compiler, "...hey, we have 2 variables called 'b' and 'fpval' and they *point* to the same value.". So the b[0]...b[3] point to the *same* 4 bytes that the float variable fpval is *constructed* with. Does that make sense?

So sending the 4 byte values 0...3 over spi, and then inserting them into the *same* union on the other end will result in the same float value since they are using the same 4 bytes that we sent.

Yes?

~Kam (^8*
Douglas Kennedy



Joined: 07 Sep 2003
Posts: 755
Location: Florida

View user's profile Send private message AIM Address

PostPosted: Mon Mar 03, 2008 1:34 pm     Reply with quote

When you write a number on paper it is notation. When you store a number in your PIC it is notation. Now in the case of a whole number less than 256 it fits in a byte but it is still notation for which bit is the msb is still important. However if the notation is chosen correctly the integer can be stored so that it can be directly manipulated by a PIC without conversion. This is not true of most numbers so additional translation from the notational form is needed. Some numbers are integers and require more storage since they exceed 255 others are numbers that will be expressed in floating point notation. The specific notation can vary and the PIC has chosen a specific notation namely 4 byte (one byte exponent and a 3 byte mantissa). The notation is manipulated and translated by the compiler to allow for mathematic operations. The result of any float operation returns the result in notational form. You will be sending 4 bytes of notation since you will be reading from the memory of one pic and writing to the memory of the other. Now the compiler needs to know where the notation form of a specific number is stored. This is what the union is doing. It tells the compiler that the same 4 bytes of storage have dual notation. One is the basic 4 bytes the other is a float using 4 byte notation. A call to use a float variable using the same dually notated 4 bytes will cause the compiler in both PICs to view the bytes as float notation and do mathematical operations accordingly. Many see the notation and number as identical but numbers can be notated in many forms. We have binary hex ASCII so the number two can be ASCII 34( in decimal) or 0x02 hex or 10 binary and these are just a few of the choices. You can consider the PIC as storing the notation of numbers. Numbers are like thoughts they can't be touched directly they need to be written down (notated) and manipulated indirectly.
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