by Joe Pardue
Get started now with this series!
Joe’s book & kit are available in our
webstore at www.nutsvolts.com
Registers, and the Butterfly Joystick
In Part 6, we used the Butterfly joystick with library functions
to implement a menu navigation system but didn't go into any
details of how the joystick worked. This month, we will use the
joystick as an introduction to AVR interrupts and I/O registers.
The Butterfly Joystick Hardware
The original joystick was an early aircraft control lever
that allowed the pilot to maneuver the plane left, right,
up, and down. Fighter planes added a center button to fire
machine guns. The Butterfly joystick — like many menu
navigation devices found on remote controls and cell phones
— has five positions: UP, DOWN, LEFT, RIGHT, and PUSH
(see Figure 1). Manufacturers, of course can't know which
direction the user will orient their device, so they label them
A, B, C, D, and Center (for PUSH) as in Figure 2, which
correspond to pins numbered as in Figure 3
The Butterfly orients these pins and connects them to
five of the ATmega169 port pins as in Figure 4.
Now that we've got the joystick all wired up, how does
the Butterfly know when a joystick event occurs?
Interrupts In General
In microcontrollers, we typically use either polling or
interrupts to check to see if an event has occurred. Polling
checks periodically to see if an event has happened. For
instance, in the loop in main() we might check pin 6 to see if
the voltage is + 3 or 0 and, depending on the state, do one
thing or another. If the microcontroller hardware is designed
so that pin 6 can be used to interrupt the program, then we
don't have to poll the pin. We can set the software so that
when the pin state changes — say, falling from +3V to 0V —
an interrupt function will be called automatically.
Interrupts on microcontrollers are like interrupts in daily
life. The telephone interrupts your activities by its insistent
ringing. Imagine how it would be if you had to poll the
telephone to receive calls. Periodically, you'd pick up the
receiver and say 'Hello, anybody out there?' and your caller
would shriek, 'I've been waiting for an hour! Why don't you
check your phone every five minutes like a normal person?'
The ring of the phone interrupting workflow is annoying, but
if someone wants to tell you that your garage is on fire, you
want to know about it immediately.
Microcontrollers respond to interrupts much like you would.
Maybe you are reading a book and the phone rings. You use your
fingernail to mark the line you were reading and dog-ear the
page before closing the book (librarians everywhere groan). Then,
you answer the phone, and when the call is finished and you've
put out the fire in your garage, you can refer to the desecrations to your book and go right back to where you left off.
From the hardware perspective, an interrupt causes the
microcontroller to stop what it is doing, store sufficient data
so that later it can get back to what it was doing, look to see
which interrupt happened, run the interrupt code, and when
finished, restore the machine to its state before the interrupt
occurred using the previously stored data.
Potential Interrupt Bug 1
Interrupts are great, but they provide an avenue for
■ FIGURE 1. Butterfly joystick with five button positions.
■ FIGURE 2. Joystick schematic.
March 2009 67