我想知道在这个捕获器LSM6DSO32上读取温度的最佳方法是什么,这里是寄存器


我正在和爱特梅尔SAMD21 M0公司合作做Arduino。在这里,这就是我要做的
//reading register
Wire.beginTransmission(DSO_ADDRESS);
Wire.write(0x20);
Wire.endTransmission();
Wire.requestFrom(DSO_ADDRESS, 14);
//get bytes, 14 beceause (temp,gyro,accel)
uint8_t buff[14];
Wire.readBytes(buff, 14);
int16_t raw_t = buff[1] << 8 | buff[0];
float temperature = (raw_t / 256.0) + 25.0;我的问题是,你认为这种方法可以得到负的原始值吗?例如,当温度为22°时,原始值应为-700平均值。在分配raw_t之前,我是否需要转换或转换捕获者提供的内容?
发布于 2021-03-30 12:48:11
我的问题是,你认为这种方法可以得到负的原始值吗?
下面的代码可能“工作”,但依赖于实现定义的行为。(如果int为16位,则为UB )
int16_t raw_t = buff[1] << 8 | buff[0];总是起作用的替代方案。
uint16_t uraw = ((uint16_t) buff[1]) << 8 | buff[0];
int16_t raw = (uraw & 0x8000u) ? uraw - 65536 : uraw;而不是double math和分配给float,也使用float数学。
// float temperature = (raw_t / 256.0) + 25.0;
float temperature = (raw_t / 256.0f) + 25.0f;在四舍五入中可能稍微正确一些。
float temperature = (raw_t + (int32_t)25*256) / 256.0f;https://stackoverflow.com/questions/66870596
复制相似问题