STM32F103 TIM1 output PWM settings

Source: Internet
Author: User

// TIM1 frequency division # defineTIM1_DIV1 (1-1) # defineTIM1_DIV2 (2-1) # defineTIM1_DIV4 (4-1) # defineTIM1_DIV8 (8-1) # defineTIM1_DIV9 (9-1) # defineTIM1_DIV18 (18-1) # defineTIM1_DIV72 (72-1) # defineTIM1PinA_Enb TIM1-> CCER | = 0X0001 // compare Channel 1 output to IO # defineTIM1PinB_Enb TIM1-> CCER | = 0X0010 // compare Channel 2 output to IO # defineTIM1PinC_Enb TIM1-> CCER | = 0X0100 // compare Channel 3 output to IO # defineTIM1PinA_Dis TIM1-> CCER & = ~ 0X0001 // compare Channel 1 to disable output to IO # defineTIM1PinB_Dis TIM1-> CCER & = ~ 0X0010 // compare Channel 2 disable output to IO # defineTIM1PinC_Dis TIM1-> CCER & = ~ 0X0100 // compare Channel 3 disable output to IO # defineVT_PWM1000 // PWM count maximum frequency 8 K // # defineVT_PWM500 // PWM count maximum frequency 16 K //**** * ******************************* // Function Name: timerepwmmode_init // function: TIM1 is initialized in the PWM mode and used as the Compare output. // entry parameter: Count count frequency // exit parameter: NONE // return value: no // This setting is suitable for driving three pairs of MOS in BLDC driver control //*********************** **************** void timid pwmmode_init (int16_t Tcon, uint16_t psc) {RCC_APB2PeriphClockCmd (RCC_APB2Periph_TIM1, ENABLE); TIM_DeInit (TI M1); // reset the timer TIM1-> ARR = Tcon; // The timer re-load value TIM1-> PSC = psc; // The time pre-division coefficient TIM1-> CR1 & = ~ (1 <4); // count TIM1-> CR1 |=( 1 <7 ); // enable automatic re-loading // clear update interrupt comparison 1234 interrupt mark TIM1-> SR & = ~ 0X001F; // Channel 1 2 3 is configured as PWM1 mode TIM1-> CCMR1 = 0X6868; // TIM1-> CCMR2 = 0X0068; // do not compare Channel 1 2 3 output to IO and Level 1 is valid TIM1-> CCER = 0X0000; // TIM1-> CCR1 = 0; TIM1-> CCR2 = 0; TIM1-> CCR3 = 0; TIM1-> CCR4 = 0; // update interruption comparison 1-4 TIM1-> DIER & = ~ 0X001F; // TIM1-> DIER | = 0X0002; // enable comparison 1 TIM1-> BDTR |=( 1 <15 ); // enable the primary output !!! // Clear the update event flag bit and initialize all registers TIM1-> EGR & = ~ 0X0001; TIM1-> CR1 | = 0X0001; // enable the clock }//********************************* * *** // Function Name: toPWM_Value // function: Set the PWM duty cycle // entry parameter: Count count Frequency/exit parameter: NONE // return value: none //************************************* **/void ToPWM_Value (uint16_t InValue) {TIM1-> CCR1 = InValue; TIM1-> CCR2 = InValue; TIM1-> CCR3 = InValue ;} //*************************************/ /function name: BLDC_RUN_ONESTEP // function: six-step motor direction // entry parameter: current step of motor operation // exit parameter: none //************************************* **/void BLDC_RUN_ONESTEP (uint32_t STEP) {switch (STEP) {case 1: // condition; TIM1PinC_Dis; condition; UdePinA_Off; UdePinC_Off; UdePinB_On; break; case 2: // condition; TIM1PinC_Dis; condition; UdePinA_Off; UdePinB_Off; break; case 3: // break; TIM1PinC_Dis; break; UdePinA_Off; UdePinB_Off; UdePinC_On; break; case 4: // break; TIM1PinC_Dis; break; UdePinB_Off; UdePinC_Off; off; break; case 5: // hour; TIM1PinB_Dis; hour; UdePinB_Off; UdePinC_Off; hour; break; case 6: // hour; TIM1PinB_Dis; hour; UdePinA_Off; UdePinC_Off; hour; break; default: break ;}// ---- instance application ------ // 72 M/(9) = 8 M pwm cycle = (1/8) * 1000 = 125U TIM1_PWMMode_Init (VT_PWM, TIM1_DIV9 ); // PWM frequency setting initialization frequency F = 8 K // ToPWM_Value (500); // set the PWM duty cycle of the three-phase driver 500/1000 = 50% // TIM1PinA_Enb; // PWM output to IO. At this time, the PWM signal output is displayed on IO.

 

Related Article

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.