Skip to content

Commit

Permalink
Merge pull request #47 from strom-und-spiele/adc
Browse files Browse the repository at this point in the history
Add ADC usage (for `stm32f303` devices)
  • Loading branch information
teskje committed Jul 19, 2020
2 parents 59f2221 + 26610c6 commit 2ea16b4
Show file tree
Hide file tree
Showing 5 changed files with 615 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
- Implement `InputPin` for `Output<OpenDrain>` pins ([#114](https://github.com/stm32-rs/stm32f3xx-hal/pull/114))
- Support for safe one-shot DMA transfers ([#86](https://github.com/stm32-rs/stm32f3xx-hal/pull/86))
- DMA support for serial reception and transmission ([#86](https://github.com/stm32-rs/stm32f3xx-hal/pull/86))
- ADC support for `stm32f303` devices.

### Fixed

Expand Down
5 changes: 5 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ default-features = false
panic-semihosting = "0.5"
usb-device = "0.2"
usbd-serial = "0.1"
cortex-m-semihosting = "0.3"

[features]
default = ["unproven"]
Expand Down Expand Up @@ -106,3 +107,7 @@ required-features = ["stm32f303"]
[[example]]
name = "serial_dma"
required-features = ["stm32f303"]

[[example]]
name = "adc"
required-features = ["stm32f303"]
59 changes: 59 additions & 0 deletions examples/adc.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#![no_std]
#![no_main]

//! Example usage for ADC on STM32F303

extern crate panic_semihosting;

use cortex_m_rt::entry;
use cortex_m_semihosting::hprintln;

use stm32f3xx_hal::{adc, pac, prelude::*};

#[entry]
/// Main Thread
fn main() -> ! {
// Get peripherals, clocks and freeze them
let mut dp = pac::Peripherals::take().unwrap();
let mut rcc = dp.RCC.constrain();
let clocks = rcc.cfgr.freeze(&mut dp.FLASH.constrain().acr);

// set up adc1
let mut adc1 = adc::Adc::adc1(
dp.ADC1, // The ADC we are going to control
// The following is only needed to make sure the clock signal for the ADC is set up
// correctly.
&mut dp.ADC1_2,
&mut rcc.ahb,
adc::CkMode::default(),
clocks,
);

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

// Be aware that the values in the table below depend on the input of VREF.
// To have a stable VREF input, put a condensator and a volt limiting diode in front of it.
//
// Also know that integer division and the ADC hardware unit always round down.
// To make up for those errors, see this forum entry:
// [https://forum.allaboutcircuits.com/threads/why-adc-1024-is-correct-and-adc-1023-is-just-plain-wrong.80018/]
hprintln!("
The ADC has a 12 bit resolution, i.e. if your reference Value is 3V:
approx. ADC value | approx. volt value
==================+===================
0 | 0 mV
2048 | 1500 mV
4095 | 3000 mV
If you are using a STM32F3Discovery, PA0 is connected to the User Button.
Pressing it should connect the user Button to to HIGH and the value should change from 0 to 4095.
").expect("Error using hprintln.");

loop {
let adc1_in1_data: u16 = adc1.read(&mut adc1_in1_pin).expect("Error reading adc1.");
hprintln!("PA0 reads {}", adc1_in1_data).ok();
}
}

0 comments on commit 2ea16b4

Please sign in to comment.