Below are segments of code for reading an encoder. It's written in C using the CCS compiler (BTW: I think the compiler its great value, even with its faults). I'm using the RTCC to generate an interrupt when the encoder is turned, in addition to using it to debounce the encoder inputs. Initially the RTCC is set up to be clocked externally, (from the encoder A line ENC_A) and the RTCC is preloaded with 255 (FFh). When the encoder A line goes from Hi to low, the RTCC is incremented from 255 to 0 and an interrupt is generated. The ISR then reads the state of the other encoder line ENC_B and increments/decrements a variable (current_data). The ISR then changes the mode of the RTCC to be internally clocked. This stops the encoder lines bouncing and generating false interrupts. Once the RTCC overflows, an interrupt is generated and a counter is decremented. When this counter reaches 0 the debounce delay has expired and the ISR changes the RTCC mode back to being clocked externally, etc... I hope this helps. Peter #define ENC_A a_port.a4 // Pin used to read the encoder A output #define ENC_B b_port.b3 // Pin used to read the encoder B output byte current_data; // This variable will be incremented or // decremented by the encoder ISR as the encoder is // rotated. byte encoder_timer; // Used by the encoder ISR for debouncing the //encoder inputs. boolean encoder_intrpt; // Indicates whether the RTCC interrupt is caused // by an encoder change (external) or from the RTCC // counter overflowing (internal). // Setup the RTCC to clock from the internal clock #inline void set_rtcc_internal_clock() // CCS compiler workaround { #asm movlw 0x81 movwf 0x04 bcf 0x00,5 #endasm } // Process the RTCC interrupt. #int_rtcc encoder_isr() { if (encoder_intrpt) // If the interrupt was generated by the { // ENC_A input, test the ENC_B input to if (ENC_B) // determine the direction the encoder has { // been rotated. Update the current data current_data--; // accordingly. } else { current_data++; } set_rtcc_internal_clock(); // Setup the RTCC to perform encoder input set_rtcc(0); // debouncing. encoder_timer = 50; encoder_intrpt = FALSE; // Indicate that the next RTCC interrupt } // will be from the debouncing function else { encoder_timer--; // Debounce the encoder input. if (!encoder_timer) { set_rtcc_ext_h_to_lo(); // Set the next interrupt to be from encoder_intrpt = TRUE; // the ENC_A input. set_rtcc(255); } } } --- Peter Homann Email: peterh@adacel.com.au Adacel Technologies Ltd _/_/_/ _/_/_/ _/ Phone: +61 3 9596 2991 250 Bay St, Brighton _/ _/ _/ _/ Fax: +61 3 9596 2960 Victoria 3186 _/_/_/ _/ _/ Home: +61 3 9555 5603 AUSTRALIA _/ _/ _/ _/_/_/Mobile: 0414-494578 ------------------------------------------------------------------------ |-----Original Message----- |From: pic microcontroller discussion list |[mailto:PICLIST@MITVMA.MIT.EDU]On Behalf Of Robert M. Bailey |Sent: Wednesday, 24 March 1999 11:44 |To: PICLIST@MITVMA.MIT.EDU |Subject: encoder question | | |Hello everyone, |Does anyone know of any published projects that use a |differential encoder |with a PIC? |Any tips or hints for the same would be helpful as well. |Thanks in advance |Bob Bailey |