STM32 - PWM output

Publisher:QingfangLatest update time:2018-08-15 Source: eefocusKeywords:STM32 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

    Pulse Width Modulation, or PWM for short, is a method of using digital signals to output analog signals. It has multiple modes. The main principle is to use the periodic changes in the high and low levels of digital signals to change the average value of the output voltage, thereby achieving the output mode of analog signals. It is mainly used in motor speed regulation and dimming lamp circuit control. 

    There are multiple PWM signals in STM32. Each timer output pin can be set to the output of the corresponding PWM signal. There are two main parameters of PWM signal, the duty cycle of the cycle. The cycle is mainly determined by the corresponding timer. The duty cycle is the time when the PWM outputs a high level. Assuming that the current timing cycle is 2MS, in 2MS, by setting 1MS pin to output a high level. In this way, it can be said that the duty cycle of this PWM is 50%. 

    During program execution, you can use TIM_SetCompare1() to change the corresponding duty cycle, so that you can flexibly control the output of the PWM signal.


#include "Time.h"

#include "stm32f10x.h"


void TIM4_init(void)

{

    GPIO_InitTypeDef GPIO_InitStruc; 

    TIM_TimeBaseInitTypeDef TIM_struct;

    TIM_OCInitTypeDef TIM_ocinit;


    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);

    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);

    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE); //Turn on the clock of TIM4


    GPIO_InitStruc.GPIO_Mode=GPIO_Mode_AF_PP;                         

    GPIO_InitStruc.GPIO_Pin=GPIO_Pin_6 | GPIO_Pin_7 | GPIO_Pin_8| GPIO_Pin_9;

    GPIO_InitStruc.GPIO_Speed=GPIO_Speed_50MHz;

    GPIO_Init(GPIOB,&GPIO_InitStruc); //Initialize the output pin of the PWM signal



    GPIO_InitStruc.GPIO_Mode=GPIO_Mode_Out_PP;                         

    GPIO_InitStruc.GPIO_Pin=GPIO_Pin_0 | GPIO_Pin_1;

    GPIO_InitStruc.GPIO_Speed=GPIO_Speed_50MHz;

    GPIO_Init(GPIOA,&GPIO_InitStruc);   


    TIM_struct.TIM_Period=999; //Set the timer period 72/72*1000 to 1MS

    TIM_struct.TIM_CounterMode=TIM_CounterMode_Up;

    TIM_struct.TIM_ClockDivision=TIM_CKD_DIV1 ;

    TIM_struct.TIM_Prescaler=71; //Timer 72 division


    TIM_TimeBaseInit(TIM4,&TIM_struct);

    //TIM_ITConfig(TIM4,TIM_IT_Update,ENABLE);


    TIM_ocinit.TIM_OCMode=TIM_OCMode_PWM1; //Set PWM mode to 1

    TIM_ocinit.TIM_Pulse=200; //Set the initial PWM duty cycle

    TIM_ocinit.TIM_OutputState=TIM_OutputState_Enable; //Enable pWM output enable

    TIM_ocinit.TIM_OCPolarity=TIM_OCPolarity_High; //Set PWM signal polarity


    TIM_OC1Init(TIM4,&TIM_ocinit);

    TIM_OC1PreloadConfig(TIM4, TIM_OCPreload_Enable);


    TIM_OC2Init(TIM4,&TIM_ocinit);

    TIM_OC2PreloadConfig(TIM4, TIM_OCPreload_Enable);


    TIM_OC3Init(TIM4,&TIM_ocinit);

    TIM_OC3PreloadConfig(TIM4, TIM_OCPreload_Enable);


    TIM_OC4Init(TIM4,&TIM_ocinit);

    TIM_OC4PreloadConfig(TIM4, TIM_OCPreload_Enable);


    TIM_Cmd(TIM4,ENABLE);

}

void TIM_PWM_Con(u16 DR1,u16 DR2,u16 DR3,u16 DR4)

{

    TIM_SetCompare1(TIM4,DR1); //Change the duty cycle of PWM

    TIM_SetCompare2(TIM4,DR2);

    TIM_SetCompare3(TIM4,DR3);

    TIM_SetCompare4(TIM4,DR4);

}


Keywords:STM32 Reference address:STM32 - PWM output

Previous article:STM32-Serial port IAP upgrade
Next article:STM32-SPI OLED

Recommended ReadingLatest update time:2024-11-16 13:23

Why does STM32 remap boot from Flash address 0x08000000
When I first wrote an STM32 program, I encountered a confusion. The STM32 Flash was set to start at address 0x0800 0000 in MDK, but the CM3 manual stipulates that the interrupt vector should be taken from address 0x0000 0000 when the chip is reset. So how does the STM32 execute the code? Address remapping? Or is there
[Microcontroller]
Summary of STM32 encoder counting and overflow processing debugging
Error 1: PC6 and PC7 are used for other purposes, and the GPIO mode is configured incorrectly, resulting in inaccurate counting; Error 2: The pin mode is set incorrectly. It should be set to GPIO_Mode_IPD; //GPIO_Mode_IPU GPIO_Mode_IN_FLOATING is OK; Error 3: Pin remapping does not enable AFIO clock; Summarize Regardi
[Microcontroller]
[STM32 Motor Vector Control] Record 8——ADC Three-Resistor Sampling
The basic principle of ADC:         ADC, the abbreviation of Analog-to-Digital Converter, refers to analog/digital converter or analog-to-digital converter. It is a device that converts continuously changing analog signals into discrete digital signals.        The corresponding DAC, Digital-to-Analog Converter, is the
[Microcontroller]
[STM32 Motor Vector Control] Record 8——ADC Three-Resistor Sampling
STM32 USART learning
First of all, I was inspired by this blogger's article, which deepened my understanding of USART. The following is the program I modified and experimented with. Original text: http://www.cnblogs.com/greatwgb/archive/2011/07/28/2119350.html   1. Basic concepts of serial ports In the STM32 reference manual, the serial
[Microcontroller]
STM32 study notes: simple Bootloader serial port upgrade design
Concept Introduction Before learning how to make a serial port upgrade Bootloader, let's first understand STM32's IAP (In Application Programming). IAP is the user's own program burning part of the User Flash area during operation. The purpose is to easily update the firmware program in the product through the reser
[Microcontroller]
STM32 study notes: simple Bootloader serial port upgrade design
Common library functions for STM32 MCU
1.GPIO initialization function usage: voidGPIO_Configuration(void) { GPIO_InitTypeDefGPIO_InitStructure; //GPIO status restores default parameters GPIO_InitStructure.GPIO_Pin=GPIO_Pin_label|GPIO_Pin_label; //Pin position definition, the label can be NONE, ALL, 0 to 15. GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
[Microcontroller]
STM32 V3.4 library function use to establish the project method
  I clearly remember the feeling of confusion when I just transitioned from 51 MCU to STM32. The STM32 development board I used is not suitable for beginners. It is the Shenzhou development board with gorgeous hardware. I will not comment on the Shenzhou development board here. If you are a beginner, I suggest you do n
[Microcontroller]
Jiujiu's STM32 Notes (I) TIM module timer overflow &am
First of all, we must affirm the strength of ST company and admit that STM32 is indeed a very good Cortex-M3 core microcontroller. However, its manual is really incomprehensible, especially the TIM module, which is disorganized. After reading it for two days, I still don’t understand it, which makes people very dep
[Microcontroller]
Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
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号