Hardware Reference
In-Depth Information
And that's it. All that needs to be done is to copy the values sent as param-
eters into private variables. Coni guration of the chip is done in begin() and
will look like this:
void PCF8574AP::begin()
{
Wire.begin();
// Set all pins of chip 0 to HIGH
_chip0Output = 0xFF;
Wire.beginTransmission(_chip0Address);
Wire.write(_chip0Output);
Wire.endTransmission();
// Do the same for chip 1
_chip1Output = 0xFF;
Wire.beginTransmission(_chip1Address);
Wire.write(_chip1Output);
Wire.endTransmission();
}
The function begins by calling Wire.begin() . Why does it do that? Although
the device requires the Wire library for communication, the user doesn't need
to know exactly how the shield is connected. It's up to this function to initialize
the I 2 C library and start communication with the chips. Next, the function then
sets both output variables to 0xFF (or, in binary, 1111 1111 ). It then proceeds to
write that value to each of the two chips. When the chips i rst power on, this is
their default state. So why does this function do that, if this is what is expected?
There is no guarantee that the device was powered on; it might just have been
reset, or the device is in an unstable state. This makes sure that the device is in
a known coni guration before continuing.
Now to read data. The easiest function to accomplish is readByte() . It simply
reads the 8 bits of the chip and returns that data.
uint8_t PCF8574AP::readByte(bool chipSelect)
{
byte _data = 0;
if(chipSelect == 1)
Wire.requestFrom(_chip1Address, 1);
else
Wire.requestFrom(_chip0Address, 1);
if(Wire.available())
{
_data = Wire.read();
}
Search WWH ::




Custom Search