PWM工作过程 【PWM实验】
文章图片
文章图片
PWM输出配置步骤
- 使能定时器14和相关IO口时钟
使能定时器14时钟:
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE);
使能GPIOF时钟,并配置PF9引脚映射至AF9,复用为定时器14
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOF, ENABLE);
- GPIOF9复用映射到TIM14并且初始化IO口为复用功能输出。函数:GPIO_Init();
GPIO_PinAFConfig(GPIOF,GPIO_PinSource9,GPIO_AF_TIM14);
//GF9复用为 TIM14 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
//GPIOF9
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
//复用功能
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
//速度 50MHz
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
//推挽复用输出
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
//上拉
GPIO_Init(GPIOF,&GPIO_InitStructure);
//初始化 PF9
- 初始化定时器
TIM_TimeBaseStructure.TIM_Prescaler=psc;
//定时器分频
TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up;
//向上计数模式
TIM_TimeBaseStructure.TIM_Period=arr;
//自动重装载值
TIM_TimeBaseStructure.TIM_ClockDivision=TIM_CKD_DIV1;
TIM_TimeBaseInit(TIM14,&TIM_TimeBaseStructure);
//初始化TIM14
- 初始化输出比较参数
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
//PWM调制模式 1
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
//比较输出使能
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;
//输出极性低
TIM_OC1Init(TIM14, &TIM_OCInitStructure);
//初始化外设TIM1 4OC1
PWM模式1:在向上或者向下计数时,一旦TIMx_CNT
输出极性:高电平有效;低电平有效
- 使能预装载寄存器(CCRx):
TIM_OC1PreloadConfig(TIM14, TIM_OCPreload_Enable);
- 自动重装载的预装载寄存器ARPE使能(ARR):
TIM_ARRPreloadConfig(TIM14,ENABLE);
- 使能TIM14
TIM_Cmd(TIM14, ENABLE);