Speed control of a DC motor using the dsPIC30F4011

This example demonstrates simple speed control of a DC motor using a PWM output pin on the dsPIC30F4011 microcontroller and an SN754410NE quad half H-bridge driver IC. Two digital output pins (RD0 and RD1) control the direction of the motor (via the SN754410NE). PWM1H (pin 37) outputs a PWM signal with 20 ms period and variable duty cycle which is connected to an enable pin on the SN754410NE.

The example code below carries out the following sequence of actions over and over again:

  • Forward slowly (25% duty cycle) for 2 seconds.
  • Forward fast (75% duty cycle) for 2 seconds.
  • Stop for 2 seconds.

The following video shows the working system.

The breadboard circuit is shown in the following photo gallery.

//
// dsPIC30F4011 example - Control speed of DC motor
// Written by Ted Burke, Last updated 29-9-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)
{
    // Make RD0 and RD1 digital outputs
    TRISD = 0b1100;
    
    // Configure PWM
    // PWM period = PTPER * prescale * Tcy = 9470 * 64 * 33.33ns = 20ms
    _PMOD1 = 0;   // PWM channel 1 mode: 0 for complementary, 1 for independent
    _PEN1H = 1;   // PWM1H pin enable: 1 to enable, 0 to disable
    _PTCKPS = 3;  // PWM prescaler setting: 0=1:1, 1=1:4, 2=1:16, 3=1:64
    PTPER = 9470; // Set PWM time base period to 20ms (15-bit value)
    PDC1 = 0;     // 0% duty cycle on channel 1 (16-bit value)
    _PTEN = 1;    // Enable PWM time base to start generating pulses
    
    // Control motor
    while(1)
    {
        // Forward slow for 2 seconds
        PDC1 = 0.25 * 2 * PTPER; // 25% duty cycle
        LATD = 0b01;             // forwards
        __delay32(60000000);

        // Forward fast for 2 seconds
        PDC1 = 0.75 * 2 * PTPER; // 75% duty cycle
        LATD = 0b01;             // forwards
        __delay32(60000000);

        // Stop for 2 seconds
        LATD = 0b00;             // stop
        __delay32(60000000);
    }
}

The build script I used for the program above was as follows.

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

One Response to Speed control of a DC motor using the dsPIC30F4011

  1. Pingback: Controlling a DC Motor | fitzgeraldcathal

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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