/*
   Filename            ZEXSTEPM.C                                        
   Description        Example Program For Step-Motor Drive(Unipolar)    
   Hardware          SLAB-51  + EX-STEPM                                
   Chip                   89V51RD2BN Xtal 11.0592 MHz (Speed x1)             
   Compiler            Keil PK51 v7.10                                    
    Engineer           Anan Phukittikul	                              
    Company          Sila Research Co.,Ltd.                             
*/

#include <reg52.h>
#include <absacc.h>

/*********** Define PORT  & Register **********/

#define  PORTA          XBYTE[0xE000]
#define  PORTB          XBYTE[0xE001]
#define  PORTC          XBYTE[0xE002]
#define  PORTCON        XBYTE[0xE003]  

/*********** STEP TABLE ***************/

unsigned  char code HALF[8] = {0x09,0x01,0x03,0x02,0x06,0x04,0x0c,0x08}; 
			// HALF STEP EXCITATION

/******* BASIC FUNCTION *********/
void dmsec (unsigned int count) {           // mSec Delay
    unsigned int i;                         // for Keil CA51 (Speed x 1)
    while (count) {
        i = 115; while (i>0) i--;
        count--;
    }
}
/******* DIRECTION CONTROL ************/
void CW () {                  // CLOCKWISE
    unsigned char i,j;

    PORTCON = 0x80;          // 8255 mode 0   set output all port
    for(i=0;i<=10;i++){      // step counter
        for (j=0;j<7;j++){  // open table
            PORTA = HALF[j];
            dmsec (100);     // speed control
		}
    }
    PORTA = 0xff;

}
void CCW () {                 // COUNTER CLOCKWISE
    unsigned char i,j;

    PORTCON = 0x80;          // 8255 mode 0   set output all port
    for(i=0;i<=10;i++){      // step counter
        for (j=7;j>0;j--){   // open table
            PORTA = HALF[j];
            dmsec (100);     // speed control
		}
    }
    PORTA = 0xff;
}

/********* MAIN PROGRAM *********/
void main(void){

  while(1){
		CW();
		dmsec(1000);
   		CCW();
    	                dmsec(1000);
  }
}
