/*获取转换值,包括温度和压力*/
void GetConversionValue(uint8_t deviceAddress,float *pPres,float *pTemp,uint16_t *caliPara,uint16_t *semaphore,WriteCommandToMs5837Type WriteCommandToMs5837,ReadBytesFromMs5837Type ReadBytesFromMs5837)
{
uint16_t senst1; //C1压力灵敏度
uint16_t offt1; //C2压力补偿值
uint16_t tcs; //C3压力灵敏度温度系数
uint16_t tco; //C4压力补偿温度系数
uint16_t tref; //C5参考温度
uint16_t tempsens; //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_t digitalPressureValue;
uint32_t digitalTemperatureValue;
/*读取压力数据*/
digitalPressureValue=ReadConversionFromMs5837(deviceAddress,COMMAND_CONVERTD1OSR4096,WriteCommandToMs5837,ReadBytesFromMs5837);
Delayms(20);
/*读取温度数据*/
digitalTemperatureValue=ReadConversionFromMs5837(deviceAddress,COMMAND_CONVERTD2OSR4096,WriteCommandToMs5837,ReadBytesFromMs5837);
/*对温度进行一阶修正*/
int32_t dT;
int32_t temp;
dT=digitalTemperatureValue-tref*256;
temp=(int32_t)(2000+dT*tempsens/pow(2,23));
/*对压力进行一阶修正*/
int64_t off;
int64_t sens;
int32_t pres;
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_t ti=0;
int64_t offi=0;
int64_t sensi=0;
int64_t off2=0;
int64_t sens2=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_t deviceAddress,uint8_t command);和ReadBytesFromMs5837Type(uint8_t deviceAddress,uint8_t *pData,uint16_t bytesNum);函数并调用就可以了,更换平台也只需要重写这两个函数就能实现移植。
3、结果展示
完成上述开发后,编译下载。我们也将数据结果上传到OneNET,具体显示结果如下: