Pages

Sunday, May 22, 2016

CD-ROM Bipolar stepper motor drive using PIC18F4550 and CCS PIC C

Stepper motor interfacing with PIC18F4550 microcontroller
Most of cd-rom or dvd-rom drives has a bipolar stepper motor, this motor has two windings and each winding has 2 inputs which means that this type of motor has 4 wires.
CD-ROM bipolar stepper motor windings PIC18F4550 projects
This topic shows circuit schematic and C code for controlling the bipolar stepper motor speed and direction using PIC18F4550 microcontroller.
The stepper motor can be controlled in full step mode or half step mode. The full step mode is a little bit easier than the half step control mode. In this topic the full step control mode is used.
To control the bipolar stepper motor we need two H-bridge circuits and for that L293D motor driver chip is used, this cheap chip can work as a dual H-bridge drivers.
In the full step control mode always both windings are energized according to the following two tables where table1 shows the driving sequence for rotation direction 1:

stepper motor phases drive PIC18F4550 projects
And the following table shows driving sequence for the other rotation direction:
stepper motor phases control PIC18F4550 projects
Interfacing PIC18F4550 with bipolar stepper motor circuit:
The following image shows circuit schematic diagram of this project a potentiometer connected to AN0 channel is used to control the speed as well as the rotation direction of the stepper motor as shown in the video below.

Interfacing PIC18F4550 microcontroller with CD-ROM stepper motor speed direction control L293D CCS PIC C
PIC18F4550 internal oscillator is used (8MHz) and MCLR pin function is disabled.
Interfacing PIC18F4550 with bipolar stepper motor CCS C code:
The following code is the full C code of this project where a potentiometer which is connected to channel AN0 is used to control the rotation direction and the speed of the stepper motor.
The microcontroller reads the analog value from the output of the pot and uses the digital value to control the rotation direction and the speed with ADC resolution of 8 bits.
// Interfacing PIC18F4550 with CD-ROM bipolar stepper motor CCS C code
// http://ccspicc.blogspot.com/
// electronnote@gmail.com

#include <18F4550.h>
#fuses NOMCLR INTRC_IO
#use delay(clock = 8000000)
#use fast_io(D)

unsigned int8 speed_, value;
void main()
{
   setup_oscillator(OSC_8MHZ);              // Set internal oscillator to 8MHz
   setup_adc(ADC_CLOCK_DIV_8);              // Set ADC conversion time to 8Tosc
   setup_adc_ports(AN0);                    // Configure RA0(AN0)as analog
   set_adc_channel(0);                      // Select channel 0 input
   output_d(0);
   set_tris_d(0);
   delay_ms(100);                           // Wait 100ms
   while(TRUE)
   {
      value = read_adc();
      if(value  < 2)value = 1;
      if(value  < 0x80)
      {
         speed_ = value;
         output_d(0b00000110);
         delay_ms(speed_);
         output_d(0b00000101);
         delay_ms(speed_);
         output_d(0b00001001);
         delay_ms(speed_);
         output_d(0b00001010);
         delay_ms(speed_);
      }
      else if(value  > 0x80)
      {
         speed_ = 256 - value;
         output_d(0b00000101);
         delay_ms(speed_);
         output_d(0b00000110);
         delay_ms(speed_);
         output_d(0b00001010);
         delay_ms(speed_);
         output_d(0b00001001);
         delay_ms(speed_);
      }
   }
}

Interfacing PIC18F4550 with bipolar stepper motor video:
The following video shows the project in hardware circuit.