Hardware Reference
In-Depth Information
as the Arduino Due or an Intel-based device such as the Galileo. It has to be
recompiled. Even worse, not all AVRs are the same; there are differences in
models that make binary code imports impossible. In short, releasing a binary-
only library makes that library usable on a single Arduino model.
Example Library
In Chapter 25, you created a shield for Arduino based on the PCF8574AP. Now
it is time to write a library to use this device. If you haven't created your shield
yet, or if you haven't received it, don't worry; you can still use the breadboard
version presented in that chapter, which works in exactly the same way.
The Library
The I 2 C expander shield contains two PCF8574AP chips, both of which have
coni gurable addresses. Therefore, you must select two addresses to use for
your devices. You can choose which device will be the i rst selected—either
chip 0 or chip 1 depending on the application. This will be handled in the con-
structor. The two addresses must be stored inside the class for the rest of the
application to work. To do this, they will be saved as two 8-bit variables called
_chip0Address and _chip1Address . Part of the job of the expander shield is to
provide extra outputs: two banks of 8 pins. To make this easier to the user, the
library should be designed to allow three different write operations: bit by bit,
8-bit writes, or 16-bit writes. The Arduino naming convention states that these
operations should be called write, and the functions will be called writeBit() ,
writeByte() , and writeWord() . To write a bit, two values are required: the bit
to write and its position. The bit will be a boolean, and the position will be an
8-bit value. To write a byte, again, two values are required: the byte to write
and which device to use. The byte will be coded as a byte (naturally), and the
device will be a Bool : 0 for device 0 and 1 for device 1. To write a 16-bit word,
only one parameter is required, the word itself. All three functions should return
a boolean: true if the operation succeeded and false otherwise.
The other part of the expander's job is to read data. Three functions need
to be created to read data. The Arduino naming convention states that they
should be called read: readBit() , readByte() , and readWord() . The readBit()
function should require one parameter, the bit to read, and output a boolean.
The readByte() function requires one parameter, the chip ID, as a boolean,
and returns a byte. The readWord() function does not require any parameters
and returns a word.
Since these devices are I 2 C devices, they will also require the Wire library.
There is one thing that should be taken into account. The user might want to
write a bit of data to one of the chips, but how do you do that without affecting
the other bits? Well, as far as the chip is concerned, you can't. You can write only
 
Search WWH ::




Custom Search