@@ -19,7 +19,6 @@ fn main() -> ! {
19
19
let clocks = rcc. cfgr . freeze ( & mut dp. FLASH . constrain ( ) . acr ) ;
20
20
21
21
// set up adc1
22
- #[ rustfmt:: skip]
23
22
let mut adc1 = adc:: Adc :: adc1 (
24
23
dp. ADC1 , // The ADC we are going to control
25
24
// The following is only needed to make sure the clock signal for the ADC is set up
@@ -30,47 +29,34 @@ fn main() -> ! {
30
29
clocks,
31
30
// If everything is set up correctly, we'll get `Some(adc1)`. to access it, we need to `unwrap`
32
31
// it. If there was an error, we'll get `None` and this unwrap will `panic!`.
33
- ) . unwrap ( ) ;
32
+ )
33
+ . unwrap ( ) ;
34
34
35
35
// Set up pin PA0 as analog pin.
36
36
// This pin is connected to the user button on the stm32f3discovery board.
37
37
let mut gpio_a = dp. GPIOA . split ( & mut rcc. ahb ) ;
38
38
let mut adc1_in1_pin = gpio_a. pa0 . into_analog ( & mut gpio_a. moder , & mut gpio_a. pupdr ) ;
39
39
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.
43
41
// To have a stable VREF input, put a condensator and a volt limiting diode in front of it.
44
42
//
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/]
60
46
hprintln ! ( "
61
47
The ADC has a 12 bit resolution, i.e. if your reference Value is 3V:
62
48
approx. ADC value | approx. volt value
63
49
==================+===================
64
- 0 | 0 mV
65
- 2048 | 150 mV
66
- 4095 | 300 mV
50
+ 0 | 0 mV
51
+ 2048 | 1500 mV
52
+ 4095 | 3000 mV
67
53
68
54
If you are using a STM32F3Discovery, PA0 is connected to the User Button.
69
55
Pressing it should connect the user Button to to HIGH and the value should change from 0 to 4095.
70
56
" ) . expect ( "Error using hprintln." ) ;
71
57
72
58
loop {
73
59
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 ( ) ;
75
61
}
76
62
}
0 commit comments