- Preface: precise stepper motor control principle, see my previous blog post wrote: https://blog.csdn.net/weixin_39589455/article/details/89409634
Procedure:
1. Here is pwm output, and general settings, use the TIM1 CH1 channel, without much comment here, another one can refer to my blog: https://blog.csdn.net/weixin_39589455/article / details / 87902849
the only difference is the inclusion of an interrupt count
void TIM1_PWM_Init(u16 arr,u16 psc)
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_TIM1,ENABLE);
//RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE);;
GPIO_DeInit(GPIOA);
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_8;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP;
GPIO_Init(GPIOA,&GPIO_InitStructure);
TIM_TimeBaseStructure.TIM_Period=arr;
TIM_TimeBaseStructure.TIM_Prescaler=psc;
TIM_TimeBaseStructure.TIM_ClockDivision=TIM_CKD_DIV1;
TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM1,&TIM_TimeBaseStructure);
TIM_ITConfig(TIM1,TIM_IT_Update,ENABLE );
TIM_OCInitStructure.TIM_OCMode=TIM_OCMode_PWM2;
//TIM_OCInitStructure.TIM_Channel=TIM_Channel_1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse=0;
TIM_OCInitStructure.TIM_OCPolarity=TIM_OCPolarity_Low;
TIM_OC1Init(TIM1, &TIM_OCInitStructure);
TIM_OC1PreloadConfig(TIM1, TIM_OCPreload_Enable);
TIM_CtrlPWMOutputs(TIM1,ENABLE);
TIM_ARRPreloadConfig(TIM1, ENABLE);
TIM_CtrlPWMOutputs(TIM1,ENABLE);
NVIC_InitStructure.NVIC_IRQChannel = TIM1_UP_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
TIM_Cmd(TIM1,ENABLE);
}
ISR: 100 here is the number of pulses, the motor rotation 45 °. Since: 360 / 0.45 = 800 motor pulses per revolution i.e. 0.45 °, so that the motor 100 is rotated 45 degrees = 0.45 *
void TIM1_UP_IRQHandler(void)
{
if(TIM_GetITStatus (TIM1 ,TIM_IT_Update )!=RESET )
{
TIM_ClearITPendingBit(TIM1 ,TIM_IT_Update );
Pulse_Cnt++;
if(Pulse_Cnt>100)
{
Pulse_Cnt = 0;
TIM_Cmd(TIM1,DISABLE);
}
}
}
3. The main function
int main(void)
{
pwm_init();
moto_shun();
}
Note: This control is implemented using interrupts, more chip resources, if there are other ways to welcome and I discussed it
Source: https://download.csdn.net/download/weixin_39589455/11129458