a runtime generated sin table example
This commit is contained in:
parent
1d1d8a848e
commit
ef95441442
@ -31,3 +31,4 @@ panic-probe = { version = "0.2.0", features = ["print-defmt"] }
|
||||
futures = { version = "0.3.17", default-features = false, features = ["async-await"] }
|
||||
rand = { version = "0.8.4", default-features = false }
|
||||
embedded-storage = "0.2.0"
|
||||
micromath = "2.0.0"
|
41
examples/nrf/src/bin/pwm_simple_sin.rs
Normal file
41
examples/nrf/src/bin/pwm_simple_sin.rs
Normal file
@ -0,0 +1,41 @@
|
||||
#![no_std]
|
||||
#![no_main]
|
||||
#![feature(type_alias_impl_trait)]
|
||||
#![feature(array_from_fn)]
|
||||
|
||||
#[path = "../example_common.rs"]
|
||||
mod example_common;
|
||||
use defmt::*;
|
||||
use embassy::executor::Spawner;
|
||||
use embassy::time::{Duration, Timer};
|
||||
use embassy_nrf::gpio::NoPin;
|
||||
use embassy_nrf::pwm::{CounterMode, LoopingConfig, Prescaler, Pwm, SequenceLoad};
|
||||
use embassy_nrf::Peripherals;
|
||||
use micromath::F32Ext;
|
||||
|
||||
const W1: f32 = core::f32::consts::PI / 128.0;
|
||||
|
||||
#[embassy::main]
|
||||
async fn main(_spawner: Spawner, p: Peripherals) {
|
||||
// probably not best use of resources to create the table at runtime, but makes testing fast
|
||||
let seq_values: [u16; 220] = core::array::from_fn(|n| ((W1 * n as f32).sin() * 10000.0) as u16);
|
||||
|
||||
let config = LoopingConfig {
|
||||
counter_mode: CounterMode::UpAndDown,
|
||||
top: 12000,
|
||||
prescaler: Prescaler::Div16,
|
||||
sequence: &seq_values,
|
||||
sequence_load: SequenceLoad::Common,
|
||||
repeats: 0,
|
||||
enddelay: 0,
|
||||
};
|
||||
|
||||
let _pwm = unwrap!(Pwm::simple_playback(
|
||||
p.PWM0, p.P0_13, NoPin, NoPin, NoPin, config
|
||||
));
|
||||
info!("pwm started!");
|
||||
|
||||
loop {
|
||||
Timer::after(Duration::from_millis(1000)).await;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user