0

I need to send floats from Simulink to C (embedded in a MCU) via UART.

I have found code that works for sending floats in the opposite direction, but I need to fully understand it to write code for receiving a float.

This is the original code:

 unsigned char *chptr;
 chptr = (unsigned char *) &floatvalue;
 Tx(*chptr++);Tx(*chptr++);Tx(*chptr++);Tx(*chptr);

This is the my altered code (that works):

float testFloat = 3.1416;
unsigned char *chptr;                           
chptr = (unsigned char *) &testFloat;           
ROM_UARTCharPut(UART0_BASE,*chptr++);           
ROM_UARTCharPut(UART0_BASE,*chptr++);
ROM_UARTCharPut(UART0_BASE,*chptr++);
ROM_UARTCharPut(UART0_BASE,*chptr);

I think I understand the gist of what is going on but some things I am not sure about:

Float is declared which is 4 bytes long.

float testFloat = 3.1416;

A pointer is declared which is 1 byte long

unsigned char *chptr;    

The address of the float is cast into the pointer. Because of the difference in bit length of the pointer and the char I am assuming that only the address of bits 0 to 7 of the float are cast into the pointer (little endianness)

chptr = (unsigned char *) &testFloat;    

The next four lines is where my understanding breaks down.

ROM_UARTCharPut(UART0_BASE,*chptr++);           
ROM_UARTCharPut(UART0_BASE,*chptr++);
ROM_UARTCharPut(UART0_BASE,*chptr++);
ROM_UARTCharPut(UART0_BASE,*chptr);

I understand that "*chptr" is the value of the variable the pointer is pointing to. I also understand that "*chptr++" increments the address of the pointer to the next byte. However the order does not make sense to me.

If I was to label a 4 byte float as:

byte4 byte3 byte2 byte1

It seems to me like the first send line sending *chptr++ would send byte2 not byte1

next line would send byte3,

next line byte4

and the last line a byte of a neighbouring variable or byte1.

However it does work properly on the receiving end (Simulink set to receive in little-endiann) so my understanding must be wrong.

Thank you for any clarification.

PS: once I understand this method, would it work for receiving floats? Or am I barking up the wrong tree?

Thank you

Remi M.
  • 3
  • 2
  • Tip: Often printing the value of the `float` with `"%a"` and the value of the bytes with `"%x"` is informative and helps understanding. – chux - Reinstate Monica Feb 15 '19 at 04:32
  • 1
    You're aware that `chptr++` is [post-increment](https://stackoverflow.com/questions/4445706/post-increment-and-pre-increment-concept)? – Nate Eldredge Feb 15 '19 at 04:33
  • Yes, this method would work for rx and tx. what the code is doing with the post-increment is saying, Send the byte from this address, with this derefenced value, then increase the address of the pointer so the next write has the "current" value to send – Bwebb Feb 15 '19 at 05:00

1 Answers1

0

As the comments on your question state, you're using post-increment. So your code is accessing the value at the given memory location, doing what it needs to do with it and THEN moving the pointer to the next memory location, ready for the next line of code.

If you'd been using pre-increment, then you're statement "*It seems to me like the first send line sending chptr++ would send byte2 not byte1" would be true. If you want to test this, run the code in debug mode and change *chptr++ to *++chptr.

This post has a good explanation of incrementing operations.

Ben
  • 427
  • 5
  • 17