最近在使用STM32F4系列单片机来控制三轴步进电机运动。想用定时器3和定时器4做主从模式控制定时器3的三个通道分别输出不同频率的PWM及精准脉冲数的控制信号。但是对主从控制模式还不太了解,想请教下各位有用过主从模式控制步进电机的大佬们,这样的方式能实现吗,谢谢!如果可以实现这个功能,针对各个通道的独立开启和关闭PWM输出是使用TIM_ITConfig(TIMx,TIM_IT_CCx,DISABLE);这个函数来执行吗?
另外,帮忙看下void Pulse_output(u32 Cycle, u32 PulseNum)这个函数要怎么调整才能实现各个通道独立设置频率和脉冲数的值,谢谢!
// 定时器3 主定时器
void TIM3_Mode_Config(u32 cycle)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
SET_GPIO_AF_OUTPUT(GPIOC, GPIO_Pin_6);
SET_GPIO_AF_OUTPUT(GPIOC, GPIO_Pin_7);
SET_GPIO_AF_OUTPUT(GPIOC, GPIO_Pin_8);
GPIO_PinAFConfig(GPIOC, GPIO_PinSource6, GPIO_AF_TIM3);
GPIO_PinAFConfig(GPIOC, GPIO_PinSource7, GPIO_AF_TIM3);
GPIO_PinAFConfig(GPIOC, GPIO_PinSource8, GPIO_AF_TIM3);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
TIM_TimeBaseStructure.TIM_Period = cycle - 1;
TIM_TimeBaseStructure.TIM_Prescaler = 83;
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; // 设置时钟分频系数:不分频
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; // 向上计数模式
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = (cycle + 1)/2;
TIM_OC1Init(TIM3, &TIM_OCInitStructure);
TIM_OC2Init(TIM3, &TIM_OCInitStructure);
TIM_OC3Init(TIM3, &TIM_OCInitStructure)
TIM_SelectMasterSlaveMode(TIM3, TIM_MasterSlaveMode_Enable); // 设置定时器3为主定时器模式
TIM_SelectOutputTrigger(TIM3, TIM_TRGOSource_Update); // 选择触发模式
TIM_OC1PreloadConfig(TIM3, TIM_OCPreload_Enable); // 通道1使能
TIM_OC2PreloadConfig(TIM3, TIM_OCPreload_Enable); // 通道2使能
TIM_OC3PreloadConfig(TIM3, TIM_OCPreload_Enable); // 通道3使能
TIM_ARRPreloadConfig(TIM3, ENABLE);
}
//TIM4 从定时器
void TIM4_config(u32 PulseNum)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);
TIM_TimeBaseStructure.TIM_Period = PulseNum-1;
TIM_TimeBaseStructure.TIM_Prescaler =0;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure);
TIM_SelectInputTrigger(TIM4, TIM_TS_ITR2);
TIM4->SMCR|=0x27; //设置从模式寄存器 ITR2(TS=010)
TIM_ITConfig(TIM4,TIM_IT_Update,DISABLE);
NVIC_InitStructure.NVIC_IRQChannel = TIM4_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
void Pulse_output(u32 Cycle, u32 PulseNum)
{
TIM4_config(PulseNum);
TIM_Cmd(TIM4, ENABLE);
TIM_ClearITPendingBit(TIM4,TIM_IT_Update);
TIM_ITConfig(TIM4,TIM_IT_Update,ENABLE);
TIM3_Mode_Config(Cycle);
TIM_Cmd(TIM3, ENABLE);
}
|