summaryrefslogtreecommitdiffstats
path: root/lib/OneWire/examples
diff options
context:
space:
mode:
Diffstat (limited to 'lib/OneWire/examples')
-rw-r--r--lib/OneWire/examples/DS18x20_Temperature/DS18x20_Temperature.pde112
-rw-r--r--lib/OneWire/examples/DS2408_Switch/DS2408_Switch.pde77
-rw-r--r--lib/OneWire/examples/DS250x_PROM/DS250x_PROM.pde90
3 files changed, 279 insertions, 0 deletions
diff --git a/lib/OneWire/examples/DS18x20_Temperature/DS18x20_Temperature.pde b/lib/OneWire/examples/DS18x20_Temperature/DS18x20_Temperature.pde
new file mode 100644
index 0000000..68ca194
--- /dev/null
+++ b/lib/OneWire/examples/DS18x20_Temperature/DS18x20_Temperature.pde
@@ -0,0 +1,112 @@
+#include <OneWire.h>
+
+// OneWire DS18S20, DS18B20, DS1822 Temperature Example
+//
+// http://www.pjrc.com/teensy/td_libs_OneWire.html
+//
+// The DallasTemperature library can do all this work for you!
+// http://milesburton.com/Dallas_Temperature_Control_Library
+
+OneWire ds(10); // on pin 10 (a 4.7K resistor is necessary)
+
+void setup(void) {
+ Serial.begin(9600);
+}
+
+void loop(void) {
+ byte i;
+ byte present = 0;
+ byte type_s;
+ byte data[12];
+ byte addr[8];
+ float celsius, fahrenheit;
+
+ if ( !ds.search(addr)) {
+ Serial.println("No more addresses.");
+ Serial.println();
+ ds.reset_search();
+ delay(250);
+ return;
+ }
+
+ Serial.print("ROM =");
+ for( i = 0; i < 8; i++) {
+ Serial.write(' ');
+ Serial.print(addr[i], HEX);
+ }
+
+ if (OneWire::crc8(addr, 7) != addr[7]) {
+ Serial.println("CRC is not valid!");
+ return;
+ }
+ Serial.println();
+
+ // the first ROM byte indicates which chip
+ switch (addr[0]) {
+ case 0x10:
+ Serial.println(" Chip = DS18S20"); // or old DS1820
+ type_s = 1;
+ break;
+ case 0x28:
+ Serial.println(" Chip = DS18B20");
+ type_s = 0;
+ break;
+ case 0x22:
+ Serial.println(" Chip = DS1822");
+ type_s = 0;
+ break;
+ default:
+ Serial.println("Device is not a DS18x20 family device.");
+ return;
+ }
+
+ ds.reset();
+ ds.select(addr);
+ ds.write(0x44, 1); // start conversion, with parasite power on at the end
+
+ delay(1000); // maybe 750ms is enough, maybe not
+ // we might do a ds.depower() here, but the reset will take care of it.
+
+ present = ds.reset();
+ ds.select(addr);
+ ds.write(0xBE); // Read Scratchpad
+
+ Serial.print(" Data = ");
+ Serial.print(present, HEX);
+ Serial.print(" ");
+ for ( i = 0; i < 9; i++) { // we need 9 bytes
+ data[i] = ds.read();
+ Serial.print(data[i], HEX);
+ Serial.print(" ");
+ }
+ Serial.print(" CRC=");
+ Serial.print(OneWire::crc8(data, 8), HEX);
+ Serial.println();
+
+ // Convert the data to actual temperature
+ // because the result is a 16 bit signed integer, it should
+ // be stored to an "int16_t" type, which is always 16 bits
+ // even when compiled on a 32 bit processor.
+ int16_t raw = (data[1] << 8) | data[0];
+ if (type_s) {
+ raw = raw << 3; // 9 bit resolution default
+ if (data[7] == 0x10) {
+ // "count remain" gives full 12 bit resolution
+ raw = (raw & 0xFFF0) + 12 - data[6];
+ }
+ } else {
+ byte cfg = (data[4] & 0x60);
+ // at lower res, the low bits are undefined, so let's zero them
+ if (cfg == 0x00) raw = raw & ~7; // 9 bit resolution, 93.75 ms
+ else if (cfg == 0x20) raw = raw & ~3; // 10 bit res, 187.5 ms
+ else if (cfg == 0x40) raw = raw & ~1; // 11 bit res, 375 ms
+ //// default is 12 bit resolution, 750 ms conversion time
+ }
+ celsius = (float)raw / 16.0;
+ fahrenheit = celsius * 1.8 + 32.0;
+ Serial.print(" Temperature = ");
+ Serial.print(celsius);
+ Serial.print(" Celsius, ");
+ Serial.print(fahrenheit);
+ Serial.println(" Fahrenheit");
+}
diff --git a/lib/OneWire/examples/DS2408_Switch/DS2408_Switch.pde b/lib/OneWire/examples/DS2408_Switch/DS2408_Switch.pde
new file mode 100644
index 0000000..d171f9b
--- /dev/null
+++ b/lib/OneWire/examples/DS2408_Switch/DS2408_Switch.pde
@@ -0,0 +1,77 @@
+#include <OneWire.h>
+
+/*
+ * DS2408 8-Channel Addressable Switch
+ *
+ * Writte by Glenn Trewitt, glenn at trewitt dot org
+ *
+ * Some notes about the DS2408:
+ * - Unlike most input/output ports, the DS2408 doesn't have mode bits to
+ * set whether the pins are input or output. If you issue a read command,
+ * they're inputs. If you write to them, they're outputs.
+ * - For reading from a switch, you should use 10K pull-up resisters.
+ */
+
+void PrintBytes(uint8_t* addr, uint8_t count, bool newline=0) {
+ for (uint8_t i = 0; i < count; i++) {
+ Serial.print(addr[i]>>4, HEX);
+ Serial.print(addr[i]&0x0f, HEX);
+ }
+ if (newline)
+ Serial.println();
+}
+
+void ReadAndReport(OneWire* net, uint8_t* addr) {
+ Serial.print(" Reading DS2408 ");
+ PrintBytes(addr, 8);
+ Serial.println();
+
+ uint8_t buf[13]; // Put everything in the buffer so we can compute CRC easily.
+ buf[0] = 0xF0; // Read PIO Registers
+ buf[1] = 0x88; // LSB address
+ buf[2] = 0x00; // MSB address
+ net->write_bytes(buf, 3);
+ net->read_bytes(buf+3, 10); // 3 cmd bytes, 6 data bytes, 2 0xFF, 2 CRC16
+ net->reset();
+
+ if (!OneWire::check_crc16(buf, 11, &buf[11])) {
+ Serial.print("CRC failure in DS2408 at ");
+ PrintBytes(addr, 8, true);
+ return;
+ }
+ Serial.print(" DS2408 data = ");
+ // First 3 bytes contain command, register address.
+ Serial.println(buf[3], BIN);
+}
+
+OneWire net(10); // on pin 10
+
+void setup(void) {
+ Serial.begin(9600);
+}
+
+void loop(void) {
+ byte i;
+ byte present = 0;
+ byte addr[8];
+
+ if (!net.search(addr)) {
+ Serial.print("No more addresses.\n");
+ net.reset_search();
+ delay(1000);
+ return;
+ }
+
+ if (OneWire::crc8(addr, 7) != addr[7]) {
+ Serial.print("CRC is not valid!\n");
+ return;
+ }
+
+ if (addr[0] != 0x29) {
+ PrintBytes(addr, 8);
+ Serial.print(" is not a DS2408.\n");
+ return;
+ }
+
+ ReadAndReport(&net, addr);
+}
diff --git a/lib/OneWire/examples/DS250x_PROM/DS250x_PROM.pde b/lib/OneWire/examples/DS250x_PROM/DS250x_PROM.pde
new file mode 100644
index 0000000..baa51c8
--- /dev/null
+++ b/lib/OneWire/examples/DS250x_PROM/DS250x_PROM.pde
@@ -0,0 +1,90 @@
+/*
+DS250x add-only programmable memory reader w/SKIP ROM.
+
+ The DS250x is a 512/1024bit add-only PROM(you can add data but cannot change the old one) that's used mainly for device identification purposes
+ like serial number, mfgr data, unique identifiers, etc. It uses the Maxim 1-wire bus.
+
+ This sketch will use the SKIP ROM function that skips the 1-Wire search phase since we only have one device connected in the bus on digital pin 6.
+ If more than one device is connected to the bus, it will fail.
+ Sketch will not verify if device connected is from the DS250x family since the skip rom function effectively skips the family-id byte readout.
+ thus it is possible to run this sketch with any Maxim OneWire device in which case the command CRC will most likely fail.
+ Sketch will only read the first page of memory(32bits) starting from the lower address(0000h), if more than 1 device is present, then use the sketch with search functions.
+ Remember to put a 4.7K pullup resistor between pin 6 and +Vcc
+
+ To change the range or ammount of data to read, simply change the data array size, LSB/MSB addresses and for loop iterations
+
+ This example code is in the public domain and is provided AS-IS.
+
+ Built with Arduino 0022 and PJRC OneWire 2.0 library http://www.pjrc.com/teensy/td_libs_OneWire.html
+
+ created by Guillermo Lovato <glovato@gmail.com>
+ march/2011
+
+ */
+
+#include <OneWire.h>
+OneWire ds(6); // OneWire bus on digital pin 6
+void setup() {
+ Serial.begin (9600);
+}
+
+void loop() {
+ byte i; // This is for the for loops
+ boolean present; // device present var
+ byte data[32]; // container for the data from device
+ byte leemem[3] = { // array with the commands to initiate a read, DS250x devices expect 3 bytes to start a read: command,LSB&MSB adresses
+ 0xF0 , 0x00 , 0x00 }; // 0xF0 is the Read Data command, followed by 00h 00h as starting address(the beginning, 0000h)
+ byte ccrc; // Variable to store the command CRC
+ byte ccrc_calc;
+
+ present = ds.reset(); // OneWire bus reset, always needed to start operation on the bus, returns a 1/TRUE if there's a device present.
+ ds.skip(); // Skip ROM search
+
+ if (present == TRUE){ // We only try to read the data if there's a device present
+ Serial.println("DS250x device present");
+ ds.write(leemem[0],1); // Read data command, leave ghost power on
+ ds.write(leemem[1],1); // LSB starting address, leave ghost power on
+ ds.write(leemem[2],1); // MSB starting address, leave ghost power on
+
+ ccrc = ds.read(); // DS250x generates a CRC for the command we sent, we assign a read slot and store it's value
+ ccrc_calc = OneWire::crc8(leemem, 3); // We calculate the CRC of the commands we sent using the library function and store it
+
+ if ( ccrc_calc != ccrc) { // Then we compare it to the value the ds250x calculated, if it fails, we print debug messages and abort
+ Serial.println("Invalid command CRC!");
+ Serial.print("Calculated CRC:");
+ Serial.println(ccrc_calc,HEX); // HEX makes it easier to observe and compare
+ Serial.print("DS250x readback CRC:");
+ Serial.println(ccrc,HEX);
+ return; // Since CRC failed, we abort the rest of the loop and start over
+ }
+ Serial.println("Data is: "); // For the printout of the data
+ for ( i = 0; i < 32; i++) { // Now it's time to read the PROM data itself, each page is 32 bytes so we need 32 read commands
+ data[i] = ds.read(); // we store each read byte to a different position in the data array
+ Serial.print(data[i]); // printout in ASCII
+ Serial.print(" "); // blank space
+ }
+ Serial.println();
+ delay(5000); // Delay so we don't saturate the serial output
+ }
+ else { // Nothing is connected in the bus
+ Serial.println("Nothing connected");
+ delay(3000);
+ }
+}
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+