当前位置:网站首页>Part VI, STM32 pulse width modulation (PWM) programming

Part VI, STM32 pulse width modulation (PWM) programming

2022-07-07 00:56:00 Heavy vehicle water

1.PWM Concept

PWM It's called pulse width modulation (Pulse Width Modulation), The frequency and duty cycle of the output square wave are controlled by programming ( Proportion of high and low levels ), Widely used in measurement , signal communication , Power control and other fields ( Breathing lights , The motor ).

    PWM Driven by timer ,PWM The period is the period of the timer , To adjust the duty cycle , You need to add a comparison counter to the timer , Simultaneous need GPIO Output waveform .

——————————————————————————————————————————

2.stm32 Medium PWM

    stm32 Medium PWM It belongs to timer function , By configuring the timer, you can use PWM, In addition to the basic configuration of timer , Also add a time to compare the count value to determine the turning level , It also needs to be GPIO Multiplexing function output of PWM.

    stm32 in PWM The order of high and low levels is determined by polarity ,PWM Mode and counting mode jointly determine . Polarity determines the default level ( Effective level ),PWM Mode refers to the order of effective level and invalid level in a cycle .

 

    —————————————————————————————————————————

3. Use library functions to realize PWM To configure D1 For breathing lights

(1) Turn on the clock

GPIOF The clock TIM14 The clock , Function omitted

(2) initialization GPIO For reuse functions

GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF; GPIO_Init(...);  Function omitted

(3) Set the timer 14 passageway 1 The reuse function of maps to PF9

void GPIO_PinAFConfig(GPIO_TypeDef* GPIOx, uint16_t GPIO_PinSource, uint8_t GPIO_AF); Parameters :     GPIOx -  Which group GPIO     

                GPIO_PinSource -  Which one? GPIO Pin     

                GPIO_AF -  Which reuse function ( Only the reusable functions that can be mapped )

(4) Initialize the timer

TIM_TimeBaseInit(......); Function omitted

(5) initialization PWM

void TIM_OC1Init(TIM_TypeDef* TIMx, TIM_OCInitTypeDef* TIM_OCInitStruct);

Parameters :     TIMx -  Which timer     

                TIM_OCInitStruct -  Initialization structure      t

ypedef struct {

  uint16_t TIM_OCMode;        /*!< PWM Pattern  @ref TIM_Output_Compare_and_PWM_modes */   uint16_t TIM_OutputState;   /*!<  Output status enable  @ref TIM_Output_Compare_State */   uint16_t TIM_OutputNState;  /*!<  Ignore  only for TIM1 and TIM8. */

  uint32_t TIM_Pulse;         /*!<  Compare the count  0x0000 and 0xFFFF */   

  uint16_t TIM_OCPolarity;    /*!<  Polarity  @ref TIM_Output_Compare_Polarity */   uint16_t TIM_OCNPolarity;   /*!<  Ignore  only for TIM1 and TIM8. */   uint16_t TIM_OCIdleState;   /*!<  Ignore  only for TIM1 and TIM8. */   uint16_t TIM_OCNIdleState;  /*!<  Ignore  only for TIM1 and TIM8. */ } TIM_OCInitTypeDef;    

(6) Can make PWM Preloading and reloading functions of

TIM_OC1PreloadConfig(TIM14, TIM_OCPreload_Enable); TIM_ARRPreloadConfig(TIM14, ENABLE);

(7) Start timer

TIM_Cmd(...); // Advanced timer (TIM1/TIM8), You also need to turn on another switch void TIM_CtrlPWMOutputs(TIM_TypeDef* TIMx, FunctionalState NewState);

Parameters :     TIMx -  Which timer    

               NewState - ENABLE/DISABLE

(8) The duty cycle can be adjusted during operation

void TIM_SetCompare1(TIM_TypeDef* TIMx, uint32_t Compare1); Parameters :     TIMx -  Which timer     Compare1 -  New comparison value  

__________________________________________________________________________________________________________________________________________________________

  Use library functions to configure GPIOF,TIM14, bring D1 Light breathing flickers , The code implementation is as follows :

pwm.c

#include <stm32f4xx.h>
#include <pwm.h>

void timer14_pwm_init(void)
{
	GPIO_InitTypeDef GPIO_InitStruct;
	TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStruct;
	TIM_OCInitTypeDef TIM_OCInitStruct;
	
	//1. Turn on GPIOF and TIM14 The clock 
	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOF,ENABLE);
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM14,ENABLE);
	
	//2. initialization PF9 For reuse functions 
	GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;// Reuse mode 
	GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;// Push pull output 
	GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;// High speed 
	GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL;// No up and down 
	GPIO_InitStruct.GPIO_Pin = GPIO_Pin_9;//PF9
	GPIO_Init(GPIOF,&GPIO_InitStruct);
	
	//3. take PF9 Reuse mapping to TIM14
	GPIO_PinAFConfig(GPIOF,GPIO_PinSource9,GPIO_AF_TIM14);
	
	//4. Initialize the timer 14 84M / 84 = 1MHz 1M ------ 1000 ----- 1ms
	TIM_TimeBaseInitStruct.TIM_Prescaler = 84-1;// The prescaled coefficients 
	TIM_TimeBaseInitStruct.TIM_Period = 1000-1;// Initial count 
	TIM_TimeBaseInitStruct.TIM_CounterMode = TIM_CounterMode_Down;// Count down 
	TIM_TimeBaseInitStruct.TIM_ClockDivision = TIM_CKD_DIV1;// Clock factor 
	TIM_TimeBaseInit(TIM14,&TIM_TimeBaseInitStruct);
	
	//5.PWM initialization 
	TIM_OCInitStruct.TIM_OCMode = TIM_OCMode_PWM1;//PWM Pattern 1
	TIM_OCInitStruct.TIM_OCPolarity = TIM_OCPolarity_Low;// Low level active 
	TIM_OCInitStruct.TIM_OutputState = TIM_OutputState_Enable;// Can make 
	TIM_OCInitStruct.TIM_Pulse = 800;// Compare the count 
	TIM_OC1Init(TIM14,&TIM_OCInitStruct);
	
	//6. Can make PWM Preloading and reloading functions of 
	TIM_OC1PreloadConfig(TIM14, TIM_OCPreload_Enable);
	TIM_ARRPreloadConfig(TIM14, ENABLE);
	
	//7. Enable timer 14
	TIM_Cmd(TIM14,ENABLE);
}

pwm.h

#ifndef _KEY_H_
#define _KEY_H_

#define S1 PAin(0)
#define S2 PEin(2)
#define S3 PEin(3)
#define S4 PEin(4)

void key_init(void);

#endif

The main function main.c

#include <stm32f4xx.h>
#include <includes.h>

int main()
{	
	u32 comp = 0;
	
	//1. Interrupt priority grouping  2:2
	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
	
	// initialization 
	//led_init();
	//key_init();
	beep_init();
	exti_init();
	mq2_init();
	delay_init();
	//timer2_init();
	//timer10_init();
	timer14_pwm_init();
	
	//D1 For breathing lights 
	while(1){
		//1s From darkest to brightest 
		while(comp<1000){
			TIM_SetCompare1(TIM14,comp);
			comp++;
			delay_ms(1);
		}
		
		//1s From brightest to darkest 
		while(comp>0){
			TIM_SetCompare1(TIM14,comp);
			comp--;
			delay_ms(1);
		}
		
		delay_ms(200);
	}
	
}

practice :

Use TIM1 The passage of 4 control D4 As a breathing lamp

TIM1 It's the advanced timer passageway 4

原网站

版权声明
本文为[Heavy vehicle water]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/188/202207061707109128.html