Monday, February 1, 2016

Stepper motor control using PIC16F877A

We made an interfacing of cd-rom bipolar stepper motor with PIC12F1822 microcontroller in this topic:

Stepper motor control with PIC12F1822 microcontroller

In this topic the same motor is used and the microcontroller is changed to PIC16F877A.
Circuit schematic is shown below:

As shown in the circuit schematic there are two push buttons, the first one which is connected to RB0 turns the motor in forward direction and the second button which is connected to RB1 turns the motor in backward direction. For RB0 and RB1 there are no external pull-up resistors because PIC16F877A internal pull-up resistors are enabled.
Interfacing stepper motor with PIC16F877A mikroC code:
With the help of the stepper motor library which we have discussed before, the drive of the stepper motors becomes very simple with a short code. To add this library to your mikroC Pro for PIC compiler visit the following topic:

MikroC Pro for PIC stepper motor library

When you finish with your project either hardware or simulation you should get the result shown in this video where a cd-rom bipolar stepper motor is used:

The mikroC code is posted below:

 /* Cd-rom bipolar stepper motor driver
  * Stepper motor library must be installed
  * Used microcontroller: PIC16F877A @ 12MHz
  * Written by: BENCHEROUDA Okba
  * E-mail:
  * Website:
 // Stepper motor module connections
 sbit Stepper_pin1 at RD0_bit;
 sbit Stepper_pin2 at RD1_bit;
 sbit Stepper_pin3 at RD2_bit;
 sbit Stepper_pin4 at RD3_bit;
 sbit Stepper_pin1_Direction at TRISD0_bit;
 sbit Stepper_pin2_Direction at TRISD1_bit;
 sbit Stepper_pin3_Direction at TRISD2_bit;
 sbit Stepper_pin4_Direction at TRISD3_bit;
 // End stepper motor module connections

 void main() {
   ADCON1 = 0x07;      // Configure AN pins as digital
   CMCON  = 0X07;      // Disable comparators
   OPTION_REG = 0;     // Enable pull-up resistors for port B
   TRISB = 3;
   Stepper_Init(20);       // Initialize stepper motor(20 steps/revoltion)
   stepper_speed(200);     // Set stepper motor speed to 200 rpm
   while (1) {                      // Endless loop
     while(PORTB.F0 == 0)
       Stepper_Step(1) ;   // Move motor 1 step in 1st direction
     while(PORTB.F1 == 0)
       Stepper_Step(-1) ;   // Move motor 1 step in 2nd direction
     PORTD = 0;