Stm32 Register Version Learning note PWM

Source: Internet
Author: User

STM32 can generate PWM output in addition to TIM6 and TIM7. The advanced timer TIM1 and TIM8 can simultaneously generate 7-way PWM, and the universal timer can generate 4 PWM outputs.

1.TIM1 CH1 Output PWM configuration steps

① turn on TIM1 clock, configure PA8 for multiplexed output

APB2 Peripheral Clock Enable register (RCC_APB2ENR)

APB1 Peripheral Reset Register (RCC_APB1RSTR)

Set 1 to open. Clear 0 closed.

eg:rcc->apb2enr|=1<<11; Enable TIM1 clock

Configuring I/O ports: See Stm32 Register Edition Learning Note the configuration of the Gpio port.

eg:gpioa->crh&=0xfffffff0; PA8 before clearing the settings

gpioa->crh|=0x0000000b; PA8 multiplexing function output

② set arr and PSC for TIM1

Automatic reload Register (Timx_arr)

TIM1 and TIM8 Prescaler (TIMX_PSC)

③ Setting the PWM mode and channel direction of the TIM1_CH1

TIM1 and TIM8 Capture/Compare Mode Register 1 (TIMX_CCMR1)

  

eg:tim1->ccmr1|= 7<<4; CH1 PWM2 Mode

tim1->ccmr1|= 1<<3; CH1 Pre-load enable

CH1 is output, cc1s[1:0] defaults to 00.

④ enables the TIM1 CH1 output, enabling the TIM1 to be

Set Tim1_ccer to turn on the output of Channel 1 for TIM1. Set the TIM1_CR1 to turn on the TIM1 clock.

TIM1 and TIM8 capture/Compare Enable registers (Timx_ccer)

eg:tim1->ccer|= 1<<3; OC1 output Enable

TIM1 and TIM8 Control register 1 (TIMX_CR1)

Arpe: Automatic reload preload allows bit 0:timx_arr register without buffering; 1:timx_arr registers are loaded into buffers.

CEN: Enable counter bit 0 0: Disable counter; 1: Enable counter.

eg:tim1->cr1=0x0080; Arpe Enable

tim1->cr1|=0x01; Enable Timer 1

⑤ set Moe output to enable PWM output

The normal timer can output PWM after completing the above settings, but the advanced timer must be able to brake and dead-zone registers.

TIM1 and TIM8 brakes and dead-zone registers (TIMX_BDTR)

eg:tim1->bdtr|=1<<15; Moe Main output Enable

⑥ modify TIM1_CCR1 to control duty ratio

Eg: #define Pwm_val TIM1->CCR1 (can be set directly)

2. Advanced Timer TIM1 Output PWM application

1 //timer.c 2 //tim1_ch1 PWM Output Initialization3 //arr: Auto Reload values4 //PSC: Clock Prescaler frequency5 voidTim1_pwm_init (U16 arr,u16 PSC)6 {                              7     //This section needs to manually modify the IO port settings8rcc->apb2enr|=1<< One;//TIM1 Clock Enable9gpioa->crh&=0xfffffff0;//PA8 before clearing the settingsTengpioa->crh|=0x0000000b;//multiplexing function Output One      Atim1->arr=arr;//Set counter Auto reload value -tim1->psc=psc;//prescaler Settings -    thetim1->ccmr1|=7<<4;//CH1 PWM2 Mode -tim1->ccmr1|=1<<3;//CH1 Pre-load enable -tim1->ccer|=1<<0;//OC1 output Enable -tim1->bdtr|=1<< the;//MOE main output Enable +  -tim1->cr1=0x0080;//Arpe Enable +tim1->cr1|=0x01;//Enable timer 1 A}
1 //Timer.h2 #ifndef __timer_h3 #define__timer_h4#include"Sys.h" 5 //change the duty ratio by changing the value of the TIM1->CCR1 to control the brightness of the LED06 #defineLed0_pwm_val TIM1-&GT;CCR17 8 voidTim1_pwm_init (U16 arr,u16 PSC);9 #endif

3. Normal timer TIM2 Output 2 way PWM application

1 //tim2_ch3~4 PWM Output Initialization2 //arr: Auto Reload values3 //PSC: Clock Prescaler frequency4 voidTim2_pwm_init (U16 arr,u16 PSC)5 {                              6     //This section needs to manually modify the IO port settings7rcc->apb1enr|=1<<0;//TIM2 Clock Enable8gpioa->crl&=0xffff00ff;//pa2-3 before clearing the settings9gpioa->crl|=0x0000bb00;//multiplexing function OutputTen      Onetim2->arr=arr;//Set counter Auto reload value Atim2->psc=psc;//prescaler Settings -    -      thetim2->ccmr2|=7<<4;//CH3 PWM2 mode (output) -tim2->ccmr2|=1<<3;//CH3 Pre-load enable -tim2->ccer|=1<<8;//OC3 output Enable -      +tim2->ccmr2|=7<< A;//CH4 PWM2 mode (output) -tim2->ccmr2|=1<< One;//CH4 Pre-load enable +tim2->ccer|=1<< A;//OC4 output Enable A  attim2->cr1=0x0080;//Arpe Enable -tim2->cr1|=0x01;//Enable timer 2 - }   -  -  - //set duty ratio duty<899 in //set how much is the low - voidPwm_duty1 (U16 duty) to { +      -tim2->ccr3=duty; the } *  $ voidPwm_duty2 (U16 duty)Panax Notoginseng {                       -tim2->ccr4=duty; the}

Remember to add an output function before the main function!

Eg:tim2_pwm_init (899,0);//Do not divide. PWM frequency =72000/(899+1) =80khz

Stm32 Register Version Learning note 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.