.cpp 文件
#include "pwm.h"
#include "led.h"
//TIM14 PWM部分初始化
//PWM输出初始化
//arr:自动重装值
//psc:时钟预分频数
void TIM14_PWM_Init(void)
{
//此部分需手动修改IO口设置
GPIO_InitTypeDef GPIO_InitStructure;
//定时器结构体初始化。
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
//1TIM14时钟使能
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1,ENABLE);
2使能PORTF时钟
TIM_DeInit(TIM1);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOE, ENABLE);
//3GPIOF9复用为定时器14
GPIO_PinAFConfig(GPIOE,GPIO_PinSource9,GPIO_AF_TIM1);
//GPIOF9
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
//复用功能
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
//速度100MHz
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
//推挽复用输出
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
//上拉
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
//4初始化PF9
GPIO_Init(GPIOE,&GPIO_InitStructure);
TIM_TimeBaseStructure.TIM_Prescaler=400; //定时器分频
TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up; //向上计数模式
TIM_TimeBaseStructure.TIM_Period=100; //自动重装载值
TIM_TimeBaseStructure.TIM_ClockDivision=TIM_CKD_DIV1;
// TIM_TimeBaseStructure.TIM_RepetitionCounter = 1;
//5初始化定时器14
TIM_TimeBaseInit(TIM1,&TIM_TimeBaseStructure);
//初始化TIM14 Channel1 PWM模式
//选择定时器模式:TIM脉冲宽度调制模式2
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Reset;
//6根据T指定的参数初始化外设TIM1 4OC1
// TIM_OCInitStructure.TIM_Pulse = 100;
TIM_OC1Init(TIM1, &TIM_OCInitStructure);
//7使能TIM14在CCR1上的预装载寄存器
// TIM_OC1PreloadConfig(TIM1, TIM_OCPreload_Enable);
// //8ARPE使能
// TIM_ARRPreloadConfig(TIM1,ENABLE);
//9使能TIM14
TIM_Cmd(TIM1, ENABLE);
TIM_CtrlPWMOutputs(TIM1, ENABLE);
}
|