Advertisement
Guest User

Untitled

a guest
May 20th, 2019
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.40 KB | None | 0 0
  1. ; File STEP508.ASM
  2. ; ... for PIC12f508 microcontroller
  3. ; Program to use PIC as a step and direction controller for a unipolar
  4. ; step motor. Step and direction pins are GPIO-5, GPIO-3; GPIO_0, GPIO_1, GPIO_2, GPIO_4, are ; the windings; in order (driven by NPN small sig transistors or MOSFETS)
  5. ; Steps on negative going edge of step pulse.
  6.  
  7. ; CPU configuration
  8. ; (It's a 12f508, Internal RC oscillator,
  9. ; watchdog timer off, power-up timer on)
  10. LIST P=12f508
  11. processor 12f508
  12. include <p12f508.inc>
  13. __CONFIG _OSC_IntRC & _WDT_OFF & _CP_OFF & _MCLRE_OFF
  14.  
  15. ; Declare variables
  16.  
  17. pattA equ H'0D' ;Current step pattern number (0-7) for axis A
  18. lastA equ H'0E' ;Last state of step pin on axis A (1 is high, 0 is low)
  19. inport equ H'11' ;Value of port A when read (stored for later access)
  20. temp equ H'12'
  21.  
  22. #DEFINE STEP inport,5 ; Step pulse input
  23. #DEFINE DIR inport,3 ; Direction Input
  24.  
  25. OPMASK EQU B'11000000'
  26. IOMASK EQU B'00101000' ; all bits output (except GP3 and GP5)
  27. ; GP0 , GP1 , GP2 , GP4 controls The stepper Coils
  28. ; GP3 controls direction
  29. ; GP5 Controls Step Pulses
  30.  
  31.  
  32.  
  33. ORG 0
  34.  
  35. ; start of main code
  36. ;***************************************************
  37. ;
  38. ; START OF PIC 12f508 CODE FOR STEP;
  39. ;
  40. ;***************************************************
  41. ;
  42.  
  43. ;------------------------------------------
  44. ;****Power on reset startpoint
  45. ;------------------------------------------
  46.  
  47. ;***Initialization of program
  48. MOVWF OSCCAL
  49.  
  50. MOVLW OPMASK
  51. OPTION
  52.  
  53. ; Set GPIO for input & output
  54.  
  55. MOVLW IOMASK
  56. TRIS GPIO
  57.  
  58.  
  59.  
  60.  
  61. ;Clear port and zero motors
  62.  
  63.  
  64. movlw B'00000001'
  65. movwf GPIO
  66. clrf lastA
  67. clrf pattA
  68.  
  69. ;Loop around for a while to let everything stabilize
  70.  
  71. movlw d'255'
  72. movwf inport
  73. loop: decfsz inport, f
  74. goto loop
  75.  
  76. ;***Basic program loop
  77.  
  78. ;Main routine - check pin states and step on negative edge
  79. ;Get port data and store, then check axis A
  80. ;A10 checks if old is 0, new is 1 (update register)
  81. ;A01 checks if old is 1, new is 0 (step and update register)
  82. ;Similarly for axis B
  83.  
  84. main: movf GPIO, w
  85. movwf inport
  86. CLRWDT
  87. A10: btfsc lastA, 0
  88. goto A01
  89. btfss STEP
  90. goto A01
  91. bsf lastA, 0
  92. A01: btfss lastA, 0
  93. goto B10
  94. btfsc STEP
  95. goto B10
  96. bcf lastA, 0
  97. goto stepA
  98.  
  99. B10: goto main
  100.  
  101. ;------------------------------------------
  102. ;***stepA - sub to cycle axis A one Full step
  103. ; Dir of 1 is increase, else decrease
  104.  
  105. stepA: btfss DIR
  106. decf pattA, f
  107. btfsc DIR
  108. incf pattA, f
  109.  
  110. ;Check for pattern overflow and fix
  111.  
  112.  
  113. movf pattA, w
  114. XORLW D'255'
  115. movlw D'07'
  116. btfsc STATUS, Z
  117. movwf pattA
  118.  
  119. movf pattA, w
  120. XORLW D'08'
  121. btfsc STATUS, Z
  122. clrf pattA
  123.  
  124. ;Get step pattern and send to GPIO on bits 0-1-2-4
  125.  
  126. movf pattA, w
  127. call dcode
  128. movwf GPIO
  129.  
  130. goto main
  131.  
  132.  
  133.  
  134. ;------------------------------------------
  135. ;***stepcode - sub to generate bit pattern for number in w (!!MUST BE 0-7!!)
  136. ; pattern is stored in w register
  137.  
  138. dcode: addwf PCL, f ; Use below column for the half Step
  139. retlw B'00000001' ;0 retlw B'00000001' ;0
  140. retlw B'00000010' ;1 retlw B'00000011' ;1
  141. retlw B'00000100' ;2 retlw B'00000010' ;2
  142. retlw B'00010000' ;3 retlw B'00000110' ;3
  143. retlw B'00000001' ;4 retlw B'00000100' ;4
  144. retlw B'00000010' ;5 retlw B'00010100' ;5
  145. retlw B'00000100' ;6 retlw B'00010000' ;6
  146. retlw B'00010000' ;7 retlw B'00010001' ;7
  147.  
  148.  
  149. ;Mandatory end of program command
  150.  
  151. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement