Sunday, November 20, 2011

8051 interfacing with stepper motor

8051 Interfacing with stepper motor
fig shows interfacing connection
-------------------------------
; 4 PINS OF STEPPER MOTOR CONTROLLED BY 4 BITS OF 8051 P1.0 - P1.3
; IT IS INTERFACED USING DARLINGTON ARRAYS SUCH AS ULN2003 AS
; 8051 LACKS CURRENT TO RUN THE MOTOR. SO ULN2003 IS USED FOR ENERGIZING STATOR.
; PROGRAM BELOW SHOWS JUST ROTATING THE MOTOR STEP WISE
; BUT STEP WIDTH IS UNKNOWN

MOV A, #66H    ; LOAD THE STEP SEQUENCE

BACK :
MOV P1, A    ; LOAD SEQUENCE TO PORT
RR A        ; CHANGE SEQUENCE ROTATE CLOCKWISE
ACALL DELAY    : WAIT FOR IT
SJMP BACK    ; NOW KEEP GOING

DELAY :
MOV R2, #100

H1 :
MOV R3, #255

H2 :
DJNZ R3, H2
DJNZ R2, H1
RET

END

; THIS OTHER PROGRAM CODE HOW IN SAME INTERFACING TO ROTATE A STEPPER MOTOR
; 64 DEGREES IN CLOCKWISE DIRECTION USING 4 STEP SEQUENCE
; IT TAKES SOME CALCULATIONS
; CONSIDER A MOTOR WITH STEP ANGLE 2 DEGREE
; SO STEPS PER REVOLUTION = 180
; NO OF ROTOR TEETH = 45
; MOVEMENT PER 4 STEP SEQUENCE IS 8 DEGREES
; FOR 64 DEGREE MOVEMENT WE NEED TO SEND 8 CONSECUTIVE 4 STEP SEQUENCE
; THAT IS IT WILL COVER 32 STEPS

ORG 0000H
MOV A, #66H
MOV R1, #32H    ; 32 STEPS TO BE TAKEN

BACK :
RR A        ; ROTATE CLOCKWISE
MOV P1, A    ;
ACALL DELAY    ;
DJNZ R1, BACK    ;
END
-----------------

Now few question arises
q1) why did we took 66h as step value??
Ans))

A normal 4 step sequence is like below.
Step #
Winding A
Winding B
Winding C
Winding D
1
1
0
0
1
2
1
1
0
0
3
0
1
1
0
4
0
0
1
1
Going from step 1 to 4 we rotate motor clockwise
Going from step 4 to 1 we rotate motor counter clockwise

The stepper motor discussed here has total 6 leads. 4 leads representing 4 windings and 2 commons for centre tapped leads. Here we have used 2 phase 4 step sequence. It must also be noted we can start from any step.  Be it step 1,2,3 or 4. But we must continue in proper order for proper rotation.
In programs on blog I have mostly begun from step 3.
 other step sequences are as below

Half Step 8 Step Sequence
Step #
Winding A
Winding B
Winding C
Winding D
1
1
0
0
1
2
1
0
0
0
3
1
1
0
0
4
0
1
0
0
5
0
1
1
0
6
0
0
1
0
7
0
0
1
1
8
0
0
0
1



Wave drive 4 step sequence

Step #
Winding A
Winding B
Winding C
Winding D
1
1
0
0
0
2
0
1
0
0
3
0
0
1
0
4
0
0
0
1





 q2) how much movement is associated with a step angle??

Step angle
Steps per revolution
.72
500
1.8
200
2
180
2.5
144
5
72
7.5
48
15
24


 ------------------

Edit - 1: Changed the Line DJNZ R0 to R1 as it was a mistake in typing.

