STM32 PWM output and Keil software simulation

Publisher:WhisperingWavesLatest update time:2018-06-29 Source: eefocusKeywords:STM32 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

STM32 PWM output


Pulse width mode (PWM mode) can be used to generate a signal with a frequency determined by the TIMx_ARR register and a duty cycle determined by the TIMx_CCRx register. In STM32 development, the official library functions provide a relatively complete set of functions, making our development work quite easy. We can even complete our development work and achieve the functions we need without knowing too much about the hardware structure. Here, the author also recommends that you try to familiarize yourself with the functions provided in the firmware library to adjust the frequency and duty cycle of PWM when you are just starting out, and minimize the operation of the underlying registers.


This article uses the STM32F103RB chip, and the output channel is the TIM2_CH2 channel. STM32 has strong portability. If the reader's chip type is different from mine, you can make appropriate modifications to complete your own development.


The library functions used are: 

stm32f10x.h: used for system initialization. No matter what development you do, this library must include 

stm32f10x_tim.h: TIM timer library function 

stm32f10x_rcc.h: clock configuration library function 

stm32f10x_gpio.h: GPIO configuration library functions


From the above library functions, we can see that the contents we need to initialize include TIM2 timer, clock enable configuration, and GPIO enable configuration.


void RCC_Config(void);

void GPIO_Config(void);

void TIM_Config(void);


The next step is to write the function body of each function. In these contents, the official has actually given examples. We configure them according to the official function library, and then modify some official variable properties.


RCC_Config function body


void RCC_Config(void)

{

        // Enable GPIOA, TIM2

        RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);

        RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE);

}


GPIO_Config function body


void GPIO_Config(void)

{

//GPIO configuration, the official library gives some parameters that need to be configured. If you forget, just refer to it. I configure GPIOA_Pin_1 here.

        GPIO_InitTypeDef GPIO_InitStructure;

        GPIO_InitStructure.GPIO_Pin=GPIO_Pin_1;

        GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP;

        GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;

        GPIO_Init(GPIOA,&GPIO_InitStructure);

}


TIM_Config function body 

Before configuring the function body, first understand how the duty cycle and frequency of stm32 are calculated 

①Frequency: The APB1 clock source we use is 72MHz. We do not do frequency division here. We set the input frequency by configuring related parameters. The calculation method is: Input frequency = APB1 clock / (pre-division coefficient + 1) = 72000000Hz / 360 = 200000Hz 

②The TIM_TImeBaseStructure.TIM_Period parameter determines the frequency of the output PWM waveform. The frequency of the output PWM waveform = the input frequency of the timer / TIM_TImeBaseStructure.TIM_Period. In this example, 20000Hz/100=200Hz, that is, one cycle of 5ms 

③Configure the duty cycle: Duty cycle = configured duty cycle value / TIM_TImeBaseStructure.TIM_Period. Use this calculation to determine the duty cycle. In this case, the duty cycle is 50/100=50% 

④Timer enable


void TIM_Config(void)

{

        TIM_TimeBaseInitTypeDef TIM_TImeBaseStructure;

        TIM_OCInitTypeDef  TIM_OCInitStructure;


    //Configure the clock output frequency of TIM2 and initialize other related parameters

        TIM_TImeBaseStructure.TIM_Prescaler=360-1; //Set the frequency of PWM

        TIM_TImeBaseStructure.TIM_CounterMode=0;

        TIM_TImeBaseStructure.TIM_Period=100;

        TIM_TimeBaseInit(TIM2,&TIM_TImeBaseStructure);



    //Set the PWM output mode

        TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2;

        TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;

        TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable;

        TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;

        TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High;

        TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set;

        TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCIdleState_Reset;   

    //Configure the duty cycle

        TIM_OCInitStructure.TIM_Pulse=50;


        TIM_OC2Init(TIM2,&TIM_OCInitStructure);

        TIM_ForcedOC1Config(TIM2,TIM_ForcedAction_Active);

        TIM_Cmd(TIM2,ENABLE);

        TIM_CtrlPWMOutputs(TIM2,ENABLE);


}


