标题:
51单片机+oled显示 气压,温度,湿度(bmp180模块+DHT11)
[打印本页]
作者:
Archer串
时间:
2018-12-7 16:13
标题:
51单片机+oled显示 气压,温度,湿度(bmp180模块+DHT11)
单片机源程序如下:
#include "reg52.h"
#include "BMP180.h"
#include <math.h>
#include <stdlib.h>
#include <stdio.h>
#include "intrins.h"
#include "oled.h"
long temperature; //定义温度
long pressure; //定义气压
short ac1,ac2,ac3,b1,b2,mb,mc,md;
unsigned short ac4,ac5,ac6;
/**************************************
延时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 BMP180_Start()
{
SDA = 1; //拉高数据线
SCL = 1; //拉高时钟线
Delay5us(); //延时
SDA = 0; //产生下降沿
Delay5us(); //延时
SCL = 0; //拉低时钟线
}
/**************************************
停止信号
**************************************/
void BMP180_Stop()
{
SDA = 0; //拉低数据线
SCL = 1; //拉高时钟线
Delay5us(); //延时
SDA = 1; //产生上升沿
Delay5us(); //延时
}
/**************************************
发送应答信号
入口参数:ack (0:ACK 1:NAK)
**************************************/
void BMP180_SendACK(bit ack)
{
SDA = ack; //写应答信号
SCL = 1; //拉高时钟线
Delay5us(); //延时
SCL = 0; //拉低时钟线
Delay5us(); //延时
}
/**************************************
接收应答信号
**************************************/
bit BMP180_RecvACK()
{
SCL = 1; //拉高时钟线
Delay5us(); //延时
CY = SDA; //读应答信号
SCL = 0; //拉低时钟线
Delay5us(); //延时
return CY;
}
/**************************************
向IIC总线发送一个字节数据
**************************************/
void BMP180_SendByte(BYTE dat)
{
BYTE i;
for (i=0; i<8; i++) //8位计数器
{
dat <<= 1; //移出数据的最高位
SDA = CY; //送数据口
SCL = 1; //拉高时钟线
Delay5us(); //延时
SCL = 0; //拉低时钟线
Delay5us(); //延时
}
BMP180_RecvACK();
}
/**************************************
从IIC总线接收一个字节数据
**************************************/
BYTE BMP180_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内部数据,连续两个
//*********************************************************
short Multiple_read(uchar ST_Address)
{
uchar msb, lsb;
short _data;
BMP180_Start(); //起始信号
BMP180_SendByte(BMP180_SlaveAddress); //发送设备地址+写信号
BMP180_SendByte(ST_Address); //发送存储单元地址
BMP180_Start(); //起始信号
BMP180_SendByte(BMP180_SlaveAddress+1); //发送设备地址+读信号
msb = BMP180_RecvByte(); //BUF[0]存储
BMP180_SendACK(0); //回应ACK
lsb = BMP180_RecvByte();
BMP180_SendACK(1); //最后一个数据需要回NOACK
BMP180_Stop(); //停止信号
Delay5ms(); //延时5MS
_data = msb << 8;
_data |= lsb;
return _data;
}
//********************************************************************
long bmp180ReadTemp(void) //读取未校准温度数据
{
BMP180_Start(); //起始信号
BMP180_SendByte(BMP180_SlaveAddress); //发送设备地址+写信号
BMP180_SendByte(0xF4); // write register address
BMP180_SendByte(0x2E); // write register data for temp
BMP180_Stop(); //发送停止信号
Delay5ms(); //延时5MS
// max time is 4.5ms
return (long) Multiple_read(0xF6);
}
//*************************************************************
long bmp180ReadPressure(void) //读取未校准气压数据
{
long pressure = 0;
BMP180_Start(); //起始信号
BMP180_SendByte(BMP180_SlaveAddress); //发送设备地址+写信号
BMP180_SendByte(0xF4); // write register address
BMP180_SendByte(0x34); // write register data for pressure
BMP180_Stop(); //发送停止信号
Delay5ms(); // max time is 4.5ms
pressure = Multiple_read(0xF6);
pressure &= 0x0000FFFF;
return pressure;
//return (long) bmp085ReadShort(0xF6);
}
//初始化BMP085,根据需要请参考pdf进行修改**************
void Init_BMP180()
{
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 conversion(long temp_data)
{
BMP_shiwan=temp_data/100000+0x30 ;
temp_data=temp_data%100000; //取余运算
BMP_wan=temp_data/10000+0x30 ;
temp_data=temp_data%10000; //取余运算
BMP_qian=temp_data/1000+0x30 ;
temp_data=temp_data%1000; //取余运算
BMP_bai=temp_data/100+0x30 ;
temp_data=temp_data%100; //取余运算
BMP_shi=temp_data/10+0x30 ;
temp_data=temp_data%10; //取余运算
BMP_ge=temp_data+0x30;
}
void bmp180Convert()
{
long ut;
long up;
long x1, x2, b5, b6, x3, b3, p;
unsigned long b4, b7;
ut = bmp180ReadTemp();
ut = bmp180ReadTemp(); // 读取温度
up = bmp180ReadPressure();
up = bmp180ReadPressure(); // 读取压强
//*************************************************************************
x1 = ((long)ut - ac6) * ac5 >> 15; //温度校正
x2 = ((long) mc << 11) / (x1 + md);
b5 = x1 + x2;
temperature = (b5 + 8) >> 4;
……………………
…………限于本文篇幅 余下代码请从51黑下载附件…………
复制代码
所有资料51hei提供下载:
oled显示.rar
(81.32 KB, 下载次数: 207)
2018-12-7 16:13 上传
点击文件名下载附件
下载积分: 黑币 -5
作者:
admin
时间:
2018-12-8 01:29
补全原理图或者详细说明一下电路连接即可获得100+黑币
作者:
Archer串
时间:
2018-12-10 17:21
气压模块就 IIC总线+VCC+GND 没了
作者:
剪云者
时间:
2019-2-21 17:05
可以发下仿真图吗
作者:
tigerzq
时间:
2019-2-21 22:39
说一下电路图呀!
作者:
ontheroad
时间:
2019-3-1 17:13
下载了,可是代码不全。没有dht11的代码。
楼主能重发一份完整的吗?
作者:
3078
时间:
2019-9-16 14:10
下载了但是打不开呢
作者:
ljj200526
时间:
2019-9-17 21:57
学习一下。谢谢分享!
作者:
Roseup
时间:
2019-10-22 08:49
Archer串 发表于 2018-12-10 17:21
气压模块就 IIC总线+VCC+GND 没了
文件里DH11模块没有 可以麻烦发一下吗
作者:
0..3
时间:
2020-12-17 20:22
向大佬低头
作者:
xwx大房东
时间:
2021-4-13 14:39
感谢分享
欢迎光临 (http://www.51hei.com/bbs/)
Powered by Discuz! X3.1