Keil Logo

C51: TRANSMITTING FLOATING-POINT NUMBERS


Information in this article applies to:

  • C51 Version 5.50

QUESTION

How can I send and receive floating-point numbers via the on-chip UART? I have tried using a printf/scanf combination, but I cannot get it to work.

ANSWER

A floating-point number is stored in memory using four bytes. A good way to transmit the number is to simply send those four bytes, using a pointer to access the bytes.

Here is an example:

float f = 4.7838322;

void main(void)
{
  unsigned char *ptr, i;

  ptr = (unsigned char *)&f;
  for (i = 0; i < sizeof(float); transmit(*(ptr + i)), i++);
}

where the function transmit is your custom function that transmits an unsigned char using the on-chip UART or whatever method/peripheral you choose.

To receive the floating-point number simply reverse the process, again using a pointer.

MORE INFORMATION

SEE ALSO

Last Reviewed: Friday, July 15, 2005


Did this article provide the answer you needed?
 
Yes
No
Not Sure
 
  Arm logo
Important information

This site uses cookies to store information on your computer. By continuing to use our site, you consent to our cookies.

Change Settings

Privacy Policy Update

Arm’s Privacy Policy has been updated. By continuing to use our site, you consent to Arm’s Privacy Policy. Please review our Privacy Policy to learn more about our collection, use and transfers
of your data.