float32_t到Uint16_t数组
我在Ti C2000微控制器上遇到了一些问题,这不支持UINT8_T。
我需要在can上发送一个浮点值,而TX缓冲区是长度8的UINT16_T数组,每个值应在UINT8_T的范围内。我通常在支持UINT8_T的微控制器上使用的是直接使用memcpy,但这在这里行不通。
因此,我尝试的是以下内容:
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;
我尝试在UINT16_T数组中首先转换浮子,然后我可以在其中使用Bitshift操作员。但是,这仍然是错误的,并给了我错误的价值观。我还尝试在浮点值上直接使用bitshift,但这给出了编译器错误。
有什么想法,如何做到这一点?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Memcpy期望字节的数量复制。您提供了UINT16_T元素的数量,即两个。尝试以下操作:
memcpy expects the number of bytes to copy. You provided the number of uint16_t elements, which is two. Try this: