CMPE 401 - Laboratory #4

Stepper Motor Control Using the Motorola Time Processing Unit (TPU)


Lab Dates:

Nov. 14*, Nov. 18, Nov. 13 and Nov. 20 for lab sections D1, D2, D3 and D4, respectively.

Report Due Dates:

Nov. 27*, Dec. 1, Nov. 26 and Dec. 3 for lab sections D1, D2, D3 and D4, respectively.

* Lab Dates and Report Due Dates for Lab Section D1:

The University is closed over the Remembrance Day holiday on November 11 (as well as on Monday, November 10). This holiday only affects the students in laboratory section D1; consequently, a special lab session will be held on Friday, November 14 for the students in lab section D1 only. If you are in lab section D1 and cannot attend this rescheduled lab session, then please contact Nancy Minderman (nem@ece.ualberta.ca) to reschedule yourself into one of the other three lab sections for the purposes of the last lab. Your report due date will be determined accordingly (13 days after your lab day).

Objectives:

Equipment, Parts and Provided Software:

PC host running Linux, with two serial ports (/dev/ttyS0 and /dev/ttyS1)
New Micros NMIX-0332-OEM 68332-based microcomputer
One 68681 DUART chip
One MAX232 RS-232 driver/receiver chip
One DB9 Header and one DB9 ribbon cable
One 3.6864 MHz crystal oscillator (for the DUART baud rate generator)
Two 15 pF capacitors
Four 1 uF electrolytic capacitors
One STMicroelectronics L298 dual full-bridge (also called an "H-bridge") driver IC
One Jameco 12-VDC, 480-mA, 2-phase bipolar stepper motor, 3.6 degrees per step
One clock face and pointer, already attached to the axle of the stepper motor
NOTE: The motor and clock face will be provided in the lab.
The lab1 tar file, which includes all of the MicroC/OS-II functions.
DO NOT install this file twice or you will lose all your work from lab #1!
The tar file in directory ~cmpe401/labs/lab4

Documentation:

Course Notes (chapter 10 especially)
The M68000 Programming Reference Card
MicroC/OS-II Function Summary
MC68681 DUART Datasheet
L298 Dual Full-Bridge Driver Datasheet
Jameco Part No. 105881 Stepper Motor Datasheet
Motorola TPU Reference Manual
Motorola Application Note on the TPU Stepper Motor Function
Motorola Application Note on the TPU Discrete I/O Function
Lab 4 Schematic Diagram
Lab 4 Suggested Breadboard Layout

Background:

Microcomputers are sometimes used to control rotating mechanisms, such as shafts and axles as well as attached pulleys, gears and/or wheels. A stepper motor is a motor whose rotor position can be controlled precisely by switching direct current (DC) voltages to two or more stator windings (also called coils). Each winding can be energized in the forward direction, energized in the reverse direction, or not energized at all. Such a motor is convenient to control using a microcomputer since only buffered digital coil control signals are required. Motorola's Time Processing Unit (TPU) is a programmable sub-system in the MC68332 microcontroller that has built-in functions that greatly simplify the generation of the digital waveforms that are required to control a stepper motor.

The stator windings in a stepper motor must be energized in the correct direction and in the correct sequence in order to produce controlled rotation of the rotor. The first six steps in the full-step sequence are given in the table below, which assumes the use of the L298 dual full-bridge driver. Note that the provided TPU driver drives both L298 enable inputs EnA and EnB using TPU timer channel 11 with the built-in discrete I/O function, and drives L298 inputs IN1, IN2, IN3 and IN4 using TPU timer channels 12, 13, 14 and 15, respectively, with the built-in stepper motor function. Each step in the full-step sequence advances the rotor of the Jameco motor by 3.6 degrees; thus 100 steps advance the rotor through one full revolution.

Full-Step Sequence
Step Winding A
Current
Direction
Winding B
Current
Direction
L298 EnA = EnB = 1
L298 Outputs L298 Inputs
OUT1 OUT2 OUT3 OUT4 IN1 IN2 IN3 IN4
#1 forward forward source sink source sink 1 0 1 0
#2 forward reverse source sink sink source 1 0 0 1
#3 reverse reverse sink source sink source 0 1 0 1
#4 reverse forward sink source source sink 0 1 1 0
#1 forward forward source sink source sink 1 0 1 0
#2 forward reverse source sink sink source 1 0 0 1

