Sunday, January 31, 2016

MikroC Pro for PIC stepper motor library


This is a new library for mikroC compiler that allows us to control stepper motors easily. With this library we can control unipolar or bipolar stepper motors.
The following variables must be defined in all projects using stepper library:
 extern sfr sbit Stepper_pin1;                                    // Pin1 line.
 extern sfr sbit Stepper_pin2;
                                    // Pin2 line.          
 extern sfr sbit Stepper_pin3;
                                   // Pin3 line.                       
 extern sfr sbit Stepper_pin4;
                                    // Pin4 line.                   
 extern sfr sbit Stepper_pin1_Direction;
                  // Pin1 direction pin.                       
 extern sfr sbit Stepper_pin2_Direction;
                  // Pin2 direction pin.                    
 extern sfr sbit Stepper_pin3_Direction;
                  // Pin3 direction pin.              
 extern sfr sbit Stepper_pin4_Direction;
                  // Pin4 direction pin.                      
Library routines:
Stepper_Init(unsigned int steps) ;
Initialize stepper motor module where 'steps' is the number of steps in one revolution of the motor.
Stepper_Speed (unsigned int Speed) ;
Sets the motor speed in rotations per minute (RPMs). This function doesn't make the motor turn, just sets the speed at which it will move when you call Stepper_Step().
Stepper_Step(int step) ;
Turns the motor a specific number of steps where 'step' is the number of steps to turn the motor - positive to turn one direction, negative to turn the other direction.
Library download:

Stepper motor

How to add a new library to mikroC:
A new library to microC can be added easily using Package Manager software. See the following video for more details:
 
 
Library Example:
Interfacing unipolar stepper motor with PIC microcontroller:
The following example shows how to use the stepper motor library where PIC16F877A microcontroller is used. The used motor is unipolar stepper motor (5 or 6 wires) and has 20 steps per revolution.
For the unipolar type there are some different circuit schematics can be used. Here the first circuit uses L293 IC which is a dual H-bridge motor driver, and the second circuit use ULN2004 (ULN2003) which is a pack of transistors.
Both circuits have the same mikroC code.

 
 
 MikroC code:

 // Unipolar stepper motor library example
 // Stepper library must be installed
 // Used microcontroler: PIC16F877A @ 8MHz
 // Written by: BENCHEROUDA Okba
 // electronnote@gmail.com
 // http://elecnote.blogspot.com/

 // Stepper motor module connections
 sbit Stepper_pin1 at RB3_bit;
 sbit Stepper_pin2 at RB1_bit;
 sbit Stepper_pin3 at RB2_bit;
 sbit Stepper_pin4 at RB0_bit;
 sbit Stepper_pin1_Direction at TRISB3_bit;
 sbit Stepper_pin2_Direction at TRISB1_bit;
 sbit Stepper_pin3_Direction at TRISB2_bit;
 sbit Stepper_pin4_Direction at TRISB0_bit;
 // End stepper motor module connections

 void main() {
   ADCON1 = 0x07;      // Configure AN pins as digital
   CMCON  = 0X07;      // Disable comparators
   Stepper_Init(20);       // Initialize stepper motor(20 steps/revoltion)
   stepper_speed(50);     // Set stepper motor speed to 50 rpm
   while (1) {                      // Endless loop
     Stepper_Step(20) ;   // Move motor 20 steps in 1st direction (1 rev)
     delay_ms(500);
     Stepper_Step(-20) ;  // Move motor 20 steps in 2nd direction (1 rev)
     delay_ms(500);
     }
   }

Simulation video:

 
Interfacing unipolar stepper motor with PIC microcontroller:
Now, let's see how to interface a bipolar stepper motor (4 wires) with PIC16F877A. For this interfacing L293 half H-bridge driver is needed.
Circuit schematic as shown below:

 
 MikroC code:

 // Bipolar stepper motor library example
 // Stepper library must be installed
 // Used microcontroler: PIC16F877A @ 8MHz
 // Written by: BENCHEROUDA Okba
 // electronnote@gmail.com
 // http://elecnote.blogspot.com/

 // Stepper motor module connections
 sbit Stepper_pin1 at RB0_bit;
 sbit Stepper_pin2 at RB1_bit;
 sbit Stepper_pin3 at RB2_bit;
 sbit Stepper_pin4 at RB3_bit;
 sbit Stepper_pin1_Direction at TRISB0_bit;
 sbit Stepper_pin2_Direction at TRISB1_bit;
 sbit Stepper_pin3_Direction at TRISB2_bit;
 sbit Stepper_pin4_Direction at TRISB3_bit;
 // End stepper motor module connections

 void main() {
   ADCON1 = 0x07;      // Configure AN pins as digital
   CMCON  = 0X07;      // Disable comparators
   Stepper_Init(20);       // Initialize stepper motor(20 steps/revoltion)
   stepper_speed(50);     // Set stepper motor speed to 50 rpm
   while (1) {                      // Endless loop
     Stepper_Step(20) ;   // Move motor 20 steps in 1st direction (1 rev)
     delay_ms(500);
     Stepper_Step(-20) ;  // Move motor 20 steps in 2nd direction (1 rev)
     delay_ms(500);
     }
   }

Simulation video: