国民技术
直播中

donatello1996

8年用户 687经验值
擅长:处理器/DSP 控制/MCU RF/无线
私信 关注
[经验]

【国民技术N32项目移植】模拟I2C读取各类传感器,三回啊三回

       I2C是个让人又爱又恨的总线,一方面非常方便,只要上拉电压足够,一个I2C总线可以接多组I2C从机,二来,每个I2C器件对于时序和数据字节的要求又不尽相同,有些器件喜欢延时长一点,有些喜欢延时短一点,有些是8位器件地址+8位寄存器+8位数据,有些是8位器件地址+8位寄存器+16位数据,有些是寄存器不公开,直接是8位器件地址+N*8位数据,整合一个项目里面的所有I2C器件可是非常烦人的事,所以我就简单接个气压传感器BMP280就完事。

  1. uint8_t msb , lsb , xlsb;
  2. float var1 , var2;
  3. bmp280Calib  bmp280Cal;

  4. uint8_t BMP280_Init(void)
  5. {
  6.     uint8_t bmp280_id;
  7.     uint8_t tmp[1];

  8.     I2C_Read_Datas_PB6_PB7(BMP280_SLAVE_ADDRESS_0x77, BMP280_CHIPID_REG, 1, &bmp280_id);

  9.         printf("bmp280_id = 0x%xn" , bmp280_id);

  10.     I2C_Read_Datas_PB6_PB7(BMP280_SLAVE_ADDRESS_0x77, BMP280_DIG_T1_LSB_REG , 24 , (u8 *)&bmp280Cal);
  11.        
  12.         //printf("%d %d %d %d %d %d %d %d %d %d %d %dn" ,
  13.         // bmp280Cal.dig_T1 , bmp280Cal.dig_T2 , bmp280Cal.dig_T3 ,
  14.         // bmp280Cal.dig_P1 , bmp280Cal.dig_P2 , bmp280Cal.dig_P3 ,
  15.         // bmp280Cal.dig_P4 , bmp280Cal.dig_P5 , bmp280Cal.dig_P6 ,
  16.         // bmp280Cal.dig_P7 , bmp280Cal.dig_P8 , bmp280Cal.dig_P9);

  17.     tmp[0] = BMP280_MODE;
  18.     I2C_Write_Reg_Datas_PB6_PB7(BMP280_SLAVE_ADDRESS_0x77 , BMP280_CTRLMEAS_REG , 1 , tmp);

  19.     tmp[0] = 5 << 2;
  20.     I2C_Write_Reg_Datas_PB6_PB7(BMP280_SLAVE_ADDRESS_0x77 , BMP280_CONFIG_REG , 1 , tmp);

  21.     return bmp280_id;
  22. }

  23. float BMP280PressureToAltitude(float* pressure)
  24. {
  25.     if (*pressure > 0)
  26.     {
  27.         return ((pow((1015.7f / (*pressure)), CONST_PF) - 1.0f) * (FIX_TEMP + 273.15f)) / 0.0065f;
  28.     }
  29.     else
  30.     {
  31.         return 0;
  32.     }
  33. }

  34. void BMP280_Read_Pressure_Tempature(float * pres , float * temp)
  35. {
  36.         msb = I2C_Read_Addr_PB6_PB7(BMP280_SLAVE_ADDRESS_0x77 , BMP280_PRESSURE_MSB_REG);
  37.         lsb = I2C_Read_Addr_PB6_PB7(BMP280_SLAVE_ADDRESS_0x77 , BMP280_PRESSURE_LSB_REG);
  38.         xlsb = I2C_Read_Addr_PB6_PB7(BMP280_SLAVE_ADDRESS_0x77 , BMP280_PRESSURE_XLSB_REG);
  39.    

  40.     *pres = (msb * 65536 | lsb * 256 | xlsb) >> 4;

  41.         msb = I2C_Read_Addr_PB6_PB7(BMP280_SLAVE_ADDRESS_0x77 , BMP280_TEMPERATURE_MSB_REG);
  42.         lsb = I2C_Read_Addr_PB6_PB7(BMP280_SLAVE_ADDRESS_0x77 , BMP280_TEMPERATURE_LSB_REG);
  43.         xlsb = I2C_Read_Addr_PB6_PB7(BMP280_SLAVE_ADDRESS_0x77 , BMP280_TEMPERATURE_XLSB_REG);

  44.     *temp = (msb * 65536 | lsb * 256 | xlsb) >> 4;

  45.     var1 = ((*temp) / 16384.0 - bmp280Cal.dig_T1 / 1024.0)*(bmp280Cal.dig_T2);
  46.     var2 = (((*temp) / 131072.0 - bmp280Cal.dig_T1 / 8192.0) *
  47.          ((*temp) / 131072.0 - bmp280Cal.dig_T1 / 8192.0)) * bmp280Cal.dig_T3;
  48.     *temp = var1 + var2;
  49.     *temp /= 5120.0;

  50.     var1 = ((*temp) / 2.0) - 64000.0;
  51.     var2 = var1 * var1*(bmp280Cal.dig_P6) / 32768.0;
  52.     var2 = var2 + var1 * (bmp280Cal.dig_P5)*2.0;
  53.     var2 = (var2 / 4.0) + ((bmp280Cal.dig_P4)*65536.0);
  54.     var1 = (bmp280Cal.dig_P3)*var1*var1 / 524288.0 + (bmp280Cal.dig_P2)*var1 / 524288.0;
  55.     var1 = (1.0 + var1 / 32768.0)*(bmp280Cal.dig_P1);

  56.     *pres = 1048576.0 - (*pres);
  57.     *pres = ((*pres) - (var2 / 4096.0))*6250.0 / var1;
  58.     var1 = (bmp280Cal.dig_P9) * (*pres) * (*pres) / 2147483648.0;
  59.     var2 = (*pres) * (bmp280Cal.dig_P8) / 32768.0;
  60.     *pres = (*pres) + (var1 + var2 + (bmp280Cal.dig_P7)) / 16.0;
  61. }
15.jpg 14.jpg

更多回帖

发帖
×
20
完善资料,
赚取积分