Skip to content

Commit

Permalink
demo: add pwm demo for ch32v103
Browse files Browse the repository at this point in the history
  • Loading branch information
andelf committed May 5, 2024
1 parent fec9bef commit 4a045b9
Showing 1 changed file with 44 additions and 0 deletions.
44 changes: 44 additions & 0 deletions examples/ch32v103/src/bin/pwm.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#![no_std]
#![no_main]
#![feature(type_alias_impl_trait)]

use hal::delay::Delay;
use hal::println;
use hal::time::Hertz;
use hal::timer::low_level::CountingMode;
use hal::timer::simple_pwm::{PwmPin, SimplePwm};
use {ch32_hal as hal, panic_halt as _};

#[qingke_rt::entry]
fn main() -> ! {
hal::debug::SDIPrint::enable();
let p = hal::init(Default::default());

let ch1 = PwmPin::new_ch1::<0>(p.PA8);
let mut pwm = SimplePwm::new(
p.TIM1,
Some(ch1),
None,
None,
None,
Hertz::khz(1),
CountingMode::default(),
);
let ch = hal::timer::Channel::Ch1;

let max_duty = pwm.get_max_duty();
println!("max duty: {}", max_duty);
pwm.set_duty(ch, 2000);
pwm.enable(ch);

loop {
for i in 0..100 {
pwm.set_duty(ch, i * 80);
Delay.delay_ms(1_0);
}
for i in (0..100).rev() {
pwm.set_duty(ch, i * 80);
Delay.delay_ms(1_0);
}
}
}

0 comments on commit 4a045b9

Please sign in to comment.