用定时器PWM输出
配置定时器
void TIME5_config(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStruct;//时基结构体
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM5, ENABLE);//使能时钟
TIM_TimeBaseInitStruct.TIM_Prescaler = 90-1; //分频系数
TIM_TimeBaseInitStruct.TIM_Period = 100 -1; //范围:1-65535配置ARR
TIM_TimeBaseInitStruct.TIM_CounterMode = TIM_CounterMode_Up; //上拉
TIM_TimeBaseInitStruct.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseInit(TIM5, &TIM_TimeBaseInitStruct);//初始化
}
配置通道
void TIME5_OC1_config(void)
{
TIM_OCInitTypeDef TIM_OCInitStruct;//通道结构体
TIM_OCInitStruct.TIM_OCMode = TIM_OCMode_PWM1;//CCR前部有效
TIM_OCInitStruct.TIM_OutputState = TIM_OutputState_Enable;//输出使能
TIM_OCInitStruct.TIM_OCNPolarity = TIM_OCPolarity_Low;//输出低有效
TIM_OCInitStruct.TIM_Pulse = 0;//初始CCR的数值
TIM_OC1Init(TIM5, &TIM_OCInitStruct);//初始化通道
TIM_Cmd(TIM5, ENABLE);//使能整个时钟
}