⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 motors.sdi

📁 代码保护功能处于持续发展中。Microchip 承诺将不断改进产品的代码保护功能。任何试图破坏Microchip 代码保护功能的行为均可视 为违反了《数字器件千年版权法案(Digital Mille
💻 SDI
字号:
,,,;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
,,,;
,,,;       Project:                Interfacing PICs
,,,;       Source File Name:       MOTORS.ASM
,,,;       Devised by:             MPB
,,,;       Date:                   19-8-05
,,,;       Status:                 Working
,,,;
,,,;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
,,,;
,,,;       Demonstrates DC, SERVO & STEPPER MOTOR control
,,,;       Select motor and direction using push button inputs
,,,;       DC Motor PWM speed control - working
,,,;       DC Servo position control - rollover not fixed
,,,;       Stepper direction control - working
,,,;
,,,;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
,,,
,,,        PROCESSOR 16F877A
,,,;       Clock = XT 4MHz, standard fuse settings
00002007,3731,,        __CONFIG 0x3731
,,,
,,,;       LABEL EQUATES   ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
,,,
,,,        INCLUDE "P16F877A.INC"
,,,        ; standard register labels
,,,
,,,;----------------------------------------------------------
,,,; User register labels
,,,;----------------------------------------------------------
,,,
,,,Count1  EQU     20              ; delay counter
,,,Count2  EQU     21              ; delay counter
,,,Target  EQU     22              ; servo target position
,,,
,,,;----------------------------------------------------------
,,,; PROGRAM BEGINS
,,,;----------------------------------------------------------
,,,
,,,        ORG     0               ; Default start address
00000000,0000,,        NOP                     ; required for ICD mode
,,,
,,,;----------------------------------------------------------
,,,; Port & PWM setup
,,,
00000001,0000,init,init    NOP
00000002,1683 1303,,        BANKSEL TRISB           ; Select control registers
00000004,0187,,        CLRF    TRISC           ; Output for dc motors
00000005,0188,,        CLRF    TRISD           ; Output for stepper
00000006,3002,,        MOVLW   B'00000010'     ; Analogue input setup code
,,,                                ; PortA = analogue inputs
,,,                                ; Vref = Vdd
00000007,009F,,        MOVWF   ADCON1          ; Port E = digital inputs
00000008,30F9,,        MOVLW   D'249'          ; PWM = 4kHz
00000009,0092,,        MOVWF   PR2             ; TMR2 preload value
,,,
0000000A,1283 1303,,        BANKSEL PORTB           ; Select output registers
0000000C,0187,,        CLRF    PORTC           ; Outputs off
0000000D,0188,,        CLRF    PORTD           ; Outputs off
0000000E,3041,,        MOVLW   B'01000001'     ; Analogue input setup code
0000000F,009F,,        MOVWF   ADCON0          ; f/8, RA0, done, enable
00000010,3080,,        MOVLW   D'128'          ; intial servo position
00000011,00A2,,        MOVWF   Target
,,,
,,,;----------------------------------------------------------
,,,; MAIN LOOP
,,,;----------------------------------------------------------
,,,
00000012,1809,but0,but0    BTFSC   PORTE,0         ; wait for select button
00000013,2812,,        GOTO    but0
,,,
00000014,300C,,        MOVLW   B'00001100'     ; Select PWM mode
00000015,0097,,        MOVWF   CCP1CON         ;
00000016,3080,,        MOVLW   D'128'          ; PWM = 50%
00000017,0095,,        MOVWF   CCPR1L          ;
,,,
00000018,1C09,but1,but1    BTFSS   PORTE,0         ; wait for button release
00000019,2818,,        GOTO    but1
0000001A,202D,,        CALL    motor           ; check for speed change
0000001B,1809,,        BTFSC   PORTE,0         ; wait for select button
0000001C,2818,,        GOTO    but1
0000001D,3000,,        MOVLW   B'00000000'     ; deselect PWM mode
0000001E,0097,,        MOVWF   CCP1CON         ;
0000001F,0187,,        CLRF    PORTC           ; switch off outputs
,,,
00000020,1C09,but2,but2    BTFSS   PORTE,0         ; wait for button release
00000021,2820,,        GOTO    but2
00000022,203B,,        CALL    servo           ; move servo cw or ccw
00000023,1809,,        BTFSC   PORTE,0         ; wait for select button
00000024,2820,,        GOTO    but2
00000025,0187,,        CLRF    PORTC           ; switch off servo
,,,
00000026,1C09,but3,but3    BTFSS   PORTE,0         ; wait for button release
00000027,2826,,        GOTO    but3
00000028,2060,,        CALL    step            ; output one step cycle
00000029,1809,,        BTFSC   PORTE,0         ; wait for select button
0000002A,2826,,        GOTO    but3
0000002B,0188,,        CLRF    PORTD           ; disable stepper outputs
0000002C,2812,,        GOTO    but0            ; start again
,,,
,,,;-----------------------------------------------------------
,,,; SUBROUTINES
,,,;-----------------------------------------------------------
,,,
,,,; Change dc motor speed by one step and wait 1ms
,,,; to debounce and control rate of change....................
,,,
0000002D,1487,motor,motor   BSF     PORTC,1         ; switch on motor LED
,,,
0000002E,1C89,,        BTFSS   PORTE,1         ; inc speed?
0000002F,0A95,,        INCF    CCPR1L          ; yes
00000030,30F8,,        MOVLW   D'248'          ; max speed?
00000031,0215,,        SUBWF   CCPR1L,W
00000032,1D03,,        BTFSS   STATUS,Z
00000033,2835,,        GOTO    lower           ; no
00000034,0395,,        DECF    CCPR1L          ; yes - dec speed
,,,
,,,
00000035,1D09,lower,lower   BTFSS   PORTE,2         ; dec speed?
00000036,0B95,,        DECFSZ  CCPR1L          ; yes - min speed?
00000037,2839,,        GOTO    done            ; no
00000038,0A95,,        INCF    CCPR1L          ; yes - inc speed
,,,
00000039,2072,done,done    CALL    onems           ; 1ms debounce
0000003A,0008,,        RETURN
,,,
,,,; Move servo 10 bits cw or ccw................................
,,,
0000003B,1607,servo,servo   BSF     PORTC,4         ; switch on servo LED
0000003C,1787,,        BSF     PORTC,7         ; enable drive chip
0000003D,1889,,        BTFSC   PORTE,1         ; move forward?
0000003E,284E,,        GOTO    rev             ; no
,,,
0000003F,1C89,wait1,wait1   BTFSS   PORTE,1         ; yes- wait for button..
00000040,283F,,        GOTO    wait1           ; ..release
00000041,300A,,        MOVLW   D'10'           ; add 10...
00000042,07A2,,        ADDWF   Target          ; ..to servo target position
00000043,1687,,        BSF     PORTC,5         ; move..
00000044,1307,,        BCF     PORTC,6         ; .. forward
,,,
00000045,2078,getfor,getfor  CALL    getADC          ; get position
00000046,1403,,        BSF     STATUS,C        ; set carry flag
00000047,0822,,        MOVF    Target,W        ; load position
00000048,029E,,        SUBWF   ADRESH          ; compare with target
00000049,1C03,,        BTFSS   STATUS,C        ; far enough?
0000004A,2845,,        GOTO    getfor          ; no - repeat
,,,
0000004B,1287,,        BCF     PORTC,5         ; yes - stop
0000004C,30FA,,        MOVLW   D'250'          ; wait 250ms ..
0000004D,206D,,        CALL    xms             ; .. before next step
0000004E,1909,rev,rev     BTFSC   PORTE,2         ; move reverse?
0000004F,0008,,        RETURN                  ; no
,,,
00000050,1D09,wait2,wait2   BTFSS   PORTE,2         ; yes- wait for button..
00000051,2850,,        GOTO    wait2           ; ..release
00000052,300A,,        MOVLW   D'10'           ; yes - sub 10 from...
00000053,02A2,,        SUBWF   Target          ; .. servo target position
00000054,1287,,        BCF     PORTC,5         ; move ..
00000055,1707,,        BSF     PORTC,6         ; ..reverse
,,,
00000056,2078,getrev,getrev  CALL    getADC          ; get position
00000057,1403,,        BSF     STATUS,C        ; set carry flag
00000058,0822,,        MOVF    Target,W        ; load position
00000059,029E,,        SUBWF   ADRESH          ; compare with target
0000005A,1803,,        BTFSC   STATUS,C        ; far enough?
0000005B,2856,,        GOTO    getrev          ; no - repeat
,,,
0000005C,1307,,        BCF     PORTC,6         ; yes - stop
0000005D,30FA,,        MOVLW   D'250'          ; wait 250ms ..
0000005E,206D,,        CALL    xms             ; .. before next step
0000005F,0008,,        RETURN
,,,
,,,; Output one cycle of stepper clock.........................
,,,
00000060,1408,step,step    BSF     PORTD,0         ; switch on stepper LED
00000061,1488,,        BSF     PORTD,1         ; enable stepper drive
,,,
00000062,1C89,,        BTFSS   PORTE,1         ; test cw button
00000063,1508,,        BSF     PORTD,2         ; select clockwise
00000064,1D09,,        BTFSS   PORTE,2         ; test ccw button
00000065,1108,,        BCF     PORTD,2         ; select counter-clockwise
,,,
00000066,1588,,        BSF     PORTD,3         ; clock high
00000067,3019,,        MOVLW   D'25'           ; load delay time
00000068,206D,,        CALL    xms
00000069,1188,,        BCF     PORTD,3         ; clock low
0000006A,3019,,        MOVLW   D'25'           ; load delay time
0000006B,206D,,        CALL    xms
,,,
0000006C,0008,,        RETURN
,,,
,,,; Stepper software delay ...................................
,,,
0000006D,00A1,xms,xms     MOVWF   Count2          ; receive x ms in W
0000006E,2072,down2,down2   CALL    onems
0000006F,0BA1,,        DECFSZ  Count2
00000070,286E,,        GOTO    down2
00000071,0008,,        RETURN
,,,
00000072,30F9,onems,onems   MOVLW   D'249'          ; delay one millisec
00000073,00A0,,        MOVWF   Count1
00000074,0000,down1,down1   NOP
00000075,0BA0,,        DECFSZ  Count1
00000076,2874,,        GOTO    down1
00000077,0008,,        RETURN
,,,
,,,; Read ADC input and store .................................
,,,
00000078,151F,getADC,getADC  BSF     ADCON0,GO       ; start ADC..
00000079,191F,wait,wait    BTFSC   ADCON0,GO       ; ..and wait for finish
0000007A,2879,,        GOTO    wait
,,,
0000007B,081E,,        MOVF    ADRESH,W        ; store result, high 8 bits
0000007C,0008,,        RETURN
,,,
,,,;----------------------------------------------------------
,,,        END                     ; of source code

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -