The principle, use and configuration of PWM peripherals in STM32

Publisher:YuexiangLatest update time:2018-07-01 Source: eefocusKeywords:STM32 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

As for what PWM is, just search on Baidu. In fact, the content is simple. First of all, the PWM function in STM32 is implemented using a counter, which is similar to 51, but there are also differences. 51 uses the interrupt of the counter to adjust the duty cycle, which takes up the time of the MCU. However, STM32 uses hardware to implement the PWM function, so PWM is a peripheral function. (The counter's up-counting, down-counting, and center-aligned modes are not introduced).

PWM mainly configures 2 registers. TIMx_ARR = T (set period T, frequency setting)

     TIMx_CCRx = t (set duty cycle, this is a library function)

**P.S.: PWM still enters the interrupt operation of the connected TIMx. However, the 51 interrupt is interrupted as a small part and then counts to perform pwm modulation. However, STM32 enters when it reaches ti and has already been modulated.


  #include "PWM.h"


void PWM_init()
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;  
GPIO_InitTypeDef GPIO_InitStructure;              
TIM_OCInitTypeDef TIM_OCInitStructure;             

//Hook up the clock RCC

RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE); 
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC,ENABLE);  
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);  

/* Timer configuration */

TIM_TimeBaseStructure.TIM_Period = 900 ; //Set timer frequency           
TIM_TimeBaseStructure.TIM_Prescaler = 0; //No frequency division 
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //Count up    
TIM_TimeBaseInit(TIM3, & TIM_TimeBaseStructure); 

TIM_Cmd(TIM3, ENABLE);                          

/* PWM configuration*/
TIM_OCInitStructure.TIM_OCMode=TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState=TIM_OutputState_Enable;   
TIM_OCInitStructure.TIM_OCPolarity=TIM_OCPolarity_Low;      
TIM_OC2Init(TIM3, &TIM_OCInitStructure);                   
TIM_OC2PreloadConfig(TIM3, TIM_OCPreload_Enable);           

GPIO_PinRemapConfig(GPIO_FullRemap_TIM3, ENABLE); //Specify the mapping of the pin, this configured pwm is mapped to TIM3 

/* Configure pwm to adjust the brightness of the led, so configure the GPIO of the led*/
GPIO_InitStructure.GPIO_Speed ​​= GPIO_Speed_50MHz; 
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;        
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //Push-pull multiplexing function, pwm controls the led, if it is not an MCU, GPIO_Mode_Out_PP is not used
GPIO_Init(GPIOC,&GPIO_InitStructure);            

}

Functions used in interrupt files

void EXTI2_IRQHandler(void) //Here the external interrupt is used as the ti (duty cycle) of modulated pwm
{
if(EXTI_GetITStatus(EXTI_Line2)==SET) //Judge whether to enter external interrupt 2
{
EXTI_ClearITPendingBit(EXTI_Line2); // Clear the suspension of line EXIT
delay_ms(10); //
if(GPIO_ReadInputDataBit(GPIOE,GPIO_Pin_2)==Bit_RESET) //Read whether key_up is pressed
{
if(ti<500)
ti=ti+100;
else 
ti=0; //Press the button to increase the duty cycle
 }
while(GPIO_ReadInputDataBit(GPIOE,GPIO_Pin_2)==0); //Yes, here is the release judgment
}
}


void TIM3_IRQHandler(void) //When ti is reached, enter the interrupt and give the led a high level with a certain duty cycle
{
static u8 i=0;
       TIM_ClearITPendingBit(TIM3,TIM_IT_Update); //Clear the interrupt source
GPIO_Write(GPIOC,(u16)~(0x01<if(i==8) i=0;
}


main.c The main function is simple

#include "public.h"
#include "Systick.h"
#include "PWM.h"
#include "ExInter.h"

int main()

{

PWM_init();
ExInter_init();
 while(1)
{
delay_ms(10);
TIM_SetCompare2(TIM3,ti); //Connect channel 2,
}  

}

To be honest, it is a good habit to write down your understanding in comments.


Keywords:STM32 Reference address:The principle, use and configuration of PWM peripherals in STM32

Previous article:stm32 simulates output of multiple PWM channels through IO port
Next article:stm32f103 timer1 generates 400Hz PWM

Recommended ReadingLatest update time:2024-11-16 16:00

Difference between DSP and STM32
The difference between DSP and ordinary 51 AVR and STM32      DSP is a chip designed for computing. Its most powerful feature is its numerical computing performance, which is supported by its instruction set. Forget about comparing DSP and STM32. If you are familiar with both, you will know that there is no comparison
[Microcontroller]
STM32 general timer introduction
The STM32 timer is a powerful module, and the frequency of the timer is also very high. The timer can do some basic timing, and can also do PWM output or input capture functions. Clock source problem: There are eight TIMx, of which TIM1 and TIM8 are connected to the APB2 bus, while TIM2-TIM7 are connected to t
[Microcontroller]
STM32 general timer introduction
stm32 FSMC-External SRAM IS62WV51216
Pin Definition  FSMC configuration steps 1. Enable the corresponding pin GPIO clock  2. Configure the GPIO pin mode  3. Enable the FSMC clock  4. FSMC initialization  5. Memory block enable Example  #define Bank1_SRAM3_ADDR ((u32)(0x68000000)) //First address 0x60000000, each block 0x40000000 void SRAM_gp
[Microcontroller]
stm32 FSMC-External SRAM IS62WV51216
STM32 timer for external pulse counting
Because stm32f103c8 is used as the main controller to control the car, the speed of the car is input by two photoelectric encoders (one on the left and one on the right). Therefore, the external clock trigger mode (TIM-ETRClockMode2Config) is considered.         After trying for a long time, I found that TIM1 could
[Microcontroller]
About BOOT0 and BOOT1 of STM32
STM32 has three startup modes. In RM0008 downloaded from the ST official website, you can find the startup-related configuration instructions: Translated into Chinese: The storage media corresponding to the three boot modes of STM32 are all built-in to the chip, they are: 1) User Flash = Flash built into the chi
[Microcontroller]
Detailed explanation of the hardware composition of the STM32 minimum system
Detailed explanation of the hardware composition of the STM32 minimum system 0 composition: power reset clock debug interface start 1. Power supply: generally 3.3V LDO power supply plus multiple 0.01uf decoupling capacitors   2. Reset: There are three reset methods: power-on reset, manual reset, and program auto
[Microcontroller]
Detailed explanation of the hardware composition of the STM32 minimum system
Queues and stacks based on STM32
Use ESP8266 to receive data transmitted from the cloud. The data sent from the cloud may be several groups of data sent at the same time, and the lower computer can only process them one by one. Therefore, it is necessary to establish a buffer array to receive cloud data and then process them one by one -- queue. queu
[Microcontroller]
Queues and stacks based on STM32
STM32 register operation mode learning - GPIO
1. When a port is to be configured as an external interrupt line, the port must be configured in input mode. 2. For bidirectional multiplexing functions, the port bit must be configured with the multiplexing function output mode (push-pull or open-drain). At this time, the input driver is configured as floating input
[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号