Tuesday, February 2, 2016

Stepper motor controller using PIC18F4550 example1

This is an example shows how to control a cd rom bipolar stepper motor using PIC18F4550 microcontroller.
The interfaced stepper motor has 20 steps per revolution and maximum speed about 600 rpm, it is just cd-rom stepper motor.
In this topic this stepper motor is controlled with potentiometer, the potentiometer controls the position of the stepper motor as shown in the following video:

 Circuit schematic is shown below:


Internal oscillator is used @ 8MHz and MCLR pin function is disabled
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:

/*
 * PIC18F4550 and cd-rom bipolar stepper motor example
 * First motor position is 0 and last position is 160
 * Reading of analog value at RA0 is converted to position
   which means when analog value is 0 the motor at position 0
   and when analog value is 1023 the motor is at position 160
   or we can say the motor moved 160 steps.
 * Stepper motor library must be installed
 * Written by: BENCHEROUDA Okba
 * E-mail: electronnote@gmail.com
 * Website: http://elecnote.blogspot.com/
 * 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
 
 unsigned int i, k;
 short step0, step;
 void main() {
   OSCCON =  0X70;       // Set internal oscillator to 8MHz
   ADCON1 = 0x0E;        // Configure AN0 pin only as analog
   TRISA.F0 = 1;
   Stepper_Init(20);       // Initialize stepper motor(20 steps/revoltion)
   stepper_speed(500);     // Set stepper motor speed to 500 rpm
   while(1){
    i = ADC_Read(0);
    k = (i + 1)/25 ;  // Maximum value when i=1023 yields k=40 (integer number)
    step = k - step0;       // Steps to move = new steps - previous steps
    Stepper_Step((4 * step)) ;  // For maximum position we get 4*40 = 160
    step0 = k;        // Store old value
    PORTD = 0;
    delay_ms(10);
    }
   }