On 10.6.2013 13:42, Mark Hanchey wrote: > On 6/9/2013 6:43 PM, Manu Abraham wrote: >> Hi, >> >> What's the best way to decode a rotary encoder (gray code) output, >> when the outputs of the encoder are connected to edge detectors >> both of which result in an IRQ each ? > > > One thing I like to do if I don't have the free ports that are edge > changeable is using a small external PIC chip, you can get a 8 pin chip > that can handle multiple encoder and output the count on a single pin , > then check the count with your main chip whenever you have the free > cycles. The extra chip doesn't have to have edge detectors since the > rotary decoding is all it will be doing. > > The extra chip can keep the count for you and the main chip can then > check the status of a single pin, any pin you like using a one wire > based communication to get the current count. > Mark > There are chips for exactly this i.e. ls7083: http://www.lsicsi.com/pdfs/Data_Sheets/LS7083_LS7084.pdf We are making a product that was designed 10+ years ago and uses LS7083.=20 Unfortunately these are not stocked by distributors and not that cheap=20 either so we replaced it with PIC12F508 that outputs COUNT_UP and COUNT=20 DOWN. This is the code, you can choose between X1, X2 and X4 operation=20 somehow, I don't remember the details. Djula ; Device: 12f508 ; Oscillator: INTOSC ; Watchdog Timer: Off ; Code Protect: On ; Master Clear Enable: Internal list p=3D12f508,f=3Dinhx8m,r=3Ddec include p12f508.inc __config _IntRC_OSC & _WDT_OFF & _CP_OFF & _MCLRE_OFF #define IDLE B'00101111' #define POS B'00101110' #define NEG B'00101101' Old_Data equ 0x10 Counter equ 0x12 Pos_Neg equ 0x13 Temp_1 equ 0x14 ;*************************** START MAIN ******************************** org 0 goto StartP org 0x40 StartP movwf OSCCAL clrf FSR movlw B'00101111' movwf GPIO ;Init PORTA movlw B'00111100' tris GPIO clrf Old_Data clrf Counter clrf Pos_Neg clrf Temp_1 Loop_0: nop movlw IDLE movwf GPIO Loop: movfw GPIO ;Save input pins andlw B'00001100' btfsc Old_Data, 2 iorlw B'00000001' btfsc Old_Data, 3 iorlw B'00000010' movwf Old_Data addwf PCL, f Data_00_00: goto No_Change Data_00_01: goto Decrement ;X2 - goto No_Change Data_00_10: goto Increment Data_00_11: goto Two_Steps Data_01_00: goto Increment ;X2 - goto No_Change Data_01_01: goto No_Change Data_01_10: goto Two_Steps Data_01_11: goto Decrement Data_10_00: goto Decrement Data_10_01: goto Two_Steps Data_10_10: goto No_Change Data_10_11: goto Increment ;X2 - goto No_Change Data_11_00: goto Two_Steps Data_11_01: goto Increment Data_11_10: goto Decrement ;X2 - goto No_Change Data_11_11: goto No_Change Increment: clrf Pos_Neg incfsz Counter, f goto End_Read goto Loop_0 Decrement: bsf Pos_Neg, 7 decfsz Counter, f goto End_Read goto Loop_0 No_Change: movf Counter, f skpz goto End_Read goto Loop_0 Two_Steps: movlw 1 ;X4 - movlw 2 btfsc Pos_Neg, 7 movlw -1 ;X4 - movlw -2 addwf Counter, f skpnz goto Loop_0 End_Read: movlw POS btfsc Counter, 7 movlw NEG movwf GPIO movlw -1 btfsc Counter, 7 movlw 1 addwf Counter,f movlw d'3' movwf Temp_1 decfsz Temp_1, f goto $-1 goto Loop_0 END --=20 http://www.piclist.com/techref/piclist PIC/SX FAQ & list archive View/change your membership options at http://mailman.mit.edu/mailman/listinfo/piclist .