Re: decoding quadrature encoders
you have some isr issues
1.
; Not Necessary for Chips with >2k of Codespace
; movwf wsave
; swapf STATUS, W
; clrf STATUS
; movwf ssave
; movf PCLATH, W
; movwf psave
; Not Necessary for Chips with >2k of Codespace what ?
yet at the end of your isr pclath and status are restored
all isrs need to save and restore mcu status
2.
the chip you are using has more than 1 code pages ,your isr is located in page 0 ,
pclath should be set accordingly
USUAL
ISR BEGIN
movwf wsave
swapf STATUS, W
clrf STATUS
movwf ssave
movf PCLATH, W
movwf psave
CLRF PCLATH
YOUR CODE
.............
3.
a RE stream moving clockwise goes phase 1 - 2 - 3 - 4 -1 -2 .......
where phase 1=00
2=10
3=11
4=01
your code
;check to see if encoder has moved
movf _enc_old,W ;move enc_old to W
movwf _enc_tmp ;put W to enc_tmp
movf _enc_new,W ;move enc_new to W for XOR
xorwf _enc_tmp,F ;XOR enc_tmp to detect encoder movement
btfsc _enc_tmp,7 ;if bit is clear, encoder moved.
goto Continue ;no movement exit isr
say enc_old=01 ,enc_new=00 ; ie clockwise move
your xor would yield 0b01000000
and bit 7 is clear so no move is recorded , wrong it did move .it may also cause the 0 point to creep as direction
changes occur
the test should be is the result 0 not is bit7 0
4. bank sel ?
your isr forces bank0 the chip has more than one bank available
all vars used in isr need to be assigned to bank 0
ie enc_old VAR BYTE bank0 .
etc
5. enc_counter a word a non atomic entity
any non atomic entity that can be changed in an isr and is used outside the isr needs to be handled
correctly to prevent glitches caused by rollover/under of the low byte.
best way is to use a buffered copy or that entity
eg
enc_counter VAR WORD bank0
buffered_enc_counter VAR WORD
gosub get_enc_count
get_enc_count:
intcon.3=0 ; rbc_int off
buffered_enc_counter=enc_counter
intcon.3=1 ; rbc_int on
return
Last edited by richard; - 8th February 2017 at 03:44.
Warning I'm not a teacher
Bookmarks