当前位置:网站首页>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
边栏推荐
- QT tutorial: creating the first QT program
- Chapter 5 DML data operation
- 英雄联盟|王者|穿越火线 bgm AI配乐大赛分享
- Lombok makes ⽤ @data and @builder's pit at the same time. Are you hit?
- JS+SVG爱心扩散动画js特效
- Basic information of mujoco
- 【JokerのZYNQ7020】AXI_EMC。
- New feature of Oracle 19C: automatic DML redirection of ADG, enhanced read-write separation -- ADG_ REDIRECT_ DML
- 浅谈测试开发怎么入门,如何提升?
- [C language] dynamic address book
猜你喜欢
[force buckle]41 Missing first positive number
[Batch dos - cmd Command - Summary and Summary] - String search, find, Filter Commands (FIND, findstr), differentiation and Analysis of Find and findstr
深度学习之数据处理
Return to blowing marshland -- travel notes of zhailidong, founder of duanzhitang
Js+svg love diffusion animation JS special effects
Set (generic & list & Set & custom sort)
Configuring the stub area of OSPF for Huawei devices
Telerik UI 2022 R2 SP1 Retail-Not Crack
【YoloV5 6.0|6.1 部署 TensorRT到torchserve】环境搭建|模型转换|engine模型部署(详细的packet文件编写方法)
《安富莱嵌入式周报》第272期:2022.06.27--2022.07.03
随机推荐
第六篇,STM32脉冲宽度调制(PWM)编程
Let's talk about 15 data source websites I often use
Attention SLAM:一种从人类注意中学习的视觉单目SLAM
A brief history of deep learning (II)
Periodic flash screen failure of Dell notebook
批量获取中国所有行政区域经边界纬度坐标(到县区级别)
C Primer Plus Chapter 14 (structure and other data forms)
Summary of being a microservice R & D Engineer in the past year
深度学习简史(二)
Installation and testing of pyflink
STM32开发资料链接分享
Dr selection of OSPF configuration for Huawei devices
深入探索编译插桩技术(四、ASM 探秘)
Lombok makes ⽤ @data and @builder's pit at the same time. Are you hit?
[software reverse - solve flag] memory acquisition, inverse transformation operation, linear transformation, constraint solving
Linear algebra of deep learning
Hero League | King | cross the line of fire BGM AI score competition sharing
省市区三级坐标边界数据csv转JSON
Notes of training courses selected by Massey school
「精致店主理人」青年创业孵化营·首期顺德场圆满结束!