Software Section

                                                                                                                           <Back to Topics

Code to Control a Stepper Motor

The explanation on functionality of a stepper motor, and relevant circuit diagrams are provided in the electronics section of this web site, please go through this link --> Controlling  a stepper motor.

The code provided below is written for microcontroller atmega8535  which has a crystal oscillator of 8Mhz, this means a microcontroller having  a lower crystal may result in slower execution of program (stepper motor runs slower than what it does with 8Mhz crystal).

 

Example program to control stepper motor( code compiled with CodeVision)

/*****************************************************
This program was produced by the
CodeWizardAVR V2.04.1 Evaluation
Automatic Program Generator
© Copyright 1998-2009 Pavel Haiduc, HP InfoTech s.r.l.
http://www.hpinfotech.com

Project :
Version :
Date   : 11/20/2005
Author : Freeware, for evaluation and non-commercial use only
Company :
Comments:


Chip type              : ATmega8535
Program type           : Application
AVR Core Clock frequency: 8.000000 MHz
Memory model           : Small
External RAM size      : 0
Data Stack size        : 128
*****************************************************/
 

#include <mega8535.h>

void delay_100millisecond(void);

// Declare your global variables here

void main(void)
{
// Declare your local variables here

// Input/Output Ports initialization
// Port A initialization
// Func7=In Func6=In Func5=In Func4=In Func3=Out Func2=Out Func1=Out Func0=Out
// State7=T State6=T State5=T State4=T State3=0 State2=0 State1=0 State0=0
PORTA=0x00;
DDRA=0x0F;

// Port B initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTB=0x00;
DDRB=0x00;

// Port C initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTC=0x00;
DDRC=0x00;

// Port D initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTD=0x00;
DDRD=0x00;

// Timer/Counter 0 initialization
// Clock source: System Clock
// Clock value: Timer 0 Stopped
// Mode: Normal top=FFh
// OC0 output: Disconnected
TCCR0=0x00;
TCNT0=0x00;
OCR0=0x00;

// Timer/Counter 1 initialization
// Clock source: System Clock
// Clock value: Timer 1 Stopped
// Mode: Normal top=FFFFh
// OC1A output: Discon.
// OC1B output: Discon.
// Noise Canceler: Off
// Input Capture on Falling Edge
// Timer 1 Overflow Interrupt: Off
// Input Capture Interrupt: Off
// Compare A Match Interrupt: Off
// Compare B Match Interrupt: Off
TCCR1A=0x00;
TCCR1B=0x00;
TCNT1H=0x00;
TCNT1L=0x00;
ICR1H=0x00;
ICR1L=0x00;
OCR1AH=0x00;
OCR1AL=0x00;
OCR1BH=0x00;
OCR1BL=0x00;

// Timer/Counter 2 initialization
// Clock source: System Clock
// Clock value: Timer 2 Stopped
// Mode: Normal top=FFh
// OC2 output: Disconnected
ASSR=0x00;
TCCR2=0x00;
TCNT2=0x00;
OCR2=0x00;

// External Interrupt(s) initialization
// INT0: Off
// INT1: Off
// INT2: Off
MCUCR=0x00;
MCUCSR=0x00;

// Timer(s)/Counter(s) Interrupt(s) initialization
TIMSK=0x00;

// Analog Comparator initialization
// Analog Comparator: Off
// Analog Comparator Input Capture by Timer/Counter 1: Off
ACSR=0x80;
SFIOR=0x00;

while (1)

    {

    // Place your code here

    /* commutate the stepper motor */

    /* fire the first phase by setting the PORT A0 pin to high */

    PORTA=0x01;

    /* 100 milliseconds delay */

    delay_100millisecond();

    /* fire the Second phase by setting the PORT A0 pin to high */

    PORTA=0x02;

    /* 100 milliseconds delay */

    delay_100millisecond();

    /* fire the Third phase by setting the PORT A0 pin to high */

    PORTA=0x04;

    /* 100 milliseconds delay */

    delay_100millisecond();

    /* fire the forth phase by setting the PORT A0 pin to high */

    PORTA=0x08;

    /* 100 milliseconds delay */

    delay_100millisecond();

    /* continue with the same sequence in the infinite loop

    and stepper  motor rotates continuously */

    }

}

 

void delay_100millisecond(void)

{

int i =0;

int j = 0;

for (j=0;j<=1000;j++)

    {

    /* millisecond delay

    The delay is measured using 8 Mhz crystal */

      for (i=0;i<=400;i++)

      {

      /* delay */ 

      }

    }

}

 

/* End of Program */

 

Result

Stepper motor runs continuously as long as the microcontroller is powered ON.

The circuit diagram is explained in the Electronics section, please go through the link Drive a stepper motor

Best Viewed in Internet Explorer

With other browsers you may experience improper view

List of Topics --> Software

Simple Program to Blink an LED (using embedded C language)

Code to Control a Stepper Motor

Code to Detect Obstacle using IR(Infrared) Sensor

Articles on C

Data types (under construction)

 

List of Topics --> Electronics

Controlling  a Stepper Motor

Drive a Stepper Motor (using IC STA401A)

Detecting obstacle with IR (Infrared) Sensor

Use Motor as Dynamo, Motor as a Brake, Dynamometer Test

 

[Home] [Robotics] [Electronics] [Software] [Contact Me]