STM32 Output 3-way same frequency different duty ratio sequence of PWM

Source: Internet
Author: User

Method One: 3 channels with the same timer can output PWM with different duty ratio of the same frequency; but the order of their output must be the same, because they use the same counter

If you want to output 3 different sequence of PWM must use 3 timers

Example:

void Tim1_configuration (void)
{
Tim_timebaseinittypedef tim_timebasestructure;
Tim_ocinittypedef tim_ocinitstructure;

Gpio_inittypedef gpio_initstructure;

/* PA9 set to function foot (PWM) */
Gpio_initstructure.gpio_pin = Gpio_pin_9;
Gpio_initstructure.gpio_mode = gpio_mode_af_pp;
Gpio_initstructure.gpio_speed = Gpio_speed_50mhz;

Gpio_init (Gpioa, &gpio_initstructure);


Rcc_apb2periphclockcmd (rcc_apb2periph_tim1,enable);


Tim_deinit (TIM1);

/*TIM1 Clock Configuration */
Tim_timebasestructure.tim_prescaler = 71;//prescaler (clock divider) 72m/72=1000k
Tim_timebasestructure.tim _countermode = tim_countermode_up;//up count
Tim_timebasestructure.tim_period = 660;      //Load value 1000k/ 660=1515hz
Tim_timebasestructure.tim_clockdivision = tim_ckd_div1;
Tim_timebasestructure.tim_repetitioncounter = 0x0;
Tim_timebaseinit (tim1,&tim_timebasestructure);

/* Channel 2 Configuration in PWM mode */
Tim_ocinitstructure.tim_ocmode = tim_ocmode_pwm2; PWM Mode 2
Tim_ocinitstructure.tim_outputstate = tim_outputstate_enable; Positive Channel Valid
Tim_ocinitstructure.tim_outputnstate = tim_outputnstate_disable;//Reverse Channel invalid
Tim_ocinitstructure.tim_pulse = 430; Duty-free time
tim_ocinitstructure.tim_ocpolarity = Tim_ocpolarity_low; Output polarity Active level
tim_ocinitstructure.tim_ocnpolarity = Tim_ocnpolarity_high; Polarity of complementary ends
Tim_ocinitstructure.tim_ocidlestate = Tim_ocidlestate_set;
Tim_ocinitstructure.tim_ocnidlestate = Tim_ocidlestate_reset;

Tim_oc2init (tim1,&tim_ocinitstructure); Channel 1

/* TIM1 counter Enable */
Tim_cmd (tim1,enable);

/* TIM1 Main Output Enable */
Tim_ctrlpwmoutputs (tim1,enable);
}

PWM duty ratio Low Level setting: PWM mode 1 o'clock: CNT (counter) <tim_pulse is the active level, otherwise the invalid level; PWM Mode 2, Cnt<tim_pulse is invalid level, otherwise valid level

Method Two: When the timer resource is insufficient, with a timer, in the interrupt through 3 IO ports to simulate the 3-way PWM

STM32 Output 3-way same frequency different duty ratio sequence of PWM

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.