added a feature common to all interrupts #7

Closed
ogneyar wants to merge 20 commits from ogneyar/elbear_arduino_bsp:main into main
6 changed files with 157 additions and 9 deletions
Showing only changes of commit f8c00f6086 - Show all commits

View File

@ -1,9 +1,7 @@
/*
An SD card connected via SPI is used for this test.
The example is taken from the arduino SD library.
https://github.com/arduino-libraries/SD
The example uses the SD library, it must be installed first.
*/
#include <SPI.h>

View File

@ -33,12 +33,6 @@
#define SPI_DEFAULT_SPEED 4000000
#ifndef LSBFIRST
#define LSBFIRST 0
#endif
#ifndef MSBFIRST
#define MSBFIRST 1
#endif
// dividers for setClockDivider()
#define SPI_CLOCK_DIV4 0x01 // 8 MHz

View File

@ -0,0 +1,59 @@
#include <Arduino.h>
#include <Wire.h>
#include "AHT10.h"
static const uint8_t AHT10_ADDR = 0x38;
bool AHT10::begin() {
Wire.beginTransmission(AHT10_ADDR);
Wire.write(0xE1);
Wire.write(0x08);
Wire.write(0x00);
return Wire.endTransmission() == 0;
}
bool AHT10::reset() {
Wire.beginTransmission(AHT10_ADDR);
Wire.write(0xBA);
return Wire.endTransmission() == 0;
}
bool AHT10::measure(float *temp, float *hum) {
uint8_t data[6];
uint32_t d;
Wire.beginTransmission(AHT10_ADDR);
Wire.write(0xAC);
Wire.write(0x33);
Wire.write(0x00);
if (Wire.endTransmission())
return false;
delay(75);
if ((Wire.requestFrom(AHT10_ADDR, (uint8_t)6) != 6) || (Wire.readBytes(data, 6) != 6))
return false;
if (temp) {
d = ((uint32_t)(data[3] & 0x0F) << 16) | ((uint32_t)data[4] << 8) | data[5];
*temp = (float)d * 200 / 1048576 - 50;
}
if (hum) {
d = ((uint32_t)data[1] << 12) | ((uint32_t)data[2] << 4) | (data[3] >> 4);
*hum = (float)d * 100 / 1048576;
}
return true;
}
float AHT10::getTemperature() {
float result;
if (! measure(&result, NULL))
result = NAN;
return result;
}
float AHT10::getHumidity() {
float result;
if (! measure(NULL, &result))
result = NAN;
return result;
}

View File

@ -0,0 +1,15 @@
#ifndef __AHT10_H
#define __AHT10_H
#include <Wire.h>
class AHT10 {
public:
static bool begin();
static bool reset();
static bool measure(float *temp, float *hum);
static float getTemperature();
static float getHumidity();
};
#endif

View File

@ -0,0 +1,32 @@
/*
The AHT10 temperature and humidity sensor connected to I2C is used for this test.
*/
#include "AHT10.h"
void setup() {
Serial.begin(115200);
Serial.println();
Wire.begin();
if (! AHT10::begin()) {
Serial.println(F("AHT10 not detected!"));
Serial.flush();
}
}
void loop() {
float temp, hum;
delay(2000);
if (AHT10::measure(&temp, &hum)) {
Serial.print(F("Temperature is "));
Serial.print(temp);
Serial.print(F(" C, humidity is "));
Serial.print(hum);
Serial.println('%');
} else {
Serial.println(F("AHT10 read error!"));
}
}

View File

@ -0,0 +1,50 @@
/*
The BMP280 temperature and pressure sensor connected to I2C is used for this test.
The example uses the Adafruit_BMP280 library, it must be installed first.
*/
#include <Wire.h>
#include <Adafruit_BMP280.h>
Adafruit_BMP280 bmp;
void setup() {
Serial.begin(115200);
while( ! Serial )
;
Serial.println("BMP280 run test in terminal");
unsigned status;
status = bmp.begin(BMP280_ADDRESS_ALT, BMP280_CHIPID);
if (!status) {
Serial.println("Could not find a valid BMP280 sensor!");
while(1)
;
}
/* Default settings from datasheet. */
bmp.setSampling(Adafruit_BMP280::MODE_NORMAL, /* Operating Mode. */
Adafruit_BMP280::SAMPLING_X2, /* Temp. oversampling */
Adafruit_BMP280::SAMPLING_X16, /* Pressure oversampling */
Adafruit_BMP280::FILTER_X16, /* Filtering. */
Adafruit_BMP280::STANDBY_MS_500); /* Standby time. */
}
void loop() {
Serial.print(F("Temperature = "));
Serial.print(bmp.readTemperature());
Serial.println(" *C");
Serial.print(F("Pressure = "));
Serial.print(bmp.readPressure());
Serial.println(" Pa");
Serial.print(F("Approx altitude = "));
Serial.print(bmp.readAltitude(1013.25)); /* Adjusted to local forecast! */
Serial.println(" m");
Serial.println();
delay(2000);
}