Skip to content

Commit 655b74e

Browse files
committed
add complementary example
1 parent 2268f0f commit 655b74e

File tree

1 file changed

+60
-0
lines changed

1 file changed

+60
-0
lines changed

examples/pwm_complementary.rs

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
#![deny(unsafe_code)]
2+
#![no_main]
3+
#![no_std]
4+
5+
// Halt on panic
6+
use panic_halt as _;
7+
8+
use cortex_m_rt::entry;
9+
10+
use stm32f0xx_hal as hal;
11+
12+
use hal::{pac, prelude::*, pwm, delay::Delay};
13+
use cortex_m;
14+
15+
#[entry]
16+
fn main() -> ! {
17+
if let Some(mut dp) = pac::Peripherals::take() {
18+
// Set up the system clock.
19+
let mut rcc = dp.RCC.configure().sysclk(8.mhz()).freeze(&mut dp.FLASH);
20+
21+
let gpioa = dp.GPIOA.split(&mut rcc);
22+
let channels = cortex_m::interrupt::free(move |cs| {
23+
(
24+
gpioa.pa8.into_alternate_af2(cs), // on TIM1_CH1
25+
gpioa.pa7.into_alternate_af2(cs) // on TIM1_CH1N
26+
)
27+
});
28+
29+
let pwm = pwm::tim1(dp.TIM1, channels, &mut rcc, 20u32.khz());
30+
let (mut ch1, mut ch1n) = pwm;
31+
let max_duty = ch1.get_max_duty();
32+
ch1.set_duty(max_duty / 2);
33+
ch1.enable();
34+
ch1n.enable();
35+
36+
// simple duty sweep
37+
if let Some(cp) = cortex_m::Peripherals::take() {
38+
let mut delay = Delay::new(cp.SYST, &rcc);
39+
40+
let steps = 100;
41+
42+
loop {
43+
for i in 0..steps {
44+
ch1.set_duty(max_duty / steps * i);
45+
delay.delay_ms(30u16);
46+
}
47+
48+
for i in (1..steps).rev() {
49+
ch1.set_duty(max_duty / steps * i);
50+
delay.delay_ms(30u16);
51+
}
52+
}
53+
}
54+
}
55+
56+
// something went wrong when acquiring peripheral access
57+
loop {
58+
cortex_m::asm::nop();
59+
}
60+
}

0 commit comments

Comments
 (0)