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.
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.
**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:
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.
Previous article:Summary of STM32 program transplantation skills
Next article:STM32 drives NRF24L01
- Popular Resources
- Popular amplifiers
- Learn ARM development(16)
- Learn ARM development(17)
- Learn ARM development(18)
- Embedded system debugging simulation tool
- A small question that has been bothering me recently has finally been solved~~
- Learn ARM development (1)
- Learn ARM development (2)
- Learn ARM development (4)
- Learn ARM development (6)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- CGD and Qorvo to jointly revolutionize motor control solutions
- CGD and Qorvo to jointly revolutionize motor control solutions
- Keysight Technologies FieldFox handheld analyzer with VDI spread spectrum module to achieve millimeter wave analysis function
- Infineon's PASCO2V15 XENSIV PAS CO2 5V Sensor Now Available at Mouser for Accurate CO2 Level Measurement
- Advanced gameplay, Harting takes your PCB board connection to a new level!
- Advanced gameplay, Harting takes your PCB board connection to a new level!
- A new chapter in Great Wall Motors R&D: solid-state battery technology leads the future
- Naxin Micro provides full-scenario GaN driver IC solutions
- Interpreting Huawei’s new solid-state battery patent, will it challenge CATL in 2030?
- Are pure electric/plug-in hybrid vehicles going crazy? A Chinese company has launched the world's first -40℃ dischargeable hybrid battery that is not afraid of cold
- It is rare for home appliance manufacturers to issue price increase letters, simply because of rising costs of chips and other items!
- Ghost video sound
- Wire Harness Tester Related Operation & Application Video (Continuously Updated)
- About CPLD
- TMS320C66x dual loop and multiple loop optimization summary
- 【TI recommended course】#TI millimeter wave radar technology introduction#
- Design of multi-channel signal generator based on FPGA+PWM
- 【micropython】STM32 will support arbitrary FS
- Global variables in embedded system programming
- PWM generated voltage calculation table