为什么我与HEX枚举的比较失败了?

时间:2017-04-14 14:39:33

标签: c++ arduino

我看到一个奇怪的问题,比较存储在枚举中的HEX值和读取的寄存器值,我已将CHIPID定义如下

enum {
  BME_280_1_CHIPID = 0x60,
  BME_280_2_CHIPID = 0x58,
};

我的驱动程序中有一段代码片段,其中检查了CHIP ID

  uint8_t id = read8(BME280_REGISTER_CHIPID);
  if ( ( id != BME_280_2_CHIPID)  || (id != BME_280_1_CHIPID) ){

    #ifdef DEBUG
        uint8_t value = read8(BME280_REGISTER_CHIPID);
        debugPrint("BME280 DEBUG: read BME280_CHIPID ");
        debugPrint(value,HEX);
        debugPrint( " expected ");
        debugPrint(BME_280_1_CHIPID,HEX);
      debugPrint( " or ");
      debugPrintLn(BME_280_2_CHIPID,HEX);
    #endif
        return false;
  }

我的read8()函数是

uint8_t Adafruit_BME280::read8(byte reg)
{
  uint8_t value;
    Wire1.beginTransmission((uint8_t)_i2caddr);
    Wire1.write((uint8_t)reg);
    Wire1.endTransmission();
    Wire1.requestFrom((uint8_t)_i2caddr, (byte)1);
    value = Wire1.read();

  return value;
}

但是,调试显示

BME280 DEBUG: read BME280_CHIPID 58 expected 60 or 58
BME280 MISSING!

发生了什么事?

提前致谢,

问候!

1 个答案:

答案 0 :(得分:1)

Andriano Repetti的解决方案

  uint8_t id = read8(BME280_REGISTER_CHIPID);
  if ( ( id != BME_280_2_CHIPID)  && (id != BME_280_1_CHIPID) ){

    #ifdef DEBUG
        uint8_t value = read8(BME280_REGISTER_CHIPID);
        debugPrint("BME280 DEBUG: read BME280_CHIPID ");
        debugPrint(value,HEX);
        debugPrint( " expected ");
        debugPrint(BME_280_1_CHIPID,HEX);
      debugPrint( " or ");
      debugPrintLn(BME_280_2_CHIPID,HEX);
    #endif
        return false;
  }