STM32 learning notes - PWM basics and 720 motor drive

Publisher:chi32Latest update time:2019-01-09 Source: eefocusKeywords:STM32 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

1. Definition

Pulse Width Modulation: It is a very effective technology that uses the digital output of a microprocessor to control analog circuits. It is widely used in many fields from measurement, communication to power control and conversion. (Baidu Encyclopedia) It is a method of digitally encoding the analog signal level, and the required waveform (including shape and amplitude) is equivalently obtained by modulating the width of a series of pulses.


**SPWM waveform: **A PWM waveform whose pulse width changes according to the sinusoidal law and is equivalent to a sine wave.


insert image description here


Divide the sine half wave waveform into N equal parts, and the sine half wave can be regarded as a waveform composed of N pulses connected to each other. If the above pulse sequence is replaced by the same number of rectangular pulse sequences with equal amplitude but unequal width, so that the midpoint of the rectangular pulse coincides with the midpoint of the corresponding sine equal division, and the area (i.e. impulse) of the rectangular pulse and the corresponding sine part is equal, a set of pulse sequences is obtained, which is the PWM waveform. According to the principle of equal impulse and equal effect, the PWM waveform and the sine half wave are equivalent, as shown in the figure above.


PWM frequency: refers to the number of times the signal changes from high level to low level and then back to high level per second.


**Duty cycle: **The ratio of the time the high level is maintained in the output PWM to the clock cycle time of the PWM.


insert image description here

**Resolution:** refers to the minimum duty cycle that can be achieved. For example, the theoretical resolution of 8-bit PWM is 1:255 (single slope), and the theoretical resolution of 16-bit PWM is 1:65535 (single slope).


PWM is a method of digitally encoding analog signal levels. Through the use of high-resolution counters, the duty cycle of a square wave is modulated to encode the level of a specific analog signal. PWM signals are still digital because at any given moment, the full-scale DC supply is either fully present (ON) or fully absent (OFF). A voltage or current source is applied to an analog load in a repetitive sequence of pulses that are either on (ON) or off (OFF). The on time is when the DC supply is applied to the load, and the off time is when the supply is disconnected. Any analog value can be encoded using PWM as long as the bandwidth is sufficient.


The AC voltage value modulated by PWM mainly depends on the duty cycle we control (voltage = duty cycle x amplitude), and the waveform accuracy of the modulated AC voltage depends on the set carrier frequency.


2. Motor drive

For the output configuration of PWM wave, please refer to STM32 learning notes one by one PWM output


2.1 Circuit connection:


insert image description here


2.2 Software Implementation:

head File:

#ifndef __MOTOR__H_

#define __MOTOR__H_


#include "system.h"


#define MOTOR1_PWM GPIO_Pin_6


#define MOTOR_PWMMAX 1000


void MOTOR_GPIO_Init(void);

void TIM3_PWM_Init(void);

void MOTOR_Control(int16_t motor1_pwm);


#endif

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15


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

* Function: void MOTOR_GPIO_Init(void)

* Function: Motor pin initialization

* Parameters: None

* Return value: None

* Note: TIM3 CH1(PWM1) -> PA6

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

void MOTOR_GPIO_Init(void)

{

GPIO_InitTypeDef GPIO_InitStructure;


RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);


GPIO_InitStructure.GPIO_Pin = MOTOR1_PWM;

GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; // GPIO multiplexing push-pull output

GPIO_Init(GPIOA,&GPIO_InitStructure);

//GPIO_SetBits(GPIOA,MOTOR1_PWM);

}


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

* Function: void TIM3_PWM_Init(void)

* Function: Timer output and PWM configuration

* Parameters: None

* Return value: None

* Note: TIM3 CH1

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

void TIM3_PWM_Init(void)

{

TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure; //Timer variables

TIM_OCInitTypeDef TIM_OCInitStructure; //Output comparison structure variable


RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE);


TIM_TimeBaseInitStructure.TIM_Period = 1000-1; //Set the period value of automatic reload; f=72M/1000=72KHz

TIM_TimeBaseInitStructure.TIM_Prescaler = 100; //Set the prescaler value

TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up; //Upward counting mode

TIM_TimeBaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV1; //Set clock division

TIM_TimeBaseInit(TIM3,&TIM_TimeBaseInitStructure);


TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; //PWM1 mode

TIM_OCInitStructure.TIM_Pulse = 0; //Initialize the duty cycle to 0

TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //Output polarity high

TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //Comparison output enable

TIM_OC1Init(TIM3,&TIM_OCInitStructure);

TIM_OC1PreloadConfig(TIM3,TIM_OCPreload_Enable); //Enable TIMx preload register on ARR


TIM_Cmd(TIM3,ENABLE);

}


test:


#include "stm32f10x.h"

#include "led.h"

#include "systick.h"

#include "motor.h"


int main(void)

{

uint8_t dir=1;

uint16_t motor1_pwmval=0;

SysTick_Init();

LED_Heat();

MOTOR_GPIO_Init();

TIM3_PWM_Init();


while(1)

{

USER_LED_ON();

delay_ms(500);

USER_LED_OFF();

delay_ms(500);



if(dir)

motor1_pwmval++;

else 

motor1_pwmval--;  

  if(motor1_pwmval>999)

you=0;

if(motor1_pwmval==0)

you=1;      

TIM_SetCompare1(TIM3,motor1_pwmval); //Set duty cycle 0-999

}

}



After connecting the circuit and downloading the program, you can observe the small motor stop-rotate-stop cycle.


Keywords:STM32 Reference address:STM32 learning notes - PWM basics and 720 motor drive

Previous article:Summary of STM32 program transplantation skills
Next article:STM32 drives NRF24L01

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号