START
btfsc GPIO,4; waiting for the encoder changes
goto START; keep check for the changes
call SHAFT_0; changes detected
SHAFT_0:
CLRF A11 ; clear memory A11
CLRF A21 ; clear memory A21
BCF STATUS,C; clear carry flag incase there is carry
BTFSC GPIO,4 ; check on the changes, 0 or 1
GOTO SAVEBIT_A11 ; if 1 go savebitA11
GOTO SAVEBIT_A10 ;if 0 go savebit A10
SHAFT_1
BTFSC GPIO,5 ; check on gpio 5 changes, channel B
GOTO SAVEBIT_B11 ;if 1 go savebit B11
GOTO SAVEBIT_B10 ;if 0 go savebit B10
SHAFT_2
BTFSC GPIO,4 ;same as above to check to the 4th bit
GOTO SAVEBIT_A21 ;
GOTO SAVEBIT_A20 ;
SHAFT_3
BTFSC GPIO,5 ;
GOTO SAVEBIT_B21 ;
GOTO SAVEBIT_B20 ;
SAVEBIT_A11
MOVLW b'00000001' ; if 1 received on shaft channel A move 1 to A11 memory
MOVWF A11 ;
RLF A11,1 ; rotate left to make bit 0 avaible for channel B
GOTO SHAFT_1 ;
SAVEBIT_A10
MOVLW b'00000000' ;if 0 received on shaft channel A move 1 to A11 memory
MOVWF A11 ;
RLF A11,1 ;rotate left to make bit 0 avaible for channel B
GOTO SHAFT_1 ;
SAVEBIT_B11
MOVLW b'00000001' ; if 1 received on shaft channel B move 1 to A11 memory
IORWF A11,1 ;added up with channel B to form 1 complete bit for
RLF A11,1 ;each turn, rotate left 2 times for next bit of turn
RLF A11,1 ; of the encoder
GOTO SHAFT_2 ;
SAVEBIT_B10
MOVLW b'00000000' ; same as above ...for 0 bit on channel B
IORWF A11,1 ;
RLF A11,1 ;
RLF A11,1
GOTO SHAFT_2 ;
SAVEBIT_A21
MOVLW b'00000001' ;if 1 received on shaft channel A move 1 to A21 memory
MOVWF A21 ;
RLF A21,1 ;rotate left to make bit 0 avaible for channel B
GOTO SHAFT_3 ;
SAVEBIT_A20
MOVLW b'00000000' ;
MOVWF A21 ;same as above
RLF A21,1 ;
GOTO SHAFT_3 ;
SAVEBIT_B21
MOVLW b'00000001' ;bit 1 , added up with channel A to form 1 complete bit for
IORWF A21,0 ;each turn
IORWF A11,0 ;at here, add up 2 complete bit to determine the
GOTO DIRECTION ; direction of the shaft encoder...
SAVEBIT_B20
MOVLW b'00000000' ;same as above, but 0 bit received from previous action
IORWF A21,0 ;
IORWF A11,0 ;
GOTO DIRECTION ;
DIRECTION
CALL TABLE_1 ; call table to check on the direction after the total 4bit added
ANTI_CLOCKWISE
BSF GPIO, 0; set gpio 0 high
BCF GPIO,1; clear gpio 1
goto START;
CLOCKWISE
BSF GPIO,1;set gpio 1 high
BCF GPIO,0;clear gpio 0
GOTO START;
RESTART_PWM
GOTO START; no changes go back to start
RETURN
;************************************************
;**** DIRECTION TABLE****************************
;************************************************
TABLE_1:
ADDWF PCL,1 ;
GOTO RESTART_PWM ;
GOTO ANTI_CLOCKWISE ;
GOTO CLOCKWISE ;
GOTO RESTART_PWM ;
GOTO CLOCKWISE ;
GOTO RESTART_PWM ;
GOTO RESTART_PWM ;
GOTO ANTI_CLOCKWISE ;
GOTO ANTI_CLOCKWISE ;
GOTO RESTART_PWM ;
GOTO RESTART_PWM ;
GOTO CLOCKWISE ;
GOTO RESTART_PWM ;
GOTO CLOCKWISE ;
GOTO ANTI_CLOCKWISE ;
GOTO RESTART_PWM ;
RETURN