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

📄 motors.c

📁 eaayarm101自制小车源代码 周立功公司原创
💻 C
📖 第 1 页 / 共 2 页
字号:
    HWREG(PWM_BASE + PWM_GEN_1_OFFSET + PWM_O_X_CTL) = PWM_X_CTL_ENABLE;

    //
    // If the PWM clocking errata exists, then work around it.
    //
#ifdef PWM_ERRATA_WORKAROUND
    HWREG(SYSCTL_RCC) = ((HWREG(SYSCTL_RCC) & ~(SYSCTL_RCC_PWMDIV_MASK)) |
                         SYSCTL_RCC_USE_PWMDIV | SYSCTL_RCC_PWMDIV_2);
#endif
}

//*****************************************************************************
//
//! Starts the left motors.
//!
//! This function makes the left wheels start turning at the most recently
//! configured speed and direction.
//!
//! \return None.
//
//*****************************************************************************
void
MotorLeftRun(void)
{
    //
    // Change the GPIO pins to hardware function, which will cause the PWM to
    // be output to the H bridge.
    //
    HWREG(LEFT_PORT + GPIO_O_AFSEL) |= LEFT_FORWARD | LEFT_BACKWARD;
}

//*****************************************************************************
//
//! Stops the left motors.
//!
//! This fucntion makes the left wheels stop turning.  The drive to the motor
//! is disabled and dynamic braking is applied.  It will take some time for the
//! wheels to actually stop spinning (due to intertia); this time is
//! proportional to the speed of the motors when the stop was requested.
//!
//! \return None.
//
//*****************************************************************************
void
MotorLeftStop(void)
{
    //
    // Change the GPIO pins to GPIO function, which will cause a static signal
    // to be output to the H bridge.
    //
    HWREG(LEFT_PORT + GPIO_O_AFSEL) &= ~(LEFT_FORWARD | LEFT_BACKWARD);
}

//*****************************************************************************
//
//! Sets the speed of the left motors.
//!
//! \param ulPercent is the percentage of the full drive that should be applied
//! to the motors; valid values are 0 through 100, inclusive.
//!
//! This function sets the rotational speed of the left motors.  The speed is
//! actually specified as a percentage of the full motor drive, not as a
//! percentage of the rotational speed of the motor.  Therefore, the difference
//! in ground speed between 30% and 40% (for example) won't necessarily be the
//! same as the difference in ground speed between 60% and 70%.
//!
//! \return None.
//
//*****************************************************************************
void
MotorLeftSpeed(unsigned long ulPercent)
{
    //
    // Set the match value to equal the requested spped.
    //
    HWREG(PWM_BASE + PWM_GEN_0_OFFSET + PWM_O_X_CMPA) =
        (((PWM_CLOCK - 3) * (100 - ulPercent)) / 100) + 1;
}

//*****************************************************************************
//
//! Sets the rotational direction of the left motors.
//!
//! \param bForward is a boolean that is \e true if the left motors should
//! rotate in the forward direction and \e false if it should rotate in the
//! backward direction.
//!
//! This function set the direction in which the left motors will turn.  The
//! change in rotational direction will occur immediately; therefore, the speed
//! of the motors should be at zero before a direction change is requested
//! (though nothing is done to prevent this from occurring).
//!
//! \return None.
//
//*****************************************************************************
void
MotorLeftDir(int bForward)
{
    //
    // Set the direction signal as appropriate for the H bridge.
    //
    HWREGBITW(PWM_BASE + PWM_O_ENABLE, 0) = bForward ? 1 : 0;
    HWREGBITW(PWM_BASE + PWM_O_ENABLE, 1) = bForward ? 0 : 1;
}

//*****************************************************************************
//
//! Starts the right motors.
//!
//! This function makes the right wheels start turning at the most recently
//! configured speed and direction.
//!
//! \return None.
//
//*****************************************************************************
void
MotorRightRun(void)
{
    //
    // Change the GPIO pins to hardware function, which will cause the PWM to
    // be output to the H bridge.
    //
    HWREG(RIGHT_PORT + GPIO_O_AFSEL) |= RIGHT_FORWARD | RIGHT_BACKWARD;
}

//*****************************************************************************
//
//! Stops the right motors.
//!
//! This function makes the right wheels stop turning.  The drive to the motor
//! is disabled and dynamic braking is applied.  It will take some time for the
//! wheels to actually stop spinning (due to inertia); this time is
//! proportional to the speed of the motors when the stop was requested.
//!
//! \return None.
//
//*****************************************************************************
void
MotorRightStop(void)
{
    //
    // Change the GPIO pins to GPIO function, which will cause a static signal
    // to be output to the H bridge.
    //
    HWREG(RIGHT_PORT + GPIO_O_AFSEL) &= ~(RIGHT_FORWARD | RIGHT_BACKWARD);
}

//*****************************************************************************
//
//! Sets the speed of the right motors.
//!
//! \param ulPercent is the percentage of the full drive that should be applied
//! to the motors; valid values are 0 through 100, inclusive.
//!
//! This function sets the rotational speed of the right motors.  The speed is
//! actually specified as a percentage of the full motor drive; not as a
//! percentage of the rotational speed of the motor.  Therefore, the difference
//! in ground speed between 30% and 40% (for example) won't necessarily be the
//! same as the difference in ground speed between 60% and 70%.
//!
//! \return None.
//
//*****************************************************************************
void
MotorRightSpeed(unsigned long ulPercent)
{
    //
    // Set the match value to equal the requested spped.
    //
    HWREG(PWM_BASE + PWM_GEN_1_OFFSET + PWM_O_X_CMPA) =
        (((PWM_CLOCK - 3) * (100 - ulPercent)) / 100) + 1;
}

//*****************************************************************************
//
//! Sets the rotational direction of the right motors.
//!
//! \param bForward is a boolean that is \e true if the right motors should
//! rotate in the forward direction and \e false if it should rotate in the
//! backward direction.
//!
//! This function set the direction in which the right motors will turn.  The
//! change in rotational direction will occur immediately; therefore, the speed
//! of the motors should be at zero before a direction change is requested
//! (though nothing is done to prevent this from occurring).
//!
//! \return None.
//
//*****************************************************************************
void
MotorRightDir(int bForward)
{
    //
    // Set the direction signal as appropriate for the H bridge.
    //
    HWREGBITW(PWM_BASE + PWM_O_ENABLE, 2) = bForward ? 1 : 0;
    HWREGBITW(PWM_BASE + PWM_O_ENABLE, 3) = bForward ? 0 : 1;
}

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

⌨️ 快捷键说明

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