From 1f098f70b8f284ca96b1737a203b66f574ee3487 Mon Sep 17 00:00:00 2001 From: Andy Killorin <37423245+Speedy6451@users.noreply.github.com> Date: Wed, 1 May 2024 16:04:11 -0500 Subject: [PATCH] extract timer configuration --- src/main.rs | 14 ++++---------- src/servo.rs | 9 +++++++++ 2 files changed, 13 insertions(+), 10 deletions(-) diff --git a/src/main.rs b/src/main.rs index b84da1f..73f0b1f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,7 +3,7 @@ use panic_halt as _; mod servo; -use servo::Servo; +use servo::{configure_timer_one, Servo}; #[arduino_hal::entry] fn main() -> ! { @@ -11,15 +11,9 @@ fn main() -> ! { let pins = arduino_hal::pins!(dp); // configure timer for servos - pins.d9.into_output(); - pins.d10.into_output(); - let tc1 = dp.TC1; // 250kHz - tc1.icr1.write(|w| w.bits(4999)); // 250kHz/5000 = 50Hz - tc1.tccr1a.write(|w| w.wgm1().bits(0b10).com1a().match_clear()); - tc1.tccr1b.write(|w| w.wgm1().bits(0b11).cs1().prescale_64()); - - let carriage = &tc1.ocr1a; // pin 9 - let claw = &tc1.ocr1b; // pin 10 + pins.d9.into_output(); // carriage + pins.d10.into_output(); // claw + let (carriage, claw) = configure_timer_one(&dp.TC1); loop { carriage.set_speed(1.0); diff --git a/src/servo.rs b/src/servo.rs index 5e4c342..07b5737 100644 --- a/src/servo.rs +++ b/src/servo.rs @@ -1,5 +1,6 @@ use arduino_hal::pac::tc1::OCR1B; use arduino_hal::pac::tc1::OCR1A; +use arduino_hal::pac::TC1; const MIN: u16 = 100; // *4us = .4ms const MAX: u16 = 700; // *4us = 2.8ms @@ -34,6 +35,14 @@ impl Servo for OCR1B { // pin 10 } } +pub fn configure_timer_one(timer: &TC1) -> (&OCR1A, &OCR1B) { + timer.icr1.write(|w| w.bits(4999)); // 250kHz/5000 = 50Hz + timer.tccr1a.write(|w| w.wgm1().bits(0b10).com1a().match_clear()); + timer.tccr1b.write(|w| w.wgm1().bits(0b11).cs1().prescale_64()); + + (&timer.ocr1a, &timer.ocr1b) +} + #[cfg(test)] mod tests { // unit tests don't run on avr-hal-template projects, could be due to target rules, could be