Skip to content

ADC reads are out of order at certain frequencies #201

Closed
@TheZoq2

Description

@TheZoq2

At some ADC clock values, the ADC reads values out of order, or to the wrong channels.

let clocks = rcc.cfgr.freeze(&mut flash.acr);
hprintln!("adc freq: {}", clocks.adcclk().0).unwrap();

let mut adc1 = adc::Adc::adc1(p.ADC1, &mut rcc.apb2, clocks);

let mut gpioa = p.GPIOA.split(&mut rcc.apb2);

let mut ch0 = gpioa.pa0.into_analog(&mut gpioa.crl);
let mut ch1 = gpioa.pa1.into_analog(&mut gpioa.crl);
let mut ch2 = gpioa.pa2.into_analog(&mut gpioa.crl);

loop {
    let r1: u16 = adc1.read(&mut ch0).unwrap();
    let r2: u16 = adc1.read(&mut ch1).unwrap();
    let r3: u16 = adc1.read(&mut ch2).unwrap();

    hprintln!("{}, {}, {}", r1, r2, r3);
}

Using the above modified examples/adc.rs, with 3.3V connected to to PA0, 0V
connected to PA1, and 2V connected to PA2, the readings returned are

2514, 4095, 0
2516, 4095, 0
2519, 4092, 0
2514, 4094, 0
2528, 4094, 0
2531, 4095, 1
2512, 4093, 0
2506, 4095, 0

not 4096, 0, 2510 as expected.

This can be fixed by setting the adcclk to 2 MHz in the clock initialisation.

I'm going to work on a patch to fix this, but i'm reporting it now so noone
else has to debug the issue :)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions