Controlling stepper motor speed with PWM (problem in code)

Thread Starter

theumer

Joined Mar 29, 2012
2
I've been writing a code to control the movements and speed of stepper motor but I have been facing some problem in the speed control. Step angle of the motor is 7.5 degrees and the angle movement is from -90 to 0 to +90. But the motor does not seem to operate properly. Here's the code: (location of probable flaw/guidance would be appreciated)

Rich (BB code):
//////////////////   delay.h   ///////////////////

#ifndef    XTAL_FREQ
#define    XTAL_FREQ    4MHZ        /* Crystal frequency in MHz */
#endif

#define    MHZ    *1000L            /* number of kHz in a MHz */
#define    KHZ    *1            /* number of kHz in a kHz */

#if    XTAL_FREQ >= 12MHZ

#define    DelayUs(x)    { unsigned char _dcnt; \
              _dcnt = (x)*((XTAL_FREQ)/(12MHZ)); \
              while(--_dcnt != 0) \
                  continue; }
#else

#define    DelayUs(x)    { unsigned char _dcnt; \
              _dcnt = (x)/((12MHZ)/(XTAL_FREQ))|1; \
              while(--_dcnt != 0) \
                  continue; }
#endif

extern void DelayMs(unsigned char);

///////////////////////////////////// delay.c ///////////////////


#include    "delay.h"

void
DelayMs(unsigned char cnt)
{
#if    XTAL_FREQ <= 2MHZ
    do {
        DelayUs(996);
    } while(--cnt);
#endif

#if    XTAL_FREQ > 2MHZ    
    unsigned char    i;
    do {
        i = 4;
        do {
            DelayUs(250);
        } while(--i);
    } while(--cnt);
#endif
}

/////////////////////////////// test.c /////////////////////

#include <pic.h>
#include "delay.h"

__CONFIG(WDTDIS & XT & WRTEN);

void main () 
{
    TRISB = 0xFF;
    TRISC = 0x00;
    while (1) 
    {
        if(RB0==1)     //////////////// Clockwise Rotation /////////////
        {
            PORTC = 0x66;
            DelayMs(200);
            PORTC = 0xCC;
            DelayMs(200);
            PORTC = 0x99;
            DelayMs(200);
            PORTC = 0x33;
            DelayMs(200);
        }
        if(RB1==1)    ///////////////// Counterclockwise Rotation ///////
        {
            PORTC = 0x66;
            DelayMs(200);
            PORTC = 0x33;
            DelayMs(200);
            PORTC = 0x99;
            DelayMs(200);
            PORTC = 0xCC;
            DelayMs(200);
        }
        //if(RB0==0 & RB1==0)
        //{
        //    PORTC = 0x00;
        //    DelayMs(200);
        //}
    }
}

////////////////////////////////////////////////
 

t06afre

Joined May 11, 2009
5,934
You do know that Hi-Tech C have excellent built in delay functions __DELAY_MS and __DELAY_US. I would recommend using these functions. Remember to use the prior definition of preprocessor symbol _XTAL_FREQ in Hz like this.
Rich (BB code):
#define XTAL_FREQ 4000000
 

shortbus

Joined Sep 30, 2009
10,045
Don't know anything about 'code' but, step motors don't use PWM to rotate. The drivers only need a 'pulse'. Depending on the step setting(half step -etc) each pulse moves the shaft of the motor. Pulse length- PWM, has nothing to do with it.

Example - for your 7.5° motor in full step mode - 360° / 7.5° = 48 pulses for one revolution. To change speed, you just change frequency of pulses. Most drivers step on the positive going pulse.
 
Top