;********************************************************
;
; STEPPER MOTOR CONTROLLER
; 2008 1/21
; Programed by Takashi Amoh
;
;********************************************************
LIST P=PIC16F84A
INCLUDE P16F84A.INC
__CONFIG _HS_OSC & _WDT_OFF & _PWRTE_ON & _CP_OFF
;**************** LABEL DEFINITION ********************
CBLOCK H'0C'
MODE ;OPERATION MODE
;0=STOP 1=RIGHT 2=LEFT
COUNT1 ;WAIT COUNTER
COUNT2 ;WAIT COUNTER(FOR 1MSEC)
ENDC
RB0 EQU 0 ;RB0 OF PORTB
RB1 EQU 1 ;RB1 OF PORTB
RB2 EQU 2 ;RB2 OF PORTB
RB5 EQU 5 ;RB5 OF PORTB
RB7 EQU 7 ;RB7 OF PORTB
;**************** PROGRAM START ***********************
ORG 0 ;RESET VECTOR
GOTO INIT
ORG 4 ;INTERRUPT VECTOR
CLRF INTCON ;CLEAR INTERRUPTION REG
;**************** INITIAL PROCESS *********************
INIT
BSF STATUS,RP0 ;CHANGE TO BANK1
CLRF TRISA ;SET PORTA ALL OUT
MOVLW B'00100111' ;RB0,1,2.5=IN RB7=OUT
MOVWF TRISB ;SET PORTB
MOVLW B'10000000' ;RBPU=1 PULL UP NOT USE
MOVWF OPTION_REG ;SET OPTION_REG
BCF STATUS,RP0 ;CHANGE TO BANK0
CLRF MODE ;SET MODE = STOP
CLRF COUNT1 ;CLEAR COUNTER
CLRF COUNT2 ;CLEAR COUNTER
MOVLW B'00000101' ;SET PORTA INITIAL VALUE
MOVWF PORTA ;WRITE PORTA
BSF PORTB,RB7 ;SET RB7 = 1
BTFSC PORTB,RB5 ;RB5 = 0 ?
GOTO $-1 ;NO. WAIT
START
;************* CHECK SWITCH CONDITION *****************
BTFSC PORTB,RB1 ;RB1(STOP KEY) = ON ?
GOTO CHECK1 ;NO. NEXT
CLRF MODE ;YES. SET STOP MODE
GOTO DRIVE ;NO. JUMP TO MOTOR DRIVE
CHECK1
BTFSC PORTB,RB2 ;RB2(RIGHT KEY) = ON ?
GOTO CHECK2 ;NO. NEXT
MOVLW D'1' ;YES. SET RIGHT MODE
MOVWF MODE ;SAVE MODE
GOTO DRIVE ;NO. JUMP TO MOTOR DRIVE
CHECK2
BTFSC PORTB,RB0 ;RB0(LEFT KEY) = ON ?
GOTO DRIVE ;NO. JUMP TO MOTOR DRIVE
MOVLW D'2' ;YES. SET LEFT MODE
MOVWF MODE ;SAVE MODE
;******************** MOTOR DRIVE *********************
DRIVE
MOVF MODE,W ;READ MODE
BZ START ;MODE = STOP
BSF PORTB,RB7 ;SET RB7 = 1
BTFSC PORTB,RB5 ;RB5 = 0 ?
GOTO $-1 ;NO. WAIT
MOVLW D'5' ;SET LOOP COUNT(5MSEC)
MOVWF COUNT1 ;SAVE LOOP COUNT
LOOP CALL TIMER ;WAIT 1MSEC
DECFSZ COUNT1,F ;COUNT - 1 = 0 ?
GOTO LOOP ;NO. CONTINUE
BCF PORTB,RB7 ;SET RB7 = 0
BTFSS PORTB,RB5 ;RB5 = 1 ?
GOTO $-1 ;NO. WAIT
MOVF PORTA,W ;READ PORTA
SUBLW B'000000101' ;CHECK MOTOR POSITION
BNZ DRIVE2 ;UNMATCH
MOVF MODE,W ;READ MODE
SUBLW D'1' ;RIGHT ?
BZ DRIVE1 ;YES. RIGHT
MOVLW B'00001001' ;NO. SET LEFT DATA
GOTO DRIVE_END ;JUMP TO PORTA WRITE
DRIVE1
MOVLW B'00000110' ;SET RIGHT DATA
GOTO DRIVE_END ;JUMP TO PORTA WRITE
;-------
DRIVE2
MOVF PORTA,W ;READ PORTA
SUBLW B'000000110' ;CHECK MOTOR POSITION
BNZ DRIVE4 ;UNMATCH
MOVF MODE,W ;READ MODE
SUBLW D'1' ;RIGHT ?
BZ DRIVE3 ;YES. RIGHT
MOVLW B'00000101' ;NO. SET LEFT DATA
GOTO DRIVE_END ;JUMP TO PORTA WRITE
DRIVE3
MOVLW B'00001010' ;SET RIGHT DATA
GOTO DRIVE_END ;JUMP TO PORTA WRITE
;-------
DRIVE4
MOVF PORTA,W ;READ PORTA
SUBLW B'000001010' ;CHECK MOTOR POSITION
BNZ DRIVE6 ;UNMATCH
MOVF MODE,W ;READ MODE
SUBLW D'1' ;RIGHT ?
BZ DRIVE5 ;YES. RIGHT
MOVLW B'00000110' ;NO. SET LEFT DATA
GOTO DRIVE_END ;JUMP TO PORTA WRITE
DRIVE5
MOVLW B'00001001' ;SET RIGHT DATA
GOTO DRIVE_END ;JUMP TO PORTA WRITE
;-------
DRIVE6
MOVF PORTA,W ;READ PORTA
SUBLW B'000001001' ;CHECK MOTOR POSITION
BNZ DRIVE8 ;UNMATCH
MOVF MODE,W ;READ MODE
SUBLW D'1' ;RIGHT ?
BZ DRIVE7 ;YES. RIGHT
MOVLW B'00001010' ;NO. SET LEFT DATA
GOTO DRIVE_END ;JUMP TO PORTA WRITE
DRIVE7
MOVLW B'00000101' ;SET RIGHT DATA
GOTO DRIVE_END ;JUMP TO PORTA WRITE
;-------
DRIVE8
MOVLW B'00000101' ;COMPULSION SETTING
DRIVE_END
MOVWF PORTA ;WRITE PORTA
GOTO START ;JUMP TO START
;************* 1MSEC TIMER SUBROUTINE *****************
TIMER
MOVLW D'200' ;SET LOOP COUNT
MOVWF COUNT2 ;SAVE LOOP COUNT
TMLP NOP ;TIME ADJUST
NOP ;TIME ADJUST
DECFSZ COUNT2,F ;COUNT - 1 = 0 ?
GOTO TMLP ;NO. CONTINUE
RETURN ;YES. COUNT END
;********************************************************
; END OF STEPPER MOTOR CONTROLLER
;********************************************************
END
step_unit.asm.txt
使用する際は、ファイル名をstep_unit.asmへ変更する必要がある。