Wednesday, February 3, 2016

Stepper motor controller using PIC18F4550 example2

This is the second example for interfacing cd-rom stepper motor with PIC18F4550 microcontroller. In the first example stepper motor position is controlled using potentiometer and in this example 2 buttons control its position. Here the same stepper motor is used, to have an idea about this example watch the following video:

Interfacing stepper motor with PIC18F4550 circuit schematic is shown below:


Internal oscillator is used @ 8MHz and MCLR pin function is disabled
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 PIC18F4550 internal pull-ups are enabled.
Interfacing stepper motor with PIC18F4550 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

mikroC code:

 * Stepper motor library must be installed
 * Written by: BENCHEROUDA Okba
 * E-mail:
 * Website:
 * Internal oscillator used at 8MHz
 * Use at your own risk
  // 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() {
   OSCCON =  0X70;    // Set internal oscillator to 8MHz
   ADCON1 = 0x0F;     // Configure AN pins as digital
   CMCON = 7;         // Disable comparators
   TRISB.F0 = 1;      // Configure RB0 as input
   TRISB.F1 = 1;      // Configure RB1 as input
   INTCON2 = 0;       // Enable port B pull-up resistors
   Stepper_Init(20);  // Initialize stepper motor(20 steps/revoltion)
   stepper_speed(200); // Set stepper motor speed to 200 rpm
   while (1) {
     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;