46 lines
1.2 KiB
Rust
46 lines
1.2 KiB
Rust
#![no_std]
|
|
#![no_main]
|
|
|
|
use embassy_time::Timer;
|
|
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());
|
|
|
|
info!("Initializing TWI...");
|
|
|
|
let config = twim::Config::default();
|
|
let mut twi = Twim::new(p.TWI0, Irqs, p.P0_14, p.P0_13, config);
|
|
|
|
loop {
|
|
unwrap!(twi.blocking_write(ADDRESS, &mut [0xFD]));
|
|
|
|
Timer::after_millis(10).await;
|
|
|
|
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!("{}\t{}", temp_float, rh_float);
|
|
|
|
Timer::after_millis(50).await;
|
|
}
|
|
}
|