/*获取转换值,包括温度和压力*/
void GetConversionValue(uint8_tdeviceAddress,float *pPres,float *pTemp,uint16_t *caliPara,uint16_t*semaphore,WriteCommandToMs5837TypeWriteCommandToMs5837,ReadBytesFromMs5837Type ReadBytesFromMs5837)
{
uint16_tsenst1; //C1压力灵敏度
uint16_tofft1; //C2压力补偿值
uint16_ttcs; //C3压力灵敏度温度系数
uint16_ttco; //C4压力补偿温度系数
uint16_ttref; //C5参考温度
uint16_ttempsens; //C6温度传感器温度系数
/*从MS5837的PROM中读取校准数据*/
if(*semaphore>0)
{
GetCalibrationData(deviceAddress,caliPara,WriteCommandToMs5837,ReadBytesFromMs5837);
*semaphore=*semaphore-1;
}
senst1=caliPara[0];
offt1=caliPara[1];
tcs=caliPara[2];
tco=caliPara[3];
tref=caliPara[4];
tempsens=caliPara[5];
uint32_tdigitalPressureValue;
uint32_tdigitalTemperatureValue;
/*读取压力数据*/
digitalPressureValue=ReadConversionFromMs5837(deviceAddress,COMMAND_CONVERTD1OSR4096,WriteCommandToMs5837,ReadBytesFromMs5837);
Delayms(20);
/*读取温度数据*/
digitalTemperatureValue=ReadConversionFromMs5837(deviceAddress,COMMAND_CONVERTD2OSR4096,WriteCommandToMs5837,ReadBytesFromMs5837);
/*对温度进行一阶修正*/
int32_tdT;
int32_ttemp;
dT=digitalTemperatureValue-tref*256;
temp=(int32_t)(2000+dT*tempsens/pow(2,23));
/*对压力进行一阶修正*/
int64_toff;
int64_tsens;
int32_tpres;
off=(int64_t)(offt1*pow(2,17)+(tco*dT)/pow(2,6));
sens=(int64_t)(senst1*pow(2,16)+(tcs*dT)/pow(2,7));
pres=(int32_t)((digitalPressureValue*sens/pow(2,21)-off)/pow(2,15));
/*对温度和压力进行二阶修正*/
int64_tti=0;
int64_t offi=0;
int64_tsensi=0;
int64_toff2=0;
int64_tsens2=0;
if(temp<2000)
{
ti=(int64_t)(11*dT*dT/pow(2,35));
offi=(int64_t)(31*(temp-2000)*(temp-2000)/pow(2,3));
sensi=(int64_t)(63*(temp-2000)*(temp-2000)/pow(2,5));
off2=off-offi;
sens2=sens-sensi;
temp=temp-(int32_t)ti;
pres=(int32_t)((digitalPressureValue*sens2/pow(2,21)-off2)/pow(2,15));
}
if((-4000<=temp)&&(temp<=8500))
{
*pTemp=(float)temp/100.0;
}
if((1000<=pres)&&(pres<=120000))
{
*pPres=(float)pres/100.0;
}
}
最终在STM32的I2C接口实现通讯时,实现2个WriteCommandToMs5837Type(uint8_tdeviceAddress,uint8_t command);和ReadBytesFromMs5837Type(uint8_tdeviceAddress,uint8_t *pData,uint16_t bytesNum);函数并调用就可以了,换做其他的平台也只需要重写这两个函数就能实现通讯了。