单片机源程序如下:
#include"bmp280.h"
unsigned short xdata dig_t1,dig_p1; //注意数据类型
signed short xdata dig_t2,dig_t3,dig_p2,dig_p3,dig_p4,dig_p5,dig_p6,dig_p7,dig_p8,dig_p9;
long signed int xdata bmp280_ut,bmp280_up;
long signed int xdata t_fine;
void bmpreaddata() //原始压力温度数据突发读取
{
uchar xdata n,temp=0x09,address=0xf7;
long unsigned int xdata readdata0[6]; //注意数据类型
i2cstart();
i2csendbyte(0xec,1);
i2csendbyte(0xf4,1);
i2csendbyte(0x55,1);
i2cstop();
delay10ms(10);
while(temp!=0)
{
temp=0x09;
i2cstart();
i2csendbyte(0xec,1);
i2csendbyte(0xf3,1);
i2cstop();
i2cstart();
i2csendbyte(0xed,1);
temp&=i2creadbyte(1);
i2cstop();
}
i2cstart();
i2csendbyte(0xec,1);
i2csendbyte(address,1);
i2cstop();
i2cstart();
i2csendbyte(0xed,1);
for(n=0;n<6;n++)
{
if(n<5)
{
readdata0[n]=i2creadbyte(0);
}
else
{
readdata0[n]=i2creadbyte(1);
}
}
i2cstop();
bmp280_up=(readdata0[0]<<12)|(readdata0[1]<<4)|(readdata0[2]>>4);
bmp280_ut=(readdata0[3]<<12)|(readdata0[4]<<4)|(readdata0[5]>>4);
}
void bmpreset()
{
i2cstart();
i2csendbyte(0xec,1);
i2csendbyte(0xe0,1);
i2csendbyte(0xb6,1); //软复位
i2cstop();
}
void bmpconfig()
{
i2cstart();
i2csendbyte(0xec,1);
i2csendbyte(0xf5,1);
i2csendbyte(0x00,1); //强制模式无待机时间,关滤波,禁用3线模式
i2cstop();
i2cstart();
i2csendbyte(0xec,1);
i2csendbyte(0xf4,1);
i2csendbyte(0x55,1); //压力16倍,温度2倍过采样,转换时间最大43MS,强制模式
i2cstop();
}
void readtrimming() //补偿参数单字节读取
{
uchar xdata n,address=0x88;
uchar xdata readdata[24];
for(n=0;n<24;n++)
{
i2cstart();
i2csendbyte(0xec,1);
i2csendbyte(address,1);
i2cstop();
i2cstart();
i2csendbyte(0xed,1);
if(n<23)
{
readdata[n]=i2creadbyte(0);
}
else
{
readdata[n]=i2creadbyte(1);
}
i2cstop();
address++;
}
dig_t1=(readdata[1]<<8)|(readdata[0]);
dig_t2=(readdata[3]<<8)|(readdata[2]);
dig_t3=(readdata[5]<<8)|(readdata[4]);
dig_p1=(readdata[7]<<8)|(readdata[6]);
dig_p2=(readdata[9]<<8)|(readdata[8]);
dig_p3=(readdata[11]<<8)|(readdata[10]);
dig_p4=(readdata[13]<<8)|(readdata[12]);
dig_p5=(readdata[15]<<8)|(readdata[14]);
dig_p6=(readdata[17]<<8)|(readdata[16]);
dig_p7=(readdata[19]<<8)|(readdata[18]);
dig_p8=(readdata[21]<<8)|(readdata[20]);
dig_p9=(readdata[23]<<8)|(readdata[22]);
}
uchar bmpid()
{
uchar xdata id;
i2cstart();
i2csendbyte(0xec,1);
i2csendbyte(0xd0,1);
i2cstop();
i2cstart();
i2csendbyte(0xed,1);
id=i2creadbyte(1);
i2cstop();
return id;
}
long signed int tempcal(long signed int adc_t) //c51单片机不支持64位整数,使用简化计算
{
long signed int var1,var2,t;
var1=((((adc_t>>3)-((long signed int)dig_t1<<1)))*((long signed int)dig_t2))>>11;
var2=(((((adc_t>>4)-((long signed int)dig_t1))*((adc_t>>4)-((long signed int)dig_t1)))>>12)*((long signed int)dig_t3))>>14;
t_fine=var1+var2;
t=(t_fine*5+128)>>8;
return t;
}
long unsigned int presscal(long signed int adc_p)
{
long signed int var1,var2;
long unsigned int p;
var1=(((long signed int)t_fine)>>1)-(long signed int)64000;
var2=(((var1>>2)*(var1>>2))>>11)*((long signed int)dig_p6);
var2=var2+((var1*((long signed int)dig_p5))<<1);
var2=(var2>>2)+(((long signed int)dig_p4)<<16);
var1=(((dig_p3*(((var1>>2)*(var1>>2))>>13))>>3)+((((long signed int)dig_p2)*var1)>>1))>>18;
var1=((((32768+var1))*((long signed int)dig_p1))>>15);
if(var1==0)
{
return 0;
}
p=(((long unsigned int)(((long signed int)1048576)-adc_p)-(var2>>12)))*3125;
if(p<0x80000000)
……………………
上一篇:单片机电梯控制程序
下一篇:基于51单片机PWM直流电机调速程序 0到20级的调速