#include "pwm.h"
void TIM2_PWM_Init(u16 arr,u16 psc)
{
GPIO_InitTypeDef GPIO_InitStruct;
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStruct;
TIM_OCInitTypeDef TIM_OCInitStruct;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_AFIO ,ENABLE);
GPIO_InitStruct.GPIO_Pin=GPIO_Pin_1; //PA1
GPIO_InitStruct.GPIO_Speed=GPIO_Speed_50MHz; //50MHZ的输出速度
GPIO_InitStruct.GPIO_Mode=GPIO_Mode_AF_PP; //复用推挽输出
GPIO_Init(GPIOA,&GPIO_InitStruct);
TIM_TimeBaseInitStruct.TIM_Prescaler=arr; //设置作为时钟频率除数的预分频值
TIM_TimeBaseInitStruct.TIM_CounterMode=TIM_CounterMode_Up; //设置为向上计数
TIM_TimeBaseInitStruct.TIM_Period=psc; //设置在下一个更新事件装入活动的自动重装载寄存器周期的值
TIM_TimeBaseInitStruct.TIM_ClockDivision=0; //设置时钟分割
TIM_TimeBaseInit(TIM2,&TIM_TimeBaseInitStruct);
TIM_OCInitStruct.TIM_OCMode=TIM_OCMode_PWM1; //设置为PWM模式1
TIM_OCInitStruct.TIM_OutputState=TIM_OutputState_Enable; //比较输出使能1
TIM_OCInitStruct.TIM_OCPolarity=TIM_OCPolarity_High; //TIM比较输出极性高
TIM_OCInitStruct.TIM_Pulse=0;
TIM_OC2Init(TIM2,&TIM_OCInitStruct);
TIM_OC2PreloadConfig(TIM2,TIM_OCPreload_Enable); //使能预装载值
TIM_Cmd(TIM2, ENABLE); //使能TIM2
TIM_SetCompare2(TIM2,0);
}
|