;;;;;;; RPG subroutine ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ; ; This subroutine decyphers RPG changes into values of DELRPG of 0, +1, or -1. ; DELRPG = +1 for CW change, 0 for no change, and -1 for CCW change. RPG clrf DELRPG ;Clear for "no change" return value movf PORTD,W ;Copy PORTD into W movwf TEMP ; and TEMP xorwf OLDPORTD,W ;Any change? andlw B'00000011' ;If not, set Z flag IF_ .NZ. ;If the two bits have changed then... rrcf OLDPORTD,W ;Form what a CCW change would produce IF_ .C. ;Make new bit 1 = complement of old bit 0 bcf WREG,1 ELSE_ bsf WREG,1 ENDIF_ xorwf TEMP,W ;Did the RPG actually change to this output? andlw B'00000011' IF_ .Z. ;If so, then change DELRPG to -1 for CCW decf DELRPG,F ELSE_ ;Otherwise, change DELRPG to +1 for CW incf DELRPG,F ENDIF_ ENDIF_ movff TEMP,OLDPORTD ;Save PORTD as OLDPORTD for ten ms from now return ;(a) The subroutine rcall RPG ;Decypher RPG inputs into DELRPG movf DELRPG,W addwf RPGCNT,F ;Increment or decrement RPGCNT from RPG DISPLAY RPGCNT ;Display RPGCNT as a binary number ;(b) Test code for mainline loop OLDPORTD ;Holds previous value of RPG inputs TEMP ;Temporary local variable DELRPG ;Generated by RPG RPGCNT ;Used to display RPG changes ;(c) Variables movff PORTD,OLDPORTD ;Initialize "old" value clrf RPGCNT ;Clear counter to be displayed ;(d) Initialization ;Figure 8-3 RPG subroutine. ;;;;;;; RateRPG subroutine ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ; ; This subroutine decyphers RPG changes into values of DELRPG. ; DELRPG = +2 for fast CW change, +1 for slow CW change, 0 for no change, ; -1 for slow CCW change and -2 for fast CCW change. Threshold equ 3 ;Value to distinguish between slow and fast RateRPG bcf PORTA,RA2 ;Turn LED off clrf DELRPG ;Clear for "no change" return value movf PORTD,W ;Copy PORTD into W movwf TEMP ; and TEMP xorwf OLDPORTD,W ;Any change? andlw B'00000011' ;If not, set Z flag IF_ .NZ. ;If the two bits have changed then... rrcf OLDPORTD,W ;Form what a CCW change would produce IF_ .C. ;Make new bit 1 = complement of old bit 0 bcf WREG,1 ELSE_ bsf WREG,1 ENDIF_ xorwf TEMP,W ;Did the RPG actually change to this output? andlw B'00000011' IF_ .Z. ;If so, then change DELRPG to -1 for CCW decf DELRPG,F movf THR,F IF_ .NZ. decf DELRPG,F ;If fast turning, decrement again bsf PORTA,RA2 ;Turn LED on ENDIF_ ELSE_ ;Otherwise, change DELRPG to +1 for CW incf DELRPG,F movf THR,F IF_ .NZ. incf DELRPG,F ;If fast turning, increment again bsf PORTA,RA2 ;Turn LED on ENDIF_ ENDIF_ MOVLF Threshold+1,THR ;Reinitialize THR ENDIF_ movf THR,F ;Does THR equal zero IF_ .NZ. ;If not, then decrement it decf THR,F ENDIF_ movff TEMP,OLDPORTD ;Save PORTD as OLDPORTD for ten ms from now return ;Figure 8-4 Rate-sensitive RateRPG subroutine with Threshold=3.