Hi Josh, coincidentally I'm currently ... doing rotary encoder stuff=20 with XC8. :) A few comments. I found that having a filter circuit between the encoder and PIC pins is=20 essential. Unlike pushbuttons, I was not able to both "debounce" the=20 encoder and have react nicely to user input in software. I'm using=20 Bourns PEC09 series, and the datasheet gives a filter circuit that works=20 nicely. I don't have acceleration yet but that is next up. The algorithm I'm using is to sample (i.e. not interrupt on encoder=20 output changes) at 1Khz, I need a timer ISR at the rate for other things=20 anyway, and in that ISR I do the quadrature checking and check for=20 press, and report that to the main loop doing the UI. I have 2 encoders and it is simple enough for me to just replicate code.=20 Since it is in an ISR it needs to be efficient, but not critically so. If I wanted to make this flexible and extensible, I'd probably do=20 something like having one general-purpose encoder handling function=20 ("ProcessEncoder"), and pass to it the state of the A&B&press, and a=20 pointer to a variable/structure for it to output its computation to (or=20 just have the function return a 8/16-bit value indicating whatever). So=20 this way that encoder handler doesn't know or care what PIC pins are used. If the encoder function needs initialization or configuration, such as=20 telling it the range of what its output should be (i.e. 0-100, or 0-5,=20 etc.) that could be in the struct. So the struct could be something like..= .. { bool doInit; uint8_t minVal; uint8_t maxVal; uint8_t currVal; // encoder computes uint8_t buttonPressed; // encoder computes uint8_t encState; // encoder internal state info } So to setup the encoder, the main code would do this: doInit=3D true; minVal=3D 0; maxVal=3D 100; and call ProcessEncoder(). ProcessEncoder() would see that doInit is=20 true and then do whatever housekeeping is necessary (and set doInit to=20 false). Subsequently, in a timer or mainloop or in response to interrupts on the=20 PIC pins the encoder is wired to, you'd read the PIC pins, and do:=20 ProcessEncoder( A,B,button, &encoder1struct ); So now you have a general-purpose encoder library routine that can be=20 used with multiple encoders and called different ways (main loop, ISR,=20 ....) depending on the design. There are a bunch of variations on this theme but as other posters have=20 said it is the beginning of OOP and code reuse. Also as other posters=20 have said it is awkward to impossible to pass to a function the pin to=20 use; I handle that by having the caller of the function read the pins=20 necessary (and possibly set output pins in response to what the function=20 says to do.) A small amount of hard-coding but it is fast. Anyway, my extended 2c J Josh Koffman wrote: > Hi all, > > So as you may have guessed, I'm working on some rotary encoder routines i= n XC8. > > I've been looking around, and I've actually found a pretty neat > Arduino library. It handles the encoder, acceleration (values change > faster if you spin faster), encoder switch status (click, double > click, held, etc). I'm considering trying to port it to XC8 as I think > it would be useful, but also a really good exercise in coding. > > I have no Arduino experience, but from what I've been reading, it > appears that in their ecosystem you can define multiple instances of a > class. So in my case if I made one routine that dealt with a single > encoder, I could create 2 instances to deal with 2 encoders, etc. As > far as I understand it, I can't do that in straight C. > > So, what is the preferred way of dealing with this? I could make the > original function deal with total number of encoders that I have. Or I > could make multiple copies of a function that dealt with a single > encoder. In both cases I'll need to deal with variables carefully. > > Is there another (possibly better) way? I'm interested in this in the > abstract as eventually I will likely run into this situation with > other routines (switch debounce, for instance). > > Thanks! > > Josh > --=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 .