çI2C芯片读取MCP9800突然开始出现问题 [英] C i2c chip reading MCP9800 suddenly starts failing

查看:315
本文介绍了çI2C芯片读取MCP9800突然开始出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些code:

#define AMB_LSB 0.0625

void Ambient::read()
{
    uint32_t raw; 
    float filtered;

    uint8_t bytes = 2;
    uint8_t buf[bytes];

    if(i2c_smbus_read_i2c_block_data(i2c_bus_address, A_TEMP_REG, bytes, buf) < 0)
        printf("AMB Block Read Failed\n");

    uint32_t va = buf[0];
    uint32_t vb = buf[1];

    uint32_t result = ((va<<8)+vb);

    // 12-bit code
    raw = result >> 4; 

    filtered = filter.execfilter( raw );
    temperature = filtered * AMB_LSB;  << CALCULATION 

    printf("AMB buffers %d %d -> result %d -> raw %d -> filtered %d -> amb C %f\n",va, vb, result, raw, filtered, temperature);
}

有code,以通过I2C从MCP9800读取信息。对不起,包括它,但也许它有什么用它做。

It is code to read information from a MCP9800 via i2c. Sorry for including it but maybe it has something to do with it.

该功能非常适用于十几周期,直到突然它开始有不正确的值。但随着一些奇怪的变化。

The function works well for about a dozen cycles until all of a sudden it starts having incorrect values. But with some strange variances.

1 如果计算如下

temperature = filtered * AMB_LSB;

我得到这样的输出:

I get this output:

temperature = ((float) filtered * (float) AMB_LSB);

工作

AMB buffers 28 240 -> result 7408 -> raw 463 -> filtered 463 -> amb C 28.937500 
AMB buffers 28 240 -> result 7408 -> raw 463 -> filtered 463 -> amb C 28.937500 
AMB buffers 28 240 -> result 7408 -> raw 463 -> filtered 1024 -> amb C 64.000000 

失败

AMB buffers 29 0 -> result 7424 -> raw 464 -> filtered -**2147483648** -> amb C -134217728.000000 
AMB buffers 29 0 -> result 7424 -> raw 464 -> filtered **2147483647** -> amb C 134217728.000000
AMB buffers 29 0 -> result 7424 -> raw 464 -> filtered -**2147483648** -> amb C -134217728.000000 

因此​​,一旦它开始出现问题,我可以看到滤波值的输出不正确。

So once it starts failing, I can see the output of the filtered value is incorrect.

2 如果计算是:

temperature = raw * AMB_LSB;

使过滤在所有未使用,则输出是这样的:

So that filtered is not used at all, the output is this:

工作

AMB buffers 29 48 -> result 7472 -> raw 467 -> filtered 0 -> amb C 29.187500 
AMB buffers 29 48 -> result 7472 -> raw 467 -> filtered 2147483647 -> amb C 29.187500
AMB buffers 29 64 -> result 7488 -> raw 468 -> filtered 468 -> amb C 29.250000 

失败

AMB buffers **255 130** -> result **65410** -> raw **4088** -> filtered 2147483647 -> amb C 255.500000 
AMB buffers **255 130** -> result **65410** -> raw **4088** -> filtered -2147483648 -> amb C 255.500000 
AMB buffers **255 130** -> result **65410** -> raw **4088** -> filtered 2147483647 -> amb C 255.500000 

查看asterixed号码是不正确的输出。出于某种原因,当你不使用过滤后的值的其他数字刚开始是不正确呢!一路回字节我脱下了I2C芯片。

See the asterixed numbers for output that is incorrect. For some reason when you don't use the filtered values the other numbers just start being incorrect too! All the way back to the bytes I pulled off the i2c chip.

所以刚开始我看到的,也许是过滤不能正常工作。但要删除它也似乎使不正确的原始未过滤的价值。所有execFilter()正在做的是一些平均为prevent大的随机变化。

So at first I see that maybe filtered is not working correctly. But to remove it also appears to make the raw unfiltered value incorrect as well. All execFilter() is doing is some averaging to prevent large random changes.

另外,我也做了通过所使用的i2cget CLI脚本,它是在其返回的值非常一致的。有没有崩溃或意外的值。

Also, I also made up a script via the CLI that used i2cget and it was very consistant in its returned values. There was no crashing or unexpected values.

为什么会变成这样是这样吗?

Why would this be happening?

推荐答案

听起来像一个硬件问题。许多你的价值观是0x7FFFFFFF的,例如SDA线被上拉电阻控制。如果在SDA线路噪音造成虚假的I2C停止条件就会发生这种情况 - 该设备将立即三态输出,你会得到什么,但高位为转移的其余部分。

Sounds like a hardware problem. Many of your values are 0x7FFFFFFF, e.g. the SDA line is being controlled by the pullup resistor. This would happen if noise on the SDA line caused a spurious I2C stop condition -- the device would immediately tristate its output, and you would get nothing but high bits for the rest of the transfer.

RC低通滤波器建议在SCL和SDA放缓边和阻止高频噪声,帮助prevent这样的通信错误。

RC lowpass filters are suggested on the SCL and SDA lines to slow down edges and block high frequency noise, helping to prevent such communication errors.

这篇关于çI2C芯片读取MCP9800突然开始出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