STM32 Advanced Timer-PWM Simple Use

Publisher:数据小巨人Latest update time:2016-10-04 Source: eefocusKeywords:STM32 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
    The advanced timer is similar to the general timer. The following is a PWM program for TIM1. TIM1 is the only advanced timer in STM32. There are 4 channels with dead zone and complementary.

 

First, configure the IO pins:

       GPIO_InitTypeDef GPIO_InitStructure;

 

       /* PA8 is set as functional pin (PWM) */

       GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;

       GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;

       GPIO_InitStructure.GPIO_Speed ​​= GPIO_Speed_50MHz;

      

       GPIO_Init(GPIOA, &GPIO_InitStructure);

        /*PB13 is set to reverse polarity output of PWM*/

       GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13;

       GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;

       GPIO_InitStructure.GPIO_Speed ​​= GPIO_Speed_50MHz;

      

       GPIO_Init(GPIOB, &GPIO_InitStructure);

 

/*Open clock PWM and GPIO*/

       RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1,ENABLE);

       RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);

       RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);

/*Configure TIM1*/

       TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;

       TIM_OCInitTypeDef TIM_OCInitStructure;

void Tim1_Configuration(void)
{
    TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
    TIM_OCInitTypeDef TIM_OCInitStructure;

 
     TIM_DeInit(TIM1); //Reset to default values

 /*TIM1 clock configuration*/
 TIM_TimeBaseStructure.TIM_Prescaler = 4000; //Prescaler (clock division) 72M/4000=18K
 TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //Count up
 TIM_TimeBaseStructure.TIM_Period = 144; //Load value 18k/144=125hz, which means that 144 added up will be full
 TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; //Set the clock division, don't know how to ignore it
 TIM_TimeBaseStructure.TIM_RepetitionCounter = 0x0; //Period counter value, don't know how to ignore it
 TIM_TimeBaseInit(TIM1,&TIM_TimeBaseStructure); //Initialize the time base unit of TIMx

 /* Channel 1 Configuration in PWM mode Channel 1 PWM */
 TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2; //PWM mode 2
 TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //Forward channel is valid PA8 
 TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable; //Reverse channel is also valid PB13
 TIM_OCInitStructure.TIM_Pulse = 40; //Duty time 144 has 40 time high, complementary output is just the opposite
 TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low; //Output polarity
 TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_Low; //Complementary end polarity  
 TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Reset; //Ignore the non-working state in idle state
 TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCIdleState_Reset; //Ignore it for now

TIM_OC1Init(TIM1,&TIM_OCInitStructure); //Initialize peripheral TIMx channel 1 Here the 2.0 library is TIM_OCInit
 

 /* TIM1 counter enable start timer */
 TIM_Cmd(TIM1,ENABLE);
 
      /* TIM1 Main Output Enable enable the main output of TIM1 peripheral */
 TIM_CtrlPWMOutputs(TIM1,ENABLE);
}

 

//Set capture register 1
void SetT1Pwm1(u16 pulse)
{
 TIM1->CCR1=pulse;
}

/*Operation register changes duty time*/

/****************************************************** *************************************************** ******************

The timer channels 1 to 4 of TIM1 are PB8, PA9, PA10, and PA11 respectively, and the complementary outputs are PB13, PB14, and PB15 respectively.

Discontinue PB12.

STM32 Advanced Timer-PWM Simple Use- Java - stm32 Learning Log

If the output and complementary output polarity are the same, the output is high and the complementary output is low. As for the difference between PWM mode 1 and mode 2

In the following figure:

STM32 Advanced Timer-PWM Simple Use- Java - stm32 Learning Log

This is mode 1. Green is output and yellow is complementary.

*************************************************** ***********************************************

STM32 Advanced Timer-PWM Simple Use- Java - stm32 Learning Log

 

The above picture shows the situation of mode 2, which is exactly the opposite of mode 1. Among the 144, 40 are high and the complementary 40 are low.

*************************************************** ***********************************************/

 

 

//Add some keyboard scanning in MAIN to change the duty cycle

/************************************************************
      **Experiment name: PWM
      **Function: PA8 generates PWM output. PA8 is the IO that drives LED1 and the motor.
       By pressing the UP or DOWN key, the duty cycle can be changed, so that ED1 and the small motor can be changed
      . **Note: LED is low effective, and the motor is high effective, so the motor speed reaches the highest when all LEDs are off.
      **Author: Electronic Cabbage
      ************************************************************/

#include "STM32Lib\\stm32f10x.h"
       #include "hal.h"

extern void SetT1Pwm1(u16 pulse);

int main(void)
{
 u16 pulse=40;
 ChipHalInit(); //Initialize on-chip hardware
 ChipOutHalInit(); //Initialize off-chip hardware
 
 for(;;)
 {
  if(GET_UP())
  {
   while(GET_UP());
   if(pulse<=144)
   {
    pulse+=5;
    SetT1Pwm1(pulse);
   }
  }
  
  if(GET_DOWN())
  {
   while(GET_DOWN());
   if(pulse>30)
   {
    pulse-=5;
    SetT1Pwm1(pulse);
   }
  }
 }
 
 
}

 

STM32 Advanced Timer-PWM Simple Use- Java - stm32 Learning Log

/****************************************************** ***************************************/

Here are two more minimum system boards

/****************************************************** ***************************************/

STM32 Advanced Timer-PWM Simple Use- Java - stm32 Learning Log

STM32 Advanced Timer-PWM Simple Use- Java - stm32 Learning Log

 

All you need is a board like this, a little time and motivation to make this journey work.

Keywords:STM32 Reference address:STM32 Advanced Timer-PWM Simple Use

Previous article:STM32 I2C hard usage
Next article:Unique ID code of stm32

Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号