#[embassy_executor::main]
async fn main(_spawner: Spawner) {
let mut read_buffer = unsafe { &mut DMA_BUF[..] };
let mut config = Config::default();
{
use embassy_stm32::rcc::*;
// config.rcc.hse = Some(
// Hse{
// freq:Hertz::mhz(8),
// mode:HseMode::Bypass
// }
// );
config.rcc.mux.adcsel = mux::Adcsel::SYS;
}
let p = embassy_stm32::init(config);
info!("Hello World!");
let mut adc = Adc::new(p.ADC1);
let mut dma = p.DMA1_CH1;
let mut vrefint_channel: AnyAdcChannel<embassy_stm32::peripherals::ADC1> = adc.enable_vrefint().degrade_adc();
let mut pa0 = p.PA0.degrade_adc();
let pc7 = p.PC7;
let mut led1 = Output::new(pc7, Level::Low, Speed::Low);
let sampling_interval = Duration::from_micros(156); // 156.25微秒
let samples_per_cycle = 10; // 每个周期128个点
let cycles = 5; // 5个周期
loop {
// for i in 0..samples_per_cycle {
// adc.read(
// &mut dma,
// [
// (&mut vrefint_channel, SampleTime::CYCLES47_5),
// (&mut pa0, SampleTime::CYCLES12_5),
// ]
// .into_iter(),
// &mut read_buffer,
// )
// .await;
led1.toggle();
Timer::after(Duration::from_micros(100)).await
// }
// 每个
// Timer::after_secs(1).await;
}
}
The code in the program uses 100 microseconds as the stop time, but it is observed to be 200 microseconds from the oscilloscope. How should this be adjusted?
Are you measuring the full period, or the pulse width? The code is delaying for 100 μs for each pulse, resulting in a 200 μs period. In other words, it creates a square wave with a 50% duty cycle, where the duty cycle is 100 μs.
At 500 μs per division, the period does appear to take longer than expected. If you've ruled out measurement error, then I would begin to consider some other possible explanations:
Make sure you are running --release builds on the device (I'm not sure if non-optimized debug builds can even be compiled on your target, some embedded targets don't work right unless built with --release).
embassy-time is pretty configurable, defaulting to a 1MHz tick rate. Have you changed it to anything that might affect timer precision?
I'm going to second the suggestion to open the source code of embassy-time (cargo-crev), see where it's getting its clock from, and compare that against the datasheet for your microcontroller.