按照TIM15为例子配置PWM输出
void TIM15_Config(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB, ENABLE); //开启管脚时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM15, ENABLE); //打开定时器15的时钟
GPIO_PinAFConfig(GPIOB, GPIO_PinSource15, GPIO_AF_1); //打开复用功能
GPIO_InitStructure.GPIO_Pin = PB15_LASUO_PWM_OUTPUT; //PWM2输出引脚PB15
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; //复用功能模式
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; //推挽
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; //上拉
GPIO_Init(GPIOB,&GPIO_InitStructure);
TIM_DeInit(TIM15); //定时器15 整体复位
TIM_TimeBaseStructure.TIM_Period = 47999; //计数值
TIM_TimeBaseStructure.TIM_Prescaler = 0; //预分频值
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //向上计数模式
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; //这个值的作用暂时不清楚,配置那个参数都可以
TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;
TIM_TimeBaseInit(TIM15, &TIM_TimeBaseStructure);
TIM_PrescalerConfig(TIM15,0,TIM_PSCReloadMode_Immediate); //预分频系数(0+1),预分频值即时装入
TIM_ARRPreloadConfig(TIM15, ENABLE);//使能ARR预装载缓冲器
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; //PWMI1模式
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;//输出比较状态使能
TIM_OCInitStructure.TIM_OCPolarity =TIM_OCPolarity_High;//初始电平为高
TIM_OCInitStructure.TIM_Pulse = 0; //占空比的多少
TIM_OC2Init(TIM15, &TIM_OCInitStructure); //不同的定时器通道是不一样的
TIM_OC2PreloadConfig(TIM15, TIM_OCPreload_Enable);//使能外设预装载寄存器在CCR4
TIM_CtrlPWMOutputs(TIM15, ENABLE);//使能TIM3外设的主输出
TIM_Cmd(TIM15, ENABLE);
}
|