#include "hp203b.h" #include "i2c.h" #include "pdebug.h" #include "assertions.h" #include "filter.h" #include "cmsis_os.h" uint8_t hp20X_SOFT_RST = 0x06; //软复位 uint8_t hp20X_READ_P = 0x30; //压力 uint8_t hp20X_READ_A = 0x31; //高度 uint8_t hp20X_READ_T = 0x32; //温度 uint8_t hp20X_READ_PT = 0x10; //温度压力 uint8_t hp20X_READ_AT = 0x11; //温度海拔 /**************************** *名称:HP203_soft_reset *功能:对hp203发送软复位指令 *参数:无 *返回:无 *****************************/ void HP203_soft_reset(void) { HAL_I2C_Master_Transmit(&hi2c3, HP20X_ADDRESSCMD, &hp20X_SOFT_RST, 1, 0xffff);//0xec,0x06----进行软件复位 osDelay(30); //延时20MS thread误差+10 } /**************************** *名称:Hp203bReadPressureTemperature *功能:获取气压、海拔、温度数据 *参数: Press--气压值 * Altitude--海拔 * Temp--温度 *返回:无 *****************************/ void Hp203bReadPressureTemperature(double *Hp203bPressure,double *Hp203bAltitude,double *Hp203bTemp) { uint8_t Osr_Cfg = HP20X_CONVERT_OSR1024; //0x08 uint8_t DataBuf[6]; long Hp203b_Temp; long Hp203b_Pressure; long Hp203b_Altitude; uint8_t status = 0xFF; uint8_t command = HP20X_WR_CONVERT_CMD | Osr_Cfg; status = HAL_I2C_Master_Transmit(&hi2c3, HP20X_ADDRESSCMD, &command, 1, 0xffff); while(status != HAL_OK){} // if(HAL_I2C_Master_Transmit(&hi2c3, HP20X_ADDRESSCMD, &command, 1, 0xffff) == HAL_OK) //0x40 | 0x08 = 0x48----010: fixed; 010: OSR = 1024; 00: sensor pressure and temperature channel; // { osDelay(74); //延时64MS 具体可以根据OSR值调整 thread误差+10 HAL_I2C_Master_Transmit(&hi2c3, HP20X_ADDRESSCMD, &hp20X_READ_PT, 1, 0xffff); //命令:0x10----读取温度和压力数据 HAL_I2C_Master_Receive(&hi2c3, HP20X_ADDRESSCMD, DataBuf, 6, 0xffff); //读取6个字节数据:3个字节的温度数据和3个字节的压力数据 Hp203b_Temp = DataBuf[0]; //将3个字节的温度数据整合成24位的温度数据 Hp203b_Temp <<= 8; Hp203b_Temp |= DataBuf[1]; Hp203b_Temp <<= 8; Hp203b_Temp |= DataBuf[2]; if(Hp203b_Temp > 0x7FFFFF) //判断是否负值 Hp203b_Temp = Hp203b_Temp - 16777216; *Hp203bTemp = (double)Hp203b_Temp / 100; //计算温度数据 Hp203b_Pressure = DataBuf[3]; //将3个字节的压力数据整合成24位的压力数据 Hp203b_Pressure <<= 8; Hp203b_Pressure |= DataBuf[4]; Hp203b_Pressure <<= 8; Hp203b_Pressure |= DataBuf[5]; *Hp203bPressure = (double)Hp203b_Pressure / 100; //计算压力数据 HAL_I2C_Master_Transmit(&hi2c3, HP20X_ADDRESSCMD, &hp20X_READ_A, 1, 0xffff); //命令:0x31----读取高度数据 HAL_I2C_Master_Receive(&hi2c3, HP20X_ADDRESSCMD, DataBuf, 3, 0xffff); //读取3个字节数据:3个字节的高度数据 Hp203b_Altitude = DataBuf[0]; //将3个字节的高度数据整合成24位的高度数据 Hp203b_Altitude <<= 8; Hp203b_Altitude |= DataBuf[1]; Hp203b_Altitude <<= 8; Hp203b_Altitude |= DataBuf[2]; if(Hp203b_Altitude > 0x7FFFFF) //判断是否负值 Hp203b_Altitude = Hp203b_Altitude - 16777216; *Hp203bAltitude = (double)Hp203b_Altitude / 100; //计算高度数据 // } }