首页 | 新闻 | 新品 | 文库 | 方案 | 视频 | 下载 | 商城 | 开发板 | 数据中心 | 座谈新版 | 培训 | 工具 | 博客 | 论坛 | 百科 | GEC | 活动 | 主题月 | 电子展
返回列表 回复 发帖

STM32F4的I2C读取BMP085模块的温度和气压(3)

STM32F4的I2C读取BMP085模块的温度和气压(3)

void temp_calibration(void)
{

uint8_t buf[2];
  I2C_Read(Open_I2C,ADDR_24LC02,0xaa,buf, 2);

ac1 = buf[0] << 8 |buf[1];
  I2C_Read(Open_I2C,ADDR_24LC02,0xac,buf, 2);
  ac2 = buf[0] << 8 |buf[1];
  I2C_Read(Open_I2C,ADDR_24LC02,0xae,buf, 2);
  ac3 = buf[0] << 8 |buf[1];
  I2C_Read(Open_I2C,ADDR_24LC02,0xb0,buf, 2);
  ac4 = buf[0] << 8 |buf[1];
  I2C_Read(Open_I2C,ADDR_24LC02,0xb2,buf, 2);
  ac5 = buf[0] << 8 |buf[1];
  I2C_Read(Open_I2C,ADDR_24LC02,0xb4,buf, 2);
  ac6 = buf[0] << 8 |buf[1];
  I2C_Read(Open_I2C,ADDR_24LC02,0xb6,buf, 2);
  b1 = buf[0] << 8 |buf[1];
  I2C_Read(Open_I2C,ADDR_24LC02,0xb8,buf, 2);
  b2 = buf[0] << 8 |buf[1];
  I2C_Read(Open_I2C,ADDR_24LC02,0xba,buf, 2);
  mb = buf[0] << 8 |buf[1];
  I2C_Read(Open_I2C,ADDR_24LC02,0xbc,buf, 2);
  mc = buf[0] << 8 |buf[1];
  I2C_Read(Open_I2C,ADDR_24LC02,0xbe,buf, 2);
  md = buf[0] << 8 |buf[1];
  printf("ac1=%d,ac2=%d,ac3=%d,ac4=%d,ac5=%d,ac6=%d,b1=%d,b2=%d,mb=%d,mc=%d,md=%d\r\n",ac1,ac2,ac3,ac4,ac5,ac6,b1,b2,mb,mc,md);
}


int main(void)
{



long x1,x2,x3,b3,b5,b6,b7,press_reg,pressure,temp_reg,temp;
  unsigned long b4;
  int ret,i;
  uint8_t ReadBuffer[10];

//unsigned short ;
  //unsigned int ;
  char oss = 0;  //这个值在读气压时可以置进寄存器

//GPIO_Configuration();

USART_Configuration();

I2C_Configuration();



printf("\r\n****************************************************************\r\n");

while (1)

{


printf("\r\n Read start \r\n");
  temp_calibration();
  
  //read uncompensated temperature
  I2C_WriteOneByte(Open_I2C,ADDR_24LC02,0xf4,0x2e);
  I2C_delay(200000);//delay 4.5ms
  ret = I2C_Read(Open_I2C,ADDR_24LC02,0xf6,ReadBuffer,2);
  temp_reg = ReadBuffer[0] << 8 | ReadBuffer[1];
  printf("temp_reg %d \r\n",temp_reg);
  
  //calculate true temperature
  x1 = ((temp_reg - ac6) * ac5) >> 15;
  x2 = (mc << 11) / (x1 + md);
  b5 = x1 + x2;
  temp = (b5 + 8) >> 4;
  printf("x1:%d, x2:%d, b5:%d, temp(*0.1):%d \r\n",x1,x2,b5,temp);
  
  //read uncompensated pressure
  I2C_WriteOneByte(Open_I2C,ADDR_24LC02,0xf4,(0x3e + oss<<6));
  I2C_delay(200000);//delay 4.5ms
  ret = I2C_Read(Open_I2C,ADDR_24LC02,0xf6,ReadBuffer,3);
  press_reg = ((ReadBuffer[0] << 16) | (ReadBuffer[1] << 8) | ReadBuffer[2]) >> (8 - oss);
  printf("press_reg %d \r\n",press_reg);
  //下面计算公式要注意括号的使用,模棱两可的都给它用上,一开始我觉得有些地方应该可以不用,计算出的气压结果却是错的,后来加上括号就对了
  b6 = b5 - 4000;
  printf("b6 %ld \r\n",b6);
  x1 = (b2 * (b6 * b6 )>> 12) >> 11;
  printf("x1 %ld \r\n",x1);
  x2 = (ac2 * b6) >> 11;
  printf("x2 %ld \r\n",x2);
  x3 = x1 + x2;
  printf("x3 %ld \r\n",x3);
  b3 = ((((long)ac1 * 4 + x3) << oss) + 2) / 4;
  printf("b3 %ld \r\n",b3);
  x1 = (ac3 * b6) >> 13;
  printf("x1 %ld \r\n",x1);
  x2 = (b1 * (b6 * b6) >> 12) >> 16;
  printf("x2 %ld \r\n",x2);
  x3 = ((x1 + x2 )+ 2) >> 2;
  printf("x3 %ld \r\n",x3);
  b4 = (ac4 * (unsigned long)(x3 + 32768)) >> 15;
  printf("b4 %ld \r\n",b4);
  b7 = ((unsigned long)press_reg - b3) * (50000 >> oss);
  printf("b7 %ld \r\n",b7);
  if(b7 < 0x80000000)
  {
    pressure = (b7 * 2) / b4;
  }
  else
  {
    pressure = (b7 / b4) * 2;
  }
  printf("pressure %ld \r\n",pressure);
  x1 = (pressure >> 8) * (pressure >> 8);
  printf("x1 %ld \r\n",x1);
  x1 = (x1 * 3038) >> 16;
  printf("x1 %ld \r\n",x1);
  x2 = (-7357 * pressure) >> 16;
  printf("x2 %ld \r\n",x2);
  pressure = pressure + ((x1 + x2 + 3791) >> 4);
  printf("pressure %ld \r\n",pressure);
  
  for(i=0; i<200*3; i++)
  I2C_delay(200000);//delay 4.5ms

}
}

终端看到的打印:
Read start
ac1=8361,ac2=-816,ac3=-14464,ac4=33064,ac5=25477,ac6=25325,b1=5498,b2=38,mb=-32768,mc=-11075,md=2432
temp_reg 33455
x1:6321, x2:-2591, b5:3730, temp(*0.1):233
press_reg 33455
b6 -270
x1 0
x2 107
x3 107
b3 8388
x1 476
x2 1
x3 119
b4 33184
b7 1253350000
pressure 75539
x1 87025
x1 4034
x2 -8480
pressure 75498

温度233×0.1=23.3度
气压为75498Pa
继承事业,薪火相传
返回列表