×
单片机 > 单片机程序设计 > 详情

pic16f508步进电机程序

发布时间:2020-06-19 发布时间:
|

; 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       
;    __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 



;------------------------------------------ 
;***stepcode - sub to generate bit pattern for number in w (!!MUST BE 0-7!!) 
;  pattern is stored in w register  

dcode:    addwf    PCL, f        ;     Use below column for the half Step 
    retlw    B’00000001’    ;0    retlw    B’00000001’    ;0 
    retlw    B’00000010’    ;1    retlw    B’00000011’    ;1 
    retlw    B’00000100’    ;2    retlw    B’00000010’    ;2 
    retlw    B’00010000’    ;3    retlw    B’00000110’    ;3 
    retlw    B’00000001’    ;4    retlw    B’00000100’    ;4 
    retlw    B’00000010’    ;5    retlw    B’00010100’    ;5 
    retlw    B’00000100’    ;6    retlw    B’00010000’    ;6 
    retlw    B’00010000’    ;7    retlw    B’00010001’    ;7 


;Mandatory end of program command 

    end 


关键字:pic16f508  步进电机程序

『本文转载自网络,版权归原作者所有,如有侵权请联系删除』

热门文章 更多
浅谈AVR中定时器几种工作模式