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