Skip to content

Commit 7fbce26

Browse files
NandoBongersNando Bongers
andauthored
Added a PWM example that puts the timer in PWM mode using the specified pin with a frequency of 100Hz and a duty cycle of 50% (#43)
Co-authored-by: Nando Bongers <[email protected]>
1 parent c709aca commit 7fbce26

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

examples/pwm.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
//This example puts the timer in PWM mode using the specified pin with a frequency of 100Hz and a duty cycle of 50%.
2+
#![no_main]
3+
#![no_std]
4+
5+
use cortex_m_rt::entry;
6+
use hal::gpio::gpioa::PA8;
7+
use hal::gpio::Alternate;
8+
use hal::gpio::AF6;
9+
use hal::prelude::*;
10+
use hal::stm32;
11+
use stm32g4xx_hal as hal;
12+
mod utils;
13+
extern crate cortex_m_rt as rt;
14+
15+
#[entry]
16+
fn main() -> ! {
17+
let dp = stm32::Peripherals::take().expect("cannot take peripherals");
18+
let mut rcc = dp.RCC.constrain();
19+
let gpioa = dp.GPIOA.split(&mut rcc);
20+
let pin: PA8<Alternate<AF6>> = gpioa.pa8.into_alternate();
21+
22+
let mut pwm = dp.TIM1.pwm(pin, 100.hz(), &mut rcc);
23+
24+
pwm.set_duty(pwm.get_max_duty() / 2);
25+
pwm.enable();
26+
27+
loop {
28+
cortex_m::asm::nop()
29+
}
30+
}

0 commit comments

Comments
 (0)