MP6602 library
Loading...
Searching...
No Matches
MP6602 Class Reference

#include <MP6602.h>

Public Member Functions

 MP6602 ()
 The default constructor.
void setChipSelectPin (uint8_t pin)
void resetSettings ()
bool verifySettings ()
void applySettings ()
void setCurrentMilliamps (uint16_t current)
void setHoldCurrentMilliamps (uint16_t current)
void setAutoHoldTimeMs (uint16_t time)
void enableDriver ()
void disableDriver ()
void setDirection (bool value)
bool getDirection ()
void step ()
void setStepMode (MP6602StepMode mode)
void setStepMode (uint16_t mode)
uint16_t readFault ()
uint8_t readOCP ()
void clearFaults ()
uint16_t getCachedReg (MP6602RegAddr address)
void setReg (MP6602RegAddr address, uint16_t value)

Public Attributes

MP6602SPI driver

Protected Member Functions

uint16_t * cachedRegPtr (MP6602RegAddr address)
void writeCachedReg (MP6602RegAddr address)
 Writes the cached value of the given register to the device.

Protected Attributes

uint16_t ctrl
uint16_t ctrl2
uint16_t iset
uint16_t stall
uint16_t bemf

Detailed Description

This class provides high-level functions for controlling an MP6602 stepper motor driver.

Definition at line 163 of file MP6602.h.

Constructor & Destructor Documentation

◆ MP6602()

MP6602::MP6602 ( )
inline

The default constructor.

Definition at line 167 of file MP6602.h.

Member Function Documentation

◆ applySettings()

void MP6602::applySettings ( )
inline

Re-writes the cached settings stored in this class to the device.

You should not normally need to call this function because settings are written to the device whenever they are changed. However, if verifySettings() returns false (due to a power interruption, for instance), then you could use applySettings() to get the device's settings back into the desired state.

Definition at line 229 of file MP6602.h.

◆ cachedRegPtr()

uint16_t * MP6602::cachedRegPtr ( MP6602RegAddr address)
inlineprotected

Returns a pointer to the variable containing the cached value for the given register.

Definition at line 499 of file MP6602.h.

◆ clearFaults()

void MP6602::clearFaults ( )
inline

Clears any fault conditions that are currently latched in the driver, including bits in the OCP register.

WARNING: Calling this function clears latched faults, which might allow the motor driver outputs to reactivate. If you do this repeatedly without fixing an abnormal condition (like a short circuit), you might damage the driver.

Definition at line 456 of file MP6602.h.

◆ disableDriver()

void MP6602::disableDriver ( )
inline

Disables the driver (EN = 0).

You can use this function to disable the driver and simply leave the ENBL (EN) pin disconnected. If ENBL is high, calling this function will enable the driver instead (the driver state is controlled by the logical XOR of the EN bit and the value of the ENBL pin).

Definition at line 318 of file MP6602.h.

◆ enableDriver()

void MP6602::enableDriver ( )
inline

Enables the driver (EN = 1).

You can use this function to enable the driver and simply leave the ENBL (EN) pin disconnected. If ENBL is high, calling this function will disable the driver instead (the driver state is controlled by the logical XOR of the EN bit and the value of the ENBL pin).

Definition at line 306 of file MP6602.h.

◆ getCachedReg()

uint16_t MP6602::getCachedReg ( MP6602RegAddr address)
inline

Gets the cached value of a register. If the given register address is not valid, this function returns 0.

Definition at line 469 of file MP6602.h.

◆ getDirection()

bool MP6602::getDirection ( )
inline

Returns the cached value of the motor direction (DIR).

This does not perform any SPI communication with the driver.

Definition at line 347 of file MP6602.h.

◆ readFault()

uint16_t MP6602::readFault ( )
inline

Reads the FAULT status register of the driver.

The return value is a 10-bit unsigned integer that has one bit for each FAULT condition. You can simply compare the return value to 0 to see if any of the bits are set, or you can use the logical AND operator (&) and the MP6602FaultBit enum to check individual bits. Fault bits are latched and remain set until cleared by writing a 1 to the appropriate bit (you can call clearFaults() to clear all faults).

Example usage:

if (sd.readFault() & ((uint16_t)1 << MP6602FaultBit::OVP))
{
// Over-voltage protection fault has been triggered.
}
@ OVP
Over-voltage protection.
Definition MP6602.h:142

Definition at line 431 of file MP6602.h.

◆ readOCP()

uint8_t MP6602::readOCP ( )
inline

Reads the OCP status register of the driver.

The return value is a 4-bit unsigned integer that has one bit for each OCP condition. You can simply compare the return value to 0 to see if any of the bits are set, or you can use the logical AND operator (&) and the MP6602OCPBit enum to check individual bits. OCP bits are latched and remain set until cleared by writing a 1 to the appropriate bit (you can call clearFaults() to clear all faults, including OCP faults).

Definition at line 444 of file MP6602.h.

◆ resetSettings()

void MP6602::resetSettings ( )
inline

Changes all of the driver's settings back to their default values.

It is good to call this near the beginning of your program to ensure that there are no settings left over from an earlier time that might affect the operation of the driver.

