|
发表于 2013-7-22 14:28:50
|
显示全部楼层
小乔123 发表于 2013-7-22 08:42
大侠呀 终于等到你了 求源程序。。。
我从开始转换后延时了10MS不过还是没能读出来
#include "MS5611.h"
///////////////////////////////////////
//#define OSR 256 // 0.60 mSec conversion time (1666.67 Hz)
//#define OSR 512 // 1.17 mSec conversion time ( 854.70 Hz)
//#define OSR 1024 // 2.28 mSec conversion time ( 357.14 Hz)
//#define OSR 2048 // 4.54 mSec conversion time ( 220.26 Hz)
#define OSR 4096 // 9.04 mSec conversion time ( 110.62 Hz)
///////////////////////////////////////
uint16_t c1, c2, c3, c4,c5, c6;
int32_t d1;
int32_t d2;
int64_t dT;
void readTemperatureRequestPressure(void)
{
uint8_t data[3];
IICreadBytes2(MS5611_ADDRESS, 0x00, 3, data); // Request temperature read
d2 = ((int32_t)data[0] << 16) | ((int32_t)data[1] << 8) | data[2];
#if (OSR == 256)
IICwriteByte2(MS5611_ADDRESS, 0x40); // Request pressure conversion
#elif (OSR == 512)
IICwriteByte2(MS5611_ADDRESS, 0x42);
#elif (OSR == 1024)
IICwriteByte2(MS5611_ADDRESS, 0x44);
#elif (OSR == 2048)
IICwriteByte2(MS5611_ADDRESS, 0x46);
#elif (OSR == 4096)
IICwriteByte2(MS5611_ADDRESS, 0x48);
#endif
}
void readPressureRequestPressure(void)
{
uint8_t data[3];
IICreadBytes2(MS5611_ADDRESS, 0x00, 3, data); // Request pressure read
d1 = ((int32_t)data[0] << 16) | ((int32_t)data[1] << 8) | data[2];
#if (OSR == 256)
IICwriteByte2(MS5611_ADDRESS, 0x40); // Request pressure conversion
#elif (OSR == 512)
IICwriteByte2(MS5611_ADDRESS, 0x42);
#elif (OSR == 1024)
IICwriteByte2(MS5611_ADDRESS, 0x44);
#elif (OSR == 2048)
IICwriteByte2(MS5611_ADDRESS, 0x46);
#elif (OSR == 4096)
IICwriteByte2(MS5611_ADDRESS, 0x48);
#endif
}
void readPressureRequestTemperature(void)
{
uint8_t data[3];
IICreadBytes2(MS5611_ADDRESS, 0x00, 3, data); // Request pressure read
d1 = ((int32_t)data[0] << 16) | ((int32_t)data[1] << 8) | data[2];
#if (OSR == 256)
IICwriteByte2(MS5611_ADDRESS, 0x50); // Request pressure conversion
#elif (OSR == 512)
IICwriteByte2(MS5611_ADDRESS, 0x52);
#elif (OSR == 1024)
IICwriteByte2(MS5611_ADDRESS, 0x54);
#elif (OSR == 2048)
IICwriteByte2(MS5611_ADDRESS, 0x56);
#elif (OSR == 4096)
IICwriteByte2(MS5611_ADDRESS, 0x58);
#endif
}
///////////////////////////////////////////////////////////////////////////////
// Calculate Temperature
///////////////////////////////////////////////////////////////////////////////
int32_t calculateTemperature(void)
{
int32_t dd;
dT = d2 - ((int32_t)c5 << 8);
dd = 2000 + (int32_t)((dT * c6 ) >> 23);
return dd/10;
}
///////////////////////////////////////////////////////////////////////////////
// Calculate Pressure Altitude
///////////////////////////////////////////////////////////////////////////////
int32_t calculatePressureAltitude(void)
{
int64_t offset;
int32_t pressureAlt;
int64_t sens;
int32_t p;
// int32_t d1Average = 110;
offset = ((uint32_t)c2 << 16) + ((c4 * (int64_t)dT) >> 7);
sens = ((uint32_t)c1 << 15) + ((c3 * (int64_t)dT) >> 8);
p = (((d1 * sens) >> 21) - offset) >> 15;
pressureAlt = (44330.0f * (1.0f - pow((float)p / 101325.0f, 0.190295f)));
return p;
}
void MS5611_init(void)
{
uint8_t data[2];
IICwriteByte2(MS5611_ADDRESS, 0x1E); // Reset Device
delay_us(10);
IICreadBytes2(MS5611_ADDRESS, 0xA2, 2, data); // Read Calibration Data C1
c1 = ((uint16_t)data[0] << 8) | data[1];
IICreadBytes2(MS5611_ADDRESS, 0xA4, 2, data); // Read Calibration Data C2
c2 = ((uint16_t)data[0] << 8) | data[1];
IICreadBytes2(MS5611_ADDRESS, 0xA6, 2, data); // Read Calibration Data C3
c3 = ((uint16_t)data[0] << 8) | data[1];
IICreadBytes2(MS5611_ADDRESS, 0xA8, 2, data); // Read Calibration Data C4
c4 = ((uint16_t)data[0] << 8) | data[1];
IICreadBytes2(MS5611_ADDRESS, 0xAA, 2, data); // Read Calibration Data C5
c5 = ((uint16_t)data[0] << 8) | data[1];
IICreadBytes2(MS5611_ADDRESS, 0xAC, 2, data); // Read Calibration Data C6
c6 = ((uint16_t)data[0] << 8) | data[1];
#if (OSR == 256)
IICwriteByte2(MS5611_ADDRESS, 0x50); // Request temperature conversion
#elif (OSR == 512)
IICwriteByte2(MS5611_ADDRESS, 0x52);
#elif (OSR == 1024)
IICwriteByte2(MS5611_ADDRESS, 0x54);
#elif (OSR == 2048)
IICwriteByte2(MS5611_ADDRESS, 0x56);
#elif (OSR == 4096)
IICwriteByte2(MS5611_ADDRESS, 0x58);
#endif
delay_us(10);
readTemperatureRequestPressure();
} |
|