|
|
View previous topic :: View next topic |
Author |
Message |
Christophe
Joined: 10 May 2005 Posts: 323 Location: Belgium
|
sending floats to the PC and displaying them on the PC |
Posted: Tue Nov 15, 2005 6:49 am |
|
|
Hi,
my PIC measures the battery voltage. That is a float number. I want to send that to the PC and then display the voltage. Can anyone help how to do that?
Code: | void toIEEE(int *Value)
{
int1 sign;
sign = shift_left(&Value[1], 1, 0);
sign = shift_right(&Value[0], 1, sign);
shift_right(&Value[1], 1, sign);
}
//******************************* STUUR VBAT *********************************//
void Send_Vbat(float Vbat)
{
union {
float f;
char b[4];
} Value;
value.f=Vbat;
toIEEE(&value.b[0]);
//floatgrootte = sizeof(Vbat);
//printf(%c"BATT%i",floatgrootte);
//for( n = 0 ; n<4 ; n++)
// putc(value.b[n]);
//}
Printf("%c%S%c%c%c%c%c",COMMANDO_BYTE,BATTERIJ_COMMANDO,sizeof(Vbat),value.b[0],value.b[1],value.b[2],value.b[3]);
} |
|
|
|
Foppie
Joined: 16 Sep 2005 Posts: 138 Location: The Netherlands
|
|
Posted: Tue Nov 15, 2005 8:19 am |
|
|
why don't you use Code: | printf("%f", value.f); | ? |
|
|
Ttelmah Guest
|
|
Posted: Tue Nov 15, 2005 10:23 am |
|
|
It depends how you want to send the number, and how large it may be. The advantage of the 'binary' form, is that you are just sending the bytes representing a float, and four bytes will transfer the number, even if it is some huge value like 1E12.
Using the printf form is easier, if the number is likely to fall into a restricted range,and has the advantage that what is sent, is 'human readable'. It is easier to decode, if you either add a delimiter (perhaps a ','), or always justify the number to a standard size.
If you want to send the full value, the code shown is basically fine. However sending the 'sizeof' the foat is basically a wasted character, since the float will always be just four bytes. At the other end, you need to use almost the identical code the 'other way', transferring the four bytes, into a byte array, which is itself mapped into the same space as a _single precision_ float. Beware that the default 'float' on many languages is an 8byte double precision type.
Best Wishes |
|
|
arunb
Joined: 08 Sep 2003 Posts: 492 Location: India
|
RE: |
Posted: Wed Nov 16, 2005 3:37 am |
|
|
Hi,
You could divide the data into the intergral part and the fraction part.
To send 123.456 you have to send 123456 in two or three parts of one byte each. Then recompose the parts received in the PC to form 123456 (ie the original number), now send 1000 in two parts , recompose the received data to get 1000, divide the two numbers to get 123.456.
thanks
arunb |
|
|
kender
Joined: 09 Aug 2004 Posts: 768 Location: Silicon Valley
|
|
|
Ttelmah Guest
|
|
Posted: Wed Nov 16, 2005 4:21 am |
|
|
This still has the problem of just how large/small the value may be. For example, I transfer the binary number almost as shown (in fact the toIEEE code was some I published a while ago), on an instrument, that potentially works over a range of 14 decades. In fact no individual instrument does this, with different calibrations, to the sensor cell being needed, but the possible range of output values, from a set of instruments on the bus needs to handle this scale. This is the whole 'point' of a float (otherwise you might as well use a scaled integer, and gain the extra precision involved in 32bits of numeric storage).
It really does come down to the range that the numbers have to cover.
Best Wishes |
|
|
|
|
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
|