本帖最后由 liaiyu0128 于 2012-12-15 16:19 编辑
本人用TIM1输出4路PWM时,PA8,PA10,PA11均可以输出PWM,且占空比可调,PA9没有输出。TIM1的PWM
配置函数如下:
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
/*GPIOA Configuration: TIM1 channel1, 2, 3 and 4
//PA8 = TIM1_CH1
//PA9 = TIM1_CH2
//PA10 = TIM1_CH3
//PA11 = TIM1_CH4
*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10 | GPIO_Pin_11;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
RCC_APB1PeriphClockCmd(RCC_APB2Periph_TIM1,ENABLE);//使能TIM1时钟
TIM_DeInit(TIM1);//复位时钟TIM1,恢复到初始状态
// TIM_InternalClockConfig(TIM1);//使用TIM1内部时钟
TIM_TimeBaseStructure.TIM_Prescaler = 0x0;//2分频
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseStructure.TIM_Period = 7200;//f=72MHz/2/(3600+1)=10K
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
// TIM_TimeBaseStructure.TIM_RepetitionCounter=0;//重复寄存器,用于自动更新pwm占空比
TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_OutputNState=TIM_OutputNState_Disable;//选择互补输出比较状态
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;//设置输出极性
TIM_OCInitStructure.TIM_OCNPolarity=TIM_OCNPolarity_Low;//设置互补输出极性
TIM_OCInitStructure.TIM_OCIdleState=TIM_OCIdleState_Set;//空闲状态下得非工作模式
TIM_OCInitStructure.TIM_OCNIdleState=TIM_OCNIdleState_Reset;//互补空闲状态下得非工作模式
/* PWM1 Mode configuration: Channel1 */
TIM_OCInitStructure.TIM_Pulse = 3600;
TIM_OC1Init(TIM1, &TIM_OCInitStructure);
TIM_OC1PreloadConfig(TIM1, TIM_OCPreload_Enable);
/* PWM1 Mode configuration: Channel2 */
TIM_OCInitStructure.TIM_Pulse = 3600;
TIM_OC2Init(TIM1, &TIM_OCInitStructure);
TIM_OC2PreloadConfig(TIM1, TIM_OCPreload_Enable);
/* PWM1 Mode configuration: Channel3 */
TIM_OCInitStructure.TIM_Pulse = 3600;
TIM_OC3Init(TIM1, &TIM_OCInitStructure);
TIM_OC3PreloadConfig(TIM1, TIM_OCPreload_Enable);
/* PWM1 Mode configuration: Channel4 */
TIM_OCInitStructure.TIM_Pulse = 3600;
TIM_OC4Init(TIM1, &TIM_OCInitStructure);
TIM_OC4PreloadConfig(TIM1, TIM_OCPreload_Enable);
TIM_ARRPreloadConfig(TIM1, ENABLE);
TIM_Cmd(TIM1,ENABLE);
TIM_CtrlPWMOutputs(TIM1,ENABLE);
请各位帮忙把把关!多谢了!
|