当前位置:网站首页>第六篇,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
边栏推荐
- Advantages and disadvantages of code cloning
- 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
- 【JokerのZYNQ7020】AXI_ EMC。
- Memory optimization of Amazon memorydb for redis and Amazon elasticache for redis
- AI超清修复出黄家驹眼里的光、LeCun大佬《深度学习》课程生还报告、绝美画作只需一行代码、AI最新论文 | ShowMeAI资讯日报 #07.06
- Encryption algorithm - password security
- If the college entrance examination goes well, I'm already graying out at the construction site at the moment
- 509 certificat basé sur Go
- Leetcode (547) - number of provinces
- 一图看懂对程序员的误解:西方程序员眼中的中国程序员
猜你喜欢

2021 SASE integration strategic roadmap (I)

The way of intelligent operation and maintenance application, bid farewell to the crisis of enterprise digital transformation

Attention SLAM:一種從人類注意中學習的視覺單目SLAM

【软件逆向-求解flag】内存获取、逆变换操作、线性变换、约束求解

Dr selection of OSPF configuration for Huawei devices

Attention SLAM:一种从人类注意中学习的视觉单目SLAM

学习光线跟踪一样的自3D表征Ego3RT

基於GO語言實現的X.509證書

Mujoco second order simple pendulum modeling and control

Attention slam: a visual monocular slam that learns from human attention
随机推荐
Use mujoco to simulate Cassie robot
【vulnhub】presidential1
509 certificat basé sur Go
Advanced learning of MySQL -- basics -- multi table query -- subquery
【JokerのZYNQ7020】AXI_EMC。
Jenkins' user credentials plug-in installation
How to judge whether an element in an array contains all attribute values of an object
Leetcode(547)——省份数量
[Niuke classic question 01] bit operation
Policy Gradient Methods
Hero League | King | cross the line of fire BGM AI score competition sharing
Mujoco produces analog video
准备好在CI/CD中自动化持续部署了吗?
Markov decision process
再聊聊我常用的15个数据源网站
【批处理DOS-CMD命令-汇总和小结】-查看或修改文件属性(ATTRIB),查看、修改文件关联类型(assoc、ftype)
Zynq transplant ucosiii
AI超清修复出黄家驹眼里的光、LeCun大佬《深度学习》课程生还报告、绝美画作只需一行代码、AI最新论文 | ShowMeAI资讯日报 #07.06
Interface master v3.9, API low code development tool, build your interface service platform immediately
[C language] dynamic address book