From 5682479aad07c0e7c0e6245bb34227c92376db8b Mon Sep 17 00:00:00 2001 From: Thomas Kolb Date: Fri, 6 Jan 2023 23:42:08 +0100 Subject: [PATCH] adc: fixed channel readout and convert values --- src/ext_adc.rs | 4 ++-- src/main.rs | 28 +++++++++++++++++++++++----- 2 files changed, 25 insertions(+), 7 deletions(-) diff --git a/src/ext_adc.rs b/src/ext_adc.rs index 05e9cdd..fe224aa 100644 --- a/src/ext_adc.rs +++ b/src/ext_adc.rs @@ -49,8 +49,8 @@ where AdcContext:: { dma_rx_chan: rx_chan, dma_tx_chan: tx_chan, - rx_buf: singleton!(: [u8; 3] = [0x06, 0x40, 0x00]).unwrap(), - tx_buf: singleton!(: [u8; 3] = [0; 3]).unwrap(), + tx_buf: singleton!(: [u8; 3] = [0x06, 0x80, 0x00]).unwrap(), + rx_buf: singleton!(: [u8; 3] = [0; 3]).unwrap(), spi: spi_if, cs_pin: cs_pin } diff --git a/src/main.rs b/src/main.rs index c08af92..b6d3631 100644 --- a/src/main.rs +++ b/src/main.rs @@ -56,6 +56,15 @@ const HIGH: u16 = 25000; /// if your board has a different frequency const XTAL_FREQ_HZ: u32 = 12_000_000u32; +fn convert_adc_measurements(raw: &[u16; 4]) -> (u32, u32, u32) +{ + let iout = (raw[0] - raw[1]) as u32 * 3300 * 20 / 4096 / 28; // *20 = division by 50 mΩ shunt + let vout = raw[2] as u32 * 3300 * (47 + 10) / 10 / 4096; + let vin = raw[3] as u32 * 3300 * (10 + 10) / 10 / 4096; + + (vin, vout, iout) /* units: mV, mV, mA */ +} + /// Entry point to our bare-metal application. /// /// The `#[rp2040_hal::entry]` macro ensures the Cortex-M start-up code calls this function @@ -189,20 +198,29 @@ fn main() -> ! { let mut adc_value: [u16; 4] = [0; 4]; (adc_ctrl, adc_value[0]) = adc_ctrl.sample_adc_channel(0); - delay.delay_us(50); (adc_ctrl, adc_value[1]) = adc_ctrl.sample_adc_channel(1); - delay.delay_us(50); (adc_ctrl, adc_value[2]) = adc_ctrl.sample_adc_channel(2); - delay.delay_us(50); (adc_ctrl, adc_value[3]) = adc_ctrl.sample_adc_channel(3); + let (vin, vout, iout) = convert_adc_measurements(&adc_value); + { let mut data: String<16>; - uart.write_full_blocking(b"ADC channels: ["); + uart.write_full_blocking(b"Vin: "); + data = String::from(vin); + uart.write_full_blocking(data.as_bytes()); + uart.write_full_blocking(b"mV - Vout: "); + data = String::from(vout); + uart.write_full_blocking(data.as_bytes()); + uart.write_full_blocking(b"mV - Iout: "); + data = String::from(iout); + uart.write_full_blocking(data.as_bytes()); + + uart.write_full_blocking(b"mA - raw: ["); for i in 0..adc_value.len() { - data = String::from(adc_value[0]); + data = String::from(adc_value[i]); uart.write_full_blocking(data.as_bytes()); if i < 3 {