TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
uint8_t pin_source;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM14,ENABLE); //TIM14时钟使能
switch(pinx)
{
case(GPIO_Pin_0):
{
pin_source = GPIO_PinSource0;
break;
}
case(GPIO_Pin_1):
{
pin_source = GPIO_PinSource1;
break;
}
case(GPIO_Pin_2):
{
pin_source = GPIO_PinSource2;
break;
}
case(GPIO_Pin_3):
{
pin_source = GPIO_PinSource3;
break;
}
case(GPIO_Pin_4):
{
pin_source = GPIO_PinSource4;
break;
}
case(GPIO_Pin_5):
{
pin_source = GPIO_PinSource5;
break;
}
case(GPIO_Pin_6):
{
pin_source = GPIO_PinSource6;
break;
}
case(GPIO_Pin_7):
{
pin_source = GPIO_PinSource7;
break;
}
case(GPIO_Pin_8):
{
pin_source = GPIO_PinSource8;
break;
}
case(GPIO_Pin_9):
{
pin_source = GPIO_PinSource9;
break;
}
case(GPIO_Pin_10):
{
pin_source = GPIO_PinSource10;
break;
}
case(GPIO_Pin_11):
{
pin_source = GPIO_PinSource11;
break;
}
case(GPIO_Pin_12):
{
pin_source = GPIO_PinSource12;
break;
}
case(GPIO_Pin_13):
{
pin_source = GPIO_PinSource13;
break;
}
case(GPIO_Pin_14):
{
pin_source = GPIO_PinSource14;
break;
}
case(GPIO_Pin_15):
{
pin_source = GPIO_PinSource15;
break;
}
default:
{
Serial_Send(ERR_SERIAL,"PWM_Init_ERR, please connect xjc\r\n");
}
}
GPIO_PinAFConfig(GPIOx,pin_source,GPIO_AF_TIM14); //GPIO复用为定时器9
GPIO_Format_Init(GPIOx, pinx, GPIO_Mode_AF, GPIO_OType_PP, GPIO_PuPd_UP, GPIO_Speed_100MHz);
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); //初始化定时器e
//初始化TIM4 Channel1 PWM模式
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; //选择定时器模式:TIM脉冲宽度调制模式1
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //比较输出使能
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High ; //输出极性:TIM输出比较极性低
TIM_OC1Init(TIM14, &TIM_OCInitStructure); //根据T指定的参数初始化外设TIM4OC1
TIM_OC1PreloadConfig(TIM14, TIM_OCPreload_Enable); //使能TIM14在CCR1上的预装载寄存器
TIM_ARRPreloadConfig(TIM14,ENABLE);//ARPE使能
TIM_Cmd(TIM14, ENABLE); //使能TIM4
几个问题 :
- AF_Config的pin_source和GPIO_Init的不一样 是从1~15的uint8_t类型 不是uint16_t的可位带的uint16_t 非常吃屎
2.我是弱智