2021-11-03 23:26:44 +01:00
|
|
|
#![no_std]
|
|
|
|
#![no_main]
|
|
|
|
#![feature(type_alias_impl_trait)]
|
|
|
|
|
|
|
|
use defmt::*;
|
2022-08-17 23:40:16 +02:00
|
|
|
use embassy_executor::Spawner;
|
2021-11-04 02:25:44 +01:00
|
|
|
use embassy_nrf::pwm::{Prescaler, SimplePwm};
|
2023-10-15 01:57:25 +02:00
|
|
|
use embassy_time::Timer;
|
2022-06-12 22:15:44 +02:00
|
|
|
use {defmt_rtt as _, panic_probe as _};
|
2022-04-02 04:35:06 +02:00
|
|
|
|
2022-07-29 21:58:35 +02:00
|
|
|
#[embassy_executor::main]
|
2022-08-17 18:49:55 +02:00
|
|
|
async fn main(_spawner: Spawner) {
|
|
|
|
let p = embassy_nrf::init(Default::default());
|
2022-02-12 01:04:01 +01:00
|
|
|
let mut pwm = SimplePwm::new_1ch(p.PWM0, p.P0_05);
|
2021-11-03 23:26:44 +01:00
|
|
|
// sg90 microervo requires 50hz or 20ms period
|
|
|
|
// set_period can only set down to 125khz so we cant use it directly
|
|
|
|
// Div128 is 125khz or 0.000008s or 0.008ms, 20/0.008 is 2500 is top
|
|
|
|
pwm.set_prescaler(Prescaler::Div128);
|
|
|
|
pwm.set_max_duty(2500);
|
|
|
|
info!("pwm initialized!");
|
|
|
|
|
2023-10-15 01:57:25 +02:00
|
|
|
Timer::after_millis(5000).await;
|
2021-11-03 23:26:44 +01:00
|
|
|
|
|
|
|
// 1ms 0deg (1/.008=125), 1.5ms 90deg (1.5/.008=187.5), 2ms 180deg (2/.008=250),
|
|
|
|
loop {
|
|
|
|
info!("45 deg");
|
2021-11-12 07:32:34 +01:00
|
|
|
// poor mans inverting, subtract our value from max_duty
|
2021-11-03 23:26:44 +01:00
|
|
|
pwm.set_duty(0, 2500 - 156);
|
2023-10-15 01:57:25 +02:00
|
|
|
Timer::after_millis(5000).await;
|
2021-11-03 23:26:44 +01:00
|
|
|
|
|
|
|
info!("90 deg");
|
|
|
|
pwm.set_duty(0, 2500 - 187);
|
2023-10-15 01:57:25 +02:00
|
|
|
Timer::after_millis(5000).await;
|
2021-11-03 23:26:44 +01:00
|
|
|
|
|
|
|
info!("135 deg");
|
|
|
|
pwm.set_duty(0, 2500 - 218);
|
2023-10-15 01:57:25 +02:00
|
|
|
Timer::after_millis(5000).await;
|
2021-11-03 23:26:44 +01:00
|
|
|
|
|
|
|
info!("180 deg");
|
|
|
|
pwm.set_duty(0, 2500 - 250);
|
2023-10-15 01:57:25 +02:00
|
|
|
Timer::after_millis(5000).await;
|
2021-11-03 23:26:44 +01:00
|
|
|
|
|
|
|
info!("0 deg");
|
|
|
|
pwm.set_duty(0, 2500 - 125);
|
2023-10-15 01:57:25 +02:00
|
|
|
Timer::after_millis(5000).await;
|
2021-11-03 23:26:44 +01:00
|
|
|
}
|
|
|
|
}
|