PWM配置如下:
/*************************************************
Func
tion: TIM1_Init()
Description: pwm2输出
*************************************************/
void TIM1_Init_PWM2(uint16_t arr,uint16_t psc)
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOE,ENABLE);
//PE9
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOE, &GPIO_InitStructure);
GPIO_PinAFConfig(GPIOE, GPIO_PinSource9, GPIO_AF_2); //TIM1channel
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE); //定时器TIM1时钟使能
//TIM_DeInit(TIM1);
//设置在下一个更新事件装入活动的自动重装载寄存器周期的值
TIM_TimeBaseStructure.TIM_Period=arr;
//设置用来作为TIMx时钟频率出书的预分频值
TIM_TimeBaseStructure.TIM_Prescaler =psc;
//设置时钟分割
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
//设置计数模式
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;
//初始化参数
TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);
//TIM1的channel
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2;
TIM_OCInitStructure.TIM_Pulse = 0x90;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;
TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High;
TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCNIdleState_Reset;
TIM_OC1Init(TIM1, &TIM_OCInitStructure);
//TIM_OC1PreloadConfig(TIM1, TIM_OCPreload_Enable);
//TIM_ARRPreloadConfig(TIM1,ENABLE);
TIM_Cmd(TIM1, ENABLE); //使能TIM1
TIM_CtrlPWMOutputs(TIM1, ENABLE);
}
PWM配置后,PE9输出无波形
arc = 2000,psc = 1000
一周热门 更多>