首页 > 代码库 > 步进电机转台设计

步进电机转台设计

步进电机驱动设置:

 

STM32单片机程序:

    配置STM32的定时器模式,TIM2为主定时器,TIM3为从定时器,其中PulseFrequency为TIM输出PWM的频率。其主要功能,将TIM2的输出中断TR1指定到TIM3的计数器输入中,每当TIM2输出完成,TIM3就进行一次计数,当TIM3完成计数时,向系统提出一次中断。

void TIM2_Master__TIM3_Slave_Configuration(u32 PulseFrequency) 
{ 
    TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; 
    TIM_OCInitTypeDef TIM_OCInitStructure; 
    
    u16 nPDTemp ; 
    //配置TIM2为输出PWM的端口,PulseFrequency为设置的PWM频率
    TIM_Cmd(TIM2, DISABLE); 
    nPDTemp = 72000UL/PulseFrequency;                // 更改频率时,注意处理72000UL及TIM_Prescaler,使TIM_Period不要超过0xFFFF
    
    // 时基配置:配置PWM输出定时器——TIM2 
    /* Time base configuration */ 
    TIM_TimeBaseStructure.TIM_Period = nPDTemp-1; 
    TIM_TimeBaseStructure.TIM_Prescaler = 999; 
    TIM_TimeBaseStructure.TIM_ClockDivision = 0; 
    TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; 
    TIM_TimeBaseStructure.TIM_RepetitionCounter = 0; 
    TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure); 
    
    // 输出配置:配置PWM输出定时器——TIM2 
    /* PWM1 Mode configuration: Channel1 */    
    TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; 
    TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; 
    TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; 
    TIM_OCInitStructure.TIM_Pulse = nPDTemp>>1;//50% 
    TIM_OC1Init(TIM2, &TIM_OCInitStructure); 
    TIM_OC1PreloadConfig(TIM2, TIM_OCPreload_Enable); 
    TIM_ARRPreloadConfig(TIM2, ENABLE); 

    
    // 时基配置:配置脉冲计数寄存器——TIM3 
    TIM_TimeBaseStructure.TIM_Period = 0xFFFF;              
    TIM_TimeBaseStructure.TIM_Prescaler = 0; 
    TIM_TimeBaseStructure.TIM_ClockDivision = 0; 
    TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; 
    TIM_TimeBaseStructure.TIM_RepetitionCounter = 0; 
    TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure); 
    
    // Output Compare Active Mode configuration: Channel1 
    TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_Inactive; 
    TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; 
    TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; 
    TIM_OCInitStructure.TIM_Pulse = 0xFFFF; // 这里的配置值意义不大   
    TIM_OC1Init(TIM3, &TIM_OCInitStructure); 
    

    // 配置TIM2为主定时器 
    // Select the Master Slave Mode 
    TIM_SelectMasterSlaveMode(TIM2, TIM_MasterSlaveMode_Enable); 
    // Master Mode selection  
    TIM_SelectOutputTrigger(TIM2, TIM_TRGOSource_Update); 
    
    // 配置TIM3为从定时器 
    // Slave Mode selection: TIM3 
    TIM_SelectSlaveMode(TIM3, TIM_SlaveMode_Gated); 
    TIM_SelectInputTrigger(TIM3, TIM_TS_ITR1); 
    TIM_ITRxExternalClockConfig(TIM3, TIM_TS_ITR1);
//    TIM_ITConfig(TIM3, TIM_IT_CC1, ENABLE);               
    
    TIM_Cmd(TIM2, DISABLE); 
    TIM_Cmd(TIM3, DISABLE); 
} 

再添加TIM的比较数值,当计数超过该数值时产生一次中断:

void Output_Pulse(u16 Num)
{
        TIM3->CCR1 = Num; 
        TIM3->CNT = 0; 
        TIM_Cmd(TIM3, ENABLE); 
        TIM_ITConfig(TIM3, TIM_IT_CC1, ENABLE); 
        TIM_Cmd(TIM2, ENABLE); 

}

在TIM3的中断程序中,若发送中断后,则停止TIM2,和TIM3的定时功能,因此该程序完成了使用TIM2和TIM3完成指定个数的PWM波输出。

void TIM3_IRQHandler(void) 
{ 
    if (TIM_GetITStatus(TIM3, TIM_IT_CC1) != RESET)        // TIM_IT_CC1
    { 
        TIM_ClearITPendingBit(TIM3, TIM_IT_CC1); // 清除中断标志位 
        TIM_Cmd(TIM2, DISABLE); // 关闭定时器 
        TIM_Cmd(TIM3, DISABLE); // 关闭定时器 
        TIM_ITConfig(TIM3, TIM_IT_CC1, DISABLE); 
        TIM2_Pulse_TIM3_Counter_OK=1;
    } 
}

 

步进电机转台设计