I think so, you need to add the code that controls the stepper motor running
STM32f103ZET6 Using PWM output and timer interrupt to control the stepper motor rotation fixed number of laps problem
Specific configuration:
TB6600 stepper motor driver, dip is subdivided into 200 pulse turn a circle;
Stepper motor is 42 stepper motor;
SCM PWM output pin PA0, PC13 to control the positive and negative rotation
timer uses TIM5, channel TIM5_CH1;
This is the timer and PWM configuration:
void TIM5_Init(u16 arr,u16 psc)
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
//1、使能时钟,此处使用的是TIM5,相关引脚为PA0:TIM5_CH1
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM5,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
//2、设置GPIO口为复用功能
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);
//3、初始化定时器TIM5
TIM_TimeBaseStructure.TIM_Period = arr;
TIM_TimeBaseStructure.TIM_Prescaler = psc;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM5,&TIM_TimeBaseStructure);
//4、初始化PWM
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OC1Init(TIM5, &TIM_OCInitStructure);
//5、使能PWM的预装载和重装载功能
TIM_OC1PreloadConfig(TIM5, TIM_OCPreload_Enable);
TIM_ARRPreloadConfig(TIM5,ENABLE);
//6、启动定时器
TIM_ITConfig(TIM5, TIM_IT_Update, ENABLE);
TIM_Cmd(TIM5,ENABLE);
}
This is the timer interrupt handler
void TIM5_IRQHandler(void)
{
static int count = 1;
if (TIM_GetITStatus(TIM5, TIM_IT_Update) != RESET)
{
TIM_ClearITPendingBit(TIM5, TIM_IT_Update);
u1_printf("%d ",count);
count++;
if(count == 200){
TIM_Cmd(TIM5,DISABLE);
}
}
}
This is the main function
int main()
{
//使用的PC13来控制步进电机正反转
PCout(13) = 1;
//计数值为99+1,周期为719+1
TIM5_Init(99,719);
//设置占空比50%
TIM_SetCompare1(TIM5,50);
NVIC_Config();
}
Supposedly, 200 pulses rotate once, but in fact, the motor rotates many times. I really can't find the problem, please give me some advice, thank you!
0 Answer
这家伙很懒,什么都没留下...