/**************************************************************************
* 函数名:PWM_Init
* 描述 :PWM初始化
* 输入 :无
*
* 输出 :无
* 返回 :无
* 调用 :外部调用
*************************************************************************/
void PWM_Init(void)
{
TIM2_PSCR =0x04;
/* Set the Autoreload value */
TIM2_ARRH = (uint8_t)(ARR >> 8);
TIM2_ARRL = (uint8_t)(ARR);
/*TIM2 Frequency=16M/16/(499+1)=2K*/
/* PWM1 Mode configuration: Channel1
TIM2 Channel1 duty cycle = [TIM2_CCR1/(TIM2_ARR + 1)] * 100 = 50%*/
#if TIM2_Channel==TIM2_Channel1
/*TIM2 Frequency = TIM2 counter clock/(ARR + 1) */
TIM2_OC1Init(TIM2_OCMODE_PWM1, TIM2_OUTPUTSTATE_ENABLE,250, TIM2_OCPOLARITY_HIGH);
#elif TIM2_Channel==TIM2_Channel2
TIM2_OC2Init(TIM2_OCMODE_PWM2, TIM2_OUTPUTSTATE_ENABLE,500, TIM2_OCPOLARITY_HIGH);
#elif TIM2_Channel==TIM2_Channel3
TIM2_OC3Init(TIM2_OCMODE_PWM1, TIM2_OUTPUTSTATE_ENABLE,250, TIM2_OCPOLARITY_HIGH);
#endif
TIM2_CR1 |= (uint8_t)MASK_TIM2_CR1_CEN;
}
|