stm32 HAL读取GZP6816D传感器
GZP6816D是一款国产的压力传感器。
读取数据的指令如下:
代码如下。
但在换算中,最后加上去的20,感觉跟实际大气压差了10Kpa,所有,在计算的时候加上去的是30。
void Get_GZP6816D_Data(void)
{
const unsigned char write_address = 0xF0;//(0x78<<1);
const unsigned char read_address = 0xF1;//(0x78<<1)+1;
uint8_t sendbuffer[1] = {0xAC};
uint8_t readBuffer[6] = {0};
HAL_I2C_Master_Transmit(&hi2c1,write_address,sendbuffer,1,0xff);
HAL_Delay(230);//默认 OSR128X,单次测量时间为 203mS
if(HAL_I2C_Master_Receive(&hi2c1,read_address,readBuffer,6,0xff) == HAL_OK)
{
unsigned int pressureTmp = (readBuffer[1]<<16) + (readBuffer[2]<<8) + readBuffer[3];
/*
实际某压力点电桥校准值换算:将 0x9B 0xB0 0xC5 转换为十进制数为 10203333,
本次计算假设校准时使用的量程为 30Kpa-110Kpa,对应的 AD 输出为 1677722~15099494(10%AD~90%AD),
根据输入输出关系公式(pressure= ((PMAX-PMIN)/(DMAX-DMIN)*(Dtest-DMIN)+PMIN))得到:
实际压力值=(110-30)/(15099494-1677722)*(10203333-1677722)+20=70.816 Kpa
*/
//float pressure = (110-30)/(15099494-1677722)*(pressureTmp-1677722)+20;
float pressure = 80.0/13421772*(pressureTmp-1677722)+30;
float pressurePa = pressure*1000;
printf("status=0x%x,pressureTmp = %d,pressure = %fKpa,pressurePa:%f\n",readBuffer[0],pressureTmp,pressure,pressurePa);
/*
温度校准值换算:将 0x56 0xAA 转换为十进制数为 22186,由于读取到的校准数据是以百分比形式表示的,
这个百分比在数值上等于我们换算得到的十进制数与 16bits 无符号数的最大值(65535)之比,
所以在换算百分比时可进行如下计算22186/65536*100%=33.85%
温度的校准范围规定为-40℃~150℃ 所以校准值=(150-(-40))*33.85%—40=24.32℃
*/
unsigned int temperatureTmp = (readBuffer[4]<<8) + readBuffer[5];
float temperaturePercent = temperatureTmp / 65536.0;
float temperature = (150-(-40))*temperaturePercent - 40;
printf("temperatureTmp:%d, percent:%f,temperature: %.2f\n",temperatureTmp, temperaturePercent,temperature);
}
else
{
printf("read data failed.\n");
}
}