decrement in value based upon rotational direction.
As a final function, the interrupt service routine
determines the knob switch state (ON, OFF) and
provides debounce for this switch. The Timer 1
interrupt service code is shown in Figure 7.
Scrolling Text Display
Application Using the
The first demo verifies operation of the rotary
encoder. How about a demo that uses the encoder
in a real application? In this second demo
(Scrolling demo.MCP), we use the encoder to
assist the user in scrolling through a large amount
of text that is stored in the microcontroller Flash
memory and viewed on the LCD. Our
Experimenter LCD is a 16 x 3 display, limiting
how much text we can view at a time. In this
demo, we have hardcoded President Lincoln’s
entire Gettysburg Address in microcontroller
Flash. The speech is approximately 1,172 characters
and 64 lines of text. The rotary encoder will be
used to scroll backward and forward to facilitate
viewing the entire speech one line at a time.
We use the same interrupt and timer set-ups as in the
earlier demo, and to this add two new functions:
■ FIGURE 8. Scrolling demo.
• Scroll Down() — This function advances forward to the
next line of text in the speech. For each rotary detent, it
retrieves this line for display and halts when it reaches
the last line of the speech.
• Scroll Back() — This function moves backwards to the
previous line of text in the speech. For each rotary
detent, it retrieves this line for display and halts when it
reaches the first line of the speech.
Timers are a broad subject and there are many uses for them.
A good reference book for this material and other applications
is available in the Nuts & Volts Webstore entitled Programming
16 bit Microcontrollers in C by Lucio Di Jasio. For your
convenience, there is mini-kit for this article containing all
interface electronics, mechanical connectors, and the
associated printed circuit board available from Nuts & Volts.
The mini-kit not only contains a rotary knob interface but
also a full PS/2 keyboard interface. We’ll cover more of
this keyboard interface at another time. NV
Figure 8 shows the scrolling demo in action. Enjoy!
The “Mini Kit” used in this article can be purchased online
from the Nuts & Volts Webstore at www.nutsvolts.com
or call our order desk at 800-783-4624.
Where To Go
■ FIGURE 9. Prototype
We have covered a new
interface capability for the
Experimenter. The rotary encoder
really increases our “bag of tricks’
and has wide applicability. We also
discussed the PIC24F timer and its
interrupt capability, applying timers
to interface to these input devices.
■ FIGURE 10. Rotary Encoder
Qty Part List Source
3 10K Resistor Various
1 Rotary Encoder SparkFun.com COM-09117
December 2010 35