Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using timer1 channel 4 for ADC triggering #173

Closed
MilliUser opened this issue Dec 14, 2023 · 0 comments
Closed

Using timer1 channel 4 for ADC triggering #173

MilliUser opened this issue Dec 14, 2023 · 0 comments

Comments

@MilliUser
Copy link

MilliUser commented Dec 14, 2023

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();
}

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant