MS5607使用中的问题,温度低于20℃
马后炮:因为是网上找的代码,懒得看手册,导致后面低于20摄氏度还会有问题,只好认真的看了手册,发现我的代码是ms5611的,照着手册重新改写后就木有问题了!!!不能省的地方就别省了。
公司使用MS5607设计压力传感器,在网上找了一段MS5607的代码,平时用着挺好的,可是测试拿到高低温试验箱中测试我的程序,低于20℃后,压力值直接就起飞了,几十万,哈哈哈,惊喜!
下面是原来的代码:
uint16_t Cal_C[7]; //用于存放PROM中的6组数据
uint32_t D1_Pres, D2_Temp; // 存放数字压力和温度
float Pressure; //温度补偿大气压
float dT, Temperature, Temperature2; //实际和参考温度之间的差异,实际温度,中间值
double OFF, SENS; //实际温度抵消,实际温度灵敏度
float Aux, OFF2, SENS2; //温度校验值
uint32_t ex_Pressure; //串口读数转换值
uint8_t exchange_num[8];
char MS56XX_GetTemperature ( uint8_t OSR_Temp )
{
D2_Temp = MS56XX_Do_Conversion ( OSR_Temp );
osDelay ( 10 );
dT = D2_Temp - ( ( ( uint32_t ) Cal_C[5] ) << 8 );
Temperature = 2000 + dT * ( ( float ) Cal_C[6] ) / 8388608; //算出温度值的100倍,2001表示20.01°
return 1;
}
char MS56XX_GetPressure ( uint8_t OSR_Pres )
{
D1_Pres = MS56XX_Do_Conversion ( OSR_Pres );
osDelay ( 10 );
OFF = ( ( int64_t )Cal_C[2] << 17 ) + ( ( int64_t ) Cal_C[4] * dT ) / 64.0;
SENS = ( ( int64_t ) Cal_C[1] << 16 ) + ( ( int64_t ) Cal_C[3] * dT ) / 128.0;
//温度补偿
if ( Temperature < 2000 ) // second order temperature compensation when under 20 degrees C
{
Temperature2 = ( dT * dT ) / 0x80000000;
Aux = ( Temperature - 2000 ) * ( Temperature - 2000 );
OFF2 = 2.5 * Aux;
SENS2 = 1.25 * Aux;
if ( Temperature < -1500 )
{
Aux = ( Temperature + 1500 ) * ( Temperature + 1500 );
OFF2 = OFF2 + 7 * Aux;
SENS2 = SENS + 5.5 * Aux;
}
}
else //(Temperature > 2000)
{
Temperature2 = 0;
OFF2 = 0;
SENS2 = 0;
}
Temperature = Temperature - Temperature2;
OFF = OFF - OFF2;
SENS = SENS - SENS2;
Pressure = ( D1_Pres * SENS / 2097152 - OFF ) / 32768;
Temperature /= 100;
Pressure /= 100;
return 1;
}
现在的问题是,低于20℃后,压力值直接就起飞了。
所以,先看看和温度相关的计算。
dT = D2_Temp - ( ( ( uint32_t ) Cal_C[5] ) << 8 );
D2_Temp和Cal_C[5] )都是无符号数,但是这里用的减法,却没法出现负值,所以要修改一下。
char MS56XX_GetTemperature ( uint8_t OSR_Temp )
{
D2_Temp = MS56XX_Do_Conversion ( OSR_Temp );
osDelay ( 10 );
if(D2_Temp > ( ( ( uint32_t ) Cal_C[5] ) << 8 ))
{
dT = D2_Temp - ( ( ( uint32_t ) Cal_C[5] ) << 8 );
}
else
{
dT = ( ( ( uint32_t ) Cal_C[5] ) << 8 ) - D2_Temp;
dT *= -1;
}
Temperature = 2000 + dT * ( ( float ) Cal_C[6] ) / 8388608; //算出温度值的100倍,2001表示20.01°
return 1;
}
恩,问题就这么解决了。
以上是ms5611的,我贴出来Ms5607的,其实就是改动了计算公式:
/************************************************************
* 函数名:MS56XX_GetTemperature
* 描述 : 读取数字温度
* 输入 :过采样率
* 输出 :无
*/
char MS56XX_GetTemperature ( uint8_t OSR_Temp )
{
D2_Temp = MS56XX_Do_Conversion ( OSR_Temp );
if(D2_Temp == 0)
return 0;
osDelay ( 10 );
if(D2_Temp > ( ( ( uint32_t ) Cal_C[5] ) << 8 ))
dT = D2_Temp - ( ( ( uint32_t ) Cal_C[5] ) << 8 );
else
{
dT = ( ( ( uint32_t ) Cal_C[5] ) << 8 ) - D2_Temp;
dT *= -1;
}
Temperature = 2000 + ( dT * Cal_C[6] ) / (0x00000001 << 23); //算出温度值的100倍,2001表示20.01°
return 1;
}
/************************************************************
* 函数名:MS56XX_GetPressure
* 描述 : 读取数字气压
* 输入 :过采样率
* 输出 :无
*/
char MS56XX_GetPressure ( uint8_t OSR_Pres )
{
D1_Pres = MS56XX_Do_Conversion ( OSR_Pres );
if(D1_Pres == 0)
return 0;
osDelay ( 10 );
OFF = ( ( int64_t )Cal_C[2] * (0x00000001 << 17) ) + ( ( int64_t ) Cal_C[4] * dT ) / 64.0;
SENS = ( ( int64_t ) Cal_C[1] * (0x00000001 << 16) ) + ( ( int64_t ) Cal_C[3] * dT ) / 128.0;
//温度补偿
if ( Temperature < 2000 ) // second order temperature compensation when under 20 degrees C
{
T2 = ( dT * dT ) / (( uint64_t )0x0000000001 << 31);
Aux = ( Temperature - 2000 ) * ( Temperature - 2000 );
OFF2 = 61 * Aux / 16;
SENS2 = 2 * Aux;
if ( Temperature < -1500 )
{
Aux = ( Temperature + 1500 ) * ( Temperature + 1500 );
OFF2 = OFF2 + 15 * Aux;
SENS2 = SENS2 + 8 * Aux;
}
}
else //(Temperature > 2000)
{
T2 = 0;
OFF2 = 0;
SENS2 = 0;
}
Temperature = Temperature - T2;
OFF = OFF - OFF2;
SENS = SENS - SENS2;
//P = D1 * SENS - OFF = (D1 * SENS / 2 21 - OFF) / 2 15
Pressure = ( D1_Pres * SENS / (0x00000001 << 21) - OFF ) / (0x00000001 << 15);
Temperature /= 100;
Pressure /= 100;
return 1;
}