Hardware Reference
In-Depth Information
Figure 11-1. Stop it's proof-of-concept setup
There are two common methods to accomplish the sweep of an LED in a series:
The first uses an array to hold the state of each LED and uses a loop to perform a series of
digital writes to each individual pin.
The other method is to directly manipulate the pin registers.
The register method is used for Stop it because it simplifies the program's logic. Register manipulation was
introduced in Chapter 6 to create a fast 10-bit digital-to-analog converter. The method for changing the pin state is the
same: a single integer is used to hold the pattern that will be used to turn on or off the pins using bitwise shifts along
with AND masks to turn the entire register at once. Stop it's code, shown in Listing 11-1, is broken up into 11 parts and
contains 12 functions.
Coding Stop It
Part 1 of Listing 11-1 sets up the variables for Stop it and the pins' data direction. The proof of concept has five
variables in total: one integer, one byte, and three Booleans. The integer is used to manipulate the pattern of the LEDs;
this variable is used for everything that will be displayed to the user, and also to determine the direction of the sweep
and whether a win has been archived. The byte variable is used to determine the level and to increase the speed of the
sweep. The Booleans are used as flags to tell what direction the sweep needs to travel, and tell if a win condition has
been achieved and if the button has been pressed.
 
Search WWH ::




Custom Search