您当前的位置:五五电子网电子知识单片机-工控设备PIC单片机pic16f508步进电机程序 正文
pic16f508步进电机程序

pic16f508步进电机程序

点击数:7254 次   录入时间:03-04 11:37:22   整理:http://www.55dianzi.com   PIC单片机

; File STEP508.ASM
; ... for PIC12C508A microcontroller
; Program to use PIC as a step and direction controller for a unipolar
; 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)
; Steps on negative going edge of step pulse.

; CPU configuration
;     (It’s a 12C508A, Internal RC oscillator,
;     watchdog timer off, power-up timer on)
    LIST   P=12C508A
    processor 12c508A
    include      <p12c508A.inc>
;    __config  _IntRC_OSC & _WDT_OFF & _PWRTE_ON & _MCLRE_OFF

; Declare variables

pattA    equ    H’0D’    ;Current step pattern number (0-7) for axis A
lastA    equ    H’0E’   ;Last state of step pin on axis A (1 is high, 0 is low)
inport    equ    H’11’    ;Value of port A when read (stored for later aCCess)
temp    equ    H’12’

#DEFINE STEP    inport,5    ; Step pulse input
#DEFINE DIR    inport,3    ; Direction Input

OPMASK    EQU    B’11000000’
IOMASK    EQU    B’00101000’    ; all bits output (except GP3 and GP5)
                                ; GP0 , GP1 , GP2 , GP4  controls The stepper Coils
                                ; GP3 controls direction
                ; GP5 Controls Step Pulses


    
    ORG 0
    
; start of main code
;***************************************************
;
;    START OF PIC 12c508A CODE FOR STEP;
;
;***************************************************
;

;------------------------------------------
;****Power on reset startpoint
;------------------------------------------

;***Initialization of program    
    MOVWF    OSCCAL
    
    MOVLW    OPMASK
    OPTION    

; Set GPIO for input & output    
    
    MOVLW    IOMASK
    TRIS    GPIO        




;Clear port and zero motors


    MOVlw    B’00000001’
    MOVwf    GPIO
    clrf    lastA
    clrf    pattA

;Loop around for a while to let everything stabilize

    MOVlw    d’255’
    MOVwf    inport
loop:    decfsz    inport, f
;    goto loop

;***Basic program loop

;Main routine - check pin states and step on negative edge
;Get port data and store, then check axis A
;A10 checks if old is 0, new is 1 (update register)
;A01 checks if old is 1, new is 0 (step and update register)
;Similarly for axis B

main:    MOVf    GPIO, w
    MOVwf    inport
    CLRWDT
A10:    btfsc    lastA, 0
    goto A01
    btfss    STEP
    goto A01
    bsf    lastA, 0
A01:    btfss    lastA, 0
    goto B10
    btfsc    STEP
    goto B10
    bcf    lastA, 0
    goto stepA

B10:    goto main

;------------------------------------------
;***stepA - sub to cycle axis A one Full step
;  Dir of 1 is increase, else decrease

stepA:    btfss    DIR
    decf    pattA, f
    btfsc    DIR
    incf    pattA, f

;Check for pattern overflow and fix


    MOVf    pattA, w
    XORLW    D’255’
    MOVlw    D’07’
    btfsc    STATUS, Z
    MOVwf    pattA

    MOVf    pattA, w
    XORLW    D’08’
    btfsc    STATUS, Z
    clrf    pattA    

;Get step pattern and send to GPIO on bits 0-1-2-4

    MOVf    pattA, w
    call     dcode
    MOVwf    GPIO

  goto main



;------------------------------------------

[1] [2]  下一页


本文关键字:步进电机  程序  PIC单片机单片机-工控设备 - PIC单片机