Hi all, I want to count pulses from a rotary encoder who's output is 2 channel gray code. I need to increment a counter when the encoder turns one way, and decrement it when it turns the other way. 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. My test circuit has the two signals connected to RB0 and RB1, and i'm using the INT0 and INT1 interrupts to detect the signal changes. For now, i'm working in C on a PIC18f252. The final device I'll use i'm not certain of yet but it will probably be one of the newer pic18 devices. But that's not really very important at this stage. 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. Here's my code so far. Conceptually, are they any optimizations i'm missing= ? 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; 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; if (PORTBbits.RB1 =3D=3D PORTBbits.RB0) { count--; } else { count++; } } 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 .