Test application for the STMicroelectronics X-NUCLEO-IHM01A1 Stepper Motor Control Expansion Board.
Dependencies: X_NUCLEO_IHM01A1
Fork of MotorControl_IHM01A1 by
Motor Control with the X-NUCLEO-IHM01A1 Expansion Board
This application provides a more complex example of usage of the X-NUCLEO-IHM01A1 Stepper Motor Control Expansion Board.
It shows how to use one stepper motor connected to the board, with an example of ISR and error handler, as well as an almost complete coverage of the available APIs, e.g.:
- moving the rotor to a specific position;
- moving the rotor for a certain amount of time;
- setting the speed value;
- setting the direction of rotation;
- changing the stepper motor mode;
- soft/hard stopping the rotor;
- powering off the power bridge.
Diff: main.cpp
- Revision:
- 5:dfec8da854e5
- Parent:
- 4:43df256f26d9
- Child:
- 6:ff72e3344fb1
--- a/main.cpp Fri Nov 20 18:13:01 2015 +0000 +++ b/main.cpp Wed Nov 25 12:16:28 2015 +0000 @@ -7,8 +7,6 @@ * @brief mbed test application for the STMicrolectronics X-NUCLEO-IHM01A1 * Motor Control Expansion Board: control of 1 motor showing the usage * of all the related APIs. - * This application makes use of a C++ component architecture obtained - * from the C component architecture through the Stm32CubeTOO tool. ****************************************************************************** * @attention * @@ -58,6 +56,82 @@ L6474 *motor; +/* Functions -----------------------------------------------------------------*/ + +/** + * @brief This is an example of user handler for the flag interrupt. + * @param None + * @retval None + * @note If needed, implement it, and then attach and enable it: + * + motor->AttachFlagIRQ(&FlagIRQHandler); + * + motor->EnableFlagIRQ(); + * To disable it: + * + motor->DisbleFlagIRQ(); + */ +void FlagIRQHandler(void) +{ + /* Set ISR flag. */ + motor->isrFlag = TRUE; + + /* Get the value of the status register. */ + unsigned int status = motor->GetStatus(); + + /* Check HIZ flag: if set, power brigdes are disabled. */ + if ((status & L6474_STATUS_HIZ) == L6474_STATUS_HIZ) + { /* HIZ state. Action to be customized. */ } + + /* Check direction. */ + if ((status & L6474_STATUS_DIR) == L6474_STATUS_DIR) + { /* Forward direction is set. Action to be customized. */ } + else + { /* Backward direction is set. Action to be customized. */ } + + /* Check NOTPERF_CMD flag: if set, the command received by SPI can't be performed. */ + /* This often occures when a command is sent to the L6474 while it is in HIZ state. */ + if ((status & L6474_STATUS_NOTPERF_CMD) == L6474_STATUS_NOTPERF_CMD) + { /* Command received by SPI can't be performed. Action to be customized. */ } + + /* Check WRONG_CMD flag: if set, the command does not exist. */ + if ((status & L6474_STATUS_WRONG_CMD) == L6474_STATUS_WRONG_CMD) + { /* The command received by SPI does not exist. Action to be customized. */ } + + /* Check UVLO flag: if not set, there is an undervoltage lock-out. */ + if ((status & L6474_STATUS_UVLO) == 0) + { /* Undervoltage lock-out. Action to be customized. */ } + + /* Check TH_WRN flag: if not set, the thermal warning threshold is reached. */ + if ((status & L6474_STATUS_TH_WRN) == 0) + { /* Thermal warning threshold is reached. Action to be customized. */ } + + /* Check TH_SHD flag: if not set, the thermal shut down threshold is reached. */ + if ((status & L6474_STATUS_TH_SD) == 0) + { /* Thermal shut down threshold is reached. Action to be customized. */ } + + /* Check OCD flag: if not set, there is an overcurrent detection. */ + if ((status & L6474_STATUS_OCD) == 0) + { /* Overcurrent detection. Action to be customized. */ } + + /* Reset ISR flag. */ + motor->isrFlag = FALSE; +} + +/** + * @brief This is an example of user handler for the errors. + * @param error error-code. + * @retval None + * @note If needed, implement it, and then attach it: + * + motor->AttachErrorHandler(&ErrorHandler); + */ +void ErrorHandler(uint16_t error) +{ + /* Printing to the console. */ + printf("Error: %d.\r\n", error); + + /* Aborting the program. */ + exit(EXIT_FAILURE); +} + + /* Main ----------------------------------------------------------------------*/ int main() @@ -389,7 +463,7 @@ printf("--> Changing step mode to full step mode.\r\n"); /* Selecting full step mode. */ - motor->SelectStepMode(STEP_MODE_FULL); + motor->SetStepMode(STEP_MODE_FULL); /* Setting speed and acceleration to be consistent with full step mode. */ motor->SetMaxSpeed(100); @@ -419,7 +493,7 @@ printf("--> Restoring 1/16 microstepping mode.\r\n"); /* Resetting to 1/16 microstepping mode */ - motor->SelectStepMode(STEP_MODE_1_16); + motor->SetStepMode(STEP_MODE_1_16); /* Update speed, acceleration, deceleration for 1/16 microstepping mode*/ motor->SetMaxSpeed(1600);