Wednesday, February 3, 2016

Stepper motor controller using PIC18F4550 example3


This is the third example for interfacing cd-rom stepper motor with PIC18F4550. In this example two buttons controls the direction of the stepper motor and a potentiometer controls the speed of this motor.as shown in the following video:


Circuit schematic is shown in the image below:

cd rom stepper motor control pic18f4550 mikroc code 

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
The potentiometer connected to RA0 is used to control motor speed.
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

In the software a minimum speed is 50 rpm (when potentiometer gives 0V to AN0) and a maximum speed about 561 (when potentiometer gives 5V to AN0).
mikroC code:

/*
 * Stepper motor control using PIC18F4550
 * There are two buttons used to turn the motor
   forward or backward, a potentiometer used to
   control the speed.
 * Minimum speed 50rpm when analog reading (i) is 0,
   maximum speed 561rpm when analog reading (i) is 1023
 * 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 ;
 void main() {
  OSCCON =  0X70;    // Set internal oscillator to 8MHz
  ADCON1 = 0x0E;     // Configure AN0 pin as analog
  CMCON = 7;         // Disable comparators
  TRISB.F0 = 1;      // Configure RB0 as input
  TRISB.F1 = 1;      // Configure RB1 as input
  RBPU_bit = 0;      // Enable port B pull-up resistors
  Stepper_Init(20);  // Initialize stepper motor(20 steps/revoltion)
  while (1) {                   // Endless loop
   while(PORTB.F0 == 0){
    i = ADC_Read(0);
    stepper_speed(50 + i/2); // Max speed 561rpm and min spedd 50rpm
    Stepper_Step(1) ; // Move motor 1 step in 1st direction
    }
   while(PORTB.F1 == 0){
    i = ADC_Read(0);
    stepper_speed(50 + i/2); // Max speed 561rpm and min spedd 50rpm
    Stepper_Step(-1) ; // Move motor 1 step in 2nd direction
    }
   PORTD = 0;
   }
  }