66 comments:

  1. Replies
    1. i dont undrstnd the step angle??

      Delete
    2. Step angle is the angle covered in a single step of stepper motor.

      So if we say step angle of a stepper motor is 0.72 it means one step of stepper motor will cover 0.72 degree.
      So if a stepper motor needs to rotate a full circle it requires to rotate 0.72 angle 500 times.
      You need to give 500 pulses to a stepper motor to make it cover one complete cycle.

      Delete
  2. i want to rotate stepper motor only one step the how can i write program for that please give some guidence

    ReplyDelete
    Replies
    1. Lets looks at the same program given above.

      ; THIS OTHER PROGRAM CODE HOW IN SAME INTERFACING TO ROTATE A STEPPER MOTOR
      ; 64 DEGREES IN CLOCKWISE DIRECTION USING 4 STEP SEQUENCE
      ; IT TAKES SOME CALCULATIONS
      ; CONSIDER A MOTOR WITH STEP ANGLE 2 DEGREE
      ; SO STEPS PER REVOLUTION = 180
      ; NO OF ROTOR TEETH = 45
      ; MOVEMENT PER 4 STEP SEQUENCE IS 8 DEGREES
      ; FOR 64 DEGREE MOVEMENT WE NEED TO SEND 8 CONSECUTIVE 4 STEP SEQUENCE
      ; THAT IS IT WILL COVER 32 STEPS

      ; Now we need to cover 1 step only that is only 2 degrees so here is the program for it.
      ORG 0000H
      MOV A, #66H
      MOV R1, #01H ; 01 STEPS TO BE TAKEN

      BACK :
      RR A ; ROTATE CLOCKWISE
      MOV P1, A ;
      ACALL DELAY ;
      DJNZ R0, BACK ;
      END
      -----------------

      Delete
    2. i have one program on that one diculty is occure so can u handle that problem plzzz.....

      Delete
  3. i need a assembly language code for stepper motor -120 degree clock wise & 35 degree anti clock wise...

    ReplyDelete
  4. This comment has been removed by the author.

    ReplyDelete
  5. my qstn is here why we are taking MOV R2, #100 and MOV R3, #255 for DJNZ instrucion Simultaneously whats the need? one more is why the MOV A, #66h or 48h used for step sequence what exact statement means

    ReplyDelete
  6. is it you have facebook ,easy to me to contact you

    ReplyDelete
  7. Its not CLR how this is decided that we have to rotate right for clockwise direction

    ReplyDelete
  8. Its not CLR how this is decided that we have to rotate right for clockwise direction

    ReplyDelete
  9. Plzz tell me the "assembly language program for 8051 to rotate stepper motor in clockwise and anticlockwise direction with step angle 1.8 degree"

    ReplyDelete
    Replies
    1. Hi Saurabh following is the program that you can use to rotate clockwise or anticlockwise with step angle 1.8 degree.

      ; 120 DEGREES IN CLOCKWISE DIRECTION USING 4 STEP SEQUENCE
      ; CONSIDER A MOTOR AS IN YOUR CASE WITH STEP ANGLE 1.8 DEGREE
      ; SO STEPS PER REVOLUTION = 200
      ; NO OF ROTOR TEETH = 45
      ; MOVEMENT PER 4 STEP SEQUENCE IS 4*1.8 = 7.2 DEGREES
      ; FOR 120 DEGREE MOVEMENT WE NEED TO SEND 16 CONSECUTIVE 4 STEP SEQUENCE
      ; THAT IS IT WILL COVER 66 STEPS
      ORG 0000H
      MOV A, #66H
      MOV R1, #66H
      ; Rest of the program remains the same.

      Delete
    2. Write an Alp to run the stepper motor continuosly...

      Delete
  10. sir wht if we wnt to do it by using bipolar motor..any major changes required?

    ReplyDelete
    Replies
    1. Yes there will be major changes. This program has been made kept in mind that the poles in the motor are all the same. If you are using bipolar then the whole pulsing sequence will change accordingly.

      Delete
  11. why 66 to port 1 why we use it? what is it?

    ReplyDelete
  12. if possible could u plz give me the explanation regarding using of 66

    ReplyDelete
  13. Replies
    1. It is DJNZ R1 not R0 the value has been stored in R1 not in R0.

      Delete
  14. Replies
    1. It is DJNZ R1 not R0 the value has been stored in R1 not in R0.

      Delete
  15. Write an ALp to rotate a four coil stepper motor by 10 steps in forward direction and 2 steps in backward direction

    ReplyDelete
    Replies
    1. Try the following section of code.
      You can test out the following section of code...
      ORG 0000H
      MOV A, #66H
      MOV R1, #10H ; 10 STEPS TO BE TAKEN

      BACK :
      RR A ; ROTATE CLOCKWISE
      MOV P1, A ;
      ACALL DELAY ;
      DJNZ R1, BACK ;

      MOV R1, #02H ; 06 STEPS TO BE TAKEN

      BACK :
      RL A ; ROTATE ANTICLOCKWISE
      MOV P1, A ;
      ACALL DELAY ;
      DJNZ R1, BACK ;

      END

      Delete
  16. kindly-

    write an ALP to rotate it 75 degree
    kindly revert asap

    ReplyDelete
  17. Write an ALp to rotate a four coil stepper motor by 16 steps in backward direction and then 2 steps in forward direction

    ReplyDelete
  18. tomorrow is my exam ..kindly help me out

    ReplyDelete
  19. Kindly tell me how to programm stepper motor for analog clock....PlZ

    ReplyDelete
    Replies
    1. There are multiple ways of doing this. One of the easiest way is to setup a counter of 1 sec in the microcontroller and then give fixed pulse to stepper motor to just make it go one step. So you need to keep the motor energized then give one pulse to make it move one step. This will give a second clock. Further you need to see and make sure that it only takes 60 steps so based on the motor you purchase have equivalent step angle.
      If you cant find the motor with equivalent step angle then you need to have a check logic that will make motor rotate according for the complete cycle.

      Delete
  20. Kindly tell me how to programm stepper motor for analog clock....PlZ

    ReplyDelete
  21. This comment has been removed by a blog administrator.

    ReplyDelete
  22. Kindly tell me how to programm stepper motor for analog clock....PlZ

    ReplyDelete
  23. We have not loaded register ro and hw come we are decrementing it????? I think it should be DJNZR1

    ReplyDelete
    Replies
    1. Yes it is DJNZ R1. Thanks for noticing... I have updated the post with the same..

      Delete
  24. What will be a program to drive motor in clockwise 6 rotations and anti clockwise 6 rotations consecutively??

    ReplyDelete
    Replies
    1. You can test out the following section of code...
      ORG 0000H
      MOV A, #66H
      MOV R1, #06H ; 06 STEPS TO BE TAKEN

      BACK :
      RR A ; ROTATE CLOCKWISE
      MOV P1, A ;
      ACALL DELAY ;
      DJNZ R1, BACK ;

      MOV R1, #06H ; 06 STEPS TO BE TAKEN

      BACK :
      RL A ; ROTATE ANTICLOCKWISE
      MOV P1, A ;
      ACALL DELAY ;
      DJNZ R1, BACK ;

      END

      Delete
  25. i want to know program to drive stepper motor in clockwise direction for 6 rotation and anticlockwise direction 6 locations continuously

    ReplyDelete
    Replies
    1. You can test out the following section of code...
      ORG 0000H
      MOV A, #66H
      MOV R1, #06H ; 06 STEPS TO BE TAKEN

      BACK :
      RR A ; ROTATE CLOCKWISE
      MOV P1, A ;
      ACALL DELAY ;
      DJNZ R1, BACK ;

      MOV R1, #06H ; 06 STEPS TO BE TAKEN

      BACK :
      RL A ; ROTATE ANTICLOCKWISE
      MOV P1, A ;
      ACALL DELAY ;
      DJNZ R1, BACK ;

      END

      Delete
  26. Sir write program in timer 1 mode to generate delay of 5 ms

    ReplyDelete
  27. Sir I want a stepper motor program for sun tracking solar panel with delay

    ReplyDelete
  28. Explain the stepper motor interfacing to ARM microcontroller LPC-1768. Write an embedded C program to rotate the motor in clockwise/anticlockwise direction with 8 different speeds using a 4x4 matrix keyboard. i.e. First two rows for clockwise and the next two rows for the anti clockwise rotation.
    Please, can anyone tell me the code for this ,that runs on keil.4

    ReplyDelete
  29. Explain the stepper motor interfacing to ARM microcontroller LPC-1768. Write an embedded C program to rotate the motor in clockwise/anticlockwise direction with 8 different speeds using a 4x4 matrix keyboard. i.e. First two rows for clockwise and the next two rows for the anti clockwise rotation.
    Please, can anyone tell me the code for this ,that runs on keil.4

    ReplyDelete
  30. i wanna rotate stepper motor clockwise and counterclock wise in one program can it be done?

    ReplyDelete
  31. ULN2003 is used for sinking the current, and not to energizing the coil as it is already powered up by the voltage source connected to the central tapped winding. Correct it please!!!

    ReplyDelete
    Replies
    1. ULN2003 is a darlington pair used to drive loads that cannot be driven directly via outputs of a microcontroller. There is nothing to be corrected here.

      Delete
  32. BTW all of your code and content is copied from the book The 8051 Microcontroller and Embedded Systems by Ali Mazidi.

    ReplyDelete
  33. Why 66H and not 6H 3rd step is 0110.

    ReplyDelete
  34. HELP ME

    Choose one type of stepper motor .
    i. Design a small apparatus to rotate the stepper motor clockwise, anticlockwise and to measure the RPM after pressing correspondent switches and draw the connection diagram of your apparatus.
    ii. Explain about the interfacing devices that you have used in your design.
    iii. Write an ALP of your desin

    ReplyDelete
  35. Write an Alp to run stepper motor continuosly.update the answer as fast as possible....

    ReplyDelete
  36. Write an ALP for half stepping of stepper motor in clockwise and anticlock direction considering water sensor input.please sir give early reply.

    ReplyDelete
  37. For rotating stepper motor in anticlockwise direction through 180 degrees if step angle is 1.8 degrees what will be an ALP?

    ReplyDelete
    Replies
    1. Only count for 4 steps will change and RL a instruction is used instead of RR A for anticlockwise direction


      MOV A, #66H
      MOV R0,#25
      UP : MOV P1,A
      AGAIN : RL A
      ACALL DELAY
      DJNZ R0,AGAIN
      SMJP UP
      remain same

      Delete
  38. Alp for clockwise rotation at 200rpm please? ????

    ReplyDelete
  39. Please,
    Writer anALP to run a stepper motor with step angle 1.8 interfaced to 8051 alternatively in slow & high speeds in anti clock direction

    ReplyDelete
  40. Writer anALP to run a stepper motor with step angle 1.8 interfaced to 8051 alternatively in slow & high speeds in anti clock direction?
    Please give me the answer. Please...

    ReplyDelete
  41. Rotate stepper motor 90 degree in clockwise direction, stop there for 1 sec and again rotate 45 degree it the opposite direction. Consider Half Drive Mode.

    ReplyDelete
  42. hi
    colud you tell me how to Contact betwen the 8051 microcontrolar and matlab?

    ReplyDelete
  43. hello sir,
    i want to draw a 70 mm diameter circle using two stepper motor with the help of AT89C51 controller but i unable to write code

    and i use port 2 for 1 motor and port 3 for other motor

    sir can you code for me in assembly or c..??

    ReplyDelete
  44. Design a system to rotate the stepper motor five degree in clockwise and twenty five degree anticlockwise for 10 times and stop
    Please help me urgently solve this

    ReplyDelete
  45. develop an assembly language program to rotate stepper motor by 90° in anticlockwise direction assume 1.8 step angle.

    ReplyDelete
  46. develop an assembly language program to rotate stepper motor by 90° in anticlockwise direction assume 1.8 step angle

    ReplyDelete