gpt4 book ai didi

c - MCP3021字节处理

转载 作者:行者123 更新时间:2023-11-30 16:52:35 25 4
gpt4 key购买 nike

我使用 MCP3021 作为环境光传感器。我在读取时成功获取字节,但无法正确处理它们。我使用 MPLABX 作为我的 IDE。

我的代码如下,

UINT32 res, val, upper, lower;
UINT8 i2cbyte[10]; // I2C values get stored in this one...

// example values : i2cbyte[0]=0x0e, i2cbyte[1]=0x32
upper = i2cbyte[0];
lower = i2cbyte[1];

val = ( upper << 6) + (lower >> 2);
//after this value is: upper = 0x00000380, lower = 0x01000032 & val is same as lower

不知道为什么变量 lower 中有一个 01。有时值为 0x00cccc32。

res = upper + lower;  // res = 0x0000320E
res = (3.3/1024) * val;

有人可以帮我解决这个问题吗?

最佳答案

How do you check these values? – Some programmer dude

in a debugger.

您不能将调试器显示的值视为理所当然,因为变量只能采用 abstract machine 中出现的值。 。 (优化 C 编译器可能会对代码进行各种重新排列,包括将寄存器中不需要的部分用于其他目的。)您只能依赖 observable behavior .

也就是说,您的程序(您实际上没有显示)也可能确实覆盖了部分变量,例如。 G。读取数据时溢出i2cbyte[10]

关于c - MCP3021字节处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41164454/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com