ES ordening
This commit is contained in:
@@ -0,0 +1,85 @@
|
||||
|
||||
#include "BME280.h"
|
||||
|
||||
// I2C read 1 byte
|
||||
uint8_t readRegister(uint8_t reg) {
|
||||
Wire.beginTransmission(BME280_ADDRESS);
|
||||
Wire.write(reg);
|
||||
Wire.endTransmission();
|
||||
Wire.requestFrom(BME280_ADDRESS, 1);
|
||||
return Wire.read();
|
||||
}
|
||||
|
||||
// I2C write 1 byte
|
||||
void writeRegister(uint8_t reg, uint8_t value) {
|
||||
Wire.beginTransmission(BME280_ADDRESS);
|
||||
Wire.write(reg);
|
||||
Wire.write(value);
|
||||
Wire.endTransmission();
|
||||
}
|
||||
|
||||
// Public functions
|
||||
uint8_t BME280_GetID() {
|
||||
return readRegister(BME280_REG_ID);
|
||||
}
|
||||
|
||||
void BME280_Reset() {
|
||||
writeRegister(BME280_REG_RESET, BME280_RESET_CMD);
|
||||
}
|
||||
|
||||
uint8_t BME280_CtrlHum() {
|
||||
return readRegister(BME280_REG_CTRL_HUM);
|
||||
}
|
||||
|
||||
void BME280_CtrlHum(uint8_t bitpattern) {
|
||||
writeRegister(BME280_REG_CTRL_HUM, bitpattern);
|
||||
}
|
||||
|
||||
uint8_t BME280_CtrlMeas() {
|
||||
return readRegister(BME280_REG_CTRL_MEAS);
|
||||
}
|
||||
|
||||
void BME280_CtrlMeas(uint8_t bitpattern) {
|
||||
writeRegister(BME280_REG_CTRL_MEAS, bitpattern);
|
||||
}
|
||||
|
||||
long BME280_ReadTemperature() {
|
||||
Wire.beginTransmission(BME280_ADDRESS);
|
||||
Wire.write(BME280_REG_TEMP_MSB);
|
||||
Wire.endTransmission();
|
||||
Wire.requestFrom(BME280_ADDRESS, 3);
|
||||
|
||||
long msb = Wire.read();
|
||||
long lsb = Wire.read();
|
||||
long xlsb = Wire.read();
|
||||
long adc_T = ((msb << 16) | (lsb << 8) | xlsb) >> 4;
|
||||
|
||||
return adc_T; // Raw value (compensation needed)
|
||||
}
|
||||
|
||||
int BME280_ReadHumidity() {
|
||||
Wire.beginTransmission(BME280_ADDRESS);
|
||||
Wire.write(BME280_REG_HUM_MSB);
|
||||
Wire.endTransmission();
|
||||
Wire.requestFrom(BME280_ADDRESS, 2);
|
||||
|
||||
int msb = Wire.read();
|
||||
int lsb = Wire.read();
|
||||
int adc_H = (msb << 8) | lsb;
|
||||
|
||||
return adc_H; // Raw value
|
||||
}
|
||||
|
||||
long BME280_ReadPressure() {
|
||||
Wire.beginTransmission(BME280_ADDRESS);
|
||||
Wire.write(BME280_REG_PRESS_MSB);
|
||||
Wire.endTransmission();
|
||||
Wire.requestFrom(BME280_ADDRESS, 3);
|
||||
|
||||
long msb = Wire.read();
|
||||
long lsb = Wire.read();
|
||||
long xlsb = Wire.read();
|
||||
long adc_P = ((msb << 16) | (lsb << 8) | xlsb) >> 4;
|
||||
|
||||
return adc_P; // Raw value
|
||||
}
|
||||
43
ES/ES assignment 4/ES assignment 3 I2C/Assignment-A/BME280.h
Normal file
43
ES/ES assignment 4/ES assignment 3 I2C/Assignment-A/BME280.h
Normal file
@@ -0,0 +1,43 @@
|
||||
#ifndef BME280_H
|
||||
#define BME280_H
|
||||
|
||||
#include <Arduino.h>
|
||||
#include <Wire.h>
|
||||
|
||||
// I2C address for BME280 (default)
|
||||
#define BME280_ADDRESS 0x76
|
||||
|
||||
// Register addresses from datasheet
|
||||
#define BME280_REG_ID 0xD0
|
||||
#define BME280_REG_RESET 0xE0
|
||||
#define BME280_REG_CTRL_HUM 0xF2
|
||||
#define BME280_REG_STATUS 0xF3
|
||||
#define BME280_REG_CTRL_MEAS 0xF4
|
||||
#define BME280_REG_CONFIG 0xF5
|
||||
#define BME280_REG_PRESS_MSB 0xF7
|
||||
#define BME280_REG_TEMP_MSB 0xFA
|
||||
#define BME280_REG_HUM_MSB 0xFD
|
||||
|
||||
// Reset command
|
||||
#define BME280_RESET_CMD 0xB6
|
||||
|
||||
// Function declarations
|
||||
uint8_t BME280_GetID();
|
||||
|
||||
void BME280_Reset();
|
||||
|
||||
uint8_t BME280_CtrlHum();
|
||||
|
||||
void BME280_CtrlHum(uint8_t bitpattern);
|
||||
|
||||
uint8_t BME280_CtrlMeas();
|
||||
|
||||
void BME280_CtrlMeas(uint8_t bitpattern);
|
||||
|
||||
long BME280_ReadTemperature();
|
||||
|
||||
int BME280_ReadHumidity();
|
||||
|
||||
long BME280_ReadPressure();
|
||||
|
||||
#endif
|
||||
37
ES/ES assignment 4/ES assignment 3 I2C/Assignment-A/main.cpp
Normal file
37
ES/ES assignment 4/ES assignment 3 I2C/Assignment-A/main.cpp
Normal file
@@ -0,0 +1,37 @@
|
||||
#include <Arduino.h>
|
||||
#include <Wire.h>
|
||||
#include "BME280.h"
|
||||
|
||||
void setup() {
|
||||
Serial.begin(9600);
|
||||
Wire.begin();
|
||||
|
||||
Serial.println("Initializing BME280...");
|
||||
|
||||
uint8_t id = BME280_GetID();
|
||||
Serial.print("Sensor ID: 0x");
|
||||
Serial.println(id, HEX);
|
||||
|
||||
BME280_Reset();
|
||||
|
||||
// Set oversampling for humidity = x1 (00000001)
|
||||
BME280_CtrlHum(0x01);
|
||||
|
||||
// Set oversampling for temp and pressure = x1, mode = normal (00100111)
|
||||
BME280_CtrlMeas(0x27);
|
||||
|
||||
Serial.println("BME280 Setup Done.");
|
||||
}
|
||||
|
||||
void loop() {
|
||||
long temp_raw = BME280_ReadTemperature();
|
||||
int hum_raw = BME280_ReadHumidity();
|
||||
long press_raw = BME280_ReadPressure();
|
||||
|
||||
Serial.print("Raw Temperature: ");
|
||||
Serial.print(temp_raw);
|
||||
Serial.print(" | Raw Humidity: ");
|
||||
Serial.print(hum_raw);
|
||||
Serial.print(" | Raw Pressure: ");
|
||||
Serial.println(press_raw);
|
||||
}
|
||||
Reference in New Issue
Block a user