Reading analog input with an STM32F103C8T6 board. ADC is calibrated to achieve higher accuracy.
Reading analog input with STM32F103C8T6 board
Import the program into your online compiler.
Schematic
main.cpp@0:5d7148c1f26a, 2017-11-18 (annotated)
- Committer:
- hudakz
- Date:
- Sat Nov 18 14:31:54 2017 +0000
- Revision:
- 0:5d7148c1f26a
- Child:
- 1:7522adef9bd0
Initial release.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
hudakz | 0:5d7148c1f26a | 1 | #include "stm32f103c8t6.h" |
hudakz | 0:5d7148c1f26a | 2 | #include "mbed.h" |
hudakz | 0:5d7148c1f26a | 3 | #include "AnalogInCal.h" |
hudakz | 0:5d7148c1f26a | 4 | |
hudakz | 0:5d7148c1f26a | 5 | const float REF_VOLT = 3300; // Reference voltage in milivolts |
hudakz | 0:5d7148c1f26a | 6 | |
hudakz | 0:5d7148c1f26a | 7 | int main() { |
hudakz | 0:5d7148c1f26a | 8 | confSysClock(); //Configure system clock (72MHz HSE clock, 48MHz USB clock) |
hudakz | 0:5d7148c1f26a | 9 | |
hudakz | 0:5d7148c1f26a | 10 | Serial pc(PA_2, PA_3); |
hudakz | 0:5d7148c1f26a | 11 | DigitalOut myled(LED1); |
hudakz | 0:5d7148c1f26a | 12 | // AnalogIn analogIn(PA_0); // Create an uncalibrated analog input |
hudakz | 0:5d7148c1f26a | 13 | AnalogInCal analogIn(PA_0); // Create an analog input with calibrated AD convertor |
hudakz | 0:5d7148c1f26a | 14 | float voltage; |
hudakz | 0:5d7148c1f26a | 15 | |
hudakz | 0:5d7148c1f26a | 16 | while (1) { |
hudakz | 0:5d7148c1f26a | 17 | voltage = analogIn.read() * REF_VOLT; // convert analog to digital |
hudakz | 0:5d7148c1f26a | 18 | pc.printf("voltage = %.0f mV\r\n", voltage); |
hudakz | 0:5d7148c1f26a | 19 | myled = !myled; |
hudakz | 0:5d7148c1f26a | 20 | wait_ms(1000); |
hudakz | 0:5d7148c1f26a | 21 | } |
hudakz | 0:5d7148c1f26a | 22 | } |
hudakz | 0:5d7148c1f26a | 23 |