Skip to content

Commit d8a007f

Browse files
update example comments
1 parent 54cf1ed commit d8a007f

File tree

1 file changed

+10
-24
lines changed

1 file changed

+10
-24
lines changed

examples/adc.rs

Lines changed: 10 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ fn main() -> ! {
1919
let clocks = rcc.cfgr.freeze(&mut dp.FLASH.constrain().acr);
2020

2121
// set up adc1
22-
#[rustfmt::skip]
2322
let mut adc1 = adc::Adc::adc1(
2423
dp.ADC1, // The ADC we are going to control
2524
// The following is only needed to make sure the clock signal for the ADC is set up
@@ -30,47 +29,34 @@ fn main() -> ! {
3029
clocks,
3130
// If everything is set up correctly, we'll get `Some(adc1)`. to access it, we need to `unwrap`
3231
// it. If there was an error, we'll get `None` and this unwrap will `panic!`.
33-
).unwrap();
32+
)
33+
.unwrap();
3434

3535
// Set up pin PA0 as analog pin.
3636
// This pin is connected to the user button on the stm32f3discovery board.
3737
let mut gpio_a = dp.GPIOA.split(&mut rcc.ahb);
3838
let mut adc1_in1_pin = gpio_a.pa0.into_analog(&mut gpio_a.moder, &mut gpio_a.pupdr);
3939

40-
// Be aware that the values in the table below depend on the input of VREF
41-
// Also know that and that the ADC hardware unit always rounds down.
42-
//
40+
// Be aware that the values in the table below depend on the input of VREF.
4341
// To have a stable VREF input, put a condensator and a volt limiting diode in front of it.
4442
//
45-
// To compensate for the unit and integer math rounding down, use the following formula:
46-
// ```rust
47-
// // [setup as above]
48-
// let mut adc: u3216 = adc1.read(&mut adc1_in1_pin).expect('Error reading adc1.');
49-
// // add '0.5' to the adc value, compensating the ADC hardware rounding down
50-
// adc <<= 1;
51-
// adc += 1;
52-
// // multiply it by '300 mV', converting to your voltage level
53-
// let mut volt_mv = adc * 300;
54-
// // make up for the coming integer division
55-
// volt_mv += 1 << 12;
56-
// // undo the first bitshift above and the 12bit ADC size
57-
// volt_mv >>= 13;
58-
// println!("Was reading {} mV", volt_mv);
59-
// ```
43+
// Also know that integer division and the ADC hardware unit always round down.
44+
// To make up for those errors, see this forum entry:
45+
// [https://forum.allaboutcircuits.com/threads/why-adc-1024-is-correct-and-adc-1023-is-just-plain-wrong.80018/]
6046
hprintln!("
6147
The ADC has a 12 bit resolution, i.e. if your reference Value is 3V:
6248
approx. ADC value | approx. volt value
6349
==================+===================
64-
0 | 0 mV
65-
2048 | 150 mV
66-
4095 | 300 mV
50+
0 | 0 mV
51+
2048 | 1500 mV
52+
4095 | 3000 mV
6753
6854
If you are using a STM32F3Discovery, PA0 is connected to the User Button.
6955
Pressing it should connect the user Button to to HIGH and the value should change from 0 to 4095.
7056
").expect("Error using hprintln.");
7157

7258
loop {
7359
let adc1_in1_data: u16 = adc1.read(&mut adc1_in1_pin).expect("Error reading adc1.");
74-
hprintln!("PA0 reads {}", adc1_in1_data).unwrap_or(());
60+
hprintln!("PA0 reads {}", adc1_in1_data).ok();
7561
}
7662
}

0 commit comments

Comments
 (0)