Simple stepper motor control using the dsPIC30F4011

This example demonstrates simple control of a 28BYJ-48 5V stepper motor using a dsPIC30F4011 microcontroller and an SN754410NE quad half H-bridge driver IC. The program basically just drives the motor forwards at a step frequency of 500 steps per second (the step time is approximately 2 ms).

In this example, the dsPIC uses the internal fast RC oscillator (7.5 MHz) with the 16x PLL multiplier enabled (Fosc = 16 x 7.5 MHz = 120MHz). The dsPIC30F4011 performs one instruction every four clock cycles. Hence the instruction frequency is

Fcy = Fosc / 4 = 30 MHz

In other words, the dsPIC is running at 30 MIPS. Consequently, the instruction cycle is

Tcy = 1/30000000 = 33.33 ns

The stepper has four windings (although strictly speaking I suppose it’s two windings, each with a centre tap). Each of the four digital outputs on the dsPIC (RD0, RD1, RD2, RD3) controls a single winding, via the SN754410NE driver IC.

The following video shows the working system.

This is the example code:

// dsPIC30F4011 example - simple stepper control
// Written by Ted Burke, Last updated 27-12-2015
#include <xc.h>
#include <libpic30.h>
// Configuration settings
_FOSC(CSW_FSCM_OFF & FRC_PLL16); // Fosc=16x7.5MHz, i.e. 30 MIPS
_FWDT(WDT_OFF);                  // Watchdog timer off
_FBORPOR(MCLR_DIS);              // Disable reset pin
int main(void)
    // Declare a variable for the step time
    // so that it can be changed easily
    long step_time = 200000L;
    // Make RD0-3 digital outputs
    TRISD = 0b0000;
    // Cycle through the four windings to make
    // the stepper turn forwards
        LATD = 0b1000; __delay32(step_time);
        LATD = 0b0100; __delay32(step_time);
        LATD = 0b0010; __delay32(step_time);
        LATD = 0b0001; __delay32(step_time);

The build script I used for the above program is

xc16-gcc main.c -mcpu=30F4011 -Wl,--script=p30F4011.gld
if errorlevel 0 xc16-bin2hex a.out
This entry was posted in Uncategorized. Bookmark the permalink.

1 Response to Simple stepper motor control using the dsPIC30F4011

  1. Pingback: Stepper Motor Control | RoboRoBlog

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