中级会员
积分 298
金钱 298
注册时间 2024-7-13
在线时间 31 小时
4 金钱
在没有复用前可以正常使用
void PWM_UserConfig_2(u16 Period,u16 Prescaler){
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_InitStructure;
TIM_OCInitTypeDef PWM_InitStructure;
RCC_APB2PeriphClockCmd (RCC_APB2Periph_GPIOA|RCC_APB2Periph_AFIO, ENABLE);
RCC_APB1PeriphClockCmd (RCC_APB1Periph_TIM2 , ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA,&GPIO_InitStructure);
TIM_InitStructure.TIM_Period = Period;
TIM_InitStructure.TIM_Prescaler = Prescaler;
TIM_InitStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_InitStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_InitStructure.TIM_RepetitionCounter = DISABLE;
TIM_TimeBaseInit(TIM2,&TIM_InitStructure);
TIM_Cmd(TIM2,ENABLE);
PWM_InitStructure.TIM_OCMode = TIM_OCMode_PWM2;
PWM_InitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
PWM_InitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OC1Init(TIM2,& WM_InitStructure);
TIM_OC1PreloadConfig(TIM2,ENABLE);
TIM_Cmd(TIM2,ENABLE);
}
void PWM_UserConfig_5(u16 Period,u16 Prescaler)
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_InitStructure;
TIM_OCInitTypeDef PWM_InitStructure;
RCC_APB2PeriphClockCmd (RCC_APB2Periph_GPIOB|RCC_APB2Periph_AFIO, ENABLE);
RCC_APB1PeriphClockCmd (RCC_APB1Periph_TIM2 , ENABLE);
GPIO_PinRemapConfig(GPIO_PartialRemap1_TIM2, ENABLE);
GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOB,&GPIO_InitStructure);
TIM_InitStructure.TIM_Period = Period;
TIM_InitStructure.TIM_Prescaler = Prescaler;
TIM_InitStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_InitStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_InitStructure.TIM_RepetitionCounter = DISABLE;
TIM_TimeBaseInit(TIM2,&TIM_InitStructure);
TIM_Cmd(TIM2,ENABLE);
PWM_InitStructure.TIM_OCMode = TIM_OCMode_PWM2;
PWM_InitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
PWM_InitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OC2Init(TIM2,& WM_InitStructure);
TIM_OC2PreloadConfig(TIM2,ENABLE);
TIM_Cmd(TIM2,ENABLE);
}
我来回答