ES ordening

This commit is contained in:
Rens Pastoor
2025-05-27 23:20:05 +02:00
parent d9244f1ae3
commit 39269a71a7
49 changed files with 1952 additions and 0 deletions

View File

@@ -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
}

View 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

View File

@@ -0,0 +1,61 @@
#include <Arduino.h>
#include <Wire.h>
#include "BME280.h"
#define PARALLEL_SLAVE_ADDR 0x77
void writeToRegister(uint8_t deviceAddr, uint8_t reg, uint8_t value) {
Wire.beginTransmission(deviceAddr);
Wire.write(reg);
Wire.write(value);
Wire.endTransmission();
}
uint8_t readFromRegister(uint8_t deviceAddr, uint8_t reg) {
Wire.beginTransmission(deviceAddr);
Wire.write(reg);
Wire.endTransmission();
Wire.requestFrom(deviceAddr, 1);
return Wire.available() ? Wire.read() : 0xFF;
}
void setup() {
Serial.begin(9600);
Wire.begin();
// BME280 initialisatie
BME280_Reset();
BME280_CtrlHum(0x01); // x1 oversampling
BME280_CtrlMeas(0x27); // temp/press x1, mode normal
Serial.println("Setup done.");
}
void loop() {
// === Lezen van BME280 ===
long temp = BME280_ReadTemperature();
long press = BME280_ReadPressure();
int hum = BME280_ReadHumidity();
Serial.println("--- BME280 Readings ---");
Serial.print("Raw Temp: "); Serial.println(temp);
Serial.print("Raw Press: "); Serial.println(press);
Serial.print("Raw Hum: "); Serial.println(hum);
// === Testen van parallelle slave ===
uint8_t a = random(0, 100);
uint8_t b = random(0, 100);
writeToRegister(PARALLEL_SLAVE_ADDR, 0x21, a); // INA
writeToRegister(PARALLEL_SLAVE_ADDR, 0x22, b); // INB
uint8_t minVal = readFromRegister(PARALLEL_SLAVE_ADDR, 0x23); // MIN
uint8_t maxVal = readFromRegister(PARALLEL_SLAVE_ADDR, 0x24); // MAX
Serial.println("--- Parallel Slave ---");
Serial.print("a: "); Serial.print(a);
Serial.print(", b: "); Serial.print(b);
Serial.print(" => MIN: "); Serial.print(minVal);
Serial.print(", MAX: "); Serial.println(maxVal);
}

View File

@@ -0,0 +1,65 @@
#include <Arduino.h>
#include <Wire.h>
#define SLAVE_ADDRESS 0x77
uint8_t registers[256] = {0}; // Simuleer registermap
uint8_t regAddr = 0; // Registeradres voor read/write
void setup() {
Wire.begin(SLAVE_ADDRESS);
Wire.onReceive(receiveEvent);
Wire.onRequest(requestEvent);
Serial.begin(9600);
Serial.println("Slave ready.");
}
void loop() {
}
// Ontvangen van master (write)
void receiveEvent(int howMany) {
if (howMany < 1) return;
regAddr = Wire.read(); // Eerste byte is altijd registeradres
if (howMany > 1) {
// Daarna komen data bytes
int bytesToRead = howMany - 1;
for (int i = 0; i < bytesToRead; i++) {
if (Wire.available()) {
uint8_t val = Wire.read();
registers[regAddr + i] = val;
Serial.print("Reg ");
Serial.print(regAddr + i);
Serial.print(" written with ");
Serial.println(val);
}
}
// Update min/max registers als 0x21 of 0x22 is gewijzigd
if (regAddr == 0x21 || regAddr == 0x22) {
uint8_t a = registers[0x21];
uint8_t b = registers[0x22];
registers[0x23] = (a < b) ? a : b; // MIN
registers[0x24] = (a > b) ? a : b; // MAX
Serial.print("Updated MIN (0x23): ");
Serial.println(registers[0x23]);
Serial.print("Updated MAX (0x24): ");
Serial.println(registers[0x24]);
}
}
// else alleen registeradres ontvangen, niks schrijven (read setup)
}
// Master vraagt data op (read)
void requestEvent() {
uint8_t val = registers[regAddr];
Wire.write(val);
Serial.print("Sent reg ");
Serial.print(regAddr, HEX);
Serial.print(": ");
Serial.println(val);
}