Advanced Microcontrollers: Using chipKIT Pro MX7

Project 0: MPLAB® X Integrated Development Environment

Using chipKIT™ Pro and MPLAB X

This project introduces you to the synthesis and analysis tools for producing microprocessor C code using the MPLAB® X integrated development environment (IDE) on the chipKIT™ Pro MX7 processor board.

8.33K
×
Project 1: chipKIT™ Pro and I/O Control

Digital Input and Output

The purpose of this project is to familiarize you with the methods of reading from and writing to the input and output (I/O) pins of the PIC™32 microcontroller.

6.66K
×
Project 2: chipKIT™ Pro and Delays

Software Timing Delays

The purpose of this project is to investigate methods of creating software time delays to pace processor operations.

4.43K
×
Project 3: Using chipKIT™ Pro with Stepper Motors

Software-Based Finite State Machines

The purpose of this project is to investigate the application of software-based state machines to controlling the speed, direction of rotation, and operational mode of stepper motors.

4.43K
×
Project 4: Using chipKIT™ Pro with Stepper Motors

PIC™32 Timers

The purpose of this project is to understand the operation of PIC™32 timers so that they can be used to implement a synchronized multi-rate periodic control system by polling the timer interrupt flag.

3.85K
×
Project 5: chipKIT™ Pro and Interrupts

Process Speed Controls Using Interrupts

Explore detecting events using interrupts or by using preemption that implements a nested interrupt management scheme.

5.59K
×
Project 6: Using chipKIT™ Pro to Control LCDs

Handshaking and LCD Control

Investigate concepts involving parallel communications and handshaking.

5.32K
×
Project 7: chipKIT™ Pro and Serial Communication

Asynchronous Serial Communications

Learn about asynchronous communications and communicate with a microcontroller using a terminal emulation program to implement a point to point serial link between the chipKIT™ PRO MX7 and a PC.

5.14K
×
Project 8: chipKIT™ Pro and Serial Communications

Synchronous I2C Serial Communications

Investigate concepts involving synchronous communications using a basic master-slave multi-drop network communications and use the I2C protocol to communicate with the 24LC256 I2C™ CMOS Serial EEPROM.

5.29K
×
Project 8a: chipKIT™ Pro and Serial Communications

Synchronous SPI Serial Communications

Investigate synchronous communications with the SPI master-slave serial bus

3.98K
×
Project 9: Controlling a DC Motor

PWM Using Timer Compare

Generate a proportional output using the output compare resource on the PIC®32MX processor to implement digital-to-analog conversion with pulse width modulation (PWM), thus controlling the speed of a DC motor.

6.68K
×
Project 10: Using chipKIT™ Pro and DC Motors

Frequency Measurement Using Input Capture

Use the PIC™ 32 input capture to measure frequency to determine the speed of a DC motor.

4.51K
×

Project 3: Using chipKIT™ Pro with Stepper Motors

Software-Based Finite State Machines

Using MPLAB® X Integrated Development Environment and chipKIT™Pro

Project 3: Software-Based Finite State Machines

Introduction

The purpose of this project is to investigate the application of software-based state machines to controlling the speed, direction of rotation, and operational mode of stepper motors. This project requires knowledge of concepts presented in Projects 1 and 2 to provide real-time open loop control.

Before you begin, you should:

Inventory:

Qty Description
1 chipKIT™Pro MX7 processor board with USB cable
1 PmodSTEP™
1 Stepper Motor (5V-12V, 25 Ω, unipolar or bi-polar)
1 Logic analyzer or oscilloscope (Digilent Analog Discovery™ recommended)

Stepper Motors and Finite State Machines

After a search of the Internet, one will find that the stepper motor is one of the most frequently used as examples of an application of a finite state machine (FSM). Applications that use stepper motors include robotics, disk drives, and office products like laser printers and copiers.

Stepper motors, like the one shown in Fig. 1, are variable reluctance electric motors that are designed to control the angular position of the rotor shaft in discrete steps. The stepper motor consists of two sets of field windings positioned around a permanent magnet rotor. The combinations of voltages applied to the four control terminals of the field windings control the magnitude and direction of the current through the windings, as illustrated in Fig. 2. The current through the windings create an electromagnet. The motor shaft rotates to a position that minimizes the reluctance path between the field winding electromagnet's north and south poles of the rotor.

