STM32定时器产生PWM--呼吸灯

说明:本工程代码在STM32F407ZET6上测试通过。

1、PWM定义

Pulse Width Modulation:脉冲宽度调制(PWM)

2、用途

(1)控制输出的电压和电流
(2)灯光的亮度
(3)电机控制

3、分析PWM产生与重点

(1)分析PWM产生(看图示PWM)
(2)区别PWM1与PWM2模式(STM32F4xx中文参考手册 433页)
(2)高低电平极性区别(STM32F4xx中文参考手册 457页)

4、PWM库函数开发流程

(1)使能定时器14和相关IO口时钟:

使能定时器14时钟:RCC_APB1PeriphClockCmd();    
使能GPIOF时钟:RCC_AHB1PeriphClockCmd ();

(2)初始化IO口为复用功能输出:

函数:GPIO_Init();  
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;       //复用功能

(3)GPIOF9复用映射到定时器14:(PE9引脚复用为TIM14,同时与一个LED灯连接)

GPIO_PinAFConfig(GPIOF,GPIO_PinSource9,GPIO_AF_TIM14); 

(4)初始化定时器:ARR,PSC等:

TIM_TimeBaseInit();

(5)初始化输出比较参数:

TIM_OC1Init();

(6)使能预装载寄存器:

TIM_OC1PreloadConfig(TIM14, TIM_OCPreload_Enable); 

(7)使能自动重装载的预装载寄存器允许位:

TIM_ARRPreloadConfig(TIM14,ENABLE);

(8)使能定时器;
(9)不断改变比较值CCRx,达到不同的占空比效果:

TIM_SetCompare1();

5、函数说明

从库函数当中可以截取出相关函数的说明

  * @brief  Initializes the TIMx Channel1 according to the specified parameters in
  *         the TIM_OCInitStruct.
  * @param  TIMx: where x can be 1 to 14 except 6 and 7, to select the TIM peripheral.
  * @param  TIM_OCInitStruct: pointer to a TIM_OCInitTypeDef structure that contains
  *         the configuration information for the specified TIM peripheral.
  * @retval None
  */
void TIM_OC1Init(TIM_TypeDef* TIMx, TIM_OCInitTypeDef* TIM_OCInitStruct);

//本实验只是用到PWM,所以只需配置该结构体中的三个成员
typedef struct
{
  uint16_t TIM_OCMode;        //PWM1模式或PWM2模式
  uint16_t TIM_OutputState;   //输出是否使能
  uint16_t TIM_OCPolarity;    //输出极性                           
} TIM_OCInitTypeDef;

//改变CCR值
/**
  * @brief  Sets the TIMx Capture Compare1 Register value
  * @param  TIMx: where x can be 1 to 14 except 6 and 7, to select the TIM peripheral.
  * @param  Compare1: specifies the Capture Compare1 register new value.
  * @retval None
  */
void TIM_SetCompare1(TIM_TypeDef* TIMx, uint32_t Compare1);

6、具体代码

#include "pwm.h"
#include "systick.h"

//TIM14配置
void Pwm_Time14_Init(void)
{
    TIM_OCInitTypeDef       TIM_OCInitStruct;
    GPIO_InitTypeDef        GPIO_InitStruct;
    TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStruct;

    //通用定时器的时钟是APB1(42)*2 = 84MHz
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM14, ENABLE);
    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOF, ENABLE);

    GPIO_InitStruct.GPIO_Pin    = GPIO_Pin_9;
    GPIO_InitStruct.GPIO_Mode   = GPIO_Mode_AF;
    GPIO_InitStruct.GPIO_OType  = GPIO_OType_PP;
    GPIO_InitStruct.GPIO_Speed  = GPIO_Speed_100MHz;
    GPIO_InitStruct.GPIO_PuPd   = GPIO_PuPd_UP;
    GPIO_Init(GPIOF, &GPIO_InitStruct);

    //GPIOF9复用映射到TIM14
    GPIO_PinAFConfig(GPIOF, GPIO_PinSource9, GPIO_AF_TIM14);

    TIM_TimeBaseInitStruct.TIM_Prescaler     = 84-1;   
    TIM_TimeBaseInitStruct.TIM_Period        = 500-1;  
    TIM_TimeBaseInitStruct.TIM_CounterMode   = TIM_CounterMode_Up; 
    TIM_TimeBaseInitStruct.TIM_ClockDivision = TIM_CKD_DIV1; 
    TIM_TimeBaseInit(TIM14,&TIM_TimeBaseInitStruct);

    TIM_OCInitStruct.TIM_OCMode         = TIM_OCMode_PWM1;
    TIM_OCInitStruct.TIM_OutputState    = TIM_OutputState_Enable;
    TIM_OCInitStruct.TIM_OCPolarity     = TIM_OCPolarity_Low;
    TIM_OC1Init(TIM14,&TIM_OCInitStruct);

    //使能预装载寄存器
    TIM_OC1PreloadConfig(TIM14, TIM_OCPreload_Enable);
    //使能自动装载与预装载寄存器的允许位
    TIM_ARRPreloadConfig(TIM14,ENABLE);
    //使能TIM14定时器
    TIM_Cmd(TIM14, ENABLE);
}

//呼吸灯
void Led_Breath(void)
{
    uint32_t cnt;
    for(cnt=0; cnt<=500; cnt++)
    {
        TIM_SetCompare1(TIM14,cnt);
        delay_ms(5);
    }
    for(; cnt>0; cnt--)
    {
        TIM_SetCompare1(TIM14,cnt);
        delay_ms(5);
    }
}

如需下载整个工程代码,STM32F4xx用户手册以及该工程对应开发板的原理图,请点击一下链接:
http://download.csdn.net/download/xiebaocheng12138/9997960

  • 2
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值