BMP180气压传感器与STC89c51单片机
首先我想说这个东西是3.3v供电的,STC89c51好像电压太高了
嗯,首先非常感谢你能给出宝贵建议,这个东西我前两天已经做出来了
小编能分享一下代码吗?万分感谢
#include <reg52.H>
#include <math.h> //Keil library
#include <stdlib.h> //Keil library
#include <stdio.h> //Keil library
#include <INTRINS.H> //Keil library
#define uchar unsigned char
#define uint unsigned int
#define DataPort P0 //LCD1602数据端口
sbit SCL=P1^0; //IIC时钟引脚定义
sbit SDA=P1^1; //IIC数据引脚定义
sbit rs=P2^2; //LCD1602命令端口
sbit rw=P2^1; //LCD1602命令端口
sbit ep=P2^0; //LCD1602命令端口
uchar code table3[]="℃";
uchar code table1[]="温度";
uchar code table0[]="气压";
uint i,j,k;
#define BMP085_SlaveAddress 0xee //定义器件在IIC总线中的从地址
#define OSS 0 // Oversampling Setting (note: code is not set up to use other OSS values)
typedef unsigned char BYTE;
typedef unsigned short WORD;
uchar ge,shi,bai,qian,wan,shiwan; //显示变量
int dis_data; //变量
short ac1;
short ac2;
short ac3;
unsigned short ac4;
unsigned short ac5;
unsigned short ac6;
short b1;
short b2;
short mb;
short mc;
short md;
void delay(unsigned int k);
//void InitLcd(); //初始化lcd1602
void WriteDataLCM(uchar dataW);
void WriteCommandLCM(uchar CMD,uchar Attribc);
void DisplayOneChar(uchar X,uchar Y,uchar DData);
void conversion(long temp_data);
void Single_Write(uchar SlaveAddress,uchar REG_Address,uchar REG_data); //单个写入数据
uchar Single_Read(uchar REG_Address); //单个读取内部寄存器数据
void Multiple_Read(uchar,uchar); //连续的读取内部寄存器数据
//------------------------------------
void Delay5us();
void Delay5ms();
void BMP085_Start();
void BMP085_Stop();
void BMP085_SendACK(bit ack);
bit BMP085_RecvACK();
void BMP085_SendByte(BYTE dat);
BYTE BMP085_RecvByte();
void BMP085_ReadPage();
void BMP085_WritePage();
//-----------------------------------
//*********************************************************
void conversion(long temp_data)
{
shiwan=temp_data/100000+0x30 ;
temp_data=temp_data%100000; //取余运算
wan=temp_data/10000+0x30 ;
temp_data=temp_data%10000; //取余运算
qian=temp_data/1000+0x30 ;
temp_data=temp_data%1000; //取余运算
bai=temp_data/100+0x30 ;
temp_data=temp_data%100; //取余运算
shi=temp_data/10+0x30 ;
temp_data=temp_data%10; //取余运算
ge=temp_data+0x30;
}
/*******************************/
void delay(unsigned int k)
{
unsigned int i,j;
for(i=0;i<k;i++)
{
for(j=0;j<121;j++)
{;}}
}
/*******************************/
/*void WaitForEnable(void)
{
DataPort=0xff;
LCM_RS=0;LCM_RW=1;_nop_();
LCM_EN=1;_nop_();_nop_();
while(DataPort&0x80);
LCM_EN=0;
}*/
/*******************************/
void write_cmd(uchar CMD)// 写指令
{
//while(lcd_bz()) ;
rs = 0 ;
rw = 0 ;
ep = 0 ;
P0 = CMD ;
delay(1);
ep = 1 ;
delay(1);
ep = 0 ;
}
/*void WriteCommandLCM(uchar CMD,uchar Attribc)
{
if(Attribc)WaitForEnable();
LCM_RS=0;LCM_RW=0;_nop_();
DataPort=CMD;_nop_();
LCM_EN=1;_nop_();_nop_();LCM_EN=0;
} */
/*******************************/
void write_byte(uchar dataW)//写字节
{
// while(lcd_bz()) ;
rs = 1 ;
rw = 0 ;
ep = 0 ;
P0 = dataW ;
delay(1);
ep = 1 ;
delay(1);
ep = 0 ;
}
/*void WriteDataLCM(uchar dataW)
{
WaitForEnable();
LCM_RS=1;LCM_RW=0;_nop_();
DataPort=dataW;_nop_();
LCM_EN=1;_nop_();_nop_();LCM_EN=0;
} */
/***********************************/
void lcd_init()// 初始化
{
//write_cmd(0x38) ;
write_cmd(0x30);
delay(5);
// write_cmd(0x08) ;
write_cmd(0x0c) ;
delay(5);
// write_cmd(0x01) ;
// delay(5);
write_cmd(0x06) ;
delay(5);
//write_cmd(0x0c) ;
//write_cmd(0x80) ;
//delay(1);
}
/*void InitLcd()
{
WriteCommandLCM(0x38,1);
WriteCommandLCM(0x08,1);
WriteCommandLCM(0x01,1);
WriteCommandLCM(0x06,1);
WriteCommandLCM(0x0c,1);
}*/
/***********************************/
/*void DisplayOneChar(uchar X,uchar Y,uchar DData)
{
Y&=1;
X&=15;
if(Y)X|=0x40;
X|=0x80;
WriteCommandLCM(X,0);
WriteDataLCM(DData);
}*/
/**************************************
延时5微秒(STC90C52RC@12M)
不同的工作环境,需要调整此函数,注意时钟过快时需要修改
当改用1T的MCU时,请调整此延时函数
**************************************/
void Delay5us()
{
_nop_();_nop_();_nop_();_nop_();
_nop_();_nop_();_nop_();_nop_();
_nop_();_nop_();_nop_();_nop_();
_nop_();_nop_();_nop_();_nop_();
}
/**************************************
延时5毫秒(STC90C52RC@12M)
不同的工作环境,需要调整此函数
当改用1T的MCU时,请调整此延时函数
**************************************/
void Delay5ms()
{
WORD n = 560;
while (n--);
}
/**************************************
起始信号
**************************************/
void BMP085_Start()
{
SDA = 1; //拉高数据线
SCL = 1; //拉高时钟线
Delay5us(); //延时
SDA = 0; //产生下降沿
Delay5us(); //延时
SCL = 0; //拉低时钟线
}
/**************************************
停止信号
**************************************/
void BMP085_Stop()
{
SDA = 0; //拉低数据线
SCL = 1; //拉高时钟线
Delay5us(); //延时
SDA = 1; //产生上升沿
Delay5us(); //延时
}
/**************************************
发送应答信号
入口参数:ack (0:ACK 1:NAK)
**************************************/
void BMP085_SendACK(bit ack)
{
SDA = ack; //写应答信号
SCL = 1; //拉高时钟线
Delay5us(); //延时
SCL = 0; //拉低时钟线
Delay5us(); //延时
}
/**************************************
接收应答信号
**************************************/
bit BMP085_RecvACK()
{
SCL = 1; //拉高时钟线
Delay5us(); //延时
CY = SDA; //读应答信号
SCL = 0; //拉低时钟线
Delay5us(); //延时
return CY;
}
/**************************************
向IIC总线发送一个字节数据
**************************************/
void BMP085_SendByte(BYTE dat)
{
BYTE i;
for (i=0; i<8; i++) //8位计数器
{
dat <<= 1; //移出数据的最高位
SDA = CY; //送数据口
SCL = 1; //拉高时钟线
Delay5us(); //延时
SCL = 0; //拉低时钟线
Delay5us(); //延时
}
BMP085_RecvACK();
}
/**************************************
从IIC总线接收一个字节数据
**************************************/
BYTE BMP085_RecvByte()
{
BYTE i;
BYTE dat = 0;
SDA = 1; //使能内部上拉,准备读取数据,
for (i=0; i<8; i++) //8位计数器
{
dat <<= 1;
SCL = 1; //拉高时钟线
Delay5us(); //延时
dat |= SDA; //读数据
SCL = 0; //拉低时钟线
Delay5us(); //延时
}
return dat;
}
/*
//单字节写入BMP085内部数据*******************************
void Single_Write(uchar SlaveAddress,uchar REG_Address,uchar REG_data)
{
BMP085_Start(); //起始信号
BMP085_SendByte(SlaveAddress); //发送设备地址+写信号
BMP085_SendByte(REG_Address); //内部寄存器地址
BMP085_SendByte(REG_data); //内部寄存器数据
BMP085_Stop(); //发送停止信号
}
*/
/*
//单字节读取BMP085内部数据********************************
uchar Single_Read(uchar REG_Address)
{ uchar REG_data;
BMP085_Start(); //起始信号
BMP085_SendByte(BMP085_SlaveAddress); //发送设备地址+写信号
BMP085_SendByte(REG_Address); //发送存储单元地址
BMP085_Start(); //起始信号
BMP085_SendByte(BMP085_SlaveAddress+1); //发送设备地址+读信号
REG_data=BMP085_RecvByte(); //读出寄存器数据
BMP085_SendACK(1);
BMP085_Stop(); //停止信号
return REG_data;
}
*/
//*********************************************************
//读出BMP085内部数据,连续两个
//*********************************************************
short Multiple_read(uchar ST_Address)
{
uchar msb, lsb;
short _data;
BMP085_Start(); //起始信号
BMP085_SendByte(BMP085_SlaveAddress); //发送设备地址+写信号
BMP085_SendByte(ST_Address); //发送存储单元地址
BMP085_Start(); //起始信号
BMP085_SendByte(BMP085_SlaveAddress+1); //发送设备地址+读信号
msb = BMP085_RecvByte(); //BUF[0]存储
BMP085_SendACK(0); //回应ACK
lsb = BMP085_RecvByte();
BMP085_SendACK(1); //最后一个数据需要回NOACK
BMP085_Stop(); //停止信号
Delay5ms();
_data = msb << 8;
_data |= lsb;
return _data;
}
//********************************************************************
long bmp085ReadTemp(void)
{
BMP085_Start(); //起始信号
BMP085_SendByte(BMP085_SlaveAddress); //发送设备地址+写信号
BMP085_SendByte(0xF4); // write register address
BMP085_SendByte(0x2E); // write register data for temp
BMP085_Stop(); //发送停止信号
delay(10); // max time is 4.5ms
return (long) Multiple_read(0xF6);
}
//*************************************************************
long bmp085ReadPressure(void)
{
long pressure = 0;
BMP085_Start(); //起始信号
BMP085_SendByte(BMP085_SlaveAddress); //发送设备地址+写信号
BMP085_SendByte(0xF4); // write register address
BMP085_SendByte(0x34); // write register data for pressure
BMP085_Stop(); //发送停止信号
delay(10); // max time is 4.5ms
pressure = Multiple_read(0xF6);
pressure &= 0x0000FFFF;
return pressure;
//return (long) bmp085ReadShort(0xF6);
}
//**************************************************************
//初始化BMP085,根据需要请参考pdf进行修改**************
void Init_BMP085()
{
ac1 = Multiple_read(0xAA);
ac2 = Multiple_read(0xAC);
ac3 = Multiple_read(0xAE);
ac4 = Multiple_read(0xB0);
ac5 = Multiple_read(0xB2);
ac6 = Multiple_read(0xB4);
b1 = Multiple_read(0xB6);
b2 = Multiple_read(0xB8);
mb = Multiple_read(0xBA);
mc = Multiple_read(0xBC);
md = Multiple_read(0xBE);
}
//***********************************************************************
void bmp085Convert()
{
long ut;
long up;
long x1, x2, b5, b6, x3, b3, p;
unsigned long b4, b7;
long temperature;
long pressure;
ut = bmp085ReadTemp();
ut = bmp085ReadTemp(); // 读取温度
up = bmp085ReadPressure();
up = bmp085ReadPressure(); // 读取压强
x1 = ((long)ut - ac6) * ac5 >> 15;
x2 = ((long) mc << 11) / (x1 + md);
b5 = x1 + x2;
temperature = (b5 + 8) >> 4;
//*************
conversion(temperature);
write_cmd(0x80);
// for(j=0;table2[j]!='\0';j++)
// {
// write_byte(table2[j]);
// delay(5);
// }
for(j=0;table1[j]!='\0';j++)
{
write_byte(table1[j]);
delay(5);
}
write_byte(':');
write_byte(bai);
write_byte(shi);
write_byte('.');
write_byte(ge);
write_byte(' ');
for(k=0;table3[k]!='\0';k++)
{
write_byte(table3[k]);
delay(5);
}
//write_byte(0XDF);
//write_byte('C');
/* DisplayOneChar(4,0,'T'); //温度显示
DisplayOneChar(5,0,':');
DisplayOneChar(7,0,);
DisplayOneChar(8,0,);
DisplayOneChar(9,0,'.');
DisplayOneChar(10,0,);
DisplayOneChar(11,0,); //温度单位
DisplayOneChar(12,0,); */
//*************
b6 = b5 - 4000;
x1 = (b2 * (b6 * b6 >> 12)) >> 11;
x2 = ac2 * b6 >> 11;
x3 = x1 + x2;
b3 = (((long)ac1 * 4 + x3) + 2)/4;
x1 = ac3 * b6 >> 13;
x2 = (b1 * (b6 * b6 >> 12)) >> 16;
x3 = ((x1 + x2) + 2) >> 2;
b4 = (ac4 * (unsigned long) (x3 + 32768)) >> 15;
b7 = ((unsigned long) up - b3) * (50000 >> OSS);
if( b7 < 0x80000000)
p = (b7 * 2) / b4 ;
else
p = (b7 / b4) * 2;
x1 = (p >> 8) * (p >> 8);
x1 = (x1 * 3038) >> 16;
x2 = (-7357 * p) >> 16;
pressure = p + ((x1 + x2 + 3791) >> 4);
conversion(pressure);
/* DisplayOneChar(4,1,'P'); //显示压强
DisplayOneChar(5,1,);
DisplayOneChar(6,1,shiwan);
DisplayOneChar(7,1,);
DisplayOneChar(8,1,);
DisplayOneChar(9,1,);
DisplayOneChar(10,1,);
DisplayOneChar(11,1,);
DisplayOneChar(12,1,); //气压单位
DisplayOneChar(13,1,);
DisplayOneChar(14,1,); */
write_cmd(0x90);
for(i=0;table0!='\0';i++)
{
write_byte(table0);
delay(5);
}
write_byte(':');
write_byte(shiwan);
write_byte(wan);
write_byte(qian);
write_byte('.');
write_byte(bai);
write_byte(shi);
write_byte('K');
write_byte('p');
write_byte('a');
}
//*********************************************************
//******主程序********
//*********************************************************
void main()
{
delay(50); //上电延时
// InitLcd();
lcd_init(); //液晶初始化
Init_BMP085(); //初始化BMP085
while(1) //循环
{
bmp085Convert();
delay(1000);
}
}
#include <reg52.H>
#include <math.h> //Keil library
#include <stdlib.h> //Keil library
#include <stdio.h> //Keil library
#include <INTRINS.H> //Keil library
#define uchar unsigned char
#define uint unsigned int
#define DataPort P0 //LCD1602数据端口
sbit SCL=P1^0; //IIC时钟引脚定义
sbit SDA=P1^1; //IIC数据引脚定义
sbit rs=P2^2; //LCD1602命令端口
sbit rw=P2^1; //LCD1602命令端口
sbit ep=P2^0; //LCD1602命令端口
uchar code table3[]="℃";
uchar code table1[]="温度";
uchar code table0[]="气压";
uint i,j,k;
#define BMP085_SlaveAddress 0xee //定义器件在IIC总线中的从地址
#define OSS 0 // Oversampling Setting (note: code is not set up to use other OSS values)
typedef unsigned char BYTE;
typedef unsigned short WORD;
uchar ge,shi,bai,qian,wan,shiwan; //显示变量
int dis_data; //变量
short ac1;
short ac2;
short ac3;
unsigned short ac4;
unsigned short ac5;
unsigned short ac6;
short b1;
short b2;
short mb;
short mc;
short md;
void delay(unsigned int k);
//void InitLcd(); //初始化lcd1602
void WriteDataLCM(uchar dataW);
void WriteCommandLCM(uchar CMD,uchar Attribc);
void DisplayOneChar(uchar X,uchar Y,uchar DData);
void conversion(long temp_data);
void Single_Write(uchar SlaveAddress,uchar REG_Address,uchar REG_data); //单个写入数据
uchar Single_Read(uchar REG_Address); //单个读取内部寄存器数据
void Multiple_Read(uchar,uchar); //连续的读取内部寄存器数据
//------------------------------------
void Delay5us();
void Delay5ms();
void BMP085_Start();
void BMP085_Stop();
void BMP085_SendACK(bit ack);
bit BMP085_RecvACK();
void BMP085_SendByte(BYTE dat);
BYTE BMP085_RecvByte();
void BMP085_ReadPage();
void BMP085_WritePage();
//-----------------------------------
//*********************************************************
void conversion(long temp_data)
{
shiwan=temp_data/100000+0x30 ;
temp_data=temp_data%100000; //取余运算
wan=temp_data/10000+0x30 ;
temp_data=temp_data%10000; //取余运算
qian=temp_data/1000+0x30 ;
temp_data=temp_data%1000; //取余运算
bai=temp_data/100+0x30 ;
temp_data=temp_data%100; //取余运算
shi=temp_data/10+0x30 ;
temp_data=temp_data%10; //取余运算
ge=temp_data+0x30;
}
/*******************************/
void delay(unsigned int k)
{
unsigned int i,j;
for(i=0;i<k;i++)
{
for(j=0;j<121;j++)
{;}}
}
/*******************************/
/*void WaitForEnable(void)
{
DataPort=0xff;
LCM_RS=0;LCM_RW=1;_nop_();
LCM_EN=1;_nop_();_nop_();
while(DataPort&0x80);
LCM_EN=0;
}*/
/*******************************/
void write_cmd(uchar CMD)// 写指令
{
//while(lcd_bz()) ;
rs = 0 ;
rw = 0 ;
ep = 0 ;
P0 = CMD ;
delay(1);
ep = 1 ;
delay(1);
ep = 0 ;
}
/*void WriteCommandLCM(uchar CMD,uchar Attribc)
{
if(Attribc)WaitForEnable();
LCM_RS=0;LCM_RW=0;_nop_();
DataPort=CMD;_nop_();
LCM_EN=1;_nop_();_nop_();LCM_EN=0;
} */
/*******************************/
void write_byte(uchar dataW)//写字节
{
// while(lcd_bz()) ;
rs = 1 ;
rw = 0 ;
ep = 0 ;
P0 = dataW ;
delay(1);
ep = 1 ;
delay(1);
ep = 0 ;
}
/*void WriteDataLCM(uchar dataW)
{
WaitForEnable();
LCM_RS=1;LCM_RW=0;_nop_();
DataPort=dataW;_nop_();
LCM_EN=1;_nop_();_nop_();LCM_EN=0;
} */
/***********************************/
void lcd_init()// 初始化
{
//write_cmd(0x38) ;
write_cmd(0x30);
delay(5);
// write_cmd(0x08) ;
write_cmd(0x0c) ;
delay(5);
// write_cmd(0x01) ;
// delay(5);
write_cmd(0x06) ;
delay(5);
//write_cmd(0x0c) ;
//write_cmd(0x80) ;
//delay(1);
}
/*void InitLcd()
{
WriteCommandLCM(0x38,1);
WriteCommandLCM(0x08,1);
WriteCommandLCM(0x01,1);
WriteCommandLCM(0x06,1);
WriteCommandLCM(0x0c,1);
}*/
/***********************************/
/*void DisplayOneChar(uchar X,uchar Y,uchar DData)
{
Y&=1;
X&=15;
if(Y)X|=0x40;
X|=0x80;
WriteCommandLCM(X,0);
WriteDataLCM(DData);
}*/
/**************************************
延时5微秒(STC90C52RC@12M)
不同的工作环境,需要调整此函数,注意时钟过快时需要修改
当改用1T的MCU时,请调整此延时函数
**************************************/
void Delay5us()
{
_nop_();_nop_();_nop_();_nop_();
_nop_();_nop_();_nop_();_nop_();
_nop_();_nop_();_nop_();_nop_();
_nop_();_nop_();_nop_();_nop_();
}
/**************************************
延时5毫秒(STC90C52RC@12M)
不同的工作环境,需要调整此函数
当改用1T的MCU时,请调整此延时函数
**************************************/
void Delay5ms()
{
WORD n = 560;
while (n--);
}
/**************************************
起始信号
**************************************/
void BMP085_Start()
{
SDA = 1; //拉高数据线
SCL = 1; //拉高时钟线
Delay5us(); //延时
SDA = 0; //产生下降沿
Delay5us(); //延时
SCL = 0; //拉低时钟线
}
/**************************************
停止信号
**************************************/
void BMP085_Stop()
{
SDA = 0; //拉低数据线
SCL = 1; //拉高时钟线
Delay5us(); //延时
SDA = 1; //产生上升沿
Delay5us(); //延时
}
/**************************************
发送应答信号
入口参数:ack (0:ACK 1:NAK)
**************************************/
void BMP085_SendACK(bit ack)
{
SDA = ack; //写应答信号
SCL = 1; //拉高时钟线
Delay5us(); //延时
SCL = 0; //拉低时钟线
Delay5us(); //延时
}
/**************************************
接收应答信号
**************************************/
bit BMP085_RecvACK()
{
SCL = 1; //拉高时钟线
Delay5us(); //延时
CY = SDA; //读应答信号
SCL = 0; //拉低时钟线
Delay5us(); //延时
return CY;
}
/**************************************
向IIC总线发送一个字节数据
**************************************/
void BMP085_SendByte(BYTE dat)
{
BYTE i;
for (i=0; i<8; i++) //8位计数器
{
dat <<= 1; //移出数据的最高位
SDA = CY; //送数据口
SCL = 1; //拉高时钟线
Delay5us(); //延时
SCL = 0; //拉低时钟线
Delay5us(); //延时
}
BMP085_RecvACK();
}
/**************************************
从IIC总线接收一个字节数据
**************************************/
BYTE BMP085_RecvByte()
{
BYTE i;
BYTE dat = 0;
SDA = 1; //使能内部上拉,准备读取数据,
for (i=0; i<8; i++) //8位计数器
{
dat <<= 1;
SCL = 1; //拉高时钟线
Delay5us(); //延时
dat |= SDA; //读数据
SCL = 0; //拉低时钟线
Delay5us(); //延时
}
return dat;
}
/*
//单字节写入BMP085内部数据*******************************
void Single_Write(uchar SlaveAddress,uchar REG_Address,uchar REG_data)
{
BMP085_Start(); //起始信号
BMP085_SendByte(SlaveAddress); //发送设备地址+写信号
BMP085_SendByte(REG_Address); //内部寄存器地址
BMP085_SendByte(REG_data); //内部寄存器数据
BMP085_Stop(); //发送停止信号
}
*/
/*
//单字节读取BMP085内部数据********************************
uchar Single_Read(uchar REG_Address)
{ uchar REG_data;
BMP085_Start(); //起始信号
BMP085_SendByte(BMP085_SlaveAddress); //发送设备地址+写信号
BMP085_SendByte(REG_Address); //发送存储单元地址
BMP085_Start(); //起始信号
BMP085_SendByte(BMP085_SlaveAddress+1); //发送设备地址+读信号
REG_data=BMP085_RecvByte(); //读出寄存器数据
BMP085_SendACK(1);
BMP085_Stop(); //停止信号
return REG_data;
}
*/
//*********************************************************
//读出BMP085内部数据,连续两个
//*********************************************************
short Multiple_read(uchar ST_Address)
{
uchar msb, lsb;
short _data;
BMP085_Start(); //起始信号
BMP085_SendByte(BMP085_SlaveAddress); //发送设备地址+写信号
BMP085_SendByte(ST_Address); //发送存储单元地址
BMP085_Start(); //起始信号
BMP085_SendByte(BMP085_SlaveAddress+1); //发送设备地址+读信号
msb = BMP085_RecvByte(); //BUF[0]存储
BMP085_SendACK(0); //回应ACK
lsb = BMP085_RecvByte();
BMP085_SendACK(1); //最后一个数据需要回NOACK
BMP085_Stop(); //停止信号
Delay5ms();
_data = msb << 8;
_data |= lsb;
return _data;
}
//********************************************************************
long bmp085ReadTemp(void)
{
BMP085_Start(); //起始信号
BMP085_SendByte(BMP085_SlaveAddress); //发送设备地址+写信号
BMP085_SendByte(0xF4); // write register address
BMP085_SendByte(0x2E); // write register data for temp
BMP085_Stop(); //发送停止信号
delay(10); // max time is 4.5ms
return (long) Multiple_read(0xF6);
}
//*************************************************************
long bmp085ReadPressure(void)
{
long pressure = 0;
BMP085_Start(); //起始信号
BMP085_SendByte(BMP085_SlaveAddress); //发送设备地址+写信号
BMP085_SendByte(0xF4); // write register address
BMP085_SendByte(0x34); // write register data for pressure
BMP085_Stop(); //发送停止信号
delay(10); // max time is 4.5ms
pressure = Multiple_read(0xF6);
pressure &= 0x0000FFFF;
return pressure;
//return (long) bmp085ReadShort(0xF6);
}
//**************************************************************
//初始化BMP085,根据需要请参考pdf进行修改**************
void Init_BMP085()
{
ac1 = Multiple_read(0xAA);
ac2 = Multiple_read(0xAC);
ac3 = Multiple_read(0xAE);
ac4 = Multiple_read(0xB0);
ac5 = Multiple_read(0xB2);
ac6 = Multiple_read(0xB4);
b1 = Multiple_read(0xB6);
b2 = Multiple_read(0xB8);
mb = Multiple_read(0xBA);
mc = Multiple_read(0xBC);
md = Multiple_read(0xBE);
}
//***********************************************************************
void bmp085Convert()
{
long ut;
long up;
long x1, x2, b5, b6, x3, b3, p;
unsigned long b4, b7;
long temperature;
long pressure;
ut = bmp085ReadTemp();
ut = bmp085ReadTemp(); // 读取温度
up = bmp085ReadPressure();
up = bmp085ReadPressure(); // 读取压强
x1 = ((long)ut - ac6) * ac5 >> 15;
x2 = ((long) mc << 11) / (x1 + md);
b5 = x1 + x2;
temperature = (b5 + 8) >> 4;
//*************
conversion(temperature);
write_cmd(0x80);
// for(j=0;table2[j]!='\0';j++)
// {
// write_byte(table2[j]);
// delay(5);
// }
for(j=0;table1[j]!='\0';j++)
{
write_byte(table1[j]);
delay(5);
}
write_byte(':');
write_byte(bai);
write_byte(shi);
write_byte('.');
write_byte(ge);
write_byte(' ');
for(k=0;table3[k]!='\0';k++)
{
write_byte(table3[k]);
delay(5);
}
//write_byte(0XDF);
//write_byte('C');
/* DisplayOneChar(4,0,'T'); //温度显示
DisplayOneChar(5,0,':');
DisplayOneChar(7,0,);
DisplayOneChar(8,0,);
DisplayOneChar(9,0,'.');
DisplayOneChar(10,0,);
DisplayOneChar(11,0,); //温度单位
DisplayOneChar(12,0,); */
//*************
b6 = b5 - 4000;
x1 = (b2 * (b6 * b6 >> 12)) >> 11;
x2 = ac2 * b6 >> 11;
x3 = x1 + x2;
b3 = (((long)ac1 * 4 + x3) + 2)/4;
x1 = ac3 * b6 >> 13;
x2 = (b1 * (b6 * b6 >> 12)) >> 16;
x3 = ((x1 + x2) + 2) >> 2;
b4 = (ac4 * (unsigned long) (x3 + 32768)) >> 15;
b7 = ((unsigned long) up - b3) * (50000 >> OSS);
if( b7 < 0x80000000)
p = (b7 * 2) / b4 ;
else
p = (b7 / b4) * 2;
x1 = (p >> 8) * (p >> 8);
x1 = (x1 * 3038) >> 16;
x2 = (-7357 * p) >> 16;
pressure = p + ((x1 + x2 + 3791) >> 4);
conversion(pressure);
/* DisplayOneChar(4,1,'P'); //显示压强
DisplayOneChar(5,1,);
DisplayOneChar(6,1,shiwan);
DisplayOneChar(7,1,);
DisplayOneChar(8,1,);
DisplayOneChar(9,1,);
DisplayOneChar(10,1,);
DisplayOneChar(11,1,);
DisplayOneChar(12,1,); //气压单位
DisplayOneChar(13,1,);
DisplayOneChar(14,1,); */
write_cmd(0x90);
for(i=0;table0!='\0';i++)
{
write_byte(table0);
delay(5);
}
write_byte(':');
write_byte(shiwan);
write_byte(wan);
write_byte(qian);
write_byte('.');
write_byte(bai);
write_byte(shi);
write_byte('K');
write_byte('p');
write_byte('a');
}
//*********************************************************
//******主程序********
//*********************************************************
void main()
{
delay(50); //上电延时
// InitLcd();
lcd_init(); //液晶初始化
Init_BMP085(); //初始化BMP085
while(1) //循环
{
bmp085Convert();
delay(1000);
}
}
程序有问题,为什么显示乱码
小编!怎么1602显示的都是小方块呢!
小编你怎么老是不在呢?我捣鼓一周多了!
你好 ,,你这个程序写的怎么样了,,最后实现了吗
这是我琢磨的BMP180的程序,串口输出测量值。检测出的温度始终不对,气压没问题。海拔高度的计算随便从网上扒了一个。大家一起交流,集思广益。
#include <reg52.h>
#include <intrins.h>
#include <math.h>
#define uchar unsigned char
#define uint unsigned int
sbit SDA=P1^0;
sbit SCL=P1^1;
uchar code Comd0[]="temp:";
uint tempre[6];
#define OSS 1 // 过采样设置,(OSS可取0,1,2,3)
long temperature=0; //温度值
long pressure=0; //压力值
float height=0; //相对海拔高度值
short ac1;
short ac2;
short ac3;
short ac4;
short ac5;
short ac6;
short b1;
short b2;
short mb;
short mc;
short md;
void Some_Nop()
{
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
}
void Delay_ms(uchar i)
{
uint j;
for(;i>0;i--)
for(j=0;j<124;j++);
}
void Delay_s() //延时1s
{
uchar i;
for(i=0;i<20;i++)
{
TH0=0x4c;
TL0=0x00;
TR0=1;
while(TF0==0);TF0=0;
TR0=0;
}
}
/*************** I2C *******************/
/*****************************************************/
void I2C_init() //I2C初始化
{
SDA=1;
SCL=1;
}
void I2C_Start() //I2C开始
{
SDA=1;
Some_Nop();
SCL=1;
Some_Nop();
SDA=0;
Some_Nop();
SCL=0;
_nop_();
}
void I2C_Stop() //I2C结束
{
SDA=0;
Some_Nop();
SCL=1;
Some_Nop();
SDA=1;
Some_Nop();
}
void I2C_ACK(bit a)
{
if(a==1) SDA=0; /*在此发出应答或非应答信号,ack=1,发送数据正常;ack=0表示被控器无应答或损坏。 */
else SDA=1;
_nop_();
_nop_();
SCL=1;
Some_Nop(); //保持数据时间 , 即SCL为高时间大于4.7μs
SCL=0; //清时钟线,钳住I2C总线以便继续接收
_nop_();
_nop_();
}
void I2C_WriteByte(uint date) //I2C主机写从机地址、命令
{
uint i;
SCL=0;
Some_Nop();