Compare commits

...

5 Commits

Author SHA1 Message Date
maddiebaka 53287b4843 bme280 telemetry output to uart 2026-06-07 08:38:03 -04:00
maddiebaka ca0c60ad1e manual i2c id bit communication 2026-06-07 08:38:03 -04:00
maddiebaka 89a9b79aa9 usart character output 2026-06-07 08:38:03 -04:00
maddiebaka 103fa9e8a8 Add bme280 library 2026-06-07 08:38:03 -04:00
maddiebaka 55f927e62a Initial commit 2026-06-07 08:38:03 -04:00
8 changed files with 279 additions and 0 deletions
+1
View File
@@ -0,0 +1 @@
.pio
+37
View File
@@ -0,0 +1,37 @@
This directory is intended for project header files.
A header file is a file containing C declarations and macro definitions
to be shared between several project source files. You request the use of a
header file in your project source file (C, C++, etc) located in `src` folder
by including it, with the C preprocessing directive `#include'.
```src/main.c
#include "header.h"
int main (void)
{
...
}
```
Including a header file produces the same results as copying the header file
into each source file that needs it. Such copying would be time-consuming
and error-prone. With a header file, the related declarations appear
in only one place. If they need to be changed, they can be changed in one
place, and programs that include the header file will automatically use the
new version when next recompiled. The header file eliminates the labor of
finding and changing all the copies as well as the risk that a failure to
find one copy will result in inconsistencies within a program.
In C, the convention is to give header files names that end with `.h'.
Read more about using header files in official GCC documentation:
* Include Syntax
* Include Operation
* Once-Only Headers
* Computed Includes
https://gcc.gnu.org/onlinedocs/cpp/Header-Files.html
+46
View File
@@ -0,0 +1,46 @@
This directory is intended for project specific (private) libraries.
PlatformIO will compile them to static libraries and link into the executable file.
The source code of each library should be placed in a separate directory
("lib/your_library_name/[Code]").
For example, see the structure of the following example libraries `Foo` and `Bar`:
|--lib
| |
| |--Bar
| | |--docs
| | |--examples
| | |--src
| | |- Bar.c
| | |- Bar.h
| | |- library.json (optional. for custom build options, etc) https://docs.platformio.org/page/librarymanager/config.html
| |
| |--Foo
| | |- Foo.c
| | |- Foo.h
| |
| |- README --> THIS FILE
|
|- platformio.ini
|--src
|- main.c
Example contents of `src/main.c` using Foo and Bar:
```
#include <Foo.h>
#include <Bar.h>
int main (void)
{
...
}
```
The PlatformIO Library Dependency Finder will find automatically dependent
libraries by scanning project source files.
More information about PlatformIO Library Dependency Finder
- https://docs.platformio.org/page/librarymanager/ldf.html
+17
View File
@@ -0,0 +1,17 @@
; PlatformIO Project Configuration File
;
; Build options: build flags, source filter
; Upload options: custom upload port, speed and extra flags
; Library options: dependencies, extra library storages
; Advanced options: extra scripting
;
; Please visit documentation for the other options and examples
; https://docs.platformio.org/page/projectconf.html
[env:pro16MHzatmega328]
platform = atmelavr
board = pro16MHzatmega328
monitor_speed = 9600
lib_deps =
https://github.com/Sylaina/bme280.git
+126
View File
@@ -0,0 +1,126 @@
/**
* Copyright (C) PlatformIO <contact@platformio.org>
* See LICENSE for details.
*/
#define BAUD 9600
#include <avr/io.h>
#include <util/delay.h>
#include <util/setbaud.h>
#include "sensor_bme280.h"
#include "bme280.h"
#include "i2c.h"
#include <stdio.h>
void uart_init(void) {
// Set baud rate
UBRR0H = UBRRH_VALUE;
UBRR0L = UBRRL_VALUE;
// Don't use double rate
UCSR0A &= ~(1 << U2X0);
// Enable rx and tx
UCSR0B = (1 << TXEN0) | (1 << RXEN0);
// Set frame format: 8-N-1
UCSR0C = (1 << UCSZ01) | (1 << UCSZ00);
}
void uart_putchar(char c) {
while(!(UCSR0A & (1 << UDRE0)));
UDR0 = c;
}
void uart_putstr(const char *str) {
while(*str) {
uart_putchar(*str++);
}
}
char * format_temperature(float temp) {
uint8_t min_width = 3;
char * out[min_width];
dtostrf(temp, min_width, 1, out);
return out;
}
char * format_humidity(float humidity) {
uint8_t min_width = 5;
char out[min_width];
dtostrf(humidity, min_width, 2, out);
return out;
}
char * format_pressure(float pressure) {
uint8_t min_width = 3;
char out[min_width];
dtostrf(pressure, min_width, 1, out);
return out;
}
int main(void)
{
uart_init();
// make the LED pin an output for PORTB5
DDRB = 1 << 5;
float temp = 0.0;
float pressure = 0.0;
float humidity = 0.0;
i2c_init();
_delay_ms(500);
uint8_t ret = bme280_init(1);
if(ret == 0x00) {
uart_putstr("\nbme280 detected by library.\r\n");
} else if(ret == 0x01) {
uart_putstr("\nbmp280 detected by library.\r\n");
} else if(ret == 0xFF) {
uart_putstr("\nno sensor detected.\r\n");
} else {
uart_putstr("\nunknown error.\r\n");
}
// Turn the green led on
PORTB |= 1 << 5;
while (1)
{
_delay_ms(1000);
temp = bme280_readTemperature(1);
pressure = bme280_readPressure(1);
humidity = bme280_readHumidity(1);
char out[10];
dtostrf(temp, 3, 1, out);
uart_putstr("T");
uart_putstr(out);
uart_putstr("\r\n");
dtostrf(pressure, 3, 1, out);
uart_putstr("P");
uart_putstr(out);
uart_putstr("\r\n");
dtostrf(humidity, 3, 1, out);
uart_putstr("H");
uart_putstr(out);
uart_putstr("\r\n");
}
return 0;
}
+33
View File
@@ -0,0 +1,33 @@
#include "sensor_bme280.h"
#include "bme280.h"
#include "i2c.h"
#include <util/delay.h>
/**
* Checks whether the bme280 is connected to the i2c bus
* Returns 0 if success, 1 if error
*/
sensor_status check_sensor(void) {
i2c_init();
i2c_start((0x76 << 1) | 0x00);
i2c_byte(0xD0);
i2c_stop();
_delay_us(10);
i2c_start((0x76 << 1) | 0x01);
uint8_t ret = i2c_readNAck();
i2c_stop();
_delay_us(100);
i2c_start((0x76 << 1) | 0x00);
i2c_byte(BME280_REGISTER_SOFTRESET);
i2c_byte(0xB6);
i2c_stop();
if(ret == 0x60) {
return SENSOR_OK;
} else {
return SENSOR_NOT_FOUND;
}
}
+8
View File
@@ -0,0 +1,8 @@
#pragma once
typedef enum {
SENSOR_OK,
SENSOR_NOT_FOUND,
} sensor_status;
sensor_status check_sensor(void);
+11
View File
@@ -0,0 +1,11 @@
This directory is intended for PlatformIO Test Runner and project tests.
Unit Testing is a software testing method by which individual units of
source code, sets of one or more MCU program modules together with associated
control data, usage procedures, and operating procedures, are tested to
determine whether they are fit for use. Unit testing finds problems early
in the development cycle.
More information about PlatformIO Unit Testing:
- https://docs.platformio.org/en/latest/advanced/unit-testing/index.html