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.
This commit is contained in:
Colin 2019-12-28 00:05:44 -08:00
parent 1b2a368ae0
commit 858618f41c
1 changed files with 32 additions and 2 deletions

View File

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