> I have a suggestion of something to add that will make it even more useful. > Track the times beween rising and falling edges, calc and store away a > current velocity and acceleration value in a register somewhere, and send it > out as part of the poll.....i.e. edges per msec, and edges per msec-squared. > > Thanks, > > - Ted Ted, This would typically be a part of the higher level software in whatever device was polling the PIC. BTW: here is a snippet of how C code would interpret the values coming back from the poll. Note that the 16 bit counters in the PIC can overflow quite quickly, so the next level of code has to handle the rollover intelligently. It turns out that this is very simple to do. long pos1 = 0; // assumed initial positions of encoders long pos2 = 0; unsigned short last_count1 = 0; unsigned short last_count2 = 0; unsigned short last_faults1 = 0; unsigned short last_faults2 = 0; unsigned char new_faults1, new_faults2; for (;;) // process forever { unsigned short count1, count2; unsigned char faults1, faults2; // get the values from the encoder pollencoder( &count1, &count2, &faults1, &faults2 ); // We compute the amount the count has changed. It turns out the unsigned // arithmetic used below will properly handle the rollover cases. The result of // the subtraction is cast to a signed value before adding it to the current position // to compute the new position. pos1 = (short) (count1 - last_count1); pos2 = (short) (count2 - last_count2); last_count1 = count1; last_count2 = count2; // We compute the number of new faults, if any. Again, wraparound arithmetic // does the right thing for us. new_faults1 = faults1 - last_faults1; new_faults2 = faults2 - last_faults2; last_faults1 = faults1; last_faults2 = faults2; if ( new_faults1 ) report_faults( 1, new_faults1 ); if ( new_faults2 ) report_faults( 2, new_faults2 ); } Bob Ammerman RAm Systems -- http://www.piclist.com hint: To leave the PICList mailto:piclist-unsubscribe-request@mitvma.mit.edu