⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 stepper.c

📁 CNC.rar
💻 C
📖 第 1 页 / 共 3 页
字号:
        //
        pStepper->sTimerB.ulTimeOut =
            (VirtualTimeGet() + (((g_ulSysClock / 1000) *
                                  STEPPER_START_DELAY) / 1000));

        //
        // Add a virtual timer to change the winding drive.
        //
        VirtualTimerAdd(&(pStepper->sTimerB));
    }
}

//*****************************************************************************
//
//! Step the X axis motor by one step.
//!
//! \param lDir is the direction of the step to be taken.  This value should
//! only be 1 or -1 (other values will not have the desired effect).
//!
//! This function will step the X axis motor by a single step in the positive
//! or negative direction.
//!
//! \return None.
//
//*****************************************************************************
void
StepperXStep(long lDir)
{
    //
    // Step the X axis motor as requested.
    //
    StepperStep(&g_sXStepper, lDir);
}

//*****************************************************************************
//
//! Step the Y axis motor by one step.
//!
//! \param lDir is the direction of the step to be taken.  This value should
//! only be 1 or -1 (other values will not have the desired effect).
//!
//! This function will step the Y axis motor by a single step in the positive
//! or negative direction.
//!
//! \return None.
//
//*****************************************************************************
void
StepperYStep(long lDir)
{
    //
    // Step the Y axis motor as requested.
    //
    StepperStep(&g_sYStepper, lDir);
}

//*****************************************************************************
//
//! Step the Z axis motor by one step.
//!
//! \param lDir is the direction of the step to be taken.  This value should
//! only be 1 or -1 (other values will not have the desired effect).
//!
//! This function will step the Z axis motor by a single step in the positive
//! or negative direction.
//!
//! \return None.
//
//*****************************************************************************
void
StepperZStep(long lDir)
{
    //
    // Step the Z axis motor as requested.
    //
    StepperStep(&g_sZStepper, 0 - lDir);
}

//*****************************************************************************
//
//! Change the motor current to running or holding.
//!
//! \param ulCurrent is the desired motor current; must be one of
//! \b #CURRENT_RUNNING or \b #CURRENT_HOLDING.
//! \param ulPWM0 is the PWM signal for phase A of the motor.
//! \param ulPWM1 is the PWM signal for phase B of the motor.
//!
//! This function sets the duty cycle of the motor's PWM signals to achieve the
//! voltage (and therefore current) desired, either running current (the rated
//! current of the motor) or holding current (typically much smaller, possibly
//! even zero).
//!
//! \return None.
//
//*****************************************************************************
static void
StepperCurrent(unsigned long ulCurrent, unsigned long ulPWM0,
               unsigned long ulPWM1)
{
    //
    // See if the motor should be driven with running or holding current.
    //
    if(ulCurrent == CURRENT_RUNNING)
    {
        //
        // Set the pulse width of the PWM signals to provide running current to
        // the motor.
        //
        PWMPulseWidthSet(PWM_BASE, ulPWM0,
                         ((g_ulPWMClock * STEPPER_MAX_VOLTAGE) /
                          STEPPER_SUPPLY_VOLTAGE));
        PWMPulseWidthSet(PWM_BASE, ulPWM1,
                         ((g_ulPWMClock * STEPPER_MAX_VOLTAGE) /
                          STEPPER_SUPPLY_VOLTAGE));
    }
    else if(ulCurrent == CURRENT_HOLDING)
    {
        //
        // Set the pulse width of the PWM signals to provide holding current to
        // the motor.  The "if" statement (which will typically be optimized
        // out by the compiler due to the constants in the expression) is to
        // eliminate the multiply and divide when a holding current of zero is
        // requested (i.e. the typical case for a CNC machine).
        //
        if(STEPPER_HOLD_VOLTAGE != 0)
        {
            PWMPulseWidthSet(PWM_BASE, ulPWM0,
                             ((g_ulPWMClock * STEPPER_HOLD_VOLTAGE) /
                              STEPPER_SUPPLY_VOLTAGE));
            PWMPulseWidthSet(PWM_BASE, ulPWM1,
                             ((g_ulPWMClock * STEPPER_HOLD_VOLTAGE) /
                              STEPPER_SUPPLY_VOLTAGE));
        }
        else
        {
            PWMPulseWidthSet(PWM_BASE, ulPWM0, 0);
            PWMPulseWidthSet(PWM_BASE, ulPWM1, 0);
        }
    }
}

//*****************************************************************************
//
//! Change the X axis motor current to running or holding.
//!
//! \param ulCurrent is the desired motor current; must be one of
//! \b #CURRENT_RUNNING or \b #CURRENT_HOLDING.
//!
//! This function sets the duty cycle of the X axis motor to achieve the
//! voltage (and therefore current) desired, either running current (the rated
//! current of the motor) or holding current (typically much smaller, possibly
//! even zero).
//!
//! \return None.
//
//*****************************************************************************
void
StepperXCurrent(unsigned long ulCurrent)
{
    //
    // Set the current of the X axis motor.
    //
    StepperCurrent(ulCurrent, PWM_OUT_0, PWM_OUT_1);
}

//*****************************************************************************
//
//! Change the Y axis motor current to running or holding.
//!
//! \param ulCurrent is the desired motor current; must be one of
//! \b #CURRENT_RUNNING or \b #CURRENT_HOLDING.
//!
//! This function sets the duty cycle of the Y axis motor to achieve the
//! voltage (and therefore current) desired, either running current (the rated
//! current of the motor) or holding current (typically much smaller, possibly
//! even zero).
//!
//! \return None.
//
//*****************************************************************************
void
StepperYCurrent(unsigned long ulCurrent)
{
    //
    // Set the current of the Y axis motor.
    //
    StepperCurrent(ulCurrent, PWM_OUT_2, PWM_OUT_3);
}

