代码
void TIM14_PWM_Init(uint32_t arr, uint32_t psc)
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
//使能时钟
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOF, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM14, ENABLE);
GPIO_PinAFConfig(GPIOF, GPIO_PinSource9, GPIO_AF_TIM14); //复用
//GPIOF初始化
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_Init(GPIOF, &GPIO_InitStructure);
TIM_TimeBaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInitStructure.TIM_Period = arr;
TIM_TimeBaseInitStructure.TIM_Prescaler = psc;
//TIM_TimeBaseInitStructure.TIM_RepetitionCounter = ; only TIM1 TIM8
TIM_TimeBaseInit(TIM14, &TIM_TimeBaseInitStructure);
//only for TIM1 and TIM8 仅高级定时器用
//TIM_OCInitStructure.TIM_OCIdleState = ; //TIM互补输出比较状态
//TIM_OCInitStructure.TIM_OCNIdleState = ; //Idle状态下TIM输出比较引脚状态
//TIM_OCInitStructure.TIM_OCNPolarity = ; //指定互补输出极性
//TIM_OCInitStructure.TIM_OutputNState = ; //Idle状态下TIM输出比较引脚状态
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; //TIM输出比较模式
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low; //输出极性低
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //比较输出使能
//TIM_OCInitStructure.TIM_Pulse = ; //捕获/比较寄存器CCR的值,此程序在后面设置以控制LED的亮度
TIM_OC1Init(TIM14, &TIM_OCInitStructure);
TIM_OC1PreloadConfig(TIM14, TIM_OCPreload_Enable); //使能预装载寄存器
TIM_ARRPreloadConfig(TIM14,ENABLE); //ARPE使能
TIM_Cmd(TIM14, ENABLE); //使能TIM14
}
|