led on gpio2 working

This commit is contained in:
Priec
2025-10-12 14:16:17 +02:00
parent 3181081b09
commit 75f038262b

View File

@@ -42,42 +42,26 @@ async fn main(spawner: Spawner) {
let mut ledc = Ledc::new(peripherals.LEDC); let mut ledc = Ledc::new(peripherals.LEDC);
ledc.set_global_slow_clock(LSGlobalClkSource::APBClk); ledc.set_global_slow_clock(LSGlobalClkSource::APBClk);
let mut pwm_timer = ledc.timer::<LowSpeed>(timer::Number::Timer0); let mut lstimer0 = ledc.timer::<LowSpeed>(timer::Number::Timer0);
pwm_timer lstimer0.configure(timer::config::Config {
.configure(timer::config::Config { duty: timer::config::Duty::Duty5Bit,
duty: timer::config::Duty::Duty14Bit, clock_source: timer::LSClockSource::APBClk,
frequency: Rate::from_hz(1000), frequency: Rate::from_khz(24),
clock_source: timer::LSClockSource::APBClk, }).unwrap();
})
.unwrap();
let mut pwm_channel = let led = peripherals.GPIO2;
ledc.channel::<LowSpeed>(channel::Number::Channel0, peripherals.GPIO1); let mut channel0 = ledc.channel(channel::Number::Channel0, led);
channel0.configure(channel::config::Config {
pwm_channel timer: &lstimer0,
.configure(channel::config::Config { duty_pct: 10,
timer: &pwm_timer, pin_config: channel::config::PinConfig::PushPull,
duty_pct: 0, }).unwrap();
pin_config: channel::config::PinConfig::PushPull,
})
.unwrap();
info!("LEDC configured successfully");
let mut brightness: i32 = 0;
let mut direction: i32 = 5;
loop { loop {
// Set current brightness level (0100%) channel0.start_duty_fade(0, 100, 1000).unwrap();
pwm_channel.set_duty(brightness as u8).ok(); while channel0.is_duty_fade_running() {}
channel0.start_duty_fade(100, 0, 1000).unwrap();
brightness += direction; while channel0.is_duty_fade_running() {}
if brightness >= 100 {
brightness = 100;
direction = -5;
} else if brightness <= 0 {
brightness = 0;
direction = 5;
}
Timer::after(Duration::from_millis(30)).await; Timer::after(Duration::from_millis(30)).await;
} }