I have some problems on a TI C2000 microcontroller, which does not support uint8_t.
I need to send a float value over CAN and the tx buffer is a uint16_t array of length 8, where each value should be in the range of a uint8_t. What I normally do on a microcontroller supporting uint8_t is using directly memcpy, but here this does not work.
So what I tried is the following:
canComVars.txMsgData[0] = addr;
uint16_t tmp[2];
memcpy(&tmp[0], &data_f , sizeof(float32_t)/sizeof(uint16_t)); //data_f --> float32_t
canComVars.txMsgData[1] = (tmp[1]>>8) & 0x00FF;
canComVars.txMsgData[2] = (tmp[1]) & 0x00FF;
canComVars.txMsgData[3] = (tmp[0]>>8) & 0x00FF;
canComVars.txMsgData[4] = (tmp[0]) & 0x00FF;
I try to convert the float first in a uint16_t array, where I then can use the bitshift operator. But somehow this is still wrong and gives me wrong values. I also tried to directly use the bitshift on the float value, but this gives a compiler error.
Any ideas, how this can be done?
memcpy expects the number of bytes to copy. You provided the number of uint16_t elements, which is two. Try this:
float32_t data_f=3.14;
unsigned char tmp[4];
memcpy(tmp, &data_f, 4);
canComVars.txMsgData[1] = tmp[3];
canComVars.txMsgData[2] = tmp[2];
canComVars.txMsgData[3] = tmp[1];
canComVars.txMsgData[4] = tmp[0];