Datasheet
For more on integer constants in the different number bases, see the Arduino Integer
Constants (http://adafru.it/deR)page.
Reading GPIO Pins
Reading is a little trickier: You need to separate the individual pin values from the byte that is
returned from the read(). A read can return one of 5 values:
0x0 (B00000000) - Both pins LOW
0x1 (B00000001) - IOA = HIGH, IOB = LOW
0x2 (B00000010) - IOA = LOW, IOB = HIGH
0x3 (B00000011) - Both pins HIGH
0xFF (B11111111) - Read Failed!
To extract the state of an individual pin, you will need to use a little binary
math (http://adafru.it/deS). In particular the "bitwise AND" operator: "&".
If you AND the read value from the GPIO breakout with the bit pattern of the pin you are
interested in, you will get a boolean "TRUE" if the pin is HIGH and a "FALSE" if the pin is LOW.
The following code snippet prints "A" if IOA is HIGH and "B" if IOB is HIGH.
/* Write */
bool ok = false;
ok = write(B00000001);
if (!ok) Serial.println(F("Wire failed"));
delay(1000);
ok = write(B00000010);
if (!ok) Serial.println(F("Wire failed"));
delay(1000);
uint8_t state = read();
const int IOA = 0x1;
const int IOB = 0x2;
if (state == -1)
{
Serial.println(F("Failed reading the DS2413"));
}
else
{
if (state & IOA)
{
Serial.println("A");
}
if (state & IOB)
{
Serial.println("B");
}
}
© Adafruit Industries http://learn.adafruit.com/adafruit-1-wire-gpio-breakout-ds2413 Page 17 of 19