本帖最后由 1530908765 于 2015-12-28 17:23 编辑
BMP280的SPI驱动程序终于写好了。网上只有I2C接口的程序,而且也是不怎么行的。我的系统是MSP430采集温度传感器BMP280(没有用其中的气压检测功能)。
void bmp280_init(void)
{
unsigned char chipID;
//#define BMP280_CHIPID_REG 0xD0 /*Chip ID Register */
//#define BMP280_RESET_REG 0xE0 /*Softreset Register */
//#define BMP280_STATUS_REG 0xF3 /*Status Register */
//#define BMP280_CTRLMEAS_REG 0xF4 /*Ctrl Measure Register */
//#define BMP280_CONFIG_REG 0xF5 /*Configuration Register */
chipID = bme280_read_reg (BMP280_CHIPID_REG); //读取ID
if(chipID==0x58)
{
LED_KEY1_ON;
}
bme280_write_reg (BMP280_RESET_REG,0xB6); //复位寄存器
bme280_write_reg (BMP280_CTRLMEAS_REG,0xe3); //设置测量精度设置111 111 11:温度20bit 气压20bit 正常模式
bme280_write_reg (BMP280_CONFIG_REG,0xF4); //设置待机时间 滤波 3线4线设置111 111 11:温度20bit 气压20bit 正常模式
chipID = bme280_read_reg (BMP280_CTRLMEAS_REG);
if(chipID==0xE3)
{
LED_KEY2_ON;
}
}
void bmp280_read(void)
{
unsigned char x,y,z;
dig_t1 = ((unsigned short )bme280_read_reg(BMP280_DIG_T1_MSB_REG)<<8)
+((unsigned short )bme280_read_reg(BMP280_DIG_T1_LSB_REG));
dig_t2 = ((unsigned short )bme280_read_reg(BMP280_DIG_T2_MSB_REG)<<8)
+((unsigned short )bme280_read_reg(BMP280_DIG_T2_LSB_REG));
dig_t3 = ((unsigned short )bme280_read_reg(BMP280_DIG_T3_MSB_REG)<<8)
+((unsigned short )bme280_read_reg(BMP280_DIG_T3_LSB_REG));
x = bme280_read_reg(BMP280_TEMPERATURE_MSB_REG);
y = bme280_read_reg(BMP280_TEMPERATURE_LSB_REG);
z = bme280_read_reg(BMP280_TEMPERATURE_XLSB_REG);
UT = (((unsigned long )x<<16)+((unsigned short )y<<8)+z)>>4;//20bit
delay_ms(10);
// dig_t1 = 27504;
// dig_t2 = 26435;
// dig_t3 = -1000;
// UT = 519888;
var1 = (((double)UT)/16384.0 - ((double)dig_t1)/1024.0) * ((double)dig_t2);
var2 = ((((double)UT)/131072.0 - ((double)dig_t1)/8192.0) * (((double)UT)/131072.0 - ((double) dig_t1)/8192.0)) * ((double)dig_t3);
// t_fine = (unsigned short)(var1 + var2);
T = (var1 + var2) / 5120.0;
// T = 0;
}
|