求大神帮我看下 一直不能产生PWM波形,芯片是STM32F030C8T6,PWM_PER=1024,PWM_PSC=512.
程序如下:
void PWM_init()
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1,ENABLE);
GPIO_PinAFConfig(GPIOB,GPIO_PinSource13,GPIO_AF_1);//GPIO_AF_0 GPIO_AF_2
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_13;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_Level_1;
GPIO_InitStructure.GPIO_OType=GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd=GPIO_PuPd_UP;
GPIO_Init(GPIOB,&GPIO_InitStructure);
TIM_DeInit(TIM1);
TIM_TimeBaseInitStructure.TIM_Period=PWM_PER;
TIM_TimeBaseInitStructure.TIM_Prescaler=PWM_PSC;
TIM_TimeBaseInitStructure.TIM_ClockDivision=TIM_CKD_DIV1;
TIM_TimeBaseInitStructure.TIM_CounterMode=TIM_CounterMode_Up;
TIM_TimeBaseInitStructure.TIM_RepetitionCounter=0;
TIM_TimeBaseInit(TIM1,&TIM_TimeBaseInitStructure);
TIM_PrescalerConfig(TIM1,0,TIM_PSCReloadMode_Immediate);
TIM_ARRPreloadConfig(TIM1, ENABLE);
TIM_OCInitStructure.TIM_OCMode=TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OCPolarity=TIM_OCPolarity_High;
TIM_OCInitStructure.TIM_OutputState=TIM_OutputState_Enable;
TIM_OC1Init(TIM1,&TIM_OCInitStructure);
TIM_OC1PreloadConfig(TIM1,TIM_OCPreload_Enable);
TIM_ARRPreloadConfig(TIM1,ENABLE);
TIM_CtrlPWMOutputs(TIM1, ENABLE);
TIM_Cmd(TIM1,ENABLE);
}
这个程序B.13口输出高电平,库函数里注释的TIM1复用应该是AF0或者AF2,改了的话就是低电平,始终没有PWM产生,搞不懂了
|