June
2006, Issue 191
Nontraditional
Cursor Control
ATmega32-Based
Motion Sensing
by
Andrew Sawchuk & Joseph Tanen
PROGRAM
DESIGN
Figure
2 is a block diagram of our program. First, we wait
for the computer to toggle the RTS line. When it does,
we send it the “MZ” message. We then respond to five
queries, disable query response, and start mouse functioning.
|

(Click
here to enlarge)
|
Figure
2—The software running on the Atmel ATmega32 microcontroller
loops through these stages to continually measure
and send information to the computer so the computer
can update the tracking of the mouse on the screen. |
After
the mouse has started functioning, the buttons are sampled
on every cycle. If the mouse isn’t enabled, the program
cycles through the button sampling until the mouse is
enabled. After the buttons are sampled and the mouse
is enabled, a check is done to see which acceleration
axes have been sampled. If X has not been sampled, the
sample multiplexer control variable is set to Y, the
ATmega32 microcontroller sleeps until X is ready, and
X is sampled when the ADC has finished processing.
The
multiplexer is set to Y before X is sampled because
the current sample being processed by the ADC is always
taken off of the current multiplexer value. As such,
if we want the ADC to sample Y, we have to set the sample
multiplexer control variable to Y before the desired
sampling period comes around.
If
X has been sampled, we check to see if Y has been sampled.
If it hasn’t been sampled, then the mux control variable
is set to X, the microcontroller sleeps until Y is ready,
and Y is sampled when the ADC has finished processing.
If Y has been sampled, the ATmega32 microcontroller
modifies the samples to the correct two’s complement
numbers, scales them, and creates the serial mouse data
packets. Then, the serial packets are sent, the mux
is set to indicate that X needs to be sampled, and the
program returns to the beginning of its sampling loop.
The mouse is enabled and disabled by one of the sampled
buttons. If the button value is read as pressed and
its previous value was not pressed, the mouse enable/disable
is toggled.
The
tricky parts of this program were the timing and the
conversion of the accelerometer output to the proper
two’s complement number. We had some problems with the
accelerometer readings. They became erratic on certain
types of reads, specifically when we moved the mouse
on the NW-SE plane. At first, we couldn’t figure out
how to get steady readings. But then we found a web
page addressing the problem that had been put up by
students who had taken our microcontroller programming
class. Those students had performed similar sampling
operations. To get good readings, they put their microprocessor
in Sleep mode until the result was ready.
We
tried simply adding sleep times immediately after we
began our ADC conversions. The entire setup then worked
as we expected it to. Some post-change information gathering
told us that to achieve the highest levels of accuracy
with the ADC, you have to put the microprocessor in
Sleep mode during conversions. We found that to be interesting.
We
formatted the numbers by performing mathematical operations
on them. Our math often caused unexpected overflows
though. These overflows, coupled with the fact that
we could never tell if we were casting the numbers properly,
prompted us to devise an almost purely bit-manipulative
method of converting the accelerometer outputs to numbers
that we could send to the computer (see Listing
1).