Figure 1. Photograph of typical stepper motor.
Figure 2. Bipolar stepper motor diagram.

Considering the combinations of voltages on the winding terminals as possible control states, there are only eight states that produce current in the field windings, as shown in Table 1 below. In order to move the rotator shaft from one stable position to the physically adjacent stable position, the control voltages must switch to one of four out of the eight possible combinations of voltages. The action of moving from one stable position to an adjacent stable position is referred to as either a full-step or a half-step. Half-step increments are half the angular rotation of full-steps. Repeating a sequence of full- or half-step movements at a uniform rate will cause the rotator shaft to appear to rotate at a constant speed in discrete steps.

Table 1. Stepper motor control codes.

Step Control Winding Voltage
Step Name Hex Code "1a" "1b" "2a" "2b"
S0_5 0x0A H L H L
S1 0x08 H L L L
S1_5 0x09 H L L H
S2 0x01 L L L H
S2_5 0x05 L H L H
S3 0x04 L H L L
S3_5 0x06 L H H L
S0 0x02 L L H L

Binary codes are assigned here by replacing the letter “H” with a binary 1 and the letter “L” with a binary '0'. The values shown in column 2 of Table 1 are the hexadecimal equivalent of the binary representation of the field winding voltages. The four winding designations shown in Table 1 are assigned to I/O pins, as shown in Fig. 2 of the red tab on the right. The stepper motor will move to the nearest stable position generated by the voltages associated with the hexadecimal codes shown in the second column. The stepper motor will be held in a fixed position until the voltages on the windings change.

The PmodSTEP driver module was designed to use I/O PORT B pins 7 through 10 that are assigned designations SM1 through SM4 to control the voltages on the four stepper motor field windings. (See the red tab for PmodSTEP connection details.) The hexadecimal codes listed in Table 1 must be shifted left so that the bit representing winding “2b” appears on Port B pin 7, the bit representing winding “2a” appears on Port B pin 8, the bit representing winding “1b” appears on Port B pin 9, and the bit representing winding “1a” appears on Port B pin 10.

If the motor is to rotate in a clockwise direction using the full-step mode, the sequence of output codes that must be sent to the motor are represented by steps S1, S2, S3, S4, S1, etc. A 100-step per revolution motor will require the sequence of the four output combinations, S1 through S4, to be repeated 25 times for the rotator shaft to make exactly one revolution. If operating in half-step mode, then the eight-step sequence of S1, S1_5, S2, S2_5, etc., S4 must be repeated 25 times for the rotator shaft to make a complete revolution. Sequencing in one direction (up or down) through the output code found in Table 1 causes the rotator shaft to rotate in one direction. Reversing this sequence causes the rotator shaft to rotate in the reverse direction.

Programming Concepts

In a C-based program, the main function consists of an infinite software loop that repeatedly executes a sequence of tasks inside the while(1) loop. A set of tasks must be repeatedly completed in the prescribed order within the while(1) loop to correctly control the stepper motor operation. The actual steps required and the order in which they are executed are prescribed by the application requirements. One possible sequence of operations to control the stepper motor is shown below in Listing 1.

Listing 1. Five tasks used to control the stepper motor operations.

  1. Sense the button status of the control by polling the inputs that are connected to the buttons.
  2. Derive the direction, step mode, and the delay period based upon the positions of the buttons.
  3. Determine the new stepper motor control output.
  4. Output code to the stepper motor.
  5. Delay the correct amount of time using one of the software delay approaches developed in Project 2.

In this project, we are implementing a simple form of real-time open loop control. The term “real-time” implies that all tasks must be completed in a specified time. The process represented by the second task is frequently referred to as mapping and is characteristic of logic operations that can be implemented with combinational logic. The processor completes the code for the first four of the five tasks listed in Listing 1 as quickly as the processor can execute the code. The fourth task causes the stepper motor to physically move. The fifth task causes the processor to wait before repeating the first four steps again. Note that for the control implementation described in Listing 1, the buttons are sampled at the step rate. Hence, the slower the motor moves, the less frequently the buttons are polled. One may consider ways to modify the tasks listed in Listing 1 to make the control more responsive.

