野火电子论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 14808|回复: 1

通用定时器输出PWM波

[复制链接]
发表于 2015-1-23 15:54:44 | 显示全部楼层 |阅读模式
在输出PWM波时,有一部分代码没看懂,查了好多资料也没有弄明白,在stm固件库里面提供的函数中没有找到下面红色标注的部分,而且不明白 ( TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;)这句代码的意思,求大神指教?
static void TIM3_Mode_Config(void)
{
        TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
        TIM_OCInitTypeDef  TIM_OCInitStructure;

        /* PWM信号电平跳变值 */
        u16 CCR1_Val = 500;        
        u16 CCR2_Val = 375;
        u16 CCR3_Val = 250;
        u16 CCR4_Val = 125;

/* -----------------------------------------------------------------------
    TIM3 Channel1 duty cycle = (TIM3_CCR1/ TIM3_ARR+1)* 100% = 50%
    TIM3 Channel2 duty cycle = (TIM3_CCR2/ TIM3_ARR+1)* 100% = 37.5%
    TIM3 Channel3 duty cycle = (TIM3_CCR3/ TIM3_ARR+1)* 100% = 25%
    TIM3 Channel4 duty cycle = (TIM3_CCR4/ TIM3_ARR+1)* 100% = 12.5%
  ----------------------------------------------------------------------- */

  /* Time base configuration */                 
  TIM_TimeBaseStructure.TIM_Period = 999;       //当定时器从0计数到999,即为1000次,为一个定时周期
  TIM_TimeBaseStructure.TIM_Prescaler = 0;            //设置预分频:不预分频,即为72MHz
  TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1 ;        //设置时钟分频系数:不分频(这里用不到)
  TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;  //向上计数模式
  TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);

  /* PWM1 Mode configuration: Channel1 */
  TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;            //配置为PWM模式1
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;       
  TIM_OCInitStructure.TIM_Pulse = CCR1_Val;           //设置跳变值,当计数器计数到这个值时,电平发生跳变
  TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;  //当定时器计数值小于CCR1_Val时为高电平
TIM_OC1Init(TIM3, &TIM_OCInitStructure);         //使能通道1
  TIM_OC1PreloadConfig(TIM3, TIM_OCPreload_Enable);

  /* PWM1 Mode configuration: Channel2 */
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
  TIM_OCInitStructure.TIM_Pulse = CCR2_Val;          //设置通道2的电平跳变值,输出另外一个占空比的PWM
  TIM_OC2Init(TIM3, &TIM_OCInitStructure);          //使能通道2
  TIM_OC2PreloadConfig(TIM3, TIM_OCPreload_Enable);

  /* PWM1 Mode configuration: Channel3 */
  TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
  TIM_OCInitStructure.TIM_Pulse = CCR3_Val;        //设置通道3的电平跳变值,输出另外一个占空比的PWM
  TIM_OC3Init(TIM3, &TIM_OCInitStructure);         //使能通道3
  TIM_OC3PreloadConfig(TIM3, TIM_OCPreload_Enable);

  /* PWM1 Mode configuration: Channel4 */
  TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
  TIM_OCInitStructure.TIM_Pulse = CCR4_Val;        //设置通道4的电平跳变值,输出另外一个占空比的PWM
  TIM_OC4Init(TIM3, &TIM_OCInitStructure);        //使能通道4
  TIM_OC4PreloadConfig(TIM3, TIM_OCPreload_Enable);
  TIM_ARRPreloadConfig(TIM3, ENABLE);                         // 使能TIM3重载寄存器ARR

  /* TIM3 enable counter */
  TIM_Cmd(TIM3, ENABLE);                   //使能定时器3       
}

回复

使用道具 举报

发表于 2015-1-23 16:25:14 | 显示全部楼层
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
就是开启OCx输出到对应引脚

回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

联系站长|手机版|野火电子官网|野火淘宝店铺|野火电子论坛 ( 粤ICP备14069197号 ) 大学生ARM嵌入式2群

GMT+8, 2024-11-24 10:02 , Processed in 0.029126 second(s), 24 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表