Cypress Perform

Home > Design Support > Cypress Developer CommunityTM > Cypress Forums > PSoC® 3 > Writing a float value over UART

Bookmark and Share
Cypress Developer CommunityTM
Forums | Videos | Blogs | Training | Rewards Program | Community Components



Writing a float value over UART
Moderator:
RKRM

Post Reply
Follow this topic



Writing a float value over UART

RealChemist posted on 20 Apr 2012 7:36 PM PST
Member
9 Forum Posts

 

char *k="This is a test\r\n";
UART_1_Start();
UART_1_PutString(k);

 

I am able to send over strings from the psoc over UART but  I cannot figure out how to send float values over the uart.

I also tried to send integers by importing stdlib.h using the itoa function, but I get an error stating undefined reference to `itoa'.

Any snippets of code or a link to the right information would be appreciated. Thanks

 




Re: Writing a float value over UART

RealChemist posted on 20 Apr 2012 07:40 PM PST
Member
9 Forum Posts

  for(;;){
range = RangeRead();
/* Check the UART status */
ch = UART_1_GetChar();

if(ch == (unsigned int) 'a'){
setPin();
UART_1_PutString(k);

}else if(ch == (unsigned int) 'b'){
turnServoLeft();

}else if(ch == (unsigned int) 'c'){
turnServoRight();

}else if(ch == (unsigned int) 's'){
//UART_1_PutString();

}
}

 

Also, how can I read a string or a word at a time, instead of receiving a character at a time? Sorry if this is too much for one post.



Re: Writing a float value over UART

Eilrem posted on 21 Apr 2012 10:52 PM PST
Cypress Employee
17 Forum Posts

You can use sprinf() to convert float to char string. Sample code can be found here: http://www.cypress.com/?id=4&rID=39791



Re: Writing a float value over UART

Bob Marlowe posted on 21 Apr 2012 02:15 AM PST
Top Contributor
1768 Forum Posts

UARTs are often thought of to handle ASCII-characters only and so are used in a man-machine-conversation. But there is no constraint to stick to that rule, any uint8 may be sent (and received) via UART as long as you know what you are expecting.

A 4-byte float can be thougt of as 4 individual bytes which can be transmitted and on the receiver side put together to form a float again. Commonly used for that is the unit-declaration in C which makes it possible to access the same memory-area as a float or as an array of bytes:

union {

     float f;

    unsigned char ch[]4];

    } C_Float;

The benefit of do-it-yourself is SPEED. The number of characters sent via UART decreases and the CPU-Load for converting from one format to the other (and back) tends against zero. printf and scanf can use a lot of code due to their universality.

The drwawback? Well, sprintf is written easier than the call to the home-brewed conversion.

 

Happy coding

Bob



Re: Writing a float value over UART

Eilrem posted on 21 Apr 2012 03:45 AM PST
Cypress Employee
17 Forum Posts

Transmitting the binary data of a float is certainly faster. However, you have to go directly to the low level implementation since using a high level software interface might cause it to misinterpret certain binary data. A lot of characters with ASCII values below 32 have special meanings when transmitted via serial protocol.

 

 






ALL CONTENT AND MATERIALS ON THIS SITE ARE PROVIDED "AS IS". CYPRESS SEMICONDUCTOR AND ITS RESPECTIVE SUPPLIERS MAKE NO REPRESENTATIONS ABOUT THE SUITABILITY OF THESE MATERIALS FOR ANY PURPOSE AND DISCLAIM ALL WARRANTIES AND CONDITIONS WITH REGARD TO THESE MATERIALS, INCLUDING BUT NOT LIMITED TO, ALL IMPLIED WARRANTIES AND CONDITIONS OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT OF ANY THIRD PARTY INTELLECTUAL PROPERTY RIGHT. NO LICENSE, EITHER EXPRESS OR IMPLIED, BY ESTOPPEL OR OTHERWISE, IS GRANTED BY CYPRESS SEMICONDUCTOR. USE OF THE INFORMATION ON THIS SITE MAY REQUIRE A LICENSE FROM A THIRD PARTY, OR A LICENSE FROM CYPRESS SEMICONDUCTOR.

Content on this site may contain or be subject to specific guidelines or limitations on use. All postings and use of the content on this site are subject to the Terms and Conditions of the site; third parties using this content agree to abide by any limitations or guidelines and to comply with the Terms and Conditions of this site. Cypress Semiconductor and its suppliers reserve the right to make corrections, deletions, modifications, enhancements, improvements and other changes to the content and materials, its products, programs and services at any time or to move or discontinue any content, products, programs, or services without notice.

Spec No: None; Sunset Owner: GRAA; Secondary Owner: RAIK; Sunset Date: 01/01/20