The precision of the stepping speed depends on the accuracy of the software delay loop and the amount of time required for processing the tasks 1 through 4. One of the weaknesses of using software delays in control loops is the variance in loop speed due to changing execution times for the other tasks in the infinite task processing loop. This is also true of the hardware-assisted delay loops.

The time delay function using the hardware-assisted approach employs polling of the core timer. Because the processor is always polling inputs and timers or just executing a for loop that does nothing else except to consume time, the processor has no time to do anything else. This limitation will be rectified by changes to our methods of detecting events in Project 5: Process Speed Control Using Interrupts.

There are three parameters that will be used to control the stepper motor: the step direction as either clockwise (CW) or counter-clockwise (CCW), the step mode as either FULL or HALF, and the step delay that determines the rotator shaft rotational speed. For this project, we will be using a fixed speed of 15 RPM.

The output that must be used to control the I/O pins needed to move the motor to the next position depends on the present rotor position. This means that the present position must be remembered from step to step. The need to “remember” the step code that set the present rotator shaft position implies memory and the concept of a “state.” Thus, a state machine will be used to determine the I/O values to move rotator shaft to the next position. The I/O pin output code corresponding to the next state depends upon the present state and the button inputs. The frequency of transitions is controlled by the delay function, similar to a clock period in a hardware based FSM. Additional information regarding software implementation of finite state machines is provided in Appendix D.

The stepper motor driver module, PmodSTEP (shown in the PmodSTEP Layout tab mentioned above), has eight test points that are connected to the eight LEDs that are labeled LEDA through LEDH. The physical locations of the test points are shown in the first figure of the PmodSTEP Layout page. The wiring diagram, provided in the second figure of the PmodSTEP Layout page, shows that the pins that control LEDE through LEDH are also connected to a driver IC that amplifies the voltage and current switching capability needed to drive the stepper motor. The chipKITProMX7.h header file defines the pin constants for LEDA through LEDH and also SM1 through SM4, as shown in Table 1 of the red tab above on the right. These assignments assume that the PmodSTEP is connected to the chipKIT Pro MX7 Pmod jack JA.

Instrumentation for monitoring the period between steps (ms/step) is provided by toggling LEDB on the PmodSTEP module each time a new step output is generated. The values of this I/O pin can be toggled using the LATBINV instruction. However, use caution when changing the PORT B outputs using an assignment to LATB to change the stepper motor shaft position or to change the state of an LED. You cannot simply write the stepper motor control code to Port B using the instruction, LATB = code;. Doing so will also alter PORT B I/O pins that may used for instrumentation.

Since LEDA, LEDB, and SM1 through SM4 outputs all share the same I/O port, the bit state for LEDA and LEDB must be preserved when setting SM1 through SM4. This requires a read-modify-write sequence in software. The bit set, bit clear, and bit invert may be used for controlling single-bit operations, such as controlling the state of an LED. However, these operations should not be used for setting a group of bits to both high and low values. To do so would require two operations, which results in two different outputs being generated instead of just one. One possible method for implementing a read-modify-write sequence of code was presented in Appendix A of Project 1.

Project Design

You will write a program to control the direction and step-mode of a stepper motor at the fixed speed of 15 RPM as per the rules shown in Table 2. Your program should contain the seven software functions whose functionality is described in Listing 2. Functions numbered 3 through 7 will be continuously repeated, in order, within a while (1) software control loop. Note: Having completed Projects 1 and 2, you have already written software that partially implements all the functions in this list, except function 5. The data flow and control flow diagrams shown in the second and third tabs on the right are provided to assist you in the program development. The state diagram shown in the green tab on the above right is to assist you in developing the stepper motor control function that implements the FSM.

Table 2. Stepper motor control table.

Inputs Control Modes
BTN2 BTN1 DIRECTION STEP MODE
Off Off CW FS
Off On CW HS
On Off CCW HS
On On CCW FS

Listing 2. Task (function) list for Project 3.

  1. main : the task control function responsible for calling the support functions listed below.
  2. system_init : initializes all chipKIT Pro MX7 board I/O.
  3. read_buttons : reads the status of BTN1 and BTN2.
  4. decode_buttons : determines the values of stepper_direction, stepper_mode, and stepper_delay using the rules specified in Table 2.
  5. stepper_state_machine : determines the new output code for the stepper motor.
  6. output_to_stepper_motor : sends the four bit code to the stepper motor I/O pins.
  7. sw_delay : implements a ms software delay that toggles LEDA each ms and delays the number of milliseconds to generate a 15 RPM rotational speed and toggles bit RB3 (LEDB) each time a step is taken.

