From 858618f41c14da02668022ae980fb79a983beeb0 Mon Sep 17 00:00:00 2001 From: Colin Date: Sat, 28 Dec 2019 00:05:44 -0800 Subject: [PATCH] Explicitly set the ADC sample time I think I need an op-amp for the piezo element. It seems like just running 6" wires to it causes too much noise in the wires to be able to detect the piezo element. Need an amplifier right next to the piezo before sending it over any wire. With 0b000 sample time, reading values of 0xd0 +/- 20 With 0b111 sample time, sometimes I read all the way between 0x000 and 0xfff. But it's not random noise -- it's oscillating like a sine wave at maybe 1 hz. The noise is there when I connect long wires to the ADC pins, but regardless of what's on the other end of the wire (piezo or no piezo). Could also be worth trying differential mode. --- src/main.rs | 34 ++++++++++++++++++++++++++++++++-- 1 file changed, 32 insertions(+), 2 deletions(-) diff --git a/src/main.rs b/src/main.rs index 32e1ce2..f1aea66 100644 --- a/src/main.rs +++ b/src/main.rs @@ -151,13 +151,18 @@ fn main() -> ! { w.moder15().output() }); - // Configure push-button as input; + // Configure push-button (PA0) as input; // Configure PA1 as analog-in per.gpioa.moder.modify(|_, w| { w.moder0().input() .moder1().analog() }); + // Clear PA3. PA3 can then be a ground reference point for the piezo. + // per.gpioa.odr.write(|w| { + // w.odr3().clear_bit() + // }); + // Configure piezo as digital input per.gpiod.moder.modify(|_, w| { w.moder0().input() @@ -193,7 +198,10 @@ fn main() -> ! { delay(1000); // >= 10 us // ADC CALIBRATION (15.3.8) - // 1. set ADCALDIF=0 (default) + // 1. set ADCALDIF=0 (default) for single-ended cal + per.adc1.cr.modify(|_, w| { + w.adcaldif().clear_bit() + }); // Start cal per.adc1.cr.modify(|_, w| { @@ -214,6 +222,28 @@ fn main() -> ! { // wait for ADRDY=1 while per.adc1.isr.read().adrdy().bit_is_clear() { } + // Configure sample time (15.3.12) + // 0b000 = 1.5 clocks + // 0b001 = 2.5 clocks + // 0b010 = 4.5 clocks + // 0b011 = 7.5 clocks + // 0b100 = 19.5 clocks + // 0b101 = 61.5 clocks + // 0b110 = 181.5 clocks + // 0b111 = 601.5 clocks + per.adc1.smpr1.modify(|_, w| { + unsafe { w.smp1().bits(0b101) }; + unsafe { w.smp2().bits(0b101) }; + unsafe { w.smp3().bits(0b101) }; + unsafe { w.smp4().bits(0b101) }; + unsafe { w.smp5().bits(0b101) }; + unsafe { w.smp6().bits(0b101) }; + unsafe { w.smp7().bits(0b101) }; + unsafe { w.smp8().bits(0b101) }; + unsafe { w.smp9().bits(0b101) }; + w + }); + // Configure mux'ing: SQRx (regular conversion), JSQRx (injected conversion; don't use these) // Draw from ADC1_IN2 -- PA1 per.adc1.sqr1.modify(|_, w| {