Skip to content

Commit

Permalink
Added a PWM example that puts the timer in PWM mode using the specifi…
Browse files Browse the repository at this point in the history
…ed pin with a frequency of 100Hz and a duty cycle of 50% (#43)

Co-authored-by: Nando Bongers <n.bongers@marpower.com>
  • Loading branch information
NandoBongers and Nando Bongers authored Jan 10, 2022
1 parent c709aca commit 7fbce26
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions examples/pwm.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
//This example puts the timer in PWM mode using the specified pin with a frequency of 100Hz and a duty cycle of 50%.
#![no_main]
#![no_std]

use cortex_m_rt::entry;
use hal::gpio::gpioa::PA8;
use hal::gpio::Alternate;
use hal::gpio::AF6;
use hal::prelude::*;
use hal::stm32;
use stm32g4xx_hal as hal;
mod utils;
extern crate cortex_m_rt as rt;

#[entry]
fn main() -> ! {
let dp = stm32::Peripherals::take().expect("cannot take peripherals");
let mut rcc = dp.RCC.constrain();
let gpioa = dp.GPIOA.split(&mut rcc);
let pin: PA8<Alternate<AF6>> = gpioa.pa8.into_alternate();

let mut pwm = dp.TIM1.pwm(pin, 100.hz(), &mut rcc);

pwm.set_duty(pwm.get_max_duty() / 2);
pwm.enable();

loop {
cortex_m::asm::nop()
}
}

0 comments on commit 7fbce26

Please sign in to comment.