Definition at line 189 of file MP6602.h.

◆ setAutoHoldTimeMs()

void MP6602::setAutoHoldTimeMs ( uint16_t time)
inline

Sets the time that should pass after the last step, in milliseconds, before the driver's automatic hold current feature activates and it switches to the configured holding current.

This function takes an integer value and tries to pick the closest time period that is equal to or higher than the desired one. The available settings range from 15.6 ms to 1000 ms (1 s). See Table 5 in the MP6602 datasheet for the available settings. Calling this function with an argument of 0 disables the automatic hold current feature.

Definition at line 283 of file MP6602.h.

◆ setChipSelectPin()

void MP6602::setChipSelectPin ( uint8_t pin)
inline

Configures this object to use the specified pin as a chip select pin. You must use a chip select pin; the MP6602 requires it.

Definition at line 179 of file MP6602.h.

◆ setCurrentMilliamps()

void MP6602::setCurrentMilliamps ( uint16_t current)
inline

Sets the driver's current limit during stepping, in units of milliamps.

This function tries to pick the closest current limit that is equal to or lower than the desired one. The available settings range from 0.125 A to 4 A in increments of 125 mA.

Definition at line 247 of file MP6602.h.

◆ setDirection()

void MP6602::setDirection ( bool value)
inline

Sets the motor direction (DIR).

Allowed values are 0 or 1.

You can use this command to control the direction of the stepper motor and leave the DIR pin disconnected. (The direction is controlled by the logical XOR of the DIR bit and the value of the DIR pin.)

Definition at line 331 of file MP6602.h.

◆ setHoldCurrentMilliamps()

void MP6602::setHoldCurrentMilliamps ( uint16_t current)
inline

Sets the driver's current limit during automatic hold mode, in units of milliamps.

This setting only applies if automatic hold mode is enabled, which you can do with with setAutoHoldTimeMs(). This function tries to pick the closest current limit that is equal to or lower than the desired one. The available settings range from 0.125 A to 4 A in increments of 125 mA.

Definition at line 264 of file MP6602.h.

◆ setReg()

void MP6602::setReg ( MP6602RegAddr address,
uint16_t value )
inline

Writes the specified value to a register after updating the cached value to match.

Using this function keeps this object's cached settings consistent with the settings being written to the driver, so if you are using verifySettings(), applySettings(), and/or any of the other functions for specific settings that this library provides, you should use this function for direct register accesses instead of calling MP6602SPI::writeReg() directly.

Definition at line 485 of file MP6602.h.

◆ setStepMode() [1/2]

void MP6602::setStepMode ( MP6602StepMode mode)
inline

Sets the driver's step mode (MS).

This affects many things about the performance of the motor, including how much the output moves for each step taken and how much current flows through the coils in each stepping position. Available microstepping modes range from full step to 1/32-step.

If an invalid step mode is passed to this function, then it selects 1/8-step mode, which is the driver's default.

Example usage:

@ MicroStep32
1/32-step
Definition MP6602.h:127

Definition at line 374 of file MP6602.h.

◆ setStepMode() [2/2]

void MP6602::setStepMode ( uint16_t mode)
inline

Sets the driver's stepping mode (MS).

This version of the function allows you to express the requested microstepping ratio as a number directly.

Example usage:

sd.setStepMode(32);

Definition at line 395 of file MP6602.h.

◆ step()

void MP6602::step ( )
inline

Advances the indexer by one step (STEP = 1).

The driver automatically clears the STEP bit after it is written.

Definition at line 355 of file MP6602.h.

◆ verifySettings()

bool MP6602::verifySettings ( )
inline

Reads back the SPI configuration registers from the device and verifies that they are equal to the cached copies stored in this class.

This can be used to verify that the driver is powered on and has not lost them due to a power failure. Only registers that contain driver settings are verified (and bits that do not contain settings are masked off and not verified).

Returns
1 if the settings from the device match the cached copies, 0 if they do not.

Definition at line 213 of file MP6602.h.

◆ writeCachedReg()

void MP6602::writeCachedReg ( MP6602RegAddr address)
inlineprotected

Writes the cached value of the given register to the device.

Definition at line 513 of file MP6602.h.

Member Data Documentation

◆ bemf

uint16_t MP6602::bemf
protected

Definition at line 495 of file MP6602.h.

◆ ctrl

uint16_t MP6602::ctrl
protected

Definition at line 495 of file MP6602.h.

◆ ctrl2

uint16_t MP6602::ctrl2
protected

Definition at line 495 of file MP6602.h.

◆ driver

MP6602SPI MP6602::driver

This object handles all the communication with the MP6602. Generally, you should not need to use it in your code for basic usage of the driver, but you might want to use it to access more advanced settings that the MP6602 class does not provide functions for.

Definition at line 525 of file MP6602.h.

◆ iset

uint16_t MP6602::iset
protected

Definition at line 495 of file MP6602.h.

◆ stall

uint16_t MP6602::stall
protected

Definition at line 495 of file MP6602.h.


The documentation for this class was generated from the following file: