当前位置:网站首页>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
边栏推荐
- Attention slam: a visual monocular slam that learns from human attention
- dynamic programming
- Markov decision process
- Advantages and disadvantages of code cloning
- 英雄联盟|王者|穿越火线 bgm AI配乐大赛分享
- alexnet实验偶遇:loss nan, train acc 0.100, test acc 0.100情况
- Js+svg love diffusion animation JS special effects
- Data processing of deep learning
- Lombok 同时使⽤ @Data 和 @Builder 的坑,你中招没?
- Model-Free Control
猜你喜欢
[Batch dos - cmd Command - Summary and Summary] - String search, find, Filter Commands (FIND, findstr), differentiation and Analysis of Find and findstr
随时随地查看远程试验数据与记录——IPEhub2与IPEmotion APP
Data processing of deep learning
深度学习之线性代数
Memory optimization of Amazon memorydb for redis and Amazon elasticache for redis
Deep learning environment configuration jupyter notebook
Lombok makes ⽤ @data and @builder's pit at the same time. Are you hit?
「精致店主理人」青年创业孵化营·首期顺德场圆满结束!
Stm32f407 ------- DAC digital to analog conversion
Building a dream in the digital era, the Xi'an station of the city chain science and Technology Strategy Summit ended smoothly
随机推荐
随时随地查看远程试验数据与记录——IPEhub2与IPEmotion APP
Dell Notebook Periodic Flash Screen Fault
【批處理DOS-CMD命令-匯總和小結】-字符串搜索、查找、篩選命令(find、findstr),Find和findstr的區別和辨析
Meet the level 3 requirements of ISO 2.0 with the level B construction standard of computer room | hybrid cloud infrastructure
Attention SLAM:一種從人類注意中學習的視覺單目SLAM
Part 7: STM32 serial communication programming
ZYNQ移植uCOSIII
. Bytecode structure of class file
Attention slam: a visual monocular slam that learns from human attention
Zynq transplant ucosiii
【软件逆向-自动化】逆向工具大全
[user defined type] structure, union, enumeration
学习光线跟踪一样的自3D表征Ego3RT
Common shortcuts to idea
Web project com mysql. cj. jdbc. Driver and com mysql. jdbc. Driver differences
【JokerのZYNQ7020】AXI_ EMC。
【YoloV5 6.0|6.1 部署 TensorRT到torchserve】环境搭建|模型转换|engine模型部署(详细的packet文件编写方法)
The printf function is realized through the serial port, and the serial port data reception is realized by interrupt
Advanced learning of MySQL -- basics -- multi table query -- subquery
How do novices get started and learn PostgreSQL?