[mw_shl_code=c,true]u16 Cal_C[7]; //用于存放PROM中的8组数据
u32 D1,D2,OFF,SENS,dT,TEMP,Pressure;
float TEMP2;
static void delay_5us(void)
{
u16 x;
for(x = 0; x < 84; x++)
{
__NOP();
__NOP();
__NOP();
__NOP();
__NOP();
}
}
void MS5611_conversion(void)
{
MS5611_CS = 0;
delay_5us();
spiSwap(MS5611_CD1_256);
delay_ms(2);
MS5611_CS = 1;
}
//从PROM读取出厂校准数据
u16 MS5611_PROM_READ(void)
{
u8 i;
u32 C1[7] = {0xA0,0xA2,0xA4,0xA6,0xA8,0xAA,0xAC};
MS5611_CS = 0;
delay_5us();
for(i=0;i<=6;i++)
{
spiSwap(C1);
delay_ms(1);
Cal_C = spiSwap(C1);
Cal_C <<= 8;
Cal_C |= spiSwap(C1);
delay_ms(1);
MS5611_conversion();
MS5611_CS = 0;
}
MS5611_CS = 1;
return Cal_C[6];
}
//读取数字温度
u32 MS5611_GetTemperature(void)
{
MS5611_conversion();
MS5611_CS = 0;
delay_5us();
spiSwap(MS5611_ADC);
delay_ms(5);
D2 = spiSwap(0x50);
D2 <<= 8;
D2 |= spiSwap(0x50);
D2 <<=8;
D2 |= spiSwap(0x50);
dT = D2-(((u32)Cal_C[5])<<8);
TEMP = 2000+dT*((u32)Cal_C[6])/8388608;
MS5611_CS = 1;
return(D2);
}
//读取数字气压
u32 MS5611_GetPressure(void)
{
double Aux,OFF2,SENS2;
MS5611_conversion();
MS5611_CS = 0;
delay_5us();
spiSwap(MS5611_ADC);
delay_ms(5);
D1 = spiSwap(0x40);
D1 <<= 8;
D1 |= spiSwap(0x40);
D1 <<= 8;
D1 |= spiSwap(0x40);
delay_ms(10);
OFF = (u32)(Cal_C[2]<<16)+((u32)Cal_C[4]*dT)/128.0;
SENS =(u32)(Cal_C[1]<<15)+((u32)Cal_C[3]*dT)/256.0;
if(TEMP <2000) //低于20摄氏度时的二阶温度补偿
{
TEMP2 = (dT*dT)/0x80000000;
Aux = (TEMP-2000)*(TEMP-2000);
OFF2 = 2.5*Aux;
SENS2 = 1.25*Aux;
if(TEMP2 <-1500)
{
Aux = (TEMP2+1500)*(TEMP2+1500);
OFF2 = OFF2 + 7*Aux;
SENS2 = SENS + 5.5*Aux;
}
}else //(TEMP >2000)
{
TEMP2 = 0;
OFF2 = 0;
SENS2 = 0;
}
TEMP = TEMP - TEMP2;
OFF = OFF - OFF2;
SENS = SENS - SENS2;
MS5611_CS = 1;
Pressure=(D1*SENS/2097152.0-OFF)/32768.0;
return(D1);
}
[/mw_shl_code] |