Add DACv1 example for F4

This commit is contained in:
chemicstry 2022-08-04 03:31:59 +03:00
parent 206b7fd8ed
commit 8a25906eff
3 changed files with 38 additions and 1 deletions

View File

@ -20,7 +20,6 @@ panic-probe = { version = "0.3", features = ["print-defmt"] }
futures = { version = "0.3.17", default-features = false, features = ["async-await"] } futures = { version = "0.3.17", default-features = false, features = ["async-await"] }
heapless = { version = "0.7.5", default-features = false } heapless = { version = "0.7.5", default-features = false }
nb = "1.0.0" nb = "1.0.0"
micromath = "2.0.0"
[profile.dev] [profile.dev]
opt-level = "s" opt-level = "s"

View File

@ -21,6 +21,7 @@ futures = { version = "0.3.17", default-features = false, features = ["async-awa
heapless = { version = "0.7.5", default-features = false } heapless = { version = "0.7.5", default-features = false }
nb = "1.0.0" nb = "1.0.0"
embedded-storage = "0.3.0" embedded-storage = "0.3.0"
micromath = "2.0.0"
usb-device = "0.2" usb-device = "0.2"
usbd-serial = "0.1.1" usbd-serial = "0.1.1"

View File

@ -0,0 +1,37 @@
#![no_std]
#![no_main]
#![feature(type_alias_impl_trait)]
use defmt::*;
use embassy_executor::executor::Spawner;
use embassy_stm32::dac::{Channel, Dac, Value};
use embassy_stm32::Peripherals;
use {defmt_rtt as _, panic_probe as _};
#[embassy_executor::main]
async fn main(_spawner: Spawner, p: Peripherals) -> ! {
info!("Hello World, dude!");
let mut dac = Dac::new_1ch(p.DAC, p.PA4);
loop {
for v in 0..=255 {
unwrap!(dac.set(Channel::Ch1, Value::Bit8(to_sine_wave(v))));
unwrap!(dac.trigger(Channel::Ch1));
}
}
}
use micromath::F32Ext;
fn to_sine_wave(v: u8) -> u8 {
if v >= 128 {
// top half
let r = 3.14 * ((v - 128) as f32 / 128.0);
(r.sin() * 128.0 + 127.0) as u8
} else {
// bottom half
let r = 3.14 + 3.14 * (v as f32 / 128.0);
(r.sin() * 128.0 + 127.0) as u8
}
}