气压计(BMP180)压力测量问题

时间:2021-01-18 16:42:11

标签: arduino stm32 sensors altitude

我有一个气压计 (BMP180) 并且正在将它与 aSTM32 连接。我必须读取应该有英尺精度的高度。

在连接气压计时,我得到了一致的真实温度值,但压力在 59400 Pa(Pa - 根据博世数据表)的范围内。数据表中给出的例子也有 69964 Pa 的压力。

我已经通过使用基于软件的计数器处理了气压计采样数据所需的延迟。另外,我用可用的库检查了我的传感器,一切都一致。但由于一些学术原因,我无法使用图书馆,因此我需要帮助测量压力。

这是我的 STM32 代码:

//calibration params from the sensor
static int16_t AC1 = 8353; 
static int16_t AC2 = -1041;  
static int16_t AC3 = -14621; 
static uint16_t AC4 = 33880; 
static uint16_t AC5 = 25158; 
static uint16_t AC6 = 18608; 
static int16_t B1 = 6515; 
static int16_t B2 = 35; 
static int16_t MB = -32768; 
static int16_t MC = -11786; 
static int16_t MD = 2724;

static int TMPCNT = 0, PRSCNT=0;    //loop counters
static double temperature, pressure;
static uint8_t oss = 3;             // bmp high resolution 
static long UT, X1, X2, B5, UP;

int main() {
  while (1) {
    if (TMPCNT == 2) {  //2.7*2 > 5ms so sample temp every 5 ms 
      MSR_TEMP();       //measure for previous request
      RQST_TEMP();      // request again
      TMPCNT = 0;
    }
    TMPCNT++;       
                
    if (PRSCNT == 10) { //sample pressure every 27ms (25ms wait time for UH resolution)
      MSR_PRESS();
      RQST_PRESS();
      PRSCNT = 0;
    }
    PRSCNT++;

    /* some more code which takes 2.7 milliseconds */
  }
}

void RQST_TEMP() {
  i2c_transmit(BMP, 0xF4, 0x2E);   // transmit 0x2E to 0xF4 reg of BMP addr
}

void MSR_TEMP() {
  char buff[2];
  requestData(BMP, 0xF6, buff, 2); //request 2 bytes from BMP addr, reg 0xF6, and put it in the buff
  UT = (buff[0] << 8 | buff[1]);
  X1 = (long)((UT - AC6) * AC5 * pow(2, -15));
  X2 = (long)(MC * pow(2, 11) / (X1 + MD));
  B5 = X1 + X2;
  temperature = ((B5 + 8) * pow(2, -4)) / 10.0;
}

void RQST_PRESS(){
  i2c_transmit(BMP, 0xF4, (char)(0x34 + (oss << 6)));   
}

void MSR_PRESS() {
  long B6,x1,x2,x3,B3,p;
  unsigned long B4,B7;  
  char buff[3];

  requestData(BMP, 0xF6, buff, 3);
  UP = ((buff[0] << 16) + (buff[1] << 8) + buff[2]) >> (8-oss);
  B6 = B5 - 4000;
  x1 = (long)((B2 * (B6 * B6 * pow(2, -12))) * pow(2, -11));
  x2 = (long)(AC2 * B6 * pow(2, -11));
  x3 = x1 + x2;
  B3 = ((((long)AC1 * 4 + x3) << oss) + 2) / 4;
  x1 = AC3 * B6 * pow(2, -13);
  x2 = (B1 * (B6 * B6 * pow(2, -12))) * pow(2, -16);
  x3 = ((x1 + x2) + 2) * pow(2, -2);
  B4 = AC4 * (unsigned long)(x3 + 32768) * pow(2, -15);
  B7 = ((unsigned long)UP - B3) * (50000 >> oss);
  if (B7 < 0x80000000) {
    p = (B7 * 2) / B4;
  } else {
    p = (B7 / B4) * 2;
  }
  x1 = (p * pow(2, -8)) * (p * pow(2, -8));
  x1 = (x1 * 3038) * pow(2, -16);
  x2 = (-7357 * p) * pow(2, -16);
  pressure = p + ((x1 + x2 + 3791) * pow(2, -4)); // pressure is in Pa as per data sheet
  // SerialPrint("%lf\n",pressure);
}

编写的代码基于数据表中提到的算法。这是datasheet。我不知道我做错了什么。我的位置应该有 101209Pa 的压力,这与我得到的压力相差甚远。我的 I2C 驱动程序功能也正常工作。所以我无法弄清楚问题到底出在哪里。我一定会感谢您的帮助。

0 个答案:

没有答案