AnsweredAssumed Answered

possibly more efficient USART comm?

Question asked by Jon Henry on Jun 5, 2013
Latest reply on Jun 6, 2013 by jj.sprague

I've been working on a project with an stm32f4 and the .NETMF framework. I hit a wall with the garbage collector in .NET hampering my timing abilities and so have erased the chip and gone native. My background is in C# (where memory usage isnt a big issue) and am diving back into C.
Im reading my devices (I2C) fine and am now working on sending the readings out over USART. im using snprintf at the moment, but after some research have found that the function is quite a memory hog. I was taking my values (floats), converting them to chars and building a string with them.

IE  $TEL,-125.44,98.74,54.41,\r\n      --> About 10-12 more values, but you get the picture.

I was using 8 for the size of the snprintf()  function (signed number up to 999.99...-XXX.xx\0   plus the null terminator). So, I was converting a float into 8 bytes or chars in C while losing some resolution in limiting to two decimal places.

While contemplating how to do it better, this hit me. In converting a number to a string or char[] in C, all were doing is taking its actual number value and converting each number to an ascii byte. The USART is still pushing out and taking in 8 or 9 bits at a time for use. Whats to stop us from doing this?

float gyro;   /* Create float of 4 bytes named gyro */
char *pGyro;    /* Create pointer pGyro */
pGyro = gyro;    /* Copy address of gyro into pGyro */
int main(void)
    USART_SendData(USART6, *pGyro++);    /* Send 1st byte or 'char' of float gyro to USART */
    USART_SendData(USART6, *pGyro++);    /* Send 2nd byte or 'char' of float gyro to USART */
    USART_SendData(USART6, *pGyro++);    /* Send 3rd byte or 'char' of float gyro to USART */
    USART_SendData(USART6, *pGyro);    /* Send 4th byte or 'char' of float gyro to USART */

(Its been a long time since Ive dealt with pointers, forgive me if Im using them wrong, but the general concept I think is apparent.)


  • create a float...gyro.
  • Create a pointer.....*pGyro.  
  • Copy the address of gyro to *pGyro
  • Send *pGyro++ to USART to send out 1st 8 bits then increment the address by a ‘char’s worth of bits or a byte and repeat three more times.

If Im thinking about this correctly, this would basically send the 'float value' of gyro over USART while it sits in its memory place without having to use more memory to store its numerical values in characters somewhere. This would also cut transmitting down 4 bytes and keepi the resolution instead of 8 bytes and losing resolution. Wed also lose all the overhead of converting to char using snprintf() or something like that.
The receiving end of this transmission would have to know exactly what is coming type-wise and in what order and require some checks, but Id think this could be done.I know way too little about this microcontroller or C in general to know for sure.


Anyone ever attempt to or actually implement something like this?