Your code aside, if you're looking for speed maybe you should be writing it= in assembly? I don't know what kind of optimization your compiler is using= so maybe it's a moot point? Are you concerned about counting every transit= ion or if it's accurate will ever other transition work? When writing encod= er code I found a number of ways to write it. It was a while ago so I don't= remember the source (sorry) but I know it helped me to read about the stra= tegies before I was coding to see what the payoff was.=20 Best of luck, Charles, AC0GD --- This e-mail was sent from my iPhone to provide you my quickest response. Pl= ease forgive the "corrected" misspellings and brevity of my mobile e-mails. > On Jan 17, 2014, at 8:59 AM, slippyr4 wrote: >=20 > Hi all, >=20 > I want to count pulses from a rotary encoder who's output is 2 channel gr= ay > code. I need to increment a counter when the encoder turns one way, and > decrement it when it turns the other way. >=20 > I want to handle the signals as fast as I can (so that I can support the > highest frequency of signal I can get away with). Numerous models of the > encoder are available, and the faster I can get the code, the better > resolution encoder I can use. >=20 > My test circuit has the two signals connected to RB0 and RB1, and i'm usi= ng > the INT0 and INT1 interrupts to detect the signal changes. >=20 > For now, i'm working in C on a PIC18f252. The final device I'll use i'm n= ot > certain of yet but it will probably be one of the newer pic18 devices. Bu= t > that's not really very important at this stage. >=20 > I might consider reworking the code in assembler if there's a significant > gain to be had, but really at this stage i'm wondering if my *logic *is > correct, or could be improved. >=20 > Here's my code so far. Conceptually, are they any optimizations i'm missi= ng? >=20 > if (INTCONbits.INT0IF) // INT0 & RB0 are encoder channel A > { > // change the edge we fire on > INTCON2bits.INTEDG0 =3D !PORTBbits.RB0; > // clear the interrupt > INTCONbits.INT0IF=3D0; >=20 > if (PORTBbits.RB0 =3D=3D PORTBbits.RB1) > { > count++; > } > else > { > count--; > } > } > else if ( INTCON3bits.INT1IF ) // INT1 & RB1 are encoder channel B > { > // change the edge we fire on > INTCON2bits.INTEDG1 =3D !PORTBbits.RB1; > // clear the interrupt flag > INTCON3bits.INT1IF=3D0; >=20 > if (PORTBbits.RB1 =3D=3D PORTBbits.RB0) > { > count--; > } > else > { > count++; > } > } >=20 > thanks > slip > --=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 --=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 .