From 3bad1b474fb80856049d4c6a0eaad3a2a71c2cc8 Mon Sep 17 00:00:00 2001 From: JP Stringham Date: Fri, 13 Feb 2026 23:08:16 -0500 Subject: [PATCH] HUGE FIX for frequency setting --- src/main.rs | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/src/main.rs b/src/main.rs index d252563..1dc35c3 100644 --- a/src/main.rs +++ b/src/main.rs @@ -128,8 +128,7 @@ fn main() -> ! { const MAX_TUNE: u16 = 350u16; const MIN_TUNE: u16 = 4u16; - pwm0.set_top(current_tune); - pwm0.channel_a.set_duty(current_tune / 2); + set_tune(current_tune, pwm0); // Create the I²C drive let mut i2c = hal::I2C::i2c0( @@ -156,7 +155,7 @@ fn main() -> ! { gfx_buf.clear(); gfx_buf.draw_string(20, 14, "Hello Radio!"); - let tuned_freq = 272_000u32 / (current_tune as u32); + let tuned_freq = 300_000 / (current_tune as u32); let mut tune_str_buf = [0u8; 5]; let len = u32_into_str(tuned_freq, &mut tune_str_buf); let tune_str = str::from_utf8(&tune_str_buf[tune_str_buf.len() - len..]).unwrap(); @@ -201,24 +200,30 @@ fn main() -> ! { } if tune_up_input.is_low().unwrap() { - info!("Tune UP"); + info!("Tune DOWN"); last_input = tick; current_tune = MAX_TUNE.min(current_tune + 2); - pwm0.set_top(current_tune); - pwm0.channel_a.set_duty(current_tune / 2); + set_tune(current_tune, pwm0); continue; } if tune_dn_input.is_low().unwrap() { - info!("Tune DOWN"); + info!("Tune UP"); last_input = tick; current_tune = MIN_TUNE.max(current_tune - 2); - pwm0.set_top(current_tune); - pwm0.channel_a.set_duty(current_tune / 2); + set_tune(current_tune, pwm0); } } } +fn set_tune( + tune: u16, + pwm: &mut rp2040_hal::pwm::Slice, +) { + pwm.set_top(tune - 1); + pwm.channel_a.set_duty(tune / 2); +} + // returns the length of the final str removing leading zeroes fn u32_into_str(mut value: u32, str_buf: &mut [u8]) -> usize { let mut len = str_buf.len();