#define EncPort PORTB #define EncMask b'00111100' #define Enc1APin 2 #define Enc1BPin 3 #define Enc1Mask b'00001100' #define Enc2APin 4 #define Enc2BPin 5 #define Enc2Mask b'00110000' char enc1count; // low byte of encoder count char enc1count2; // 2nd byte of encoder count char enc1count3; // 3rd byte of encoder count char enc1count4; // high byte of encoder count char enc1new; // new encoder reading char enc1old; // previous encoder reading char enc1temp; // temp var for encoder calculations char enc1errcnt; // number of overrun errors encountered char enc2count; // low byte of encoder count char enc2count2; // 2nd byte of encoder count char enc2count3; // 3rd byte of encoder count char enc2count4; // high byte of encoder count char enc2new; // new encoder reading char enc2old; // previous encoder reading char enc2temp; // temp var for encoder calculations char enc2errcnt; // number of overrun errors encountered void enc1_poll() { #asm enc1_int BCF STATUS,RP0 BCF STATUS,RP1 movf EncPort,w ; read encoder port andlw Enc1Mask ; mask off all but encoder bits movwf enc1new ; save as new reading xorwf enc1old,w ; compare against old reading btfsc STATUS,Zero_ ; if they're equal, goto enc1done ; then we're done xorlw Enc1Mask ; see if both bits changed btfsc STATUS,Zero_ ; if so, goto enc1err ; then we have an overrun error bcf STATUS,Carry ; clear carry for rotate operation rlf enc1old,w ; shift old bits left xorwf enc1new,w ; and compare against new bits andlw B'00001000' ; mask off all but bit 1 btfsc STATUS,Zero_ ; if old:0 is the same as new:1 goto enc1up ; then we're going up enc1down ; otherwise we're going down movlw 1 subwf enc1count,f ; decrement low byte btfss STATUS,Carry ; if borrow, subwf enc1count2,f ; then decrement second byte btfss STATUS,Carry ; if borrow, subwf enc1count3,f ; then decrement third byte btfss STATUS,Carry ; if borrow, subwf enc1count4,f ; then decrement high byte goto enc1store enc1up incf enc1count,f ; increment low byte btfsc STATUS,Zero_ ; if it wrapped, incf enc1count2,f ; then increment second byte btfsc STATUS,Zero_ ; if it wrapped, incf enc1count3,f ; then increment third byte btfsc STATUS,Zero_ ; if it wrapped, incf enc1count4,f ; then increment high byte enc1store ; movf enc1count,w ; grab a byte from the count ; movwf PORTB ; and write it to the display goto enc1store2 enc1err ; overrun error! incf enc1errcnt,f enc1store2 movf enc1new,w ; save new reading... movwf enc1old ; ...as old enc1done #endasm } void enc2_poll() { #asm enc2_int BCF STATUS,RP0 BCF STATUS,RP1 movf EncPort,w ; read encoder port andlw Enc2Mask ; mask off all but encoder bits movwf enc2new ; save as new reading xorwf enc2old,w ; compare against old reading btfsc STATUS,Zero_ ; if they're equal, goto enc2done ; then we're done xorlw Enc2Mask ; see if both bits changed btfsc STATUS,Zero_ ; if so, goto enc2err ; then we have an overrun error bcf STATUS,Carry ; clear carry for rotate operation rlf enc2old,w ; shift old bits left xorwf enc2new,w ; and compare against new bits andlw B'00100000' ; mask off all but bit 1 btfsc STATUS,Zero_ ; if old:0 is the same as new:1 goto enc2up ; then we're going up enc2down ; otherwise we're going down movlw 1 subwf enc2count,f ; decrement low byte btfss STATUS,Carry ; if borrow, subwf enc2count2,f ; then decrement second byte btfss STATUS,Carry ; if borrow, subwf enc2count3,f ; then decrement third byte btfss STATUS,Carry ; if borrow, subwf enc2count4,f ; then decrement high byte goto enc2store enc2up incf enc2count,f ; increment low byte btfsc STATUS,Zero_ ; if it wrapped, incf enc2count2,f ; then increment second byte btfsc STATUS,Zero_ ; if it wrapped, incf enc2count3,f ; then increment third byte btfsc STATUS,Zero_ ; if it wrapped, incf enc2count4,f ; then increment high byte enc2store ; movf enc2count,w ; grab a byte from the count ; movwf PORTB ; and write it to the display goto enc2store2 enc2err ; overrun error! incf enc2errcnt,f enc2store2 movf enc2new,w ; save new reading... movwf enc2old ; ...as old enc2done #endasm } void poll_encoders(void) { enc1_poll(); enc2_poll(); }