/ Adafruit_DS2413 / DS2413 / DS2413.ino
DS2413.ino
  1  // SPDX-FileCopyrightText: 2014 Bill Earl for Adafruit Industries
  2  //
  3  // SPDX-License-Identifier: MIT
  4  
  5  #include <OneWire.h>
  6  
  7  #define DS2413_ONEWIRE_PIN  (8)
  8  
  9  #define DS2413_FAMILY_ID    0x3A
 10  #define DS2413_ACCESS_READ  0xF5
 11  #define DS2413_ACCESS_WRITE 0x5A
 12  #define DS2413_ACK_SUCCESS  0xAA
 13  #define DS2413_ACK_ERROR    0xFF
 14  
 15  OneWire oneWire(DS2413_ONEWIRE_PIN);
 16  uint8_t address[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
 17  
 18  void printBytes(uint8_t* addr, uint8_t count, bool newline=0) 
 19  {
 20    for (uint8_t i = 0; i < count; i++) 
 21    {
 22      Serial.print(addr[i]>>4, HEX);
 23      Serial.print(addr[i]&0x0f, HEX);
 24      Serial.print(" ");
 25    }
 26    if (newline)
 27    {
 28      Serial.println();
 29    }
 30  }
 31  
 32  byte read(void)
 33  {		
 34    bool ok = false;
 35    uint8_t results;
 36  
 37    oneWire.reset();
 38    oneWire.select(address);
 39    oneWire.write(DS2413_ACCESS_READ);
 40  
 41    results = oneWire.read();                 /* Get the register results   */
 42    ok = (!results & 0x0F) == (results >> 4); /* Compare nibbles            */
 43    results &= 0x0F;                          /* Clear inverted values      */
 44  
 45    oneWire.reset();
 46    
 47    // return ok ? results : -1;
 48    return results;
 49  }
 50  
 51  bool write(uint8_t state)
 52  {
 53    uint8_t ack = 0;
 54    
 55    /* Top six bits must '1' */
 56    state |= 0xFC;
 57    
 58    oneWire.reset();
 59    oneWire.select(address);
 60    oneWire.write(DS2413_ACCESS_WRITE);
 61    oneWire.write(state);
 62    oneWire.write(~state);                    /* Invert data and resend     */    
 63    ack = oneWire.read();                     /* 0xAA=success, 0xFF=failure */  
 64    if (ack == DS2413_ACK_SUCCESS)
 65    {
 66      oneWire.read();                          /* Read the status byte      */
 67    }
 68    oneWire.reset();
 69      
 70    return (ack == DS2413_ACK_SUCCESS ? true : false);
 71  }
 72  
 73  void setup(void) 
 74  {
 75    Serial.begin(9600);  
 76    
 77    Serial.println(F("Looking for a DS2413 on the bus"));
 78    
 79    /* Try to find a device on the bus */
 80    oneWire.reset_search();
 81    delay(250);
 82    if (!oneWire.search(address)) 
 83    {
 84      printBytes(address, 8);
 85      Serial.println(F("No device found on the bus!"));
 86      oneWire.reset_search();
 87      while(1);
 88    }
 89    
 90    /* Check the CRC in the device address */
 91    if (OneWire::crc8(address, 7) != address[7]) 
 92    {
 93      Serial.println(F("Invalid CRC!"));
 94      while(1);
 95    }
 96    
 97    /* Make sure we have a DS2413 */
 98    if (address[0] != DS2413_FAMILY_ID) 
 99    {
100      printBytes(address, 8);
101      Serial.println(F(" is not a DS2413!"));
102      while(1);
103    }
104    
105    Serial.print(F("Found a DS2413: "));
106    printBytes(address, 8);
107    Serial.println(F(""));
108  }
109  
110  void loop(void) 
111  {
112    /* Read */
113    /*
114    uint8_t state = read();
115    if (state == -1)
116      Serial.println(F("Failed reading the DS2413"));
117    else
118      Serial.println(state, BIN);
119    */
120      
121    /* Write */
122    bool ok = false;
123    ok = write(0x3);
124    if (!ok) Serial.println(F("Wire failed"));
125    delay(1000);
126    ok = write(0x0);
127    if (!ok) Serial.println(F("Wire failed"));
128    delay(1000);
129  }