View previous topic :: View next topic |
Author |
Message |
hayee
Joined: 05 Sep 2007 Posts: 252
|
sending floating numbers through spi |
Posted: Sun Mar 02, 2008 6:37 am |
|
|
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
|
|
Posted: Sun Mar 02, 2008 9:50 am |
|
|
At heart, don't!..
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
|
|
Posted: Mon Mar 03, 2008 12:01 pm |
|
|
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
|
|
Posted: Mon Mar 03, 2008 12:25 pm |
|
|
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
|
|
Posted: Mon Mar 03, 2008 1:34 pm |
|
|
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. |
|
|
|