Unipolar stepper motor control


Unipolar stepper motor control using DIO-1

Toolkit:AVR Development System

Location:/bipom/devtools/WinAVR/minimaxavrc/Examples/dio1

Code Example


void Step( UBYTE direction );
ERRCODE Dio1WriteByte( UBYTE cmd, UBYTE value );


//  Phase Status Table per each  stepper motor  step


		        		 //  A    B     C     D   phases

UBYTE Table[] = {
				0x80,	 // ON   off   off   off - 1000 0000

				0xC0,	 // ON	 ON	   off	 off - 1100 0000

				0x40, 	 // off  ON    off   off - 0100 0000

				0x60,	 // off	 ON	   ON	 off - 0110 0000

				0x20, 	 // off  off   ON    off - 0010 0000

				0x30,	 // off	 off   ON	 ON  - 0011 0000

				0x10,	 // off  off   off   ON  - 0001 0000

				0x90	 // ON	 off   off	 ON  - 1001 0000

				};

static UBYTE index;


int main (void)
{
	UINT i;
	
	// delay 500 milliseconds after power up

	_delay_ms(500);        

	// Initialize the serial port 0 to 19200 baud

	uart1Init(19200);

	// Initialize I2C Bus

    i2cInit();	

	index = 0;
			
	// Loop forever 

	
	for(;;)
	{
		// Move 200 steps in forward direction

		for( i=0; i<200; i++ )
		{			
			Step(DIRECTION_FORWARD);
		
			// This delay determines the motor speed	

			_delay_us(100);        
		}
				
		// Move 200 steps in reverse direction

		for( i=0; i<200; i++ )
		{			
			Step(DIRECTION_REVERSE);
		
			// This delay determines the motor speed	

			_delay_us(100);        
		}
	}    	
}   	


void Step( UBYTE direction )
{
	ERRCODE ec = SUCCESS;
	
	if( direction == DIRECTION_FORWARD )
	{
		index++;
		
		if( index > 7 )
			index  = 0;	
	}
	else
	{
		if( index > 0 )
			index--;
		
		if( index == 0 )
			index  = 7;	
	}
	
	
	ec = Dio1WriteByte( DIO_PORTB_REGISTER, Table[index] );
	
	if( ec != SUCCESS )
	{
	    uart1Printf( "\n\rError writing to DIO-1 board, error code=%d", ec );
	}
}


ERRCODE Dio1WriteByte( UBYTE cmd, UBYTE value )
{
	ERRCODE ec = SUCCESS;

    ec = i2cStart();

    if( ec != SUCCESS )
        return ec;

    ec = i2cTransmit( I2C_DEVICE_DIO1_WRITE );

    if( ec != SUCCESS )
        goto func_end;

    ec = i2cTransmit( cmd );

    if( ec != SUCCESS )
        goto func_end;

    ec = i2cTransmit( value );

    if( ec != SUCCESS )
        goto func_end;

func_end:

    i2cStop();
    
    return ec;
}