adc: fixed channel readout and convert values

This commit is contained in:
Thomas Kolb 2023-01-06 23:42:08 +01:00
parent 28bd3b20ab
commit 5682479aad
2 changed files with 25 additions and 7 deletions

View File

@ -49,8 +49,8 @@ where
AdcContext::<D, TXC, RXC, CSP> {
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
}

View File

@ -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 {