Test application for the STMicroelectronics X-NUCLEO-IHM01A1 Stepper Motor Control Expansion Board.

Dependencies:   X_NUCLEO_IHM01A1

Fork of MotorControl_IHM01A1 by ST

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.
Committer:
Davidroid
Date:
Wed Oct 14 15:29:42 2015 +0000
Revision:
0:fedf3cf2324a
Child:
1:5171df1bf684
mbed test application for the STMicrolectronics X-NUCLEO-IHM01A1 Motor Control Expansion Board: control of 1 motor.; ; This application mirrors the corresponding Stm32Cube's "IHM01A1_ExampleFor1Motor" example.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Davidroid 0:fedf3cf2324a 1 /**
Davidroid 0:fedf3cf2324a 2 ******************************************************************************
Davidroid 0:fedf3cf2324a 3 * @file main.cpp
Davidroid 0:fedf3cf2324a 4 * @author Davide Aliprandi / AST
Davidroid 0:fedf3cf2324a 5 * @version V1.0.0
Davidroid 0:fedf3cf2324a 6 * @date October 14th, 2015
Davidroid 0:fedf3cf2324a 7 * @brief mbed test application for the STMicrolectronics X-NUCLEO-IHM01A1
Davidroid 0:fedf3cf2324a 8 * Motor Control Expansion Board: control of 1 motor.
Davidroid 0:fedf3cf2324a 9 * This application makes use of a C++ component architecture obtained
Davidroid 0:fedf3cf2324a 10 * from the C component architecture through the Stm32CubeTOO tool.
Davidroid 0:fedf3cf2324a 11 ******************************************************************************
Davidroid 0:fedf3cf2324a 12 * @attention
Davidroid 0:fedf3cf2324a 13 *
Davidroid 0:fedf3cf2324a 14 * <h2><center>&copy; COPYRIGHT(c) 2015 STMicroelectronics</center></h2>
Davidroid 0:fedf3cf2324a 15 *
Davidroid 0:fedf3cf2324a 16 * Redistribution and use in source and binary forms, with or without modification,
Davidroid 0:fedf3cf2324a 17 * are permitted provided that the following conditions are met:
Davidroid 0:fedf3cf2324a 18 * 1. Redistributions of source code must retain the above copyright notice,
Davidroid 0:fedf3cf2324a 19 * this list of conditions and the following disclaimer.
Davidroid 0:fedf3cf2324a 20 * 2. Redistributions in binary form must reproduce the above copyright notice,
Davidroid 0:fedf3cf2324a 21 * this list of conditions and the following disclaimer in the documentation
Davidroid 0:fedf3cf2324a 22 * and/or other materials provided with the distribution.
Davidroid 0:fedf3cf2324a 23 * 3. Neither the name of STMicroelectronics nor the names of its contributors
Davidroid 0:fedf3cf2324a 24 * may be used to endorse or promote products derived from this software
Davidroid 0:fedf3cf2324a 25 * without specific prior written permission.
Davidroid 0:fedf3cf2324a 26 *
Davidroid 0:fedf3cf2324a 27 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
Davidroid 0:fedf3cf2324a 28 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
Davidroid 0:fedf3cf2324a 29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
Davidroid 0:fedf3cf2324a 30 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
Davidroid 0:fedf3cf2324a 31 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
Davidroid 0:fedf3cf2324a 32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
Davidroid 0:fedf3cf2324a 33 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
Davidroid 0:fedf3cf2324a 34 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
Davidroid 0:fedf3cf2324a 35 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
Davidroid 0:fedf3cf2324a 36 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Davidroid 0:fedf3cf2324a 37 *
Davidroid 0:fedf3cf2324a 38 ******************************************************************************
Davidroid 0:fedf3cf2324a 39 */
Davidroid 0:fedf3cf2324a 40
Davidroid 0:fedf3cf2324a 41
Davidroid 0:fedf3cf2324a 42 /* Includes ------------------------------------------------------------------*/
Davidroid 0:fedf3cf2324a 43
Davidroid 0:fedf3cf2324a 44 /* mbed specific header files. */
Davidroid 0:fedf3cf2324a 45 #include "mbed.h"
Davidroid 0:fedf3cf2324a 46
Davidroid 0:fedf3cf2324a 47 /* Helper header files. */
Davidroid 0:fedf3cf2324a 48 #include "DevSPI.h"
Davidroid 0:fedf3cf2324a 49
Davidroid 0:fedf3cf2324a 50 /* Component specific header files. */
Davidroid 0:fedf3cf2324a 51 #include "l6474_class.h"
Davidroid 0:fedf3cf2324a 52
Davidroid 0:fedf3cf2324a 53
Davidroid 0:fedf3cf2324a 54 /* Variables -----------------------------------------------------------------*/
Davidroid 0:fedf3cf2324a 55
Davidroid 0:fedf3cf2324a 56 /* Motor Control Component. */
Davidroid 0:fedf3cf2324a 57 L6474 *l6474;
Davidroid 0:fedf3cf2324a 58
Davidroid 0:fedf3cf2324a 59 /* Flag to identify whenever a PWM pulse has finished. */
Davidroid 0:fedf3cf2324a 60 volatile int pwm_pulse_finished_flag;
Davidroid 0:fedf3cf2324a 61
Davidroid 0:fedf3cf2324a 62 /* Flag to identify whenever the desired delay has expired. */
Davidroid 0:fedf3cf2324a 63 volatile int delay_expired_flag;
Davidroid 0:fedf3cf2324a 64
Davidroid 0:fedf3cf2324a 65
Davidroid 0:fedf3cf2324a 66 /* Functions -----------------------------------------------------------------*/
Davidroid 0:fedf3cf2324a 67
Davidroid 0:fedf3cf2324a 68 /*
Davidroid 0:fedf3cf2324a 69 * @brief PWM callback.
Davidroid 0:fedf3cf2324a 70 * @param None
Davidroid 0:fedf3cf2324a 71 * @retval None
Davidroid 0:fedf3cf2324a 72 */
Davidroid 0:fedf3cf2324a 73 void PWMCallback(void)
Davidroid 0:fedf3cf2324a 74 {
Davidroid 0:fedf3cf2324a 75 pwm_pulse_finished_flag = 1;
Davidroid 0:fedf3cf2324a 76 }
Davidroid 0:fedf3cf2324a 77
Davidroid 0:fedf3cf2324a 78 /*
Davidroid 0:fedf3cf2324a 79 * @brief Delay callback.
Davidroid 0:fedf3cf2324a 80 * @param None
Davidroid 0:fedf3cf2324a 81 * @retval None
Davidroid 0:fedf3cf2324a 82 */
Davidroid 0:fedf3cf2324a 83 void DelayCallback()
Davidroid 0:fedf3cf2324a 84 {
Davidroid 0:fedf3cf2324a 85 delay_expired_flag = 1;
Davidroid 0:fedf3cf2324a 86 }
Davidroid 0:fedf3cf2324a 87
Davidroid 0:fedf3cf2324a 88 /*
Davidroid 0:fedf3cf2324a 89 * @brief Waiting until PWM pulse has finished.
Davidroid 0:fedf3cf2324a 90 * @param None
Davidroid 0:fedf3cf2324a 91 * @retval None
Davidroid 0:fedf3cf2324a 92 */
Davidroid 0:fedf3cf2324a 93 void WaitForPWMPulse(void)
Davidroid 0:fedf3cf2324a 94 {
Davidroid 0:fedf3cf2324a 95 /* Waiting until PWM flag is set. */
Davidroid 0:fedf3cf2324a 96 while (pwm_pulse_finished_flag == 0);
Davidroid 0:fedf3cf2324a 97
Davidroid 0:fedf3cf2324a 98 /* Resetting PWM flag. */
Davidroid 0:fedf3cf2324a 99 pwm_pulse_finished_flag = 0;
Davidroid 0:fedf3cf2324a 100
Davidroid 0:fedf3cf2324a 101 /* Setting the device state machine. */
Davidroid 0:fedf3cf2324a 102 if (l6474->GetDeviceState() != INACTIVE)
Davidroid 0:fedf3cf2324a 103 l6474->StepClockHandler();
Davidroid 0:fedf3cf2324a 104 }
Davidroid 0:fedf3cf2324a 105
Davidroid 0:fedf3cf2324a 106 /*
Davidroid 0:fedf3cf2324a 107 * @brief Waiting while the motor is active.
Davidroid 0:fedf3cf2324a 108 * @param None
Davidroid 0:fedf3cf2324a 109 * @retval None
Davidroid 0:fedf3cf2324a 110 */
Davidroid 0:fedf3cf2324a 111 void WaitWhileActive(void)
Davidroid 0:fedf3cf2324a 112 {
Davidroid 0:fedf3cf2324a 113 while (l6474->GetDeviceState() != INACTIVE)
Davidroid 0:fedf3cf2324a 114 WaitForPWMPulse();
Davidroid 0:fedf3cf2324a 115 }
Davidroid 0:fedf3cf2324a 116
Davidroid 0:fedf3cf2324a 117 /*
Davidroid 0:fedf3cf2324a 118 * @brief Waiting until delay has expired.
Davidroid 0:fedf3cf2324a 119 * @param delay delay in milliseconds.
Davidroid 0:fedf3cf2324a 120 * @retval None
Davidroid 0:fedf3cf2324a 121 */
Davidroid 0:fedf3cf2324a 122 void WaitForDelay(int delay)
Davidroid 0:fedf3cf2324a 123 {
Davidroid 0:fedf3cf2324a 124 Timeout timeout;
Davidroid 0:fedf3cf2324a 125 timeout.attach(&DelayCallback, delay / 1E3);
Davidroid 0:fedf3cf2324a 126
Davidroid 0:fedf3cf2324a 127 delay_expired_flag = 0;
Davidroid 0:fedf3cf2324a 128 while (delay_expired_flag == 0)
Davidroid 0:fedf3cf2324a 129 WaitForPWMPulse();
Davidroid 0:fedf3cf2324a 130 }
Davidroid 0:fedf3cf2324a 131
Davidroid 0:fedf3cf2324a 132
Davidroid 0:fedf3cf2324a 133 /* Main ----------------------------------------------------------------------*/
Davidroid 0:fedf3cf2324a 134
Davidroid 0:fedf3cf2324a 135 int main()
Davidroid 0:fedf3cf2324a 136 {
Davidroid 0:fedf3cf2324a 137 /* Initializing SPI bus. */
Davidroid 0:fedf3cf2324a 138 DevSPI dev_spi(D11, D12, D13);
Davidroid 0:fedf3cf2324a 139
Davidroid 0:fedf3cf2324a 140 /* Resetting Timer PWM flag. */
Davidroid 0:fedf3cf2324a 141 pwm_pulse_finished_flag = 0;
Davidroid 0:fedf3cf2324a 142
Davidroid 0:fedf3cf2324a 143 /* Initializing Motor Control Component. */
Davidroid 0:fedf3cf2324a 144 l6474 = new L6474(D8, D7, D9, D10, dev_spi);
Davidroid 0:fedf3cf2324a 145 if (l6474->Init(NULL) != COMPONENT_OK)
Davidroid 0:fedf3cf2324a 146 return false;
Davidroid 0:fedf3cf2324a 147
Davidroid 0:fedf3cf2324a 148 /* Printing to the console. */
Davidroid 0:fedf3cf2324a 149 printf("Motor Control Application Example for 1 Motor\r\n\n");
Davidroid 0:fedf3cf2324a 150
Davidroid 0:fedf3cf2324a 151 /* Main Loop. */
Davidroid 0:fedf3cf2324a 152 while(true)
Davidroid 0:fedf3cf2324a 153 {
Davidroid 0:fedf3cf2324a 154 /*----- Moving forward 16000 steps. -----*/
Davidroid 0:fedf3cf2324a 155
Davidroid 0:fedf3cf2324a 156 /* Printing to the console. */
Davidroid 0:fedf3cf2324a 157 printf("--> Moving forward 16000 steps.\r\n");
Davidroid 0:fedf3cf2324a 158
Davidroid 0:fedf3cf2324a 159 /* Moving 16000 steps in the forward direction. */
Davidroid 0:fedf3cf2324a 160 l6474->Move(FORWARD, 16000);
Davidroid 0:fedf3cf2324a 161
Davidroid 0:fedf3cf2324a 162 /* Waiting while the motor is active. */
Davidroid 0:fedf3cf2324a 163 WaitWhileActive();
Davidroid 0:fedf3cf2324a 164
Davidroid 0:fedf3cf2324a 165 /* Waiting 2 seconds. */
Davidroid 0:fedf3cf2324a 166 wait_ms(2000);
Davidroid 0:fedf3cf2324a 167
Davidroid 0:fedf3cf2324a 168
Davidroid 0:fedf3cf2324a 169 /*----- Moving backward 16000 steps. -----*/
Davidroid 0:fedf3cf2324a 170
Davidroid 0:fedf3cf2324a 171 /* Printing to the console. */
Davidroid 0:fedf3cf2324a 172 printf("--> Moving backward 16000 steps.\r\n");
Davidroid 0:fedf3cf2324a 173
Davidroid 0:fedf3cf2324a 174 /* Moving 16000 steps in the backward direction. */
Davidroid 0:fedf3cf2324a 175 l6474->Move(BACKWARD, 16000);
Davidroid 0:fedf3cf2324a 176
Davidroid 0:fedf3cf2324a 177 /* Waiting while the motor is active. */
Davidroid 0:fedf3cf2324a 178 WaitWhileActive();
Davidroid 0:fedf3cf2324a 179
Davidroid 0:fedf3cf2324a 180 /* Setting the current position to be the home position. */
Davidroid 0:fedf3cf2324a 181 l6474->SetHome();
Davidroid 0:fedf3cf2324a 182
Davidroid 0:fedf3cf2324a 183 /* Waiting 2 seconds. */
Davidroid 0:fedf3cf2324a 184 wait_ms(2000);
Davidroid 0:fedf3cf2324a 185
Davidroid 0:fedf3cf2324a 186
Davidroid 0:fedf3cf2324a 187 /*----- Going to a specified position. -----*/
Davidroid 0:fedf3cf2324a 188
Davidroid 0:fedf3cf2324a 189 /* Printing to the console. */
Davidroid 0:fedf3cf2324a 190 printf("--> Going to position -6400.\r\n");
Davidroid 0:fedf3cf2324a 191
Davidroid 0:fedf3cf2324a 192 /* Requesting to go to a specified position. */
Davidroid 0:fedf3cf2324a 193 l6474->GoTo(-6400);
Davidroid 0:fedf3cf2324a 194
Davidroid 0:fedf3cf2324a 195 /* Waiting while the motor is active. */
Davidroid 0:fedf3cf2324a 196 WaitWhileActive();
Davidroid 0:fedf3cf2324a 197
Davidroid 0:fedf3cf2324a 198 /* Getting current position. */
Davidroid 0:fedf3cf2324a 199 int position = l6474->GetPosition();
Davidroid 0:fedf3cf2324a 200
Davidroid 0:fedf3cf2324a 201 /* Printing to the console. */
Davidroid 0:fedf3cf2324a 202 printf(" Position: %d.\r\n", position);
Davidroid 0:fedf3cf2324a 203
Davidroid 0:fedf3cf2324a 204 /* Setting the current position to be the mark position. */
Davidroid 0:fedf3cf2324a 205 l6474->SetMark();
Davidroid 0:fedf3cf2324a 206
Davidroid 0:fedf3cf2324a 207 /* Waiting 2 seconds. */
Davidroid 0:fedf3cf2324a 208 wait_ms(2000);
Davidroid 0:fedf3cf2324a 209
Davidroid 0:fedf3cf2324a 210
Davidroid 0:fedf3cf2324a 211 /*----- Going Home. -----*/
Davidroid 0:fedf3cf2324a 212
Davidroid 0:fedf3cf2324a 213 /* Printing to the console. */
Davidroid 0:fedf3cf2324a 214 printf("--> Going Home.\r\n");
Davidroid 0:fedf3cf2324a 215
Davidroid 0:fedf3cf2324a 216 /* Requesting to go to home */
Davidroid 0:fedf3cf2324a 217 l6474->GoHome();
Davidroid 0:fedf3cf2324a 218
Davidroid 0:fedf3cf2324a 219 /* Waiting while the motor is active. */
Davidroid 0:fedf3cf2324a 220 WaitWhileActive();
Davidroid 0:fedf3cf2324a 221
Davidroid 0:fedf3cf2324a 222 /* Getting current position. */
Davidroid 0:fedf3cf2324a 223 position = l6474->GetPosition();
Davidroid 0:fedf3cf2324a 224
Davidroid 0:fedf3cf2324a 225 /* Printing to the console. */
Davidroid 0:fedf3cf2324a 226 printf(" Position: %d.\r\n", position);
Davidroid 0:fedf3cf2324a 227
Davidroid 0:fedf3cf2324a 228 /* Waiting 2 seconds. */
Davidroid 0:fedf3cf2324a 229 wait_ms(2000);
Davidroid 0:fedf3cf2324a 230
Davidroid 0:fedf3cf2324a 231
Davidroid 0:fedf3cf2324a 232 /*----- Going to a specified position. -----*/
Davidroid 0:fedf3cf2324a 233
Davidroid 0:fedf3cf2324a 234 /* Printing to the console. */
Davidroid 0:fedf3cf2324a 235 printf("--> Going to position 6400.\r\n");
Davidroid 0:fedf3cf2324a 236
Davidroid 0:fedf3cf2324a 237 /* Requesting to go to a specified position. */
Davidroid 0:fedf3cf2324a 238 l6474->GoTo(6400);
Davidroid 0:fedf3cf2324a 239
Davidroid 0:fedf3cf2324a 240 /* Waiting while the motor is active. */
Davidroid 0:fedf3cf2324a 241 WaitWhileActive();
Davidroid 0:fedf3cf2324a 242
Davidroid 0:fedf3cf2324a 243 /* Getting current position. */
Davidroid 0:fedf3cf2324a 244 position = l6474->GetPosition();
Davidroid 0:fedf3cf2324a 245
Davidroid 0:fedf3cf2324a 246 /* Printing to the console. */
Davidroid 0:fedf3cf2324a 247 printf(" Position: %d.\r\n", position);
Davidroid 0:fedf3cf2324a 248
Davidroid 0:fedf3cf2324a 249 /* Waiting 2 seconds. */
Davidroid 0:fedf3cf2324a 250 wait_ms(2000);
Davidroid 0:fedf3cf2324a 251
Davidroid 0:fedf3cf2324a 252
Davidroid 0:fedf3cf2324a 253 /*----- Going to mark which was set previously after going to -6400. -----*/
Davidroid 0:fedf3cf2324a 254
Davidroid 0:fedf3cf2324a 255 /* Printing to the console. */
Davidroid 0:fedf3cf2324a 256 printf("--> Going to mark which was set previously after going to -6400.\r\n");
Davidroid 0:fedf3cf2324a 257
Davidroid 0:fedf3cf2324a 258 /* Requesting to go to mark position. */
Davidroid 0:fedf3cf2324a 259 l6474->GoMark();
Davidroid 0:fedf3cf2324a 260
Davidroid 0:fedf3cf2324a 261 /* Waiting while the motor is active. */
Davidroid 0:fedf3cf2324a 262 WaitWhileActive();
Davidroid 0:fedf3cf2324a 263
Davidroid 0:fedf3cf2324a 264 /* Getting current position. */
Davidroid 0:fedf3cf2324a 265 position = l6474->GetPosition();
Davidroid 0:fedf3cf2324a 266
Davidroid 0:fedf3cf2324a 267 /* Printing to the console. */
Davidroid 0:fedf3cf2324a 268 printf(" Position: %d.\r\n", position);
Davidroid 0:fedf3cf2324a 269
Davidroid 0:fedf3cf2324a 270 /* Waiting 2 seconds. */
Davidroid 0:fedf3cf2324a 271 wait_ms(2000);
Davidroid 0:fedf3cf2324a 272
Davidroid 0:fedf3cf2324a 273
Davidroid 0:fedf3cf2324a 274 /*----- Moving backward. -----*/
Davidroid 0:fedf3cf2324a 275
Davidroid 0:fedf3cf2324a 276 /* Printing to the console. */
Davidroid 0:fedf3cf2324a 277 printf("--> Moving backward.\r\n");
Davidroid 0:fedf3cf2324a 278
Davidroid 0:fedf3cf2324a 279 /* Requesting to run backward. */
Davidroid 0:fedf3cf2324a 280 l6474->Run(BACKWARD);
Davidroid 0:fedf3cf2324a 281
Davidroid 0:fedf3cf2324a 282 /* Waiting until delay has expired. */
Davidroid 0:fedf3cf2324a 283 WaitForDelay(5000);
Davidroid 0:fedf3cf2324a 284
Davidroid 0:fedf3cf2324a 285 /* Getting current speed. */
Davidroid 0:fedf3cf2324a 286 int speed = l6474->GetCurrentSpeed();
Davidroid 0:fedf3cf2324a 287
Davidroid 0:fedf3cf2324a 288 /* Printing to the console. */
Davidroid 0:fedf3cf2324a 289 printf(" Speed: %d.\r\n", speed);
Davidroid 0:fedf3cf2324a 290
Davidroid 0:fedf3cf2324a 291
Davidroid 0:fedf3cf2324a 292 /*----- Increasing the speed while running. -----*/
Davidroid 0:fedf3cf2324a 293
Davidroid 0:fedf3cf2324a 294 /* Printing to the console. */
Davidroid 0:fedf3cf2324a 295 printf("--> Increasing the speed while running.\r\n");
Davidroid 0:fedf3cf2324a 296
Davidroid 0:fedf3cf2324a 297 /* Increasing speed to 2400 step/s. */
Davidroid 0:fedf3cf2324a 298 l6474->SetMaxSpeed(2400);
Davidroid 0:fedf3cf2324a 299
Davidroid 0:fedf3cf2324a 300 /* Waiting until delay has expired. */
Davidroid 0:fedf3cf2324a 301 WaitForDelay(5000);
Davidroid 0:fedf3cf2324a 302
Davidroid 0:fedf3cf2324a 303 /* Getting current speed. */
Davidroid 0:fedf3cf2324a 304 speed = l6474->GetCurrentSpeed();
Davidroid 0:fedf3cf2324a 305
Davidroid 0:fedf3cf2324a 306 /* Printing to the console. */
Davidroid 0:fedf3cf2324a 307 printf(" Speed: %d.\r\n", speed);
Davidroid 0:fedf3cf2324a 308
Davidroid 0:fedf3cf2324a 309
Davidroid 0:fedf3cf2324a 310 /*----- Decreasing the speed while running. -----*/
Davidroid 0:fedf3cf2324a 311
Davidroid 0:fedf3cf2324a 312 /* Printing to the console. */
Davidroid 0:fedf3cf2324a 313 printf("--> Decreasing the speed while running.\r\n");
Davidroid 0:fedf3cf2324a 314
Davidroid 0:fedf3cf2324a 315 /* Decreasing speed to 1200 step/s. */
Davidroid 0:fedf3cf2324a 316 l6474->SetMaxSpeed(1200);
Davidroid 0:fedf3cf2324a 317
Davidroid 0:fedf3cf2324a 318 /* Waiting until delay has expired. */
Davidroid 0:fedf3cf2324a 319 WaitForDelay(5000);
Davidroid 0:fedf3cf2324a 320
Davidroid 0:fedf3cf2324a 321 /* Getting current speed. */
Davidroid 0:fedf3cf2324a 322 speed = l6474->GetCurrentSpeed();
Davidroid 0:fedf3cf2324a 323
Davidroid 0:fedf3cf2324a 324 /* Printing to the console. */
Davidroid 0:fedf3cf2324a 325 printf(" Speed: %d.\r\n", speed);
Davidroid 0:fedf3cf2324a 326
Davidroid 0:fedf3cf2324a 327
Davidroid 0:fedf3cf2324a 328 /*----- Increasing acceleration while running. -----*/
Davidroid 0:fedf3cf2324a 329
Davidroid 0:fedf3cf2324a 330 /* Printing to the console. */
Davidroid 0:fedf3cf2324a 331 printf("--> Increasing acceleration while running.\r\n");
Davidroid 0:fedf3cf2324a 332
Davidroid 0:fedf3cf2324a 333 /* Increasing acceleration to 480 step/s^2. */
Davidroid 0:fedf3cf2324a 334 l6474->SetAcceleration(480);
Davidroid 0:fedf3cf2324a 335
Davidroid 0:fedf3cf2324a 336 /* Waiting until delay has expired. */
Davidroid 0:fedf3cf2324a 337 WaitForDelay(5000);
Davidroid 0:fedf3cf2324a 338
Davidroid 0:fedf3cf2324a 339 /* Increasing speed to 2400 step/s. */
Davidroid 0:fedf3cf2324a 340 l6474->SetMaxSpeed(2400);
Davidroid 0:fedf3cf2324a 341
Davidroid 0:fedf3cf2324a 342 /* Waiting until delay has expired. */
Davidroid 0:fedf3cf2324a 343 WaitForDelay(5000);
Davidroid 0:fedf3cf2324a 344
Davidroid 0:fedf3cf2324a 345 /* Getting current speed. */
Davidroid 0:fedf3cf2324a 346 speed = l6474->GetCurrentSpeed();
Davidroid 0:fedf3cf2324a 347
Davidroid 0:fedf3cf2324a 348 /* Printing to the console. */
Davidroid 0:fedf3cf2324a 349 printf(" Speed: %d.\r\n", speed);
Davidroid 0:fedf3cf2324a 350
Davidroid 0:fedf3cf2324a 351
Davidroid 0:fedf3cf2324a 352 /*----- Increasing deceleration while running. -----*/
Davidroid 0:fedf3cf2324a 353
Davidroid 0:fedf3cf2324a 354 /* Printing to the console. */
Davidroid 0:fedf3cf2324a 355 printf("--> Increasing deceleration while running.\r\n");
Davidroid 0:fedf3cf2324a 356
Davidroid 0:fedf3cf2324a 357 /* Increasing deceleration to 480 step/s^2. */
Davidroid 0:fedf3cf2324a 358 l6474->SetDeceleration(480);
Davidroid 0:fedf3cf2324a 359
Davidroid 0:fedf3cf2324a 360 /* Waiting until delay has expired. */
Davidroid 0:fedf3cf2324a 361 WaitForDelay(5000);
Davidroid 0:fedf3cf2324a 362
Davidroid 0:fedf3cf2324a 363 /* Decreasing speed to 1200 step/s. */
Davidroid 0:fedf3cf2324a 364 l6474->SetMaxSpeed(1200);
Davidroid 0:fedf3cf2324a 365
Davidroid 0:fedf3cf2324a 366 /* Waiting until delay has expired. */
Davidroid 0:fedf3cf2324a 367 WaitForDelay(5000);
Davidroid 0:fedf3cf2324a 368
Davidroid 0:fedf3cf2324a 369 /* Getting current speed. */
Davidroid 0:fedf3cf2324a 370 speed = l6474->GetCurrentSpeed();
Davidroid 0:fedf3cf2324a 371
Davidroid 0:fedf3cf2324a 372 /* Printing to the console. */
Davidroid 0:fedf3cf2324a 373 printf(" Speed: %d.\r\n", speed);
Davidroid 0:fedf3cf2324a 374
Davidroid 0:fedf3cf2324a 375
Davidroid 0:fedf3cf2324a 376 /*----- Requiring soft-stop while running. -----*/
Davidroid 0:fedf3cf2324a 377
Davidroid 0:fedf3cf2324a 378 /* Printing to the console. */
Davidroid 0:fedf3cf2324a 379 printf("--> Requiring soft-stop while running.\r\n");
Davidroid 0:fedf3cf2324a 380
Davidroid 0:fedf3cf2324a 381 /* Requesting soft stop. */
Davidroid 0:fedf3cf2324a 382 l6474->SoftStop();
Davidroid 0:fedf3cf2324a 383
Davidroid 0:fedf3cf2324a 384 /* Waiting while the motor is active. */
Davidroid 0:fedf3cf2324a 385 WaitWhileActive();
Davidroid 0:fedf3cf2324a 386
Davidroid 0:fedf3cf2324a 387 /* Waiting 2 seconds. */
Davidroid 0:fedf3cf2324a 388 wait_ms(2000);
Davidroid 0:fedf3cf2324a 389
Davidroid 0:fedf3cf2324a 390
Davidroid 0:fedf3cf2324a 391 /*----- Requiring hard-stop while running. -----*/
Davidroid 0:fedf3cf2324a 392
Davidroid 0:fedf3cf2324a 393 /* Printing to the console. */
Davidroid 0:fedf3cf2324a 394 printf("--> Requiring hard-stop while running.\r\n");
Davidroid 0:fedf3cf2324a 395
Davidroid 0:fedf3cf2324a 396 /* Requesting to run in forward direction. */
Davidroid 0:fedf3cf2324a 397 l6474->Run(FORWARD);
Davidroid 0:fedf3cf2324a 398
Davidroid 0:fedf3cf2324a 399 /* Waiting until delay has expired. */
Davidroid 0:fedf3cf2324a 400 WaitForDelay(5000);
Davidroid 0:fedf3cf2324a 401
Davidroid 0:fedf3cf2324a 402 /* Requesting to immediatly stop. */
Davidroid 0:fedf3cf2324a 403 l6474->HardStop();
Davidroid 0:fedf3cf2324a 404
Davidroid 0:fedf3cf2324a 405 /* Waiting while the motor is active. */
Davidroid 0:fedf3cf2324a 406 WaitWhileActive();
Davidroid 0:fedf3cf2324a 407
Davidroid 0:fedf3cf2324a 408 /* Waiting 2 seconds. */
Davidroid 0:fedf3cf2324a 409 wait_ms(2000);
Davidroid 0:fedf3cf2324a 410
Davidroid 0:fedf3cf2324a 411
Davidroid 0:fedf3cf2324a 412 /*----- GOTO stopped by soft-stop. -----*/
Davidroid 0:fedf3cf2324a 413
Davidroid 0:fedf3cf2324a 414 /* Printing to the console. */
Davidroid 0:fedf3cf2324a 415 printf("--> GOTO stopped by soft-stop.\r\n");
Davidroid 0:fedf3cf2324a 416
Davidroid 0:fedf3cf2324a 417 /* Requesting to go to a specified position. */
Davidroid 0:fedf3cf2324a 418 l6474->GoTo(20000);
Davidroid 0:fedf3cf2324a 419
Davidroid 0:fedf3cf2324a 420 /* Waiting while the motor is active. */
Davidroid 0:fedf3cf2324a 421 WaitForDelay(5000);
Davidroid 0:fedf3cf2324a 422
Davidroid 0:fedf3cf2324a 423 /* Requesting to perform a soft stop */
Davidroid 0:fedf3cf2324a 424 l6474->SoftStop();
Davidroid 0:fedf3cf2324a 425
Davidroid 0:fedf3cf2324a 426 /* Waiting while the motor is active. */
Davidroid 0:fedf3cf2324a 427 WaitWhileActive();
Davidroid 0:fedf3cf2324a 428
Davidroid 0:fedf3cf2324a 429 /* Waiting 2 seconds. */
Davidroid 0:fedf3cf2324a 430 wait_ms(2000);
Davidroid 0:fedf3cf2324a 431
Davidroid 0:fedf3cf2324a 432
Davidroid 0:fedf3cf2324a 433 /*----- Reading inexistent register to test "MyFlagInterruptHandler". -----*/
Davidroid 0:fedf3cf2324a 434
Davidroid 0:fedf3cf2324a 435 /* Printing to the console. */
Davidroid 0:fedf3cf2324a 436 printf("--> Reading inexistent register to test \"MyFlagInterruptHandler\".\r\n");
Davidroid 0:fedf3cf2324a 437
Davidroid 0:fedf3cf2324a 438 /*
Davidroid 0:fedf3cf2324a 439 * Trying to read an inexistent register.
Davidroid 0:fedf3cf2324a 440 * The flag interrupt should be raised and the "MyFlagInterruptHandler"
Davidroid 0:fedf3cf2324a 441 * function called.
Davidroid 0:fedf3cf2324a 442 */
Davidroid 0:fedf3cf2324a 443 l6474->CmdGetParam(0x1F);
Davidroid 0:fedf3cf2324a 444
Davidroid 0:fedf3cf2324a 445 /* Waiting 0.5 seconds. */
Davidroid 0:fedf3cf2324a 446 wait_ms(500);
Davidroid 0:fedf3cf2324a 447
Davidroid 0:fedf3cf2324a 448
Davidroid 0:fedf3cf2324a 449 /*----- Changing step mode to full step mode. -----*/
Davidroid 0:fedf3cf2324a 450
Davidroid 0:fedf3cf2324a 451 /* Printing to the console. */
Davidroid 0:fedf3cf2324a 452 printf("--> Changing step mode to full step mode.\r\n");
Davidroid 0:fedf3cf2324a 453
Davidroid 0:fedf3cf2324a 454 /* Selecting full step mode. */
Davidroid 0:fedf3cf2324a 455 l6474->SelectStepMode(STEP_MODE_FULL);
Davidroid 0:fedf3cf2324a 456
Davidroid 0:fedf3cf2324a 457 /* Setting speed and acceleration to be consistent with full step mode. */
Davidroid 0:fedf3cf2324a 458 l6474->SetMaxSpeed(100);
Davidroid 0:fedf3cf2324a 459 l6474->SetMinSpeed(50);
Davidroid 0:fedf3cf2324a 460 l6474->SetAcceleration(10);
Davidroid 0:fedf3cf2324a 461 l6474->SetDeceleration(10);
Davidroid 0:fedf3cf2324a 462
Davidroid 0:fedf3cf2324a 463 /* Requesting to go to a specified position. */
Davidroid 0:fedf3cf2324a 464 l6474->GoTo(200);
Davidroid 0:fedf3cf2324a 465
Davidroid 0:fedf3cf2324a 466 /* Waiting while the motor is active. */
Davidroid 0:fedf3cf2324a 467 WaitWhileActive();
Davidroid 0:fedf3cf2324a 468
Davidroid 0:fedf3cf2324a 469 /* Getting current position */
Davidroid 0:fedf3cf2324a 470 position = l6474->GetPosition();
Davidroid 0:fedf3cf2324a 471
Davidroid 0:fedf3cf2324a 472 /* Printing to the console. */
Davidroid 0:fedf3cf2324a 473 printf(" Position: %d.\r\n", position);
Davidroid 0:fedf3cf2324a 474
Davidroid 0:fedf3cf2324a 475 /* Waiting 2 seconds. */
Davidroid 0:fedf3cf2324a 476 wait_ms(2000);
Davidroid 0:fedf3cf2324a 477
Davidroid 0:fedf3cf2324a 478
Davidroid 0:fedf3cf2324a 479 /*----- Restoring 1/16 microstepping mode. -----*/
Davidroid 0:fedf3cf2324a 480
Davidroid 0:fedf3cf2324a 481 /* Printing to the console. */
Davidroid 0:fedf3cf2324a 482 printf("--> Restoring 1/16 microstepping mode.\r\n");
Davidroid 0:fedf3cf2324a 483
Davidroid 0:fedf3cf2324a 484 /* Resetting to 1/16 microstepping mode */
Davidroid 0:fedf3cf2324a 485 l6474->SelectStepMode(STEP_MODE_1_16);
Davidroid 0:fedf3cf2324a 486
Davidroid 0:fedf3cf2324a 487 /* Update speed, acceleration, deceleration for 1/16 microstepping mode*/
Davidroid 0:fedf3cf2324a 488 l6474->SetMaxSpeed(1600);
Davidroid 0:fedf3cf2324a 489 l6474->SetMinSpeed(800);
Davidroid 0:fedf3cf2324a 490 l6474->SetAcceleration(160);
Davidroid 0:fedf3cf2324a 491 l6474->SetDeceleration(160);
Davidroid 0:fedf3cf2324a 492
Davidroid 0:fedf3cf2324a 493
Davidroid 0:fedf3cf2324a 494 /*----- Infinite Loop. -----*/
Davidroid 0:fedf3cf2324a 495
Davidroid 0:fedf3cf2324a 496 /* Printing to the console. */
Davidroid 0:fedf3cf2324a 497 printf("--> Infinite Loop...\r\n");
Davidroid 0:fedf3cf2324a 498
Davidroid 0:fedf3cf2324a 499 /* Infinite Loop. */
Davidroid 0:fedf3cf2324a 500 while(1)
Davidroid 0:fedf3cf2324a 501 {
Davidroid 0:fedf3cf2324a 502 /* Requesting to go to a specified position. */
Davidroid 0:fedf3cf2324a 503 l6474->GoTo(-6400);
Davidroid 0:fedf3cf2324a 504
Davidroid 0:fedf3cf2324a 505 /* Waiting while the motor is active. */
Davidroid 0:fedf3cf2324a 506 WaitWhileActive();
Davidroid 0:fedf3cf2324a 507
Davidroid 0:fedf3cf2324a 508 /* Requesting to go to a specified position. */
Davidroid 0:fedf3cf2324a 509 l6474->GoTo(6400);
Davidroid 0:fedf3cf2324a 510
Davidroid 0:fedf3cf2324a 511 /* Waiting while the motor is active. */
Davidroid 0:fedf3cf2324a 512 WaitWhileActive();
Davidroid 0:fedf3cf2324a 513 }
Davidroid 0:fedf3cf2324a 514 }
Davidroid 0:fedf3cf2324a 515 }