2023-07-13 11:16:11 +02:00
|
|
|
//! This example test the ADC (Analog to Digital Conversion) of the RS2040 pin 26, 27 and 28.
|
|
|
|
//! It also reads the temperature sensor in the chip.
|
|
|
|
|
2022-11-15 16:12:07 +01:00
|
|
|
#![no_std]
|
|
|
|
#![no_main]
|
|
|
|
#![feature(type_alias_impl_trait)]
|
|
|
|
|
|
|
|
use defmt::*;
|
|
|
|
use embassy_executor::Spawner;
|
2023-07-21 21:31:44 +02:00
|
|
|
use embassy_rp::adc::{Adc, Channel, Config, InterruptHandler};
|
2023-05-15 15:21:05 +02:00
|
|
|
use embassy_rp::bind_interrupts;
|
2023-07-05 20:05:34 +02:00
|
|
|
use embassy_rp::gpio::Pull;
|
2022-11-15 16:12:07 +01:00
|
|
|
use embassy_time::{Duration, Timer};
|
|
|
|
use {defmt_rtt as _, panic_probe as _};
|
|
|
|
|
2023-05-15 15:21:05 +02:00
|
|
|
bind_interrupts!(struct Irqs {
|
|
|
|
ADC_IRQ_FIFO => InterruptHandler;
|
|
|
|
});
|
|
|
|
|
2022-11-15 16:12:07 +01:00
|
|
|
#[embassy_executor::main]
|
|
|
|
async fn main(_spawner: Spawner) {
|
|
|
|
let p = embassy_rp::init(Default::default());
|
2023-05-15 15:21:05 +02:00
|
|
|
let mut adc = Adc::new(p.ADC, Irqs, Config::default());
|
2022-11-15 16:12:07 +01:00
|
|
|
|
2023-07-21 21:31:44 +02:00
|
|
|
let mut p26 = Channel::new_pin(p.PIN_26, Pull::None);
|
|
|
|
let mut p27 = Channel::new_pin(p.PIN_27, Pull::None);
|
|
|
|
let mut p28 = Channel::new_pin(p.PIN_28, Pull::None);
|
|
|
|
let mut ts = Channel::new_sensor(p.ADC_TEMP_SENSOR);
|
2022-11-15 16:12:07 +01:00
|
|
|
|
|
|
|
loop {
|
2023-07-05 20:05:34 +02:00
|
|
|
let level = adc.read(&mut p26).await.unwrap();
|
2022-11-15 16:12:07 +01:00
|
|
|
info!("Pin 26 ADC: {}", level);
|
2023-07-05 20:05:34 +02:00
|
|
|
let level = adc.read(&mut p27).await.unwrap();
|
2022-11-15 16:12:07 +01:00
|
|
|
info!("Pin 27 ADC: {}", level);
|
2023-07-05 20:05:34 +02:00
|
|
|
let level = adc.read(&mut p28).await.unwrap();
|
2022-11-15 16:12:07 +01:00
|
|
|
info!("Pin 28 ADC: {}", level);
|
2023-07-21 21:31:44 +02:00
|
|
|
let temp = adc.read(&mut ts).await.unwrap();
|
2022-12-20 02:11:39 +01:00
|
|
|
info!("Temp: {} degrees", convert_to_celsius(temp));
|
2022-11-15 16:12:07 +01:00
|
|
|
Timer::after(Duration::from_secs(1)).await;
|
|
|
|
}
|
|
|
|
}
|
2022-12-20 02:11:39 +01:00
|
|
|
|
|
|
|
fn convert_to_celsius(raw_temp: u16) -> f32 {
|
|
|
|
// According to chapter 4.9.5. Temperature Sensor in RP2040 datasheet
|
2023-07-13 22:47:03 +02:00
|
|
|
let temp = 27.0 - (raw_temp as f32 * 3.3 / 4096.0 - 0.706) / 0.001721;
|
|
|
|
let sign = if temp < 0.0 { -1.0 } else { 1.0 };
|
|
|
|
let rounded_temp_x10: i16 = ((temp * 10.0) + 0.5 * sign) as i16;
|
|
|
|
(rounded_temp_x10 as f32) / 10.0
|
2022-12-20 07:59:49 +01:00
|
|
|
}
|