代码如下
void Timer_Configuration(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
GPIO_InitTypeDef GPIO_InitStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
// TIMER1 定时器 PWM输出
RCC_APB1PeriphClockCmd(RCC_APB2Periph_TIM1 , ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12|GPIO_Pin_13|GPIO_Pin_14|GPIO_Pin_15;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
// GPIO_PinRemapConfig(GPIO_FullRemap_TIM3,ENABLE);
/************************************
频率 F = 16.384M / ( (1023+1)(15+1) )
*********************************/
TIM_TimeBaseStructure.TIM_Period = 1023; //设置在下一个更新事件装入活动的自动重装载寄存器周期的值
TIM_TimeBaseStructure.TIM_Prescaler = 15; //设置用来作为TIMx时钟频率除数的预分频值
TIM_TimeBaseStructure.TIM_ClockDivision = 0; //设置时钟分割:TDTS = Tck_tim
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //向上计数模式
TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;
TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure); //根据TIM_TimeBaseInitStruct中指定的参数初始化TIMx的时间基数单位
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_Toggle; //pwm模式一输出
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //比较输出使能
TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable;//反向通道使能
TIM_OCInitStructure.TIM_Pulse = 512; //设置待装入捕获比较寄存器的脉冲值
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_OCIdleState_Reset;
TIM_OC1Init(TIM1, &TIM_OCInitStructure); //使能tim4在ccr3上的预装载寄存器
TIM_OCInitStructure.TIM_Pulse = 512;
TIM_OC2Init(TIM1, &TIM_OCInitStructure);
//清中断
TIM_ClearFlag(TIM1, TIM_FLAG_CC1);
//TIM1中断源设置,开启相应通道的捕捉比较中断
TIM_ITConfig(TIM1, TIM_IT_CC1, ENABLE);
//通道输出使能
TIM_CtrlPWMOutputs(TIM1, ENABLE);
TIM_OC1PreloadConfig(TIM1, TIM_OCPreload_Enable);
TIM_ARRPreloadConfig(TIM1, ENABLE); //使能TIMx在ARR上的预装载寄存器
//TIM_ITConfig(TIM1, TIM_IT_Update, ENABLE); //使能tim1中断
TIM_Cmd(TIM1, ENABLE); //使能定时器
}
引脚不输出PWM信号 |