Project Testing

The speed specifications are given in RPM and must be converted to a delay with units of milliseconds per step. The following equation provides the needed conversion formula for a speed that is specified as $\text{X RPM}$ — \begin{equation} T_{\mathrm{DELAY}}\ (\text{ms} / \text{step}) = 60000\ (\text{ms}/\text{min}) / (X\ (\text{rev}/\text{min}) \times 100\ (\text{steps}/\text{rev}) \times MODE). \end{equation}

The factor $MODE$ in Eq. (1) is either one for full-step mode or two for half-step mode. The speed is specified as a constant 15 RPM for this project, but the mode parameter is set according to the conditions provided in Table 3 . Thus, $T_{\mathrm{DELAY}}$ will need to be adjusted to keep the motor speed constant.

For testing the direction control, one simply needs to observe the shaft of the stepper motor to verify that the direction changes when BTN2 is pressed. Use Table 3 to record your results.

Table 3. Stepper motor delay per step verification table.

Inputs Control Modes
BTN2 BTN1 STEP MODE Step Delay Calculated Step Delay Measured
Off Off FS 40 ms  
Off On HS 20 ms  
On Off HS 20 ms  
On On FS 40 ms  

Try It on Your Own!

Now that you've completed this project, you should look at the following questions/problems:

  1. Provide the results for measuring the delay values use to complete Table 3.

    ANSWER: Verification table for stepper motor delay per step based on the timing shown in the oscilloscope outputs shown in the following two figures.

  2. Inputs Control Modes
    BTN2 BTN1 STEP MODE Step Delay Calculated Step Delay Measured
    Off Off FS 40 ms 40 ms
    Off On HS 20 ms 20 ms
    On Off HS 20 ms 20 ms
    On On FS 40 ms 40 ms
    Timing for full-step mode.
    Timing for half-step mode.
  3. Describe the limitations of implementing a FSM on a microprocessor.

    ANSWER: It is all about time, memory, and I/O. You need adequate amount of these three resources.

  4. By strategically placing an instruction to turn LEDC on and off, measure the time required to execute code that is not part of the delay function. Provide documentation to verify the measurement.

    ANSWER: The time to execute all code except the delay function is 6 μs.

  5. The screenshots above are of Digilent WaveForms running on Microsoft Windows 7.
  6. Repeat this measurement using the MPLAB® X Stopwatch tool.

    ANSWER: The MPLAB X Stopwatch tool reports 496 cycles. At 80 MHz cycle rate, this equates to 6.2 μs.

  7. Report on any differences in the measurements taken using the two methods.

    ANSWER: The difference is insignificant making the MPLAB X Stopwatch tool very valuable. I am impressed given the constrains of the development environment provided by the chipKIT Pro MX7!

  8. Why is implementing a FSM using the switch-case better than using if-else?

    ANSWER: The coding gets very long and involved when if-else constructs are used. This makes the code difficult to write, debug, and maintain. Simple code is better code!

  9. As suggested in the text of this project, the buttons are polled at the rate that the stepper motor steps to a new position. At slower motor speeds, the responses to button changes will also be slower. How can the task list shown in Table 2 be modified to improve the control response rate?

    ANSWER: Table 2a. Modified tasks used to control the stepper motor operations.

    1. Sense the button status of the control by polling the inputs that are connected to the buttons.
    2. Map the button status to specific direction of rotation and step mode.
    3. Determine the new stepper motor control output.
      1. Set total delay for speed control.
      2. Set Direction and Mode.
    4. Delay the 1ms.
    5. Check to see if total delay complete.
      1. If no — go to step 1.
      2. If yes — go to step 6.
    6. Output code to the stepper motor.
    7. Reset total delay counter.

Challenge Problems

  • If you are confident in what we have covered in this project and would like some extra practice, we have provided a few problems in the links below. These problems require that you understand the concepts introduced throughout this exercise.

  • Other product and company names mentioned herein are trademarks or trade names of their respective companies. © 2014 Digilent Inc. All rights reserved.