本帖最后由 一路向北lm 于 2018-7-8 16:30 编辑
如下程序,大家帮忙看下,为什么不能输出PWM,
void TIM4_CH1_init(u32 Freq,u16 Dutycycle)
{
u32 time4_period;
u32 time4_pulse;
time4_period=1000000/(Freq*20)-1;
time4_pulse=(time4_period+1)*Dutycycle/100;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStruct;
GPIO_InitTypeDef GPIO_InitStructure;
TIM_OCInitTypeDef TIM_OCInitStructre;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
// GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_PinAFConfig(GPIOB,GPIO_PinSource6,GPIO_AF_TIM4);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4,ENABLE);
// TIM_DeInit(TIM4);
TIM_TimeBaseStruct.TIM_Prescaler=167;//Ô¤·ÖƵ
TIM_TimeBaseStruct.TIM_CounterMode=TIM_CounterMode_Up;//ÏòÉÏ
TIM_TimeBaseStruct.TIM_Period=time4_period;//³õÖµ
TIM_TimeBaseStruct.TIM_ClockDivision=0;
TIM_TimeBaseInit(TIM4,&TIM_TimeBaseStruct);
TIM_OCStructInit(&TIM_OCInitStructre);
TIM_OCInitStructre.TIM_OCMode=TIM_OCMode_PWM1;
TIM_OCInitStructre.TIM_OCNIdleState=TIM_OutputState_Enable;
TIM_OCInitStructre.TIM_Pulse=time4_pulse;
TIM_OCInitStructre.TIM_OCPolarity=TIM_OCPolarity_High;
TIM_OC1Init(TIM4,&TIM_OCInitStructre);
TIM_OC1PreloadConfig(TIM4, TIM_OCPreload_Enable);
TIM_ARRPreloadConfig(TIM4,ENABLE);
// TIM_ClearFlag(TIM4, TIM_FLAG_Update);
// TIM_ITConfig(TIM4, TIM_IT_Update,ENABLE);
TIM_Cmd(TIM4,ENABLE);
}
|