Luciano,
I added the code from the lin you posted..
I'm using interrupt on INT0 connected to channel A..
I'm able to count and detect directions..with 1 big problem
i cannot monitor or dispaly the counts either using LCDout or software serial to hyperterminal.
it's good for 2 seconds and then the whole LCD screen corrupts
...is it because the LCDout line is being interrupted by the encoder
...if i disable/enable interrupts within the LCDout line..i lose encoder counts
in the code..i use 2 leds connected to portB.6,7 to show direction.
LOOP is my main program
my code is below
Code:
GoTo start ' Skip around interrupt handler
DEFINE INTHAND display ' DEFINE INTERRUPT handler
Asm
display
movwf wsave
swapf STATUS, W
clrf STATUS
movwf ssave
movf PCLATH, W
movwf psave
;;;bsf _led ; Turn on LED (for example)
;new=PORTB & %11000000
;Get intitial input from ENCODER & put the value in OLD.
movf PORTB,W
movlw _old
;Strip off all but the last 2 LSBs in OLD.
movlw 0x03 ;Create bit mask for 2 LSBs (bits 6 & 7). 11000000
andwf _old,F ;Zero bits 0 thru 6.
;Read latest input from ENCODER & put the value in NEW.
movf PORTB,W
movwf _new
;Strip off all but the 2 LSBs in OLD.
movlw 0x03 ;Create bit mask for 2 LSBs (bits 6 & 7). 11000000
andwf _new,F ;Zero bits 0 thru 6.
movf _new,W ;Move the contents of NEW to TEMP and OLD to W
movwf _temp ;in order to compare them with XOR.
movf _old,W
xorwf _temp,F ;XOR previous inputs (in W) with latest inputs
;(in TEMP) to see if they are equal.
btfsc STATUS,Z ;Test result and skip next line if 0.
GoTo rtrn ;Result is 0. Previous inputs equal latest
;inputs. Rotary encoder did not move. Return
;to loop
;Result is 1. Rotary encoder moved. Determine direction.
bcf STATUS,C ;Clear the carry bit in the status register and
rlf _old,F ;left shift it into OLD to align bit0 of OLD
;with bit 1 of NEW.
movf _new,W ;Move the contents of NEW to W in order to XOR.
xorwf _old,F ;XOR previous inputs (in OLD) with latest
;inputs (in W) to determine CW or CCW.
btfsc _old,1 ;Test bit 1 of result (in OLD). Skip next line
;if it is 0 (direction is CCW).
GoTo Up ;Bit is 1 (direction is CW). Go around Down
Down
;Decrements COUNTER because encoder moved CCW.
decf _enc_counter,F
bsf _led
GoTo rtrn ;Branch around UP.
Up
;Increments COUNTER because encoder moved CW.
incf _enc_counter,F
bsf _led2
;and increment counter.
rtrn bcf intcon.1 ; clear interrupt flag
; Restore PCLATH, STATUS and W registers
movf psave, W
movwf PCLATH
swapf ssave, W
movwf STATUS
swapf wsave, F
swapf wsave, W
retfie
EndAsm
start:
'SerOut PORTC.0,T9600,[#enc_counter,10,13]
TRISB=%00111111
TRISC=%00000000
Low led
Low led2 ' Turn LED off
INTCON = %10010000 ' Enable INTERRUPT ON portb INT0
option_reg=%01110000
loop:
'INTCON = %00000000
'LCDOut $fe,1,DEC enc_counter
'Pause 4
'INTCON = %10010000
Low led
Low led2
GoTo loop
Bookmarks