Program Stepper motor to do a specific number of steps

Discussion in 'Embedded Systems and Microcontrollers' started by voldeparso, Jun 5, 2016.

  1. voldeparso

    Thread Starter New Member

    Apr 10, 2016
    6
    0
    Hello, I have a little problem in programming a stepper motor with PIC16f877A.
    I'm not so familiar with programming, but I have an stepper motor wich I want to program to do a specific number of steps when an input signal is applied
    to PORTB where I have 5 inputs buttons. In Proteus this code worked well, but when I made the circuit on breadboard, motor is only spinning clockwise.. and it not spinning only 50 steps as I specified in program, it just keep spinning as long as I keep the button for clockwise pushed, when the button for cclockwise is pressed, I got nothing.

    I'm using ULN2003AN as steper motor driver and 28byj-48 motor.

    The surce code is shown below:

    Code (C):
    1.  
    2. int clockwise[]={0b00001100,0b00000110,0b00000011,0b00001001};
    3. int cclockwise[]={0b00001001,0b00000011,0b00000110,0b00001100};
    4. int i;
    5. int y;
    6. int a;
    7. int count;
    8. int k;
    9.  
    10.  
    11. void main() {
    12.        
    13. TRISD = 0b00000000; // PORT D as output port
    14. TRISB = 0b11111111; // PORT B as input port
    15. PORTD = 0b00000000;
    16. PORTB = 0b00110000;
    17. k=0; //condition
    18. do
    19. {
    20. if((PORTB.RB3==1||PORTB.RB1==1) && k==0 ) //buttons input
    21. {
    22.       k=1;
    23.       a=50; // number of steps
    24.       for(y=0;y<a;y++)
    25.       {
    26.         i=y%4;
    27.         PORTD=clockwise[I];
    28.         delay_ms(70); // speed
    29.       }
    30. }
    31. else if((PORTB.RB5==0) && k==0) //buttons input
    32. {
    33.       k=1;
    34.       a=50;
    35.       for(y=0;y<a;y++)
    36.       {
    37.         i=y%4;
    38.         PORTD=clockwise[I];
    39.         delay_ms(40);
    40.       }
    41. }
    42.   else
    43. if((PORTB.RB4==1||PORTB.RB2==1) && k==1 )// buttons input
    44.     {
    45.  
    46.     k=0;
    47.     a=50;
    48.     for(y=0;y<a;y++)
    49.     {
    50.     i=y%4;
    51.     PORTD=cclockwise[I];
    52.     delay_ms(70);
    53.     }
    54.     }
    55.  
    56. }
    57. while(1);
    58. }
    59.  

    Can you please help me?

    Moderators note: used code tags
     
  2. AlbertHall

    Well-Known Member

    Jun 4, 2014
    1,886
    375
    Do the buttons have pull-up resistors?
    The '16F877 has weak pull-ups for port B but they are disabled by default.
     
  3. voldeparso

    Thread Starter New Member

    Apr 10, 2016
    6
    0
    Thank you for your reply, yes they do have pull-up resistors. I checked with multimeter and when I press cclockwise button, the voltage of the output pins of ULN2003 is 5V, all of them. I start to think is something wrong with the way I wired stepper motor to the driver
     
  4. AlbertHall

    Well-Known Member

    Jun 4, 2014
    1,886
    375
    Lines 20 and 43 enable action when the buttons are NOT pressed. If the intended action is that nothing should happen until a button is pressed then change the conditions for RB1, RB2, RB3, and RB4 to '= 0'. Then let us know what happens.
     
Loading...