PIC32MX: Driving a Stepper Motor

From Mech
Revision as of 14:53, 7 February 2010 by JarvisSchultz (talk | contribs)
Jump to navigationJump to search

Original Assignment

Do not erase this section!

Your assignment is to create code that reads a voltage from a potentiometer and controls a stepper motor to rotate at a proportional speed. The middle range of the analog input (i.e., 1.65 V) corresponds to zero speed, the minimum is maximum backward speed, and the maximum is maximum forward speed. You will create an interrupt service routine that is called every 500 microseconds. Each time through the routine, you will add 1 to a counter. When that counter (time, in units of 500 microseconds) exceeds the inverse of the desired speed (time per step), you will change the digital outputs controlling the stepper motor to the next position, depending on the direction of motion, and set the counter back to zero.

To do this, you must create a function that takes the analog input, turns it into a speed, then turns it into an inverse of speed that the ISR uses. Clearly the shortest allowable inverse of speed is 1 unit (500 microseconds per step). But can your motor really do this, or does it just vibrate if you try to rotate that fast? Based on your experiments, you should find the maximum speed that the motor actually tracks and set that as a constant. Then 3.3 V and 0 V map to this speed, in opposite directions.

You will use a geared stepper motor that we provide.

Overview

The goal of this page is to explain several pieces of code and a corresponding circuit that were designed to drive a bipolar stepper motor using a PIC32MX460F512L. Useful information about stepper motors in general can be found here and here. The specific motor that we were using is a Portescap 26M048B1B-V27. This 5V bipolar stepper motor has 48 steps per revolution (7.5 degrees per step), a 20:1 gearhead (for an output step angle of 0.375 degrees), and a resistance per phase of 19.80 ± 10% Ω. The datasheet for this motor can be found here.

In this code, the user executes a processing code, and uses it to communicate with the PIC32. More information about processing can be found here. The communication is handled with an RS232 cable and the PIC32’s UART peripheral. The processing code provides the user with the ability to send three functionally different sets of information to the PIC32. The three sets of that can be sent are as follows:

1) The ability to transmit some motor speed to the PIC32, and the PIC32 will drive the motor at that speed until the user changes it by transmitting a new speed. They can transmit a positive or a negative speed to change the direction.

2) The ability to transmit a motor speed and number of steps to the PIC32, and the PIC32 will drive the motor at the desired speed until the desired number of steps has been reached, and then it will stop the motor.

3) The ability to send a command to stop the motor right where it is.

The code uses one of the timer modules on the PIC32 to generate the correct pulse train to drive the motor. The desired speed of the motor (in steps/ second) is input into a function that calculates how often the motor should step, and in-turn what value needs to be stored in the timer’s period register such that the timer interrupt service routine is called every time that the motor steps. This ISR is what handles the setting of the phases.

Circuit

Include a schematic and give any part numbers. A photo of your circuit is OK, but not as a replacement for a schematic.

Code

Where possible, make it a single piece of well-commented cut-and-pastable code, or at least make each function that way, so others can easily copy it. Most comments should be in the code itself; outside the code (on the wiki) should only be explanatory comments that are too cumbersome to include in the code.