Stepper motor example from today’s class

This is the stepper motor example code from today’s class. It just rotates the stepper at a steady speed. The code advances the motor by one step every 5ms, equivalent to 200 steps per second. According to the data sheet of the 28BYJ-48 stepper motor, it has a step angle of 5.625°/64 (the motor has a built in 64:1 gearbox), which is equivalent to 0.087890625 degrees per step. So one complete revolution is 4096 steps. One complete revolution will therefore take approximately 20.5 seconds using the code below. That’s quite slow!

// Stepper motor example for dsPIC
// Written by Ted Burke
// Last updated 1-10-2013

#include <xc.h>
#include <libpic30.h>

// Configuration settings
_FOSC(CSW_FSCM_OFF & FRC_PLL16); // Fosc=16x7.5MHz, Fcy=30MHz
_FWDT(WDT_OFF);                  // Watchdog timer off
_FBORPOR(MCLR_DIS);              // Disable reset pin

int main()
    // Make all port D pins outputs
    TRISD = 0;
    // Now just cycle through the four
    // outputs at 200 steps per second
        LATD = 0b0001;     // RD0 on
        __delay32(150000); // 5ms delay
        LATD = 0b0010;     // RD1 on
        __delay32(150000); // 5ms delay
        LATD = 0b0100;     // RD2 on
        __delay32(150000); // 5ms delay
        LATD = 0b1000;     // RD3 on
        __delay32(150000); // 5ms delay
    return 0;
This entry was posted in Uncategorized. Bookmark the permalink.

1 Response to Stepper motor example from today’s class

  1. Pingback: Clock Prototype with Stepper Motor | robotpic

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s