While the code has been reshaped considerably here, the idea of using a Gray code to detect jitter comes from an include file I wrote for Great Cow Basic, and even earlier back to an idea suggested in an Arduino implementation by Oleg Mazurov. The result is exceedingly stable and responsive, yet imposes no hardware or software penalties for debouncing.
Compilation Constants
Your program should define four compilation constants which indicate the pins to which the rotary decoder is connected. Here's an example for the common KY-040 model and the PIC16F88:
{$DEFINE ENCODER_PORT 'PortA'} // switch on Port A
{$DEFINE ENCODER_SW 0} // center push on A.0
{$DEFINE ENCODER_A 1} // A line on A.1
{$DEFINE ENCODER_B 2} // B line on A.2
Note that Encoder_A and Encoder_B are sometimes referred to as CLK and DT, respectively.
All three of these pins should normally have 10k pull-up resistors attached. The aforementioned KY-040 encoder device already includes these.
Command
There's a single command. (The setup and initialization of the port pin is taken care of automatically).
procedure encoder(var enc_ret : integer; var enc_set : integer; enc_max : integer);
To invoke this command, use: encoder(enc_value, enc_set, enc_max), where:
- enc_value returns the current encoder value,
- enc_set returns the current value locked in (after pushing the button),
- the minimum value is 0, while the maximum is specified by
- the input parameter enc_max, but may be no greater than 16383.
Add It to Your Library Collection
You can get the rotary encoder library unit "RotaryEncoder.pas" by clicking the following link:
Be sure to read over the source code for additional details on how to use it.
Try It Out
If you'd like to see it in operation, then head over to the exercises to find the incremental rotary encoder exercise. Click here to go to it.
No comments:
Post a Comment