tag 标签: stm32四路pwm占空比可调程序

相关博文
  • 热度 7
    2013-7-25 15:55
    6790 次阅读|
    0 个评论
    本文不提供完整程序,仅点出要点 1:时钟配置。因为PWM输出属于复用功能,所以时钟使能必须使能RCC_APB2Periph_AFIO ; 2:IO口使能。因为PWM属于复用功能,所以IO输出设置要配置成GPIO_Mode_AF_PP;而不能配置成普通输出IO的GPIO_Mode_OUT_PP模式; 3:PWM初始化代码如下,本程序只用通道3输出,所以pwm输出只配置了通道3,TIM_OC3Init(TIM3, TIM_OCInitStructure);需要多通道输出PWM则需要配置多个PWM输出通道,通过改变TIM_Pulse 的值可以改变pwm的占空比(可以通过直接操作寄存器TIM3-CCR3=2400来实现)。 /* Time Base configuration */   TIM_TimeBaseStructure.TIM_Prescaler = 0;   //预分频为0   TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;//向上计数   TIM_TimeBaseStructure.TIM_Period = 4800; //8191=8.789 KHz, 4095=17.57 KHz,频率太高超过人耳听力范围   TIM_TimeBaseStructure.TIM_ClockDivision = 0;    //时钟分频系数为0   TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;   //计数周期数为0    TIM_TimeBaseInit(TIM3, TIM_TimeBaseStructure);   //TIM3配置参数写入      /* Channel 1, 2,3 and 4 Configuration in PWM mode */   TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2;             //pwm模式选择PWM2   TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;  //正向通道输出使能  // TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable;   //反向通道输出使能   TIM_OCInitStructure.TIM_Pulse = 2400;                 //占空比设置   TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;     //正向通道输出极性   //TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCPolarity_Low;         //反向通道输出极性   TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set;  //正向通道闲散电平   //TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCIdleState_Reset;   //反向通道闲散电平   TIM_OC3Init(TIM3, TIM_OCInitStructure);   TIM_OC3PreloadConfig(TIM3,TIM_OCPreload_Disable);   TIM_ClearITPendingBit(TIM3,TIM_IT_CC3);   TIM_Cmd(TIM3, ENABLE);   TIM_CtrlPWMOutputs(TIM3, ENABLE);