当前位置:网站首页>第六篇,STM32脉冲宽度调制(PWM)编程
第六篇,STM32脉冲宽度调制(PWM)编程
2022-07-06 17:07:00 【车水码浓】
1.PWM概念
PWM叫脉冲宽度调制(Pulse Width Modulation),通过编程控制输出方波的频率和占空比(高低电平的比例),广泛应用在测量,通信,功率控制等领域(呼吸灯,电机)。
PWM由定时器驱动,PWM周期就是定时器的周期,为了调节占空比,需要在定时器的基础上加上一个比较计数器,同时需要GPIO输出波形。
——————————————————————————————————————————
2.stm32中的PWM
stm32中的PWM属于定时器功能,通过配置定时器就可以使用PWM,除了定时器的基本配置以外,还要加入一个比较计数值确定翻转电平的时机,还需要GPIO的复用功能输出PWM。
stm32中PWM高低电平的顺序是由极性,PWM模式和计数模式共同决定。极性决定默认电平(有效电平),PWM模式指的是一个周期内有效电平和无效电平的顺序。
—————————————————————————————————————————
3.使用库函数实现PWM配置D1为呼吸灯
(1)开启时钟
GPIOF时钟 TIM14时钟,函数略
(2)初始化GPIO为复用功能
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF; GPIO_Init(...); 函数略
(3)将定时器14通道1的复用功能映射到PF9
void GPIO_PinAFConfig(GPIO_TypeDef* GPIOx, uint16_t GPIO_PinSource, uint8_t GPIO_AF); 参数: GPIOx - 哪一组GPIO
GPIO_PinSource - 哪个GPIO引脚
GPIO_AF - 哪个复用功能(只能映射具有的复用功能)
(4)初始化定时器
TIM_TimeBaseInit(......); 函数略
(5)初始化PWM
void TIM_OC1Init(TIM_TypeDef* TIMx, TIM_OCInitTypeDef* TIM_OCInitStruct);
参数: TIMx - 哪个定时器
TIM_OCInitStruct - 初始化结构 t
ypedef struct {
uint16_t TIM_OCMode; /*!< PWM模式 @ref TIM_Output_Compare_and_PWM_modes */ uint16_t TIM_OutputState; /*!< 输出状态使能 @ref TIM_Output_Compare_State */ uint16_t TIM_OutputNState; /*!< 忽略 only for TIM1 and TIM8. */
uint32_t TIM_Pulse; /*!< 比较计数值 0x0000 and 0xFFFF */
uint16_t TIM_OCPolarity; /*!< 极性 @ref TIM_Output_Compare_Polarity */ uint16_t TIM_OCNPolarity; /*!< 忽略 only for TIM1 and TIM8. */ uint16_t TIM_OCIdleState; /*!< 忽略 only for TIM1 and TIM8. */ uint16_t TIM_OCNIdleState; /*!< 忽略 only for TIM1 and TIM8. */ } TIM_OCInitTypeDef;
(6)使能PWM的预装载和重装载功能
TIM_OC1PreloadConfig(TIM14, TIM_OCPreload_Enable); TIM_ARRPreloadConfig(TIM14, ENABLE);
(7)启动定时器
TIM_Cmd(...); //高级定时器(TIM1/TIM8),还需要开启另一个开关 void TIM_CtrlPWMOutputs(TIM_TypeDef* TIMx, FunctionalState NewState);
参数: TIMx - 哪个定时器
NewState - ENABLE/DISABLE
(8)运行时可调节占空比
void TIM_SetCompare1(TIM_TypeDef* TIMx, uint32_t Compare1); 参数: TIMx - 哪个定时器 Compare1 - 新的比较值
__________________________________________________________________________________________________________________________________________________________
使用库函数配置GPIOF,TIM14,使得D1灯呼吸闪烁,代码实现如下:
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.开启GPIOF和TIM14时钟
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOF,ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM14,ENABLE);
//2.初始化PF9为复用功能
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;//复用模式
GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;//推挽输出
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;//高速
GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL;//无上下拉
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_9;//PF9
GPIO_Init(GPIOF,&GPIO_InitStruct);
//3.将PF9复用映射到TIM14
GPIO_PinAFConfig(GPIOF,GPIO_PinSource9,GPIO_AF_TIM14);
//4.初始化定时器14 84M / 84 = 1MHz 1M ------ 1000 ----- 1ms
TIM_TimeBaseInitStruct.TIM_Prescaler = 84-1;//预分频系数
TIM_TimeBaseInitStruct.TIM_Period = 1000-1;//初始计数值
TIM_TimeBaseInitStruct.TIM_CounterMode = TIM_CounterMode_Down;//向下计数
TIM_TimeBaseInitStruct.TIM_ClockDivision = TIM_CKD_DIV1;//时钟因子
TIM_TimeBaseInit(TIM14,&TIM_TimeBaseInitStruct);
//5.PWM初始化
TIM_OCInitStruct.TIM_OCMode = TIM_OCMode_PWM1;//PWM模式1
TIM_OCInitStruct.TIM_OCPolarity = TIM_OCPolarity_Low;//低电平有效
TIM_OCInitStruct.TIM_OutputState = TIM_OutputState_Enable;//使能
TIM_OCInitStruct.TIM_Pulse = 800;//比较计数值
TIM_OC1Init(TIM14,&TIM_OCInitStruct);
//6.使能PWM的预装载和重装载功能
TIM_OC1PreloadConfig(TIM14, TIM_OCPreload_Enable);
TIM_ARRPreloadConfig(TIM14, ENABLE);
//7.使能定时器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
主函数main.c
#include <stm32f4xx.h>
#include <includes.h>
int main()
{
u32 comp = 0;
//1.中断优先级分组 2:2
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
//初始化
//led_init();
//key_init();
beep_init();
exti_init();
mq2_init();
delay_init();
//timer2_init();
//timer10_init();
timer14_pwm_init();
//D1为呼吸灯
while(1){
//1s从最暗到最亮
while(comp<1000){
TIM_SetCompare1(TIM14,comp);
comp++;
delay_ms(1);
}
//1s从最亮到最暗
while(comp>0){
TIM_SetCompare1(TIM14,comp);
comp--;
delay_ms(1);
}
delay_ms(200);
}
}
练习:
使用TIM1的通道4控制D4作为呼吸灯
TIM1是高级定时器 通道4
边栏推荐
- What is time
- Common shortcuts to idea
- Memory optimization of Amazon memorydb for redis and Amazon elasticache for redis
- Matlab learning notes
- 【JokerのZYNQ7020】AXI_EMC。
- AI super clear repair resurfaces the light in Huang Jiaju's eyes, Lecun boss's "deep learning" course survival report, beautiful paintings only need one line of code, AI's latest paper | showmeai info
- 【批处理DOS-CMD命令-汇总和小结】-跳转、循环、条件命令(goto、errorlevel、if、for[读取、切分、提取字符串]、)cmd命令错误汇总,cmd错误
- Advanced learning of MySQL -- basics -- multi table query -- subquery
- Linear algebra of deep learning
- 37頁數字鄉村振興智慧農業整體規劃建設方案
猜你喜欢
【软件逆向-自动化】逆向工具大全
threejs图片变形放大全屏动画js特效
Stm32f407 ------- DAC digital to analog conversion
uniapp实现从本地上传头像并显示,同时将头像转化为base64格式存储在mysql数据库中
Attention SLAM:一种从人类注意中学习的视觉单目SLAM
2021 SASE integration strategic roadmap (I)
深度学习之线性代数
New feature of Oracle 19C: automatic DML redirection of ADG, enhanced read-write separation -- ADG_ REDIRECT_ DML
基於GO語言實現的X.509證書
37 pages Digital Village revitalization intelligent agriculture Comprehensive Planning and Construction Scheme
随机推荐
Article management system based on SSM framework
Attention SLAM:一種從人類注意中學習的視覺單目SLAM
alexnet实验偶遇:loss nan, train acc 0.100, test acc 0.100情况
新手如何入门学习PostgreSQL?
Leetcode (547) - number of provinces
JWT signature does not match locally computed signature. JWT validity cannot be asserted and should
【软件逆向-自动化】逆向工具大全
Advantages and disadvantages of code cloning
C9 colleges and universities, doctoral students make a statement of nature!
智能运维应用之道,告别企业数字化转型危机
Chapter 5 DML data operation
5种不同的代码相似性检测,以及代码相似性检测的发展趋势
浅谈测试开发怎么入门,如何提升?
Rails 4 asset pipeline vendor asset images are not precompiled
QT tutorial: creating the first QT program
Win10 startup error, press F9 to enter how to repair?
Amazon MemoryDB for Redis 和 Amazon ElastiCache for Redis 的内存优化
dynamic programming
48 page digital government smart government all in one solution
【软件逆向-求解flag】内存获取、逆变换操作、线性变换、约束求解