今天做了一个小实验:STM32F407的定时器2输出4路PWM波。程序如下:
/********************************************************************
函数功能:配置定时器2输出4路PWM波
入口参数:无
出口参数:无
备 注:主频168M,PWM周期100ms,占空比50%
*********************************************************************/
void TIM2PWM_Config()
{
GPIO_InitTypeDef GPIO_InitStruct;
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStruct;
TIM_OCInitTypeDef TIM_OCInitStruct;
RCC_AHB1PeriphClockCmd (RCC_AHB1Periph_GPIOA,ENABLE ); //PA时钟使能
RCC_APB1PeriphClockCmd (RCC_APB1Periph_TIM2,ENABLE); //定时器时钟使能
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3;
GPIO_InitStruct.GPIO_OType = GPIO_OType_PP ;
GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init (GPIOA,&GPIO_InitStruct); //引脚配置复用功能推挽输出
GPIO_PinAFConfig (GPIOA,GPIO_PinSource0 | GPIO_PinSource1 | GPIO_PinSource2 | GPIO_PinSource3,GPIO_AF_TIM2);
TIM_TimeBaseInitStruct.TIM_CounterMode = TIM_CounterMode_Up ;
TIM_TimeBaseInitStruct.TIM_Period = 10000;
TIM_TimeBaseInitStruct.TIM_Prescaler = 839;
TIM_TimeBaseInit (TIM2,&TIM_TimeBaseInitStruct); //增计数模式
TIM_OCInitStruct.TIM_OCMode = TIM_OCMode_PWM1 ;
TIM_OCInitStruct.TIM_OCPolarity = TIM_OCPolarity_High ;
TIM_OCInitStruct.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStruct.TIM_Pulse = 5000;
TIM_OC1Init(TIM2,&TIM_OCInitStruct);
TIM_OC2Init(TIM2,&TIM_OCInitStruct);
TIM_OC3Init(TIM2,&TIM_OCInitStruct);
TIM_OC4Init(TIM2,&TIM_OCInitStruct); //配置四个通道
TIM_OC1PreloadConfig(TIM2,TIM_OCPreload_Enable);
TIM_OC2PreloadConfig(TIM2,TIM_OCPreload_Enable);
TIM_OC3PreloadConfig(TIM2,TIM_OCPreload_Enable);
TIM_OC4PreloadConfig(TIM2,TIM_OCPreload_Enable);
TIM_Cmd (TIM2,ENABLE); //使能定时器
}
最终的效果就是,木有任何波形输出。大家看看能否说出问题出在哪里....答案回帖可见哦!!!
——回复可见内容——