Thus, our entire PWM configuration is completed 

Main function


int main()

{

        RCC_Config();

        GPIO_Config();

        TIM_Config();

        while(1)

        {

                ;

        }

}


Next, let's look at the output frequency and waveform of our GPIOA_Pin_1 pin in Keil.


Keil software simulation


①Configure debugging tools 

Write the picture description here

② Turn on debugging, and set and test the GPIO output pins 
Write the picture description here
Write the picture description here 
Write the picture description here

③Run at full speed and observe the oscilloscope 
Write the picture description here 
Write the picture description here

STM32-PWM output source code download address  
STM32 firmware library version 3.5 user manual Chinese translation version


Keywords:STM32 Reference address:STM32 PWM output and Keil software simulation

Previous article:Adjustable PWM output based on stm32 microcontroller
Next article:STM32 study notes: general timer output PWM

Recommended ReadingLatest update time:2024-11-16 20:52

Using the PWM output of Timer0 in S3C2440 to drive the buzzer
There are five 16-bit timers in S3C2440, timer0, timer1, timer2, timer3 and timer4. Among them, only timer4 is an internal timer without output pins. Therefore, only timers 0, 1, 2, and 3 have pulse width modulation (PWM) function. Timer 0 has a dead zone generator for high current devices. (Hereinafter, timer 0 is us
[Microcontroller]
About the usage of NSS bit in STM32
We all know that the SPI in STM32 has four lines, namely MISO, MOSI, NSS, SCK MISO and MOSI are bidirectional data lines, and SCK is the clock line. Let's introduce what NSS does. In the standard SPI communication protocol, in addition to the above three lines, there is also a CS line, which is the chip select line, s
[Microcontroller]
Use and calculation of STM32 internal temperature sensor
Temperature (in ℃) = {(V - V) / Avg_Slope} + 25 The V in the formula is the value read at ADC_IN16. Avg_Slope is the slope of the conversion between temperature and ADC value. Imagine an XY coordinate system, where the X-axis is the ADC voltage reading, the Y-axis is the temperature, and there is a straight line bet
[Microcontroller]
STM32 matrix keyboard scanning program
    #define __JUZHENJIANPAN_H     extern u8 KeysCAN(void);     extern void GPIO_Config(void);     #endif     #include "stm32f10x.h"     #include "juzhenjianpan.h"     #include"gpiobitmap.h"     #define PC0 PCo_0     #define PC1 PCo_1     #define PC2 PCo_2     #define PC3 PCo_3     #define PC4 PCo_4     void GPIO_Confi
[Microcontroller]
An easy-to-understand explanation of PWM
Microscopic image of a computer monitor The picture above shows a microscopic image of a computer display. The method of taking the picture is very interesting. By dropping a drop of water on the camera of a mobile phone to form a convex lens, a simple microscope can be made. Dropping water on the ph
[Embedded]
An easy-to-understand explanation of PWM
STM32 Introduction - Interrupt Initialization Process (by woody)
In the library function, the function GPIO_EXTILineConfig() that configures the mapping relationship between GPIO and interrupt lines is implemented: void GPIO_EXTILineConfig(uint8_t GPIO_PortSource, uint8_t GPIO_PinSource) This function maps the GPIO port to the interrupt line. The usage example is: GPIO_EXTILineCo
[Microcontroller]
STM32 software key debounce
introduction Usually the switches used for buttons are mechanical elastic switches. When the mechanical contacts are opened and closed, due to the elastic effect of the mechanical contacts, a button switch will not be stably connected immediately when closed, nor will it be completely disconnected at once when opene
[Microcontroller]
STM32 software key debounce
Simple DC motor PWM speed control circuit
Simple DC motor PWM speed control circuit The 555 is ubiquitous and can be used as simple PWM speed control Circuit Explaination: The 555 Ic is wired as an astable and the frequency is constant and independent of the duty cycle, as the total resistance (R charge + R discharge, not
[Industrial Control]
Simple DC motor PWM speed control circuit
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号