|
|
View previous topic :: View next topic |
Author |
Message |
Christophe
Joined: 10 May 2005 Posts: 323 Location: Belgium
|
Send 32bit float via RS232 |
Posted: Thu May 19, 2005 8:55 am |
|
|
How can i send a 4 byte float via RS232 in CCS - C ?
edit
How can I send the char \NUL (decimal 0) via RS232?
printf(%u,0) sends decimal 48, I want decimal 0 te be sent.. |
|
|
Ttelmah Guest
|
|
Posted: Thu May 19, 2005 9:50 am |
|
|
printf("%c",0);
or
putc(0);
Probably the easiest way to send numbers (assuming you want reasonably easy interpretation), is to use text, and send a limited size 'representation', with perhaps a comma delimiter.
You can of course just send the four bytes that represent the number, but if so, assuming you are talking to a PC, there is qute a bit of 'conversion' involved, because the CCS representation does not match the IEEE representation used on machines like this. The following are a pair of routines, which do the conversion for a block of four bytes representing a float, when handed the address of the block.
Code: |
void toCCS(int *Value) {
//program to convert a 4 bytes 'word' from IEEE to CCS format
int1 sign;
sign = shift_left(&Value[1],1,0);
sign = shift_left(&Value[0],1,sign);
shift_right(&Value[1],1,sign);
}
void toIEEE(int *Value) {
//complement to the above
int1 sign;
sign = shift_left(&Value[1], 1, 0);
sign = shift_right(&Value[0], 1, sign);
shift_right(&Value[1], 1, sign);
}
|
If you have variable like:
union convbuff {
int8 b[4];
float fp;
} value;
You can then use:
value.fp=1000.0;
toIEEE(&value.b[0]);
Then simply send the four bytes over the serial link to a PC, and place them into a similar union (using a single precision float), at the othr end, and the value will be correct.
Similarly going the other way, you can send the bytes from the union, and put them into the individual b[0]....b[3] locations, then use:
toCCS((&value.b[0]);
to have value.fp become the same number as the PC is sending.
Best Wishes |
|
|
Christophe
Joined: 10 May 2005 Posts: 323 Location: Belgium
|
|
Posted: Fri May 20, 2005 12:41 am |
|
|
How can I send a float?
float f;
putc(f); // sends only 1 byte |
|
|
Guest
|
|
Posted: Fri May 20, 2005 6:13 am |
|
|
You can't.
Since RS232 only transfers one byte at a time, you either have to send it in ASCII as text, or send the four bytes one after the other. This is exactly the same as in any other 'C'.
So:
Code: |
union {
float f;
int8 b[4];
} value;
int8 n;
value.f=1000.0;
for(n=0;n<4;n++)
putc(value.b[n];
//sends the four bytes
printf("%f",value.f);
//sends the text
|
Obviously this needs a suitable 'wrapper' to setup th RS232 etc..
Best Wishes |
|
|
Christophe
Joined: 10 May 2005 Posts: 323 Location: Belgium
|
|
Posted: Mon May 23, 2005 2:17 am |
|
|
I get a compiler error; ' a numeric expression must appear here' (points to union)
Code: | union {
float f;
int8 b[4];
} Value;
value.f=Vbat;
printf("BATT%i",floatgrootte);
for(int8 n=0 ; n<4 ; n++)
putc(value.b[n]); |
|
|
|
Ttelmah Guest
|
|
Posted: Mon May 23, 2005 4:08 am |
|
|
The union has to be declared at the start of the function (or as a global). Then the code itself sits further down in the function. There are two seperate 'parts' to the code here, one showing how to declare the union, and the other how to use it, they do not go together as one 'lump'...
Best Wishes |
|
|
Christophe
Joined: 10 May 2005 Posts: 323 Location: Belgium
|
|
Posted: Mon May 23, 2005 4:15 am |
|
|
Yes,
that was it, all the variables must be declared in front of the function. That is kinda different from normal C-compiler no?
How can I convert the 4 sent bytes back to a float? |
|
|
Ttelmah Guest
|
|
Posted: Mon May 23, 2005 7:28 am |
|
|
Christophe wrote: | Yes,
that was it, all the variables must be declared in front of the function. That is kinda different from normal C-compiler no?
How can I convert the 4 sent bytes back to a float? |
The requirement to declare the variables, is standard C.
You can declare the variable for 'internal' use, by creating a command 'block' (with '{}' brackets round it), and then declaring variables inside this. This is accepted by standard C, but I would not trust this on the CCS C, since the way that local variables are generated, is by appending the variable name to the function name, and a bracketted block like this, does not have a distinct name...
You just rebuild the variable the same way. Declare a similar union at the other end, and then write the bytes into this, and look at the float. However see the caveat I posted about this (and the functions called toccs, and toIEEE), in a thread a few days ago, since the floating point format in CCS, is not the IEEE format used in the PC.
Best Wishes |
|
|
bfemmel
Joined: 18 Jul 2004 Posts: 40 Location: San Carlos, CA.
|
Decalring C Variables |
Posted: Mon May 23, 2005 9:45 am |
|
|
Quote: | all the variables must be declared in front of the function. That is kinda different from normal C-compiler no?
|
Different compilers have different rules. While all require you to declare the variable before you use it, they differ in how restrictive they are in where you do that. Some compilers will not accept a decalration after any C program statements inside a function while some will let you declare them in any place as long as it is before you use them. From both the standpoint of good practices and to standardize on one way which will work all the time, you should get in the habit of always declaring variables at the front of a function.
- Bruce |
|
|
|
|
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
|