Note that in the full-step sequence, the total current drawn by the motor from the DC power supply is roughly the same in every step position. If one relaxes this constant-current restriction, then one can double the resolution of the rotor positioning by using the half-step sequence, which is shown in the table below. The additional cost of the half-step sequence is that the control waveforms are slightly more complicated and the total drawn current becomes position-dependant. In the case of the laboratory set-up, the two L298 enable inputs (EnA and EnB) should really be controlled separately. However, instead of disabling EnA to turn off the current to winding A (or EnB, in the case of winding B), one could also leave EnA (EnB) on and just make IN1 = IN2 = 0 (or IN3 = IN4 = 0). This is in fact what we recommend that you do in the lab when you implement the half-step sequence.

Half-Step Sequence
Step Winding A
Current
Winding B
Current
L298 Outputs L298 Inputs
OUT1 OUT2 OUT3 OUT4 EnA EnB IN1 IN2 IN3 IN4
#1 forward forward source sink source sink 1 1 1 0 1 0
#2 forward off source sink off off 1 0 1 0 X X
#3 forward reverse source sink sink source 1 1 1 0 0 1
#4 off reverse off off sink source 0 1 X X 0 1
#5 reverse reverse sink source sink source 1 1 0 1 0 1
#6 reverse off sink source off off 1 0 0 1 X X
#7 reverse forward sink source source sink 1 1 0 1 1 0
#8 off forward off off source sink 0 1 X X 1 0
#1 forward forward source sink source sink 1 1 1 0 1 0
#2 forward off source sink off off 1 0 1 0 X X

The stepper motor that you will be controlling in this set of laboratory exercises will be operated in open loop mode. By this we mean that the microcomputer sends out digital signals to the stepper motor and must assume that the rotor will rotate correctly through the correct number of steps. The microcomputer lacks the means to verify the actual position of the rotor before and after a sequence of step commands. Open loop operation is more risky in the sense that the microcomputer cannot verify that it does indeed have accurate control over the motor position. The motor position may in fact deviate from the expected position if, for example, the limited torque of the motor is unable to rotate the loaded shaft fast enough to keep up with the stepping commands. A better alternative to open loop operation would be to operate in closed loop mode. To do this one would need to provide a shaft encoder sensor that would allow the microcomputer to independently determine the actual position of the motor rotor. But we will not be using feedback in this lab. Instead you will initially set the rotor pointer to the "12 o'clock" position, which the TPU driver software will then associate with step number $7FFF.

Pre-lab Work:

