#Microcontrollers Stepper Motor Interfacing 8051

/*------------------Stepper Motor--------------------*/
/*
=============================================================================
Program in ASM Language to Demonstrate the Stepper Motor in FULL STEP MODE
                  on Micro-51 development Platform
-----------------------------------------------------------------------------
Peripheral:  Unipolar Stepper Motor
-----------------------------------------------------------------------------
Interfacing Pins :
                    P1.0 thru P1.3  connected to CN4.1 thru CN4.4
-----------------------------------------------------------------------------
Jumper Settings:
J1:2-3 J2:2-3 J3:2-3 J4:2-3 J5:1-2 J6:2-3
DIP switches : OFF 
=============================================================================
*/

STEPPERPORT EQU P1

org 0
ljmp start ;jump to start on reset

org 0x13 ;External Interrupt service routine
mov a,40h
cpl a
mov 40h,a
reti

delay:
mov r0,#005h
mov r1,#005h
back:
djnz r1,back
djnz r0,back
ret

;Lookup for clockwise and anti clockwise rotation    //for older stepper motor
;STEP_CLOCKWISE:  
; DB 0x5, 0x9, 0xA, 0x6
;STEP_ANTICLOCKWISE:
; DB 0x6, 0xA, 0x9, 0x5

;Lookup for clockwise and anti clockwise rotation    //for new stepper motor
STEP_CLOCKWISE:  
DB 0x7, 0xE, 0xD, 0xB
STEP_ANTICLOCKWISE:
DB 0xB, 0xD, 0xE, 0x7

;Routine to rotate clockwise
rotateclockwise:
mov dptr,#STEP_CLOCKWISE
mov r4,#04h
loop1:
clr a
movc a,@a+dptr
mov STEPPERPORT,a
lcall delay
mov STEPPERPORT,#0Fh
lcall delay
inc dptr
djnz r4,loop1
ret

;Routine to rotate motor anticlockwise
rotateanticlockwise:
mov dptr,#STEP_ANTICLOCKWISE
mov r4,#04h
loop2:
clr a
movc a,@a+dptr
mov STEPPERPORT,a
lcall delay
mov STEPPERPORT,#0Fh
lcall delay
inc dptr
djnz r4,loop2
ret

/*-----------------------------------------------
Configure INT0 (external interrupt 0) to generate
an interrupt on the falling-edge of /INT0 (P3.2).
Enable the EX0 interrupt and then enable the
global interrupt flag.
-----------------------------------------------*/

start: //main program
setb IT1   // Configure interrupt 1 for falling edge on /INT0 (P3.2)
setb EX1    // Enable EX1 Interrupt
setb EA    // Enable Global Interrupt Flag

mov a,#0h
mov 40h,a //Variable used to identify the direction

continue:
mov a,40h //Get the currebt direction
cjne a,#00h, clockwise
lcall rotateanticlockwise
sjmp continue
clockwise:
lcall rotateclockwise
sjmp continue

END


// ----------------------------------------------------------------
// Programs in Embedded C

/*
  Connect Stepper Motor Card to CN3
  Pins interfaced to stepper: P1.0 to P1.3
*/


#include <REG51.h>

unsigned int STEP_LUT[]={1,2,4,8};



void delay(unsigned int time)
{
int i,j;
for(i=0;i<time;i++)
for(j=0;j<200;j++);
}

void main(void)
{
unsigned int i,j;

while(1)
{
for(j=0;j<50;j++)
{
    for(i=0;i<=3;i++)
{
P1 = (STEP_LUT[i]);
delay(2);
P1 = 0x0F;
delay(2);
}
}
delay(500);

for(j=0;j<50;j++)
{
for(i=0;i<=3;i++)
{
P1 = (STEP_LUT[3-i]);
delay(2);
P1 = 0x0F;
delay(2);
}
}
delay(500);
}

}

Comments