Last active
October 26, 2018 05:37
-
-
Save shimarin/3d9d5939f3b734dbd9da847ecb1785e8 to your computer and use it in GitHub Desktop.
bme280.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <SPI.h> | |
struct strCALIBRATION_DATA { | |
uint16_t T1; | |
int16_t T2; | |
int16_t T3; | |
uint16_t P1; | |
int16_t P2; | |
int16_t P3; | |
int16_t P4; | |
int16_t P5; | |
int16_t P6; | |
int16_t P7; | |
int16_t P8; | |
int16_t P9; | |
uint8_t H1; | |
int16_t H2; | |
uint8_t H3; | |
int16_t H4; | |
int16_t H5; | |
int8_t H6; | |
} calibration_data; | |
void writeRegister(uint8_t reg_address, uint8_t data) { | |
SPI.transfer(reg_address & B01111111); | |
SPI.transfer(data); | |
} | |
uint16_t read16bit(uint8_t reg) { | |
SPI.transfer(reg | B10000000); | |
uint16_t d1 = SPI.transfer(0x00); | |
uint16_t d2 = SPI.transfer(0x00); | |
return (d2 << 8) | d1; | |
} | |
uint8_t read8bit(uint8_t reg) { | |
SPI.transfer(reg | B10000000); // read, bit 7 high | |
uint8_t data = SPI.transfer(0x00); | |
return data; | |
} | |
void setup() { | |
// put your setup code here, to run once: | |
SPI.begin(SCK, MISO, MOSI, SS); | |
SPI.setFrequency(10000000); | |
SPI.setDataMode(SPI_MODE3); | |
uint8_t osrs_t = 4; //OverSampling Temperature x4 | |
uint8_t osrs_p = 4; //OverSampling Pressure x4 | |
uint8_t osrs_h = 4; //OverSampling Humidity x4 | |
writeRegister(0xE0, 0xB6); //reset | |
delay(2000); | |
writeRegister(0xF2, 4/* OverSampling Humidity*/); | |
writeRegister(0xF4, (4/*OverSampling Temperature*/ << 5) | (4/*OverSampling Pressure*/ << 2) | 3 /* Normal mode*/); | |
writeRegister(0xF5, (5/*Stand-by 1000ms*/ << 5) | (0/* filter off*/ << 2) | 0 /* 4-wire SPI*/); | |
calibration_data.T1 = read16bit(0x88); | |
calibration_data.T2 = (int16_t)read16bit(0x8A); | |
calibration_data.T3 = (int16_t)read16bit(0x8C); | |
calibration_data.P1 = read16bit(0x8E); | |
calibration_data.P2 = (int16_t)read16bit(0x90); | |
calibration_data.P3 = (int16_t)read16bit(0x92); | |
calibration_data.P4 = (int16_t)read16bit(0x94); | |
calibration_data.P5 = (int16_t)read16bit(0x96); | |
calibration_data.P6 = (int16_t)read16bit(0x98); | |
calibration_data.P7 = (int16_t)read16bit(0x9A); | |
calibration_data.P8 = (int16_t)read16bit(0x9C); | |
calibration_data.P9 = (int16_t)read16bit(0x9E); | |
calibration_data.H1 = read8bit(0xA1); | |
calibration_data.H2 = (int16_t)read16bit(0xE1); | |
calibration_data.H3 = read8bit(0xE3); | |
calibration_data.H4 = (int16_t)((read8bit(0xE4) << 4) | (read8bit(0xE5) & 0x0F)); | |
calibration_data.H5 = (int16_t)((read8bit(0xE6) << 4) | (read8bit(0xE5) >> 4)); | |
calibration_data.H6 = (int8_t)read8bit(0xE7); | |
Serial.begin(115200); | |
Serial.printf("SCK=%d, MISO=%d, MOSI=%d, SS=%d\n", SCK,MISO,MOSI,SS); | |
} | |
void loop() { | |
// put your main code here, to run repeatedly: | |
Serial.println("Hello, World!"); | |
delay(1000); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment