1
Fork 0

extract timer configuration

This commit is contained in:
Andy Killorin 2024-05-01 16:04:11 -05:00
parent bc920907bd
commit 1f098f70b8
Signed by: ank
GPG key ID: 23F9463ECB67FE8C
2 changed files with 13 additions and 10 deletions

View file

@ -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);

View file

@ -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