void Motor_TIM1_NVIC_Configuration(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure_E;
//定时器时钟使能
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);
//使能GPIO时钟
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOE|RCC_AHB1Periph_GPIOD, ENABLE);
//GPIO配置: TIM1 CH3 (PE13,PE14) SV_A,SV_B
GPIO_InitStructure_E.GPIO_Pin = GPIO_Pin_13|GPIO_Pin_14;
GPIO_InitStructure_E.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure_E.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_InitStructure_E.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure_E.GPIO_PuPd = GPIO_PuPd_UP ;
GPIO_Init(GPIOE, &GPIO_InitStructure_E);
//连接 TIM1 引脚到 AF2
GPIO_PinAFConfig(GPIOE, GPIO_PinSource13, GPIO_AF_TIM1);
GPIO_PinAFConfig(GPIOE, GPIO_PinSource14, GPIO_AF_TIM1);
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
//配置定时器中断
NVIC_InitStructure.NVIC_IRQChannel = TIM1_CC_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
vu16 CCR3_Val = 8192;
vu16 CCR4_Val = 4096;
void Motor_Pwm_Init(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef
TIM_OCInitStructure;
TIM_ICInitTypeDef TIM_ICInitStructure;
Motor_TIM1_NVIC_Configuration();
/* -----------------------------------------------------------------------
TIM4 配置产生4路不同占空比的信号:
TIM4CLK 频率设置成 SystemCoreClock / 2
(Hz),
为了使定时器4获得20Mhz的频率,计算如下
- Prescaler = (TIM4CLK / TIM4 counter clock) - 1
SystemCoreClock 是120Mhz
TIM4 频率 = TIM4 counter clock/(ARR + 1)
= 20 MHz / (19999+1) = 1 KHz
TIM4 Channel1 duty cycle = (TIM3_CCR1/ TIM3_ARR)* 100 = 50%
TIM4 Channel2 duty cycle = (TIM3_CCR2/ TIM3_ARR)* 100 = 37.5%
TIM4 Channel3 duty cycle = (TIM3_CCR3/ TIM3_ARR)* 100 = 25%
TIM4 Channel4 duty cycle = (TIM3_CCR4/ TIM3_ARR)* 100 = 12.5%
ARR和TIM3_ARR即TIM_Period值
----------------------------------------------------------------------- */
//计算预分频值
PrescalerValue = (unsigned short) ((SystemCoreClock)/30000000) - 1;
//定时器基本配置
TIM_TimeBaseStructure.TIM_Period = 65535;
TIM_TimeBaseStructure.TIM_Prescaler = PrescalerValue;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);
/* Prescaler configuration */
TIM_PrescalerConfig(TIM1, PrescalerValue, TIM_PSCReloadMode_Immediate);//对TIM1进行分频
//PWM模式配置: 通道3,4
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_Toggle;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_OutputNState = TIM_OutputState_Disable;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low; //输出极性
TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High; //互补端的极性
TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set;
TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCIdleState_Reset;
TIM_OCInitStructure.TIM_Pulse = CCR3_Val;
TIM_OC3Init(TIM1, &TIM_OCInitStructure);
TIM_OC3PreloadConfig(TIM1, TIM_OCPreload_Disable);
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = CCR4_Val;
TIM_OC4Init(TIM1, &TIM_OCInitStructure);
TIM_OC4PreloadConfig(TIM1, TIM_OCPreload_Disable);
//
TIM_ARRPreloadConfig(TIM1, ENABLE);
//使能定时器1
TIM_Cmd(TIM1, ENABLE);
TIM_ITConfig(TIM1, TIM_IT_CC3 | TIM_IT_CC4, ENABLE);
//高级定时器要添加,而TIM2,TIM3等定时器不用添加
TIM_CtrlPWMOutputs(TIM1, ENABLE);
while(1);
}
//注意定时器1中断函数名为TIM1_CC_IRQHandler
void TIM1_CC_IRQHandler(void)
{
if (TIM_GetITStatus(TIM1, TIM_IT_CC3) != RESET)
{
u16 capture = 0;
TIM_ClearITPendingBit(TIM1, TIM_IT_CC3);
capture = TIM_GetCapture3(TIM1);
TIM_SetCompare3(TIM1, capture + CCR3_Val);
}
if (TIM_GetITStatus(TIM1, TIM_IT_CC4) != RESET)
{
u16 capture = 0;
TIM_ClearITPendingBit(TIM1, TIM_IT_CC4);
capture = TIM_GetCapture4(TIM1);
TIM_SetCompare4(TIM1, capture + CCR4_Val);
}
}
其他定时器如TIM2等,要
(1)将TIM_CtrlPWMOutputs(TIM1, ENABLE);去掉
(2)将TIM1换成TIM2
(3)TIM1_CC_IRQn换成TIM2_IRQn
(4)定时器中断函数换成TIM2_IRQHandler
(5)换对应的引脚