//*****************************************************************************
//
//! Change the Z axis motor current to running or holding.
//!
//! \param ulCurrent is the desired motor current; must be one of
//! \b #CURRENT_RUNNING or \b #CURRENT_HOLDING.
//!
//! This function sets the duty cycle of the Z axis motor to achieve the
//! voltage (and therefore current) desired, either running current (the rated
//! current of the motor) or holding current (typically much smaller, possibly
//! even zero).
//!
//! \return None.
//
//*****************************************************************************
void
StepperZCurrent(unsigned long ulCurrent)
{
    //
    // Set the current of the Z axis motor.
    //
    StepperCurrent(ulCurrent, PWM_OUT_4, PWM_OUT_5);
}

//*****************************************************************************
//
//! Initialize the stepper motor interface.
//!
//! This function prepares the stepper motors for operation.  The peripherals
//! used to drive the motors, along with the internal state structure, is
//! initialized.
//!
//! \return None.
//
//*****************************************************************************
void
StepperInit(void)
{
    //
    // Set the PWM divider to two and get the PWM clock rate.
    //
    SysCtlPWMClockSet(SYSCTL_PWMDIV_2);
    g_ulPWMClock = g_ulSysClock / STEPPER_PWM_FREQUENCY / 2;

    //
    // Setup PWM generator zero, which runs the X axis motor.
    //
    PWMGenConfigure(PWM_BASE, PWM_GEN_0,
                    (PWM_GEN_MODE_UP_DOWN | PWM_GEN_MODE_NO_SYNC |
                     PWM_GEN_MODE_DBG_STOP));
    PWMGenPeriodSet(PWM_BASE, PWM_GEN_0, g_ulPWMClock);
    StepperXCurrent(CURRENT_HOLDING);
    PWMGenEnable(PWM_BASE, PWM_GEN_0);

    //
    // Setup PWM generator one, which runs the Y axis motor.
    //
    PWMGenConfigure(PWM_BASE, PWM_GEN_1,
                    (PWM_GEN_MODE_UP_DOWN | PWM_GEN_MODE_NO_SYNC |
                     PWM_GEN_MODE_DBG_STOP));
    PWMGenPeriodSet(PWM_BASE, PWM_GEN_1, g_ulPWMClock);
    StepperYCurrent(CURRENT_HOLDING);
    PWMGenEnable(PWM_BASE, PWM_GEN_1);

    //
    // Setup PWM generator two, which runs the Z axis motor.
    //
    PWMGenConfigure(PWM_BASE, PWM_GEN_2,
                    (PWM_GEN_MODE_UP_DOWN | PWM_GEN_MODE_NO_SYNC |
                     PWM_GEN_MODE_DBG_STOP));
    PWMGenPeriodSet(PWM_BASE, PWM_GEN_2, g_ulPWMClock);
    StepperZCurrent(CURRENT_HOLDING);
    PWMGenEnable(PWM_BASE, PWM_GEN_2);

    //
    // Synchronize the counters for the three PWM generators.
    //
    PWMSyncTimeBase(PWM_BASE, PWM_GEN_0_BIT | PWM_GEN_1_BIT | PWM_GEN_2_BIT);

    //
    // Enable the PWM outputs for all six PWM signals.
    //
    PWMOutputState(PWM_BASE, (PWM_OUT_0_BIT | PWM_OUT_1_BIT | PWM_OUT_2_BIT |
                              PWM_OUT_3_BIT | PWM_OUT_4_BIT | PWM_OUT_5_BIT),
                   true);

    //
    // Set the PWM outputs as hardware controlled.
    //
    GPIODirModeSet(GPIO_PORTB_BASE, GPIO_PIN_0 | GPIO_PIN_1, GPIO_DIR_MODE_HW);
    GPIODirModeSet(GPIO_PORTD_BASE, GPIO_PIN_0 | GPIO_PIN_1, GPIO_DIR_MODE_HW);
    GPIODirModeSet(GPIO_PORTE_BASE, GPIO_PIN_0 | GPIO_PIN_1, GPIO_DIR_MODE_HW);
    GPIOPadConfigSet(GPIO_PORTB_BASE, GPIO_PIN_0 | GPIO_PIN_1,
                     GPIO_STRENGTH_2MA, GPIO_PIN_TYPE_STD);
    GPIOPadConfigSet(GPIO_PORTD_BASE, GPIO_PIN_0 | GPIO_PIN_1,
                     GPIO_STRENGTH_2MA, GPIO_PIN_TYPE_STD);
    GPIOPadConfigSet(GPIO_PORTE_BASE, GPIO_PIN_0 | GPIO_PIN_1,
                     GPIO_STRENGTH_2MA, GPIO_PIN_TYPE_STD);

    //
    // Configure the stepper polarity GPIOs as outputs that are driven low by
    // default.
    //
    GPIODirModeSet(GPIO_PORTD_BASE,
                   STEPPER_X_POL | STEPPER_Y_POL | STEPPER_Z_POL,
                   GPIO_DIR_MODE_OUT);
    GPIOPadConfigSet(GPIO_PORTD_BASE,
                     STEPPER_X_POL | STEPPER_Y_POL | STEPPER_Z_POL,
                     GPIO_STRENGTH_2MA, GPIO_PIN_TYPE_STD);
    GPIOPinWrite(GPIO_PORTD_BASE,
                 STEPPER_X_POL | STEPPER_Y_POL | STEPPER_Z_POL, 0);
}

//*****************************************************************************
//
// Close the Doxygen group.
//! @}
//
//*****************************************************************************

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -