How to send a float via UART on XMEGA?

Go To Last Post
6 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hey All,

A little help would be good.

I'm trying to modify some application note code to send a float with no luck.

Here's the original code, how would you modify it?

/*! \brief Put data (5-8 bit character).
 *
 *  Stores data byte in TX software buffer and enables DRE interrupt if there
 *  is free space in the TX software buffer.
 *
 *  \param usart_data The USART_data_t struct instance.
 *  \param data       The data to send.
 */
bool USART_TXBuffer_PutByte(USART_data_t * usart_data, uint8_t data)
{
	uint8_t tempCTRLA;
	uint8_t tempTX_Head;
	bool TXBuffer_FreeSpace;
	USART_Buffer_t * TXbufPtr;

	TXbufPtr = &usart_data->buffer;
	TXBuffer_FreeSpace = USART_TXBuffer_FreeSpace(usart_data);


	if(TXBuffer_FreeSpace)
	{
	  	tempTX_Head = TXbufPtr->TX_Head;
	  	TXbufPtr->TX[tempTX_Head]= data;
		/* Advance buffer head. */
		TXbufPtr->TX_Head = (tempTX_Head + 1) & USART_TX_BUFFER_MASK;

		/* Enable DRE interrupt. */
		tempCTRLA = usart_data->usart->CTRLA;
		tempCTRLA = (tempCTRLA & ~USART_DREINTLVL_gm) | usart_data->dreIntLevel;
		usart_data->usart->CTRLA = tempCTRLA;
	}
	return TXBuffer_FreeSpace;
}
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

You mean you want to change the function to put a(float) in the TX buffer instead of (uint8_t)?

-Paul

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Yes, The data sheet says

Quote:
Supports Serial Frames with 5, 6, 7, 8, or 9 Data Bits and 1 or 2 Stop Bits

And so I'm just not sure how to go about it.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

IMHO you use a serial connection to a terminal in order to communicate with ASCII characters.

So I would send "12345.67890" as a string rather than send binary values. For a modest amount of traffic, sending 10 or 12 characters instead of the four 8bit chars to make a 32bit float has many advantages. Mostly in that it is understood by ALL terminals and by most humans.

void USART_TXBuffer_PutString(USART_data_t * usart_data, uint8_t *string)
{
     while (*string) {
          USART_TXBuffer_PutByte(usart_data, *string++)
     }
}
...
     // replace # with %
     sprintf(buf, "#f", float_expression);
     USART_TXBuffer_PutString(&USARTC0, buf);
...

David.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

A float is stored as 4 bytes you just need to send the 4 bytes indevidualy and of course the reciving end needs to put them together again.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thanks all!