Skip to content

Commit fc6748e

Browse files
authored
Merge pull request #40 from mattico/comparator
Implement most comparator features
2 parents 77dace3 + 8a49086 commit fc6748e

File tree

8 files changed

+677
-38
lines changed

8 files changed

+677
-38
lines changed

examples/comp.rs

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#![deny(warnings)]
2+
#![deny(unsafe_code)]
3+
#![no_main]
4+
#![no_std]
5+
6+
extern crate cortex_m;
7+
extern crate cortex_m_rt as rt;
8+
extern crate panic_halt;
9+
extern crate stm32g0xx_hal as hal;
10+
11+
use hal::analog::comparator::{Config, Hysteresis, RefintInput};
12+
use hal::prelude::*;
13+
use hal::stm32;
14+
use rt::entry;
15+
16+
#[entry]
17+
fn main() -> ! {
18+
let dp = stm32::Peripherals::take().expect("cannot take peripherals");
19+
let mut rcc = dp.RCC.constrain();
20+
21+
let gpioa = dp.GPIOA.split(&mut rcc);
22+
23+
let (comp1, comp2) = dp.COMP.split(&mut rcc);
24+
25+
let pa1 = gpioa.pa1.into_analog();
26+
let pa0 = gpioa.pa0.into_analog();
27+
let comp1 = comp1.comparator(pa1, pa0, Config::default(), &rcc.clocks);
28+
let comp1 = comp1.enable();
29+
let mut led1 = gpioa.pa5.into_push_pull_output();
30+
31+
let pa3 = gpioa.pa3.into_analog();
32+
let comp2 = comp2.comparator(
33+
pa3,
34+
RefintInput::VRefintM12,
35+
Config::default()
36+
.hysteresis(Hysteresis::High)
37+
.output_inverted(),
38+
&rcc.clocks,
39+
);
40+
let led2 = gpioa.pa2.into_push_pull_output();
41+
// Configure PA2 to the comparator's alternate function so it gets
42+
// changed directly by the comparator.
43+
comp2.output_pin(led2);
44+
let _comp2 = comp2.enable();
45+
46+
loop {
47+
match comp1.output() {
48+
true => led1.set_high().unwrap(),
49+
false => led1.set_low().unwrap(),
50+
}
51+
}
52+
}

examples/comp_window.rs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#![deny(warnings)]
2+
#![deny(unsafe_code)]
3+
#![no_main]
4+
#![no_std]
5+
6+
extern crate cortex_m;
7+
extern crate cortex_m_rt as rt;
8+
extern crate panic_halt;
9+
extern crate stm32g0xx_hal as hal;
10+
11+
use hal::analog::comparator::{self, Config, Hysteresis, RefintInput};
12+
use hal::prelude::*;
13+
use hal::stm32;
14+
use rt::entry;
15+
16+
#[entry]
17+
fn main() -> ! {
18+
let dp = stm32::Peripherals::take().expect("cannot take peripherals");
19+
let mut rcc = dp.RCC.constrain();
20+
21+
let gpioa = dp.GPIOA.split(&mut rcc);
22+
23+
let pa1 = gpioa.pa1.into_analog();
24+
25+
let comp = comparator::window_comparator12(
26+
dp.COMP,
27+
pa1,
28+
RefintInput::VRefintM14,
29+
RefintInput::VRefintM34,
30+
Config::default().hysteresis(Hysteresis::Medium),
31+
&mut rcc,
32+
);
33+
let comp = comp.enable();
34+
35+
let mut led1 = gpioa.pa5.into_push_pull_output();
36+
let mut led2 = gpioa.pa6.into_push_pull_output();
37+
38+
loop {
39+
match comp.output() {
40+
true => led1.set_high().unwrap(),
41+
false => led1.set_low().unwrap(),
42+
}
43+
match comp.above_lower() {
44+
true => led2.set_high().unwrap(),
45+
false => led2.set_low().unwrap(),
46+
}
47+
}
48+
}

0 commit comments

Comments
 (0)