|
![](static/image/common/ico_lz.png)
楼主 |
发表于 2018-1-24 07:14:46
|
显示全部楼层
本帖最后由 andy 于 2018-1-24 11:57 编辑
第一行显示DS3231实时(时、 分、秒 、温度)
第二行显示BME280(湿度、温度、偏移量)
第三行显示气压数值xxxxxxPa
第四行显示高度米
/***************************************************************************
This is a library for the BME280 humidity, temperature & pressure sensor
Designed specifically to work with the Adafruit BME280 Breakout
----> http://www.adafruit.com/products/2650
These sensors use I2C or SPI to communicate, 2 or 4 pins are required
to interface. The device's I2C address is either 0x76 or 0x77.
Adafruit invests time and resources providing this open source code,
please support Adafruit andopen-source hardware by purchasing products
from Adafruit!
Written by Limor Fried & Kevin Townsend for Adafruit Industries.
BSD license, all text above must be included in any redistribution
***************************************************************************/
//BME280(运行SPI),SH1106(运行I2C)显示温湿度、气压Pa、高度
#include <Wire.h>
#include <SPI.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BME280.h>
#include <U8glib.h>
#include <DS3231.h>
#define BME_SCK 13
#define BME_MISO 12
#define BME_MOSI 11
#define BME_CS 6
//设置当地当时的参考气压值
#define SEALEVELPRESSURE_HPA (1010.00)
// Adafruit_BME280 bme; // I2C
// Adafruit_BME280 bme(BME_CS, BME_MOSI, BME_MISO, BME_SCK); // software SPI
// 选用硬件SPI CS,MOSI,MISO,SCK
Adafruit_BME280 bme(BME_CS); // hardware SPI
U8GLIB_SH1106_128X64 u8g(U8G_I2C_OPT_NONE); // I2C / TWI
DS3231 clock;
RTCDateTime dt;
unsigned long delayTime;
const int buttonUp = 7;
const int buttonDown = 10;
int buttonUpState = 0;
int buttonDownState = 0;
byte Old_key_Down=LOW;
byte Old_key_Up=LOW;
//偏移量
float count=25;
void draw()
{
dt = clock.getDateTime();
buttonUpState=digitalRead(buttonUp);
buttonDownState=digitalRead(buttonDown);
if (buttonUpState && buttonUpState != Old_key_Up)
{
if (count < 99)
count++;
}
Old_key_Up = buttonUpState;
if (buttonDownState && buttonDownState != Old_key_Down)
{
if (count > 0)
count--;
}
Old_key_Down = buttonDownState;
u8g.setFont(u8g_font_8x13B);
u8g.drawRFrame(0, 0, 128, 64, 4); //(0, 0, 128, 16, 4);设置外匡
//u8g.drawRFrame(0, 16, 128, 48, 4); // 设置外匡
u8g.drawHLine(0,16,128); //起点横坐标 x,起点纵坐标 y,线段长度 h(h个像素点)
//第一行显示DS3231实时(时、 分、秒 、温度)
u8g.setPrintPos(4, 13);
u8g.print(dt.hour);
u8g.print(":");
if(dt.minute < 10)
u8g.print("0");
u8g.print(dt.minute);
u8g.print(":");
if(dt.second < 10)
u8g.print("0");
u8g.print(dt.second);
u8g.print(" ");
u8g.print(clock.readTemperature(), 1);
u8g.print(char(176)); //温度符号
u8g.print("C");
//第二行显示BME280(湿度、温度、偏移量)
u8g.setPrintPos(4, 31); //10 设置下文中print()的显示位置 x:横坐标 y:纵坐标
u8g.print(bme.readHumidity(), 1);
u8g.print("% ");
u8g.print(bme.readTemperature(), 1);
u8g.print(char(176));//温度符号 u8g.drawCircle(103, 22, 2); //Grau
u8g.print("C ");
u8g.print(byte(count), DEC);
//第三行显示气压数值xxxxxxPa
//Mostra a pressao (em hPa)
u8g.drawStr(10, 45, "Pres:");
u8g.setPrintPos(55, 45);
u8g.print(bme.readPressure(), 1);
//第四行显示高度米
//Mostra a altitude
u8g.drawStr(10, 59, "Alt : "); //drawStr 显示字符串
u8g.setPrintPos(55, 59);//55
u8g.print(bme.readAltitude((SEALEVELPRESSURE_HPA) + count / 4), 1); //(偏移量/4)=0.25,(每级偏移量0.25)
u8g.print("m");
}
void setup() {
pinMode(buttonUp,INPUT_PULLUP);
pinMode(buttonDown,INPUT_PULLUP);
Wire.begin();
u8g.begin();
clock.begin();
Serial.begin(115200);
Serial.println(F("BME280 test"));
bool status;
// default settings
// (you can also pass in a Wire library object like &Wire2)
status = bme.begin();
if (!status) {
Serial.println("Could not find a valid BME280 sensor, check wiring!");
while (1);
}
Serial.println("-- Default Test --");
delayTime = 960;
Serial.println();
//*****Display Oled*********
if ( u8g.getMode() == U8G_MODE_R3G3B2 ) {
u8g.setColorIndex(255); // white
}
else if ( u8g.getMode() == U8G_MODE_GRAY2BIT ) {
u8g.setColorIndex(3); // max intensity
}
else if ( u8g.getMode() == U8G_MODE_BW ) {
u8g.setColorIndex(1); // pixel on
}
else if ( u8g.getMode() == U8G_MODE_HICOLOR ) {
u8g.setHiColorByRGB(255, 255, 255);
}
}
void loop() {
// printValues();
u8g.firstPage();
do
{
draw();
}
while ( u8g.nextPage() );
delay(700);
}
/*
void printValues() {
Serial.print("Temperature = ");
Serial.print(bme.readTemperature());
Serial.println("°C");
Serial.print("Pressure = ");
Serial.print(bme.readPressure());// bme.readPressure() / 100.0F
Serial.println("Pa"); // hPa
Serial.print("Approx. Altitude = ");
Serial.print(bme.readAltitude(SEALEVELPRESSURE_HPA));
Serial.println(" m");
Serial.print("Humidity = ");
Serial.print(bme.readHumidity());
Serial.println(" %");
Serial.println();
}
*/ |
|