单片机C语言程序设计STC15F2K60S2-28I-LQFP44单片机控制系统读取BMP280气压传感器的气压值
#include<STC15F2K60S2.h> //头文件
#include<intrins.h>
#define uchar unsigned char //定义单字节无符号整数
#define uint unsigned int //定义单字节无符号整数
//定义BMP280气压传感器地址
#define BMP280_W 0xee //写地址
#define BMP280_R 0xef //读地址
//定义BMP280气压传感器寄存器地址
#define BMP280_DIG_T1 0x88
#define BMP280_DIG_T2 0x8a
#define BMP280_DIG_T3 0x8c
#define BMP280_DIG_P1 0x8e
#define BMP280_DIG_P2 0x90
#define BMP280_DIG_P3 0x92
#define BMP280_DIG_P4 0x94
#define BMP280_DIG_P5 0x96
#define BMP280_DIG_P6 0x98
#define BMP280_DIG_P7 0x9a
#define BMP280_DIG_P8 0x9c
#define BMP280_DIG_P9 0x9e
#define BMP280_CTRL_MEAS 0xf4
#define BMP280_PRESSURE_MSB 0xf7
#define BMP280_STATUS 0xf3
//定义校准系数
uint BMP280_DIG_T1_Value,BMP280_DIG_T2_Value,BMP280_DIG_T3_Value,BMP280_DIG_P1_Value,BMP280_DIG_P2_Value,BMP280_DIG_P3_Value;
uint BMP280_DIG_P4_Value,BMP280_DIG_P5_Value,BMP280_DIG_P6_Value,BMP280_DIG_P7_Value,BMP280_DIG_P8_Value,BMP280_DIG_P9_Value;
//延时函数
void delay(unsigned int time)
{
while(time--);
}
//IIC起始信号函数
void IIC_Start()
{
SDA=1; //拉高数据线
_nop_(); //延时
SCL=1; //拉高时钟线
_nop_(); //延时
SDA=0; //产生下降沿
_nop_(); //延时
SCL=0; //拉低时钟线
_nop_(); //延时
}
//IIC停止信号函数
void IIC_Stop()
{
SDA=0; //拉低数据线
_nop_(); //延时
SCL=1; //拉高时钟线
_nop_(); //延时
SDA=1; //产生上升沿
_nop_(); //延时
}
//IIC发送数据函数
void IIC_SendData(uchar dat)
{
uchar i;
for(i=0;i<8;i++)
{
SDA=dat>>7; //发送数据的最高位
dat<<=1; //数据左移一位
_nop_(); //延时
SCL=1; //拉高时钟线
_nop_(); //延时
SCL=0; //拉低时钟线
}
}
//IIC接收数据函数
uchar IIC_ReadData(uchar ack)
{
uchar i;
uchar dat=0;
SDA=1; //拉高数据线
for(i=0;i<8;i++)
{
dat<<=1; //数据左移一位
SCL=1; //拉高时钟线
_nop_(); //延时
dat|=SDA; //读取数据
SCL=0; //拉低时钟线
_nop_(); //延时
}
if(ack)
{
SDA=0; //应答信号为低电平
}
else
{
SDA=1; //非应答信号为高电平
}
_nop_(); //延时
SCL=1; //拉高时钟线
_nop_(); //延时
SCL=0; //拉低时钟线
_nop_(); //延时
SDA=1; //释放数据线
_nop_(); //延时
return dat; //返回读取到的数据
}
//BMP280气压传感器初始化函数
void BMP280_Init()
{
IIC_Start(); //发送启动信号
IIC_SendData(BMP280_W); //发送BMP280写地址
IIC_SendData(BMP280_CTRL_MEAS); //选择控制测量寄存器
IIC_SendData(0x27); //配置增益和采样率
IIC_Stop(); //停止信号
delay(1000); //延时1秒
IIC_Start(); //发送启动信号
IIC_SendData(BMP280_W); //发送BMP280写地址
IIC_SendData(BMP280_DIG_T1); //选择校准温度寄存器
IIC_Stop(); //停止信号
IIC_Start(); //发送启动信号
IIC_SendData(BMP280_R); //发送BMP280读地址
BMP280_DIG_T1_Value=IIC_ReadData(1); //读取校准温度寄存器的值
BMP280_DIG_T1_Value|=IIC_ReadData(1)<<8;
BMP280_DIG_T2_Value=IIC_ReadData(1); //读取校准温度寄存器的值
BMP280_DIG_T2_Value|=IIC_ReadData(1)<<8;
BMP280_DIG_T3_Value=IIC_ReadData(1); //读取校准温度寄存器的值
BMP280_DIG_T3_Value|=IIC_ReadData(1)<<8;
BMP280_DIG_P1_Value=IIC_ReadData(1); //读取校准气压寄存器的值
BMP280_DIG_P1_Value|=IIC_ReadData(1)<<8;
BMP280_DIG_P2_Value=IIC_ReadData(1); //读取校准气压寄存器的值
BMP280_DIG_P2_Value|=IIC_ReadData(1)<<8;
BMP280_DIG_P3_Value=IIC_ReadData(1); //读取校准气压寄存器的值
BMP280_DIG_P3_Value|=IIC_ReadData(1)<<8;
BMP280_DIG_P4_Value=IIC_ReadData(1); //读取校准气压寄存器的值
BMP280_DIG_P4_Value|=IIC_ReadData(1)<<8;
BMP280_DIG_P5_Value=IIC_ReadData(1); //读取校准气压寄存器的值
BMP280_DIG_P5_Value|=IIC_ReadData(1)<<8;
BMP280_DIG_P6_Value=IIC_ReadData(1); //读取校准气压寄存器的值
BMP280_DIG_P6_Value|=IIC_ReadData(1)<<8;
BMP280_DIG_P7_Value=IIC_ReadData(1); //读取校准气压寄存器的值
BMP280_DIG_P7_Value|=IIC_ReadData(1)<<8;
BMP280_DIG_P8_Value=IIC_ReadData(1); //读取校准气压寄存器的值
BMP280_DIG_P8_Value|=IIC_ReadData(1)<<8;
BMP280_DIG_P9_Value=IIC_ReadData(1); //读取校准气压寄存器的值
BMP280_DIG_P9_Value|=IIC_ReadData(0)<<8;
IIC_Stop(); //停止信号
}
//BMP280气压传感器读取数据函数
uint BMP280_ReadData()
{
uchar MSB,LSB,XLSB;
uint adc_pressure;
IIC_Start(); //发送启动信号
IIC_SendData(BMP280_W); //发送BMP280写地址
IIC_SendData(BMP280_CTRL_MEAS); //选择控制测量寄存器
IIC_SendData(0xf4); //读取BMP280压力数据
IIC_Stop(); //停止信号
delay(10); //延时10ms
IIC_Start(); //发送启动信号
IIC_SendData(BMP280_W); //发送BMP280写地址
IIC_SendData(BMP280_PRESSURE_MSB); //选择压力最高位
IIC_Stop(); //停止信号
IIC_Start(); //发送启动信号
IIC_SendData(BMP280_R); //发送BMP280读地址
MSB=IIC_ReadData(1); //读取压力最高位
LSB=IIC_ReadData(1); //读取压力次高位
XLSB=IIC_ReadData(0); //读取压力最低位
IIC_Stop(); //停止信号
adc_pressure=MSB; //将压力最高位赋值给adc_pressure
adc_pressure=(adc_pressure<<8)|LSB; //将压力次高位左移8位并与adc_pressure或运算
adc_pressure=(adc_pressure<<8)|XLSB; //将压力最低位左移8位并与adc_pressure或运算
adc_pressure>>=4; //将adc_pressure右移4位
return adc_pressure; //返回压力值
}
void main()
{
float pressure;
BMP280_Init(); //BMP280气压传感器初始化
while(1)
{
pressure=0.25*BMP280_ReadData(); //读取BMP280气压传感器数据
printf("Pressure is %.2f
",pressure); //打印压力值
delay(1000); //延时1秒
}
}