Saturday, October 22, 2016

Bipolar stepper motor control using PIC12F1822 and L293D


In this blog there are several posts talking about bipolar stepper motor and how to drive it. The bipolar stepper motor has two windings and 4 wires and to drive this windings 2 H-bridge circuits are needed. L293D motor driver chip is a good choice for driving this type of motor because it's low cost and easy to use.
This post shows how to drive a cd-rom bipolar stepper motor using PIC12F1822 microcontroller and L293D.
To understand how this motor works read the following post:
Bipolar stepper motor control with PIC16F877A microcontroller
Bipolar stepper motor control using PIC12F1822 and L293D circuit:
CD-ROM Bipolar stepper motor control using PIC12F1822 and L293D
The two push buttons for moving the motor in direction 1 or direction 2.
PIC12F1822 internal oscillator is used and internal pull-ups are enabled for the 2 inputs.
The stepper motor voltage is 5V which is the same as the L293D chip VS and VSS.
Bipolar stepper motor control using PIC12F1822 and L293D CCS C code:
In this project the speed of the stepper motor is fixed by a variable called speed_delay = 10. If that number changed the speed of the motor will also change, if you increase that number the motor speed will decrease and if you decrease it the speed will increase.
// Bipolar stepper Motor control using PIC12F1822 and L293D CCS PIC C code
// http://ccspicc.blogspot.com/
// electronnote@gmail.com
// Use at your own risk

#include <12F1822.h>
#fuses NOMCLR INTRC_IO PLL_SW
#use delay(clock=32000000)
#use fast_io(A)

unsigned int8 step_number = 0, speed_delay = 10;
void stepper(int8 step){
  switch(step){
    case 0:
      output_a(0b010010);
    break;
    case 1:
      output_a(0b010001);
    break;
    case 2:
      output_a(0b100001);
    break;
    case 3:
      output_a(0b100010);
    break;
  }
}
void main() {
  setup_oscillator(OSC_8MHZ | OSC_PLL_ON);            // Set internal oscillator to 32MHz (8MHz and PLL)
  output_a(0);
  set_tris_a(0x0C);                                   // Configure RA2 & RA3 as inputs 
  port_a_pullups(0x0C);                               // Enable internal pull-ups for pins RA2 & RA3
  while(TRUE){
    output_a(0);
    while(!input(PIN_A2)){                            // If RA2 button pressed
      step_number++;
      if(step_number > 3) 
        step_number = 0;
      stepper(step_number);
      delay_ms(speed_delay);
    }
    while(!input(PIN_A3)){                            // If RA3 button pressed
      if(step_number < 1) 
        step_number = 4;
      step_number--;
      stepper(step_number);
      delay_ms(speed_delay);
    }
  }
}
Bipolar stepper motor control using PIC12F1822 and L293D video:
Project hardware video....