/*********************************************
Function: TIM3_STEP_Init
Description: 步进电机驱动信号定时器初始化
*********************************************/
void TIM3_STEP_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);//使用默认值填充结构体成员
TIM_SelectMasterSlaveMode(TIM3,TIM_MasterSlaveMode_Enable); //使能主从模式,010:更新 – 更新事件被选为触发输入(TRGO)。
//例如,一个主定时器的时钟可以被用作一个从定时器的预分频器。
TIM_SelectOutputTrigger(TIM3,TIM_TRGOSource_Update); //选择输出触发,一个主定时器的时钟可以被用作一个从定时器的预分频器。
TIM_TimeBaseStructure.TIM_Period = 101; //
TIM_TimeBaseStructure.TIM_Prescaler = 19;
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; // 时钟分频因子 ,配置死区时间时需要用到
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; // 计数器计数模式,设置为向上计数
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure); // 初始化定时器
TIM_ClearITPendingBit(TIM3, TIM_IT_Update);
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2; // 配置为PWM模式2
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; // 输出使能
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low; // 输出通道电平极性配置
TIM_OCInitStructure.TIM_Pulse = TIM3->ARR >>1; //重载值的一半,即50%
TIM_OC3Init(TIM3, &TIM_OCInitStructure);
TIM_OC4Init(TIM3, &TIM_OCInitStructure);
TIM_OC3PreloadConfig(TIM3, TIM_OCPreload_Enable);
TIM_OC4PreloadConfig(TIM3, TIM_OCPreload_Enable);
TIM_ClearFlag(TIM3, TIM_FLAG_Update); // 清除计数器中断标志位
//TIM_ITConfig(TIM3,TIM_IT_Update,ENABLE); // 开启计数器中断
//TIM_Cmd(TIM3,ENABLE);//主函数有使能
}
/*********************************************
Function: TIM1_STEP_Init
Description: 电机PWM计数定时器初始化
*********************************************/
void TIM1_STEP_Init(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
NVIC_InitTypeDef NVIC_InitStructure;
//从定时器TIM1
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1,ENABLE);
NVIC_InitStructure.NVIC_IRQChannel = TIM1_UP_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
TIM_TimeBaseStructure.TIM_Period= 0; // 自动重装载寄存器的值
TIM_TimeBaseStructure.TIM_Prescaler= 0; // 时钟预分频数
TIM_TimeBaseStructure.TIM_ClockDivision=TIM_CKD_DIV1; // 时钟分频因子 ,
TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up; // 计数器计数模式,设置为向上计数
TIM_TimeBaseStructure.TIM_RepetitionCounter=0; // 重复计数器的值
TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure); // 初始化定时器
//连接定时器3的TRGO1输出至定时器1,设置高级TIM1_SMCR寄存器的TS=’010’内部触发0(ITR2),(TIM3主TIM1从选择ITR2,P237页)
//配置定时器1为使用ITR2作为内部触发的从模式。
TIM_SelectInputTrigger(TIM1,TIM_TS_ITR2);
//然后把从模式控制器置于外部时钟模式1(TIM1_SMCR寄存器的SMS=111);
//这样定时器1即可由定时器3周期性的上升沿(即定时器3的计数器溢出)信号驱动。
TIM_SelectSlaveMode(TIM1,TIM_SlaveMode_External1);
TIM_ClearFlag(TIM1, TIM_FLAG_Update); // 清除计数器中断标志位
TIM_ITConfig(TIM1,TIM_IT_Update,ENABLE); // 开启计数器中断
TIM_Cmd(TIM1, ENABLE); // 使能计数器
}
void TIM1_UP_IRQHandler(void)
{
if(TIM_GetITStatus(TIM1, TIM_IT_Update) == SET)
{
TI1Spama++;
TIM_ClearITPendingBit(TIM1, TIM_IT_Update); //清中断标志TIM_IT_Update
}
}
|