;;;;;;; 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... bz L1 rrcf OLDPORTD,W ;Form what a CCW change would produce ;IF_ .C. ;Make new bit 1 = complement of old bit 0 bnc L2 bcf WREG,1 ;ELSE_ bra L3 L2 bsf WREG,1 ;ENDIF_ L3 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 bnz L4 decf DELRPG,F ;ELSE_ ;Otherwise, change DELRPG to +1 for CW bra L5 L4 incf DELRPG,F ;ENDIF_ L5 ;ENDIF_ L1 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... bz L6 rrcf OLDPORTD,W ;Form what a CCW change would produce ;IF_ .C. ;Make new bit 1 = complement of old bit 0 bnc L7 bcf WREG,1 ;ELSE_ bra L8 L7 bsf WREG,1 ;ENDIF_ L8 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 bnz L9 decf DELRPG,F movf THR,F ;IF_ .NZ. bz L10 decf DELRPG,F ;If fast turning, decrement again bsf PORTA,RA2 ;Turn LED on ;ENDIF_ L10 ;ELSE_ ;Otherwise, change DELRPG to +1 for CW bra L11 L9 incf DELRPG,F movf THR,F ;IF_ .NZ. bz L12 incf DELRPG,F ;If fast turning, increment again bsf PORTA,RA2 ;Turn LED on ;ENDIF_ L12 ;ENDIF_ L11 MOVLF Threshold+1,THR ;Reinitialize THR ;ENDIF_ L6 movf THR,F ;Does THR equal zero ;IF_ .NZ. ;If not, then decrement it bz L13 decf THR,F ;ENDIF_ L13 movff TEMP,OLDPORTD ;Save PORTD as OLDPORTD for ten ms from now return ;Figure 8-4 Rate-sensitive RateRPG subroutine with Threshold=3.