Skip to content

Commit 1bd7412

Browse files
authored
Merge pull request #660 from adafruit/TheKitty-patch-22
Create DS2413.ino
2 parents 9f3c003 + ada53a9 commit 1bd7412

1 file changed

Lines changed: 125 additions & 0 deletions

File tree

Adafruit_DS2413/DS2413/DS2413.ino

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

0 commit comments

Comments
 (0)