In SX Microcontrollers, SX/B Compiler and SX-Key Tool, mrchadwickusa wrote: I haven't tested this (no hardware eh?) but it compiles. This saves a couple bytes, at the cost of using a second temp in the interrupt. Since you have both encoders on the same port, there is no reason you can't save both channels in one oldbyte and save space. You can also do the shifting and xoring of both channels at the same time, since you only look at one bit of the results to determine direction. This also lends itself to extension to 4 channels on one 8 bit port, by adding the appropriate bit tests for additional channels. Or make the thing a loop with a bit mask to test the change and direction bits and increment or decrement the positions in an array. [code] [code]DEVICE SX28, OSC4MHZ, TURBO, STACKX, OPTIONX FREQ 4_000_000,4_480_000 ID "ENCODER" [code]EncPort VAR RA ' encoder port TRIS_Enc VAR TRIS_A MaxVal CON 255 [code] 'encCheck VAR Byte EncodersOld VAR byte EncodersNew VAR byte Encoder_A_Count VAR byte Encoder_B_Count VAR byte tmpB1 VAR byte tmpB2 VAR Byte tmpB3 VAR Byte ' ========================================================================= INTERRUPT ' ========================================================================= ' Runs every 64 uS @ 4 MHz (no prescaler) ISR_Start: EncodersNew = EncPort TmpB1 = EncodersOld XOR EncodersNew ' compare old with new ' to work out which channels have changed TmpB2 = TmpB1 << 1 ' make single bit that shows change on A or B phase TmpB1 = TmpB1 OR TmpB2 EncodersOld = EncodersOld << 1 ' make direction bits by comparing old A with new B EncodersOld = EncodersOld XOR EncodersNew ' xor old A with new B IF TmpB1.3 = 1 THEN IF EncodersOld.3 = 1 THEN INC Encoder_A_Count ELSE DEC Encoder_A_Count ENDIF ENDIF ' ========================================================================= IF tmpB1.1 = 1 THEN IF EncodersOld.1 = 1 THEN INC Encoder_B_Count ELSE DEC Encoder_B_Count ENDIF ENDIF EncodersOld=EncodersNew [code]'ISR_Exit: RETURNINT [code]' ========================================================================= PROGRAM Start ' ========================================================================= Start: EncodersNew = EncPort EncodersOld = EncodersNew Encoder_A_Count = 0 Encoder_B_Count = 0 OPTION = $88 ' interrupt, no prescaler Main: DO ShiftOut RC.7,RC.6,1,Encoder_A_Count Pulsout RC.0,1 ShiftOut RC.7,RC.6,1,Encoder_B_Count Pulsout RC.0,1 LOOP [/code] ---------- End of Message ---------- You can view the post on-line at: http://forums.parallax.com/forums/default.aspx?f=7&p=1&m=147541#m147660 Need assistance? Send an email to the Forum Administrator at forumadmin@parallax.com The Parallax Forums are powered by dotNetBB Forums, copyright 2002-2006 (http://www.dotNetBB.com)