Implement sensor reading

This commit is contained in:
Andreas Tsouchlos 2025-09-30 02:17:44 +02:00
parent 117a9816ed
commit 988204d9fa

View File

@ -1,26 +1,76 @@
//! Example on how to read a 24C/24LC i2c eeprom.
//!
//! Connect SDA to P0.03, SCL to P0.04
#![no_std]
#![no_main]
use defmt::info;
use embassy_executor::Spawner;
use embassy_nrf::gpio::{Level, Output, OutputDrive};
use embassy_time::Timer;
use {defmt_rtt as _, panic_probe as _};
use nrf_softdevice::{raw, Softdevice};
use defmt::*;
use embassy_executor::Spawner;
use embassy_nrf::twim::{self, Twim};
use embassy_nrf::{bind_interrupts, peripherals};
use {defmt_rtt as _, panic_probe as _};
const ADDRESS: u8 = 0x44;
bind_interrupts!(struct Irqs {
TWIM0_TWIS0_TWI0 => twim::InterruptHandler<peripherals::TWI0>;
});
#[embassy_executor::main]
async fn main(_spawner: Spawner) {
let p = embassy_nrf::init(Default::default());
let mut led = Output::new(p.P0_20, Level::Low, OutputDrive::Standard);
info!("Starting blinky");
info!("Initializing TWI...");
let config = twim::Config::default();
let mut twi = Twim::new(p.TWI0, Irqs, p.P0_14, p.P0_13, config);
loop {
led.set_high();
unwrap!(twi.blocking_write(ADDRESS, &mut [0xFD]));
Timer::after_millis(300).await;
led.set_low();
Timer::after_millis(300).await;
info!("Blinked");
let mut buf = [0u8; 6];
unwrap!(twi.blocking_read(ADDRESS, &mut buf));
let temp_int = u16::from_be_bytes([buf[0], buf[1]]);
let temp_float: f32 = -45f32 + 175f32 * (temp_int as f32 / (2u32.pow(16) - 1) as f32);
let rh_int = u16::from_be_bytes([buf[3], buf[4]]);
let rh_float: f32 = -6f32 + 125f32 * (rh_int as f32 / (2u32.pow(16) - 1) as f32);
println!("temp_float: {}", temp_float);
println!("rh_float: {}", rh_float);
Timer::after_millis(1000).await;
}
}
// #![no_std]
// #![no_main]
//
// use defmt::info;
// use embassy_executor::Spawner;
// use embassy_nrf::gpio::{Level, Output, OutputDrive};
// use embassy_time::Timer;
// use nrf_softdevice::{raw, Softdevice};
// use {defmt_rtt as _, panic_probe as _};
//
// #[embassy_executor::main]
// async fn main(_spawner: Spawner) {
// let p = embassy_nrf::init(Default::default());
// let mut led = Output::new(p.P0_20, Level::Low, OutputDrive::Standard);
//
// info!("Starting blinky");
//
// loop {
// led.set_high();
// Timer::after_millis(300).await;
// led.set_low();
// Timer::after_millis(300).await;
// info!("Blinked");
// }
// }