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);
//}
}
}
////////////////////////////////////////////////