TIM_TimeBaseInitStruct.TIM_Prescaler = 179; // 10/2MHZ=5us
TIM_TimeBaseInitStruct.TIM_Prescaler = 179; // 10/2MHZ=5us
TIM_TimeBaseInitStruct.TIM_Period = 1999;//10ms
TIM_TimeBaseInitStruct.TIM_ClockDivision = 0;
TIM_TimeBaseInitStruct.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseInitStruct);
/********以下 4路PWM设置共有**********/
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;//输出模式
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;//
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;//高优先级
/****PWM1*****/
TIM_OCInitStructure.TIM_Pulse = TIM3_CCR1_PWM1;//占空比分子1,即计数器计数到TIM3_CCR1_PWM1 发生跳变
TIM_OC1Init(TIM3, &TIM_OCInitStructure); //初始化
TIM_OC1PreloadConfig(TIM3, TIM_OCPreload_Enable);//预重载使能
/****PWM2*****/
TIM_OCInitStructure.TIM_Pulse = TIM3_CCR1_PWM2; //占空比分子2,即计数器计数到TIM3_CCR1_PWM2 发生跳变
TIM_OC2Init(TIM3, &TIM_OCInitStructure);
TIM_OC2PreloadConfig(TIM3, TIM_OCPreload_Enable);
/****PWM3*****/
TIM_OCInitStructure.TIM_Pulse = TIM3_CCR1_PWM3;//占空比分子3,即计数器计数到TIM3_CCR1_PWM3 发生跳变
TIM_OC3Init(TIM3, &TIM_OCInitStructure);
TIM_OC3PreloadConfig(TIM3, TIM_OCPreload_Enable);
/****PWM4*****/
TIM_OCInitStructure.TIM_Pulse = TIM3_CCR1_PWM4;//占空比分子4,即计数器计数到TIM3_CCR1_PWM4 发生跳变
TIM_OC4Init(TIM3, &TIM_OCInitStructure);
TIM_OC4PreloadConfig(TIM3, TIM_OCPreload_Enable);
TIM_ARRPreloadConfig(TIM3, ENABLE);//此句最好写上 防止意外发生,默认自动装载
TIM_Cmd(TIM3, ENABLE);
若需要调节占空比 则调用以下函数
TIM_SetCompare1(TIM3, TIM3_CCR1_PWM1);//PWM1,调节占空比
TIM_SetCompare2(TIM3, TIM3_CCR1_PWM2);//PWM1,调节占空比
TIM_SetCompare3(TIM3, TIM3_CCR1_PWM3);//PWM1,调节占空比
TIM_SetCompare4(TIM3, TIM3_CCR1_PWM4);//PWM1,调节占空比 |