Exercise #1:

  1. Before wiring up the motor, carefully remove the clear plastic cover over the clock face. Grasp the hub with your fingers (avoid touching the fragile pointer) and rotate the hub so that the pointer points to the 12 o'clock position. Replace the cover back onto the clock face so that the pointer will be protected when the motor is operated. You can now connect the motor to your breadboard circuit using the header that was provided. You will likely need to "reinitialize" the physical position of the pointer whenever you reset your stepper motor control software.

  2. Design C code for the empty task Task1 in lab4.c so that it issues commands to the given assembly language functions StepPower() and StepMotor(). Note that StepMotor( int ) takes a signed integer step position as the input parameter then steps the motor automatically to get to that new position from the present position. Note that the TPU initialization routine assigns the numerical position 0x7FFF to the initial motor position (which should be the 12 o'clock position). The motor will be initially set up to full-step with an initial step rate of 25 steps/second and a maximum step rate of 200 steps/second. The built-in TPU stepper motor (SM) function automatically accelerates the rotor through the increasingly fast step rates. There are initially 14 different step rates. When the desired rotor position is approached, then the SM function automatically reduces the step rate to gradually decelerate the rotor until it comes to a stop at the new position.

  3. Modify task Task1() so that it calls the StepMotor() function to rotate the pointer as follows: First, rotate clockwise by 50 steps (+180 degrees) and pause for 0.25 seconds, then rotate counterclockwise by 25 steps (-90 degrees) and pause again for 0.25 seconds. Second, rotate clockwise by 100 steps, then rotate counterclockwise by 50 steps. Third, rotate clockwise by 200 steps, then rotate counterclockwise by 100 steps. Finally, rotate clockwise by 400 steps, then rotate counterclockwise by 200 steps. Then, if necessary, return back to the 12 o'clock position using the shortest rotation. At each new position, pause for 0.25 seconds. Call the function StepPower(1) before the stepping sequence to turn on the power to the full-bridge driver; also call StepPower(0) at the end of the sequence to turn off the motor power once it is no longer required.

Exercise #2:

  1. In this exercise you will be using TPU interrupts, instead of busy waiting, to synchronize the C code with the movements of the stepper motor. Copy over the original StepMotor() routine into a new routine called StepMotor2(). Remove the busy wait loops from StepMotor2().

  2. Modify the initialization routine so that interrupts are enabled for TPU channel 12 (the primary stepper motor channel). Also modify the initialization routine so that the stepper motor operates at a maximum step rate of 180 steps/sec with the same initial step rate as before. Reduce the number of different step rates from 14 down to 10.

  3. Write an interrupt service routine (ISR) for the TPU that will be called whenever the motor reaches the desired final position. Have the ISR check and clear the appropriate TPU status registers, and then post to the semaphore StepSem().

  4. Modify Task1() so that it synchronizes itself with the motor movements using semaphore StepSem() instead of relying on busy-waiting. Task1() is to turn on the stepper motor power before starting a sequence of calls to the function StepMotor2(). After calling StepMotor2(), the task is to block on the semaphore so that it will be prevented from executing further commands while the motor is still moving to its next position. NOTE: The initialization routine will cause one interrupt to signal that initialization is complete. Your ISR should check if it is being run for the first time and, for that time only, output the message "TPU Initialization Complete" instead of signaling the semaphore.

  5. Every 10 seconds, the following sequence of motor movements is to occur when the MicroC/OS-II system is executing:
    1. Turn on the power to the motor.
    2. Rotate the pointer clockwise by 180 degrees.
    3. Rotate the pointer counter-clockwise by 90 degrees.
    4. Rotate the pointer clockwise by 180 degrees.
    5. Rotate the pointer counter-clockwise by 90 degrees.
    6. Rotate the pointer clockwise by 180 degrees.
    7. Rotate the pointer counter-clockwise by 90 degrees.
    8. Rotate the pointer clockwise by 180 degrees.
    9. Rotate the pointer counter-clockwise by 90 degrees.
    10. Pause for one second.
    11. Repeat the previous movements, but rotating in the opposite direction.
    12. Turn off the power to the motor.

  6. Demonstrate your working stepper motor control program to a TA.

Exercise #3:

  1. Create a 10-element step command buffer using the MicroC/OS-II message queue system. You will need to create a StepPost( int ) function that takes a signed integer step number an inserts it into the buffer. A counting semaphore will need to be used to prevent buffer overflow. The semaphore should block the StepPost() function whenever the buffer is full. Remember to initialize the semaphore count to 10 when you create the semaphore.

  2. Next create a new task, StepperTask(), that waits for buffer entries and sends the commands to the TPU via the StepMotor() function. Modify your Task1() code so that a series of step requests is entered by a user via the keyboard. The program should allow a user to enter between 1 and 10 step requests before the motor moves. Prompt the user to input integer numbers followed by <enter>, and then press <enter> on a blank line to begin the step sequence. The program should then notify the user when each desired step number is reached, and prompt the user again for more commands when complete.

  3. Demonstrate your working stepper motor control program to a TA.

Bonus Exercise:

Modify the TPU driver program to use the half-step sequence. Control the program from Exercise #3 using a port 9 server task running on the microcomputer. A user is to be able to telnet into port 9 and then type in commands, which are sent to the microcomputer and cause the motor to respond. This exercise must be demonstrated to a TA by no later than the due date for your lab report.

Report Requirements:

Your typed report is to briefly describe your design solution for each of the three laboratory exercises. Be sure to provide neatly formated and well commented code. Provide a brief description of each of the relevant modules and describe how the modules interact with each other. Do not attempt to describe the details of modules within lwIP: you are just responsible for the modules that you designed or modified, and the interfaces with other modules that your code used. Briefly describe how you verified that each of your design solutions was working properly.

Marking Scheme:
5%: Pre-lab work
75%: Solutions to the exercises
10%: Thoroughness of the testing and verification
10%: General appearance and quality of the report
10%: Bonus marks


Last modified November 10, 2003