stm32f0xx-hal icon indicating copy to clipboard operation
stm32f0xx-hal copied to clipboard

Using timer1 channel 4 for ADC triggering

Open MilliUser opened this issue 1 year ago • 0 comments

I need to configure the 4th channel to trigger timer1 3 complementary output and adc. macro! pwm_4_channels_with_3_complementary_outputs() macro, the point I'm stuck on is that I don't know how to access the 4th channel. I don't want to connect to the GPIO pin to which the 4th channel is connected

#![deny(unsafe_code)] #![no_main] #![no_std]

// Halt on panic use panic_halt as _;

use cortex_m; use cortex_m_rt::entry;

use stm32f0xx_hal as hal;

use hal::{delay::Delay, pac::{self, TIM1}, prelude::*, pwm, adc};

#[entry] fn main() -> ! { if let Some(mut dp) = pac::Peripherals::take() { // Set up the system clock. let mut rcc = dp.RCC.configure().sysclk(8.mhz()).freeze(&mut dp.FLASH);

    let gpioa = dp.GPIOA.split(&mut rcc);
    let gpiob = dp.GPIOB.split(&mut rcc);
    let channels = cortex_m::interrupt::free(move |cs| {
        (
            gpioa.pa8.into_alternate_af2(cs),  // on TIM1_CH1
            gpiob.pb13.into_alternate_af2(cs), // on TIM1_CH1N
            gpioa.pa9.into_alternate_af2(cs),  // on TIM1_CH2
            gpiob.pb14.into_alternate_af2(cs), // on TIM1_CH2N
            gpioa.pa10.into_alternate_af2(cs), // on TIM1_CH3
            gpiob.pb15.into_alternate_af2(cs), // on TIM1_CH3N
        )
    });

    let pwm = pwm::tim1(dp.TIM1, channels, &mut rcc, 20u32.khz());
    let (mut pwm_c_h, mut pwm_c_l, mut pwm_b_h, mut pwm_b_l, mut pwm_a_h, mut pwm_a_l) = pwm;



    let max_duty = pwm_c_h.get_max_duty();
    pwm_c_h.set_duty(max_duty / 2);
    pwm_c_h.enable();
    pwm_c_l.enable();
    pwm_b_h.set_duty(max_duty / 2);
    pwm_b_h.enable();
    pwm_b_l.enable();
    pwm_a_h.set_duty(max_duty / 2);
    pwm_a_h.enable();
    pwm_a_l.enable();

    // simple duty sweep
    if let Some(cp) = cortex_m::Peripherals::take() {
        let mut delay = Delay::new(cp.SYST, &rcc);

        let steps = 100;

        loop {
            for i in 0..steps {
                pwm_c_h.set_duty(max_duty / steps * i);
                pwm_b_h.set_duty(max_duty / steps * i);
                pwm_a_h.set_duty(max_duty / steps * i);
                delay.delay_ms(30u16);
            }

            for i in (1..steps).rev() {
                pwm_c_h.set_duty(max_duty / steps * i);
                pwm_b_h.set_duty(max_duty / steps * i);
                pwm_a_h.set_duty(max_duty / steps * i);
                delay.delay_ms(30u16);
            }
        }
    }
}

// something went wrong when acquiring peripheral access
loop {
    cortex_m::asm::nop();
}

}

MilliUser avatar Dec 14 '23 09:12 MilliUser