Sunday, January 31, 2016

Stepper motor control with PIC12F1822 microcontroller


Bipolar stepper motor can be easily interfaced with PIC12F1822 microcontroller using L293D half bridge. To do that see the following circuit schematic.
cd rom stepper motor bipolar pic microcontroller mikroc code
Project video:
The following video is for this project where a cd-rom stepper motor is used:
The motor rotates 8 revolutions in the 1st direction and after 500ms it rotates 8 revolutions in the 2nd direction and so on.

    
The following two codes give the same result you have to choose the easiest one for you. The first code uses stepper motor library and the second code don't use this library.
Drive stepper motor using stepper motor mikroC library:
This is the easiest way to drive the stepper motor, just add the stepper motor library to mikroC compiler as shown in the following topic:
MikroC PRO for PIC stepper motor library

The following code is the mikroC code for our interfacing.
Note that the motor used has 20 steps per revolution, change the number 20 according to your motor characteristics.
Stepper motor library must be installed.

 // Stepper motor interfacing with PIC12F1822
 // Written by: BENCHEROUDA Okba
 // electronnote@gmail.com
 // http://elecnote.blogspot.com/
 
 // Stepper motor module connections
 sbit Stepper_pin1 at RA0_bit;
 sbit Stepper_pin2 at RA1_bit;
 sbit Stepper_pin3 at RA2_bit;
 sbit Stepper_pin4 at RA4_bit;
 sbit Stepper_pin1_Direction at TRISA0_bit;
 sbit Stepper_pin2_Direction at TRISA1_bit;
 sbit Stepper_pin3_Direction at TRISA2_bit;
 sbit Stepper_pin4_Direction at TRISA4_bit;
 // End stepper motor module connections

 void main(){
   OSCCON = 0x70; // Internal oscillator @ 8MHz
   ANSELA = 0;    // All pins are configured as digital
   ADCON0 = 0;    // Disable ADC module
   TRISA = 0;     // All port A pins are outputs
   LATA = 0;     // Initial value of port A bits
   Stepper_Init(20);    // Initialize stepper motor(20 steps/revoltion)
   stepper_speed(200);   // Set stepper motor speed to 200 rpm
   while (1) {                      // Endless loop
     Stepper_Step(160) ;  // Turn motor 160 steps in 1st direction (8 revolutions)
     delay_ms(500);
     Stepper_Step(-160) ; // Turn motor 160 steps in 2st direction (8 revolutions)
     delay_ms(500);
     }
   }

Stepper motor interfacing with PIC12F1822 mikroC code:
Second code used to drive the same stepper motor where the previous library not used with the same result.

 // Stepper motor interfacing with PIC12F1822
 // Written by: BENCHEROUDA Okba
 // electronnote@gmail.com
 // http://elecnote.blogspot.com/

 const unsigned int number_of_steps = 20; //Stepper motor(20 steps/revoltion)
 unsigned long Step_delay;
 void Stepper_Speed (unsigned int whatSpeed){
   if(whatspeed < 1)
     whatspeed = 1;
   step_delay = 60 * 1000 / number_of_steps / whatSpeed;
   }
 void Stepper_Step(int steps_to_move){
   bit direction;
   int step_number;
   int steps_left = abs(steps_to_move);
   if (steps_to_move > 0) {direction = 1;}
   if (steps_to_move < 0) {direction = 0;}
   while(steps_left > 0) {
     Vdelay_ms(step_delay);
     if(direction == 1) {
       step_number++;
       if (step_number == number_of_steps)
         step_number = 0;
       }
     else {
       if (step_number == 0)
          step_number = number_of_steps;
       step_number--;
       }
     steps_left--;
     switch ((step_number % 4)) {
       case 0:    // 1010
        LATA.F0 = 1;
        LATA.F1 = 0;
        LATA.F2 = 1;
        LATA.F4 = 0;
        break;
       case 1:    // 0110
        LATA.F0 = 0;
        LATA.F1 = 1;
        LATA.F2 = 1;
        LATA.F4 = 0;
        break;
       case 2:    //0101
        LATA.F0 = 0;
        LATA.F1 = 1;
        LATA.F2 = 0;
        LATA.F4 = 1;
        break;
       case 3:    //1001
        LATA.F0 = 1;
        LATA.F1 = 0;
        LATA.F2 = 0;
        LATA.F4 = 1;
        break;
       }
     }
   }
 void main(){
   OSCCON = 0x70; // Internal oscillator @ 8MHz
   ANSELA = 0;    // All pins are configured as digital
   ADCON0 = 0;    // Disable ADC module
   TRISA = 0;     // All port A pins are outputs
   LATA = 0;     // Initial value of port A bits
   stepper_speed(200);   // Set stepper motor speed to 200 rpm
   while (1) {                      // Endless loop
     Stepper_Step(160) ;  // Turn motor 160 steps in 1st direction (8 revolutions)
     delay_ms(500);
     Stepper_Step(-160) ; // Turn motor 160 steps in 2st direction (8 revolutions)
     delay_ms(500);
     }
   }