float32_t到Uint16_t数组

发布于 2025-02-10 17:54:21 字数 684 浏览 1 评论 0原文

我在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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

滴情不沾 2025-02-17 17:54:21

Memcpy期望字节的数量复制。您提供了UINT16_T元素的数量,即两个。尝试以下操作:

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];

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];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文