Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
22 | jlesech | 1 | /* |
2 | DS250x add-only programmable memory reader w/SKIP ROM. |
||
3 | |||
4 | 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 |
||
5 | like serial number, mfgr data, unique identifiers, etc. It uses the Maxim 1-wire bus. |
||
6 | |||
7 | 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. |
||
8 | If more than one device is connected to the bus, it will fail. |
||
9 | Sketch will not verify if device connected is from the DS250x family since the skip rom function effectively skips the family-id byte readout. |
||
10 | thus it is possible to run this sketch with any Maxim OneWire device in which case the command CRC will most likely fail. |
||
11 | 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. |
||
12 | Remember to put a 4.7K pullup resistor between pin 6 and +Vcc |
||
13 | |||
14 | To change the range or ammount of data to read, simply change the data array size, LSB/MSB addresses and for loop iterations |
||
15 | |||
16 | This example code is in the public domain and is provided AS-IS. |
||
17 | |||
18 | Built with Arduino 0022 and PJRC OneWire 2.0 library http://www.pjrc.com/teensy/td_libs_OneWire.html |
||
19 | |||
20 | created by Guillermo Lovato <glovato@gmail.com> |
||
21 | march/2011 |
||
22 | |||
23 | */ |
||
24 | |||
25 | #include <OneWire.h> |
||
26 | OneWire ds(6); // OneWire bus on digital pin 6 |
||
27 | void setup() { |
||
28 | Serial.begin (9600); |
||
29 | } |
||
30 | |||
31 | void loop() { |
||
32 | byte i; // This is for the for loops |
||
33 | boolean present; // device present var |
||
34 | byte data[32]; // container for the data from device |
||
35 | byte leemem[3] = { // array with the commands to initiate a read, DS250x devices expect 3 bytes to start a read: command,LSB&MSB adresses |
||
36 | 0xF0 , 0x00 , 0x00 }; // 0xF0 is the Read Data command, followed by 00h 00h as starting address(the beginning, 0000h) |
||
37 | byte ccrc; // Variable to store the command CRC |
||
38 | byte ccrc_calc; |
||
39 | |||
40 | present = ds.reset(); // OneWire bus reset, always needed to start operation on the bus, returns a 1/TRUE if there's a device present. |
||
41 | ds.skip(); // Skip ROM search |
||
42 | |||
43 | if (present == TRUE){ // We only try to read the data if there's a device present |
||
44 | Serial.println("DS250x device present"); |
||
45 | ds.write(leemem[0],1); // Read data command, leave ghost power on |
||
46 | ds.write(leemem[1],1); // LSB starting address, leave ghost power on |
||
47 | ds.write(leemem[2],1); // MSB starting address, leave ghost power on |
||
48 | |||
49 | ccrc = ds.read(); // DS250x generates a CRC for the command we sent, we assign a read slot and store it's value |
||
50 | ccrc_calc = OneWire::crc8(leemem, 3); // We calculate the CRC of the commands we sent using the library function and store it |
||
51 | |||
52 | if ( ccrc_calc != ccrc) { // Then we compare it to the value the ds250x calculated, if it fails, we print debug messages and abort |
||
53 | Serial.println("Invalid command CRC!"); |
||
54 | Serial.print("Calculated CRC:"); |
||
55 | Serial.println(ccrc_calc,HEX); // HEX makes it easier to observe and compare |
||
56 | Serial.print("DS250x readback CRC:"); |
||
57 | Serial.println(ccrc,HEX); |
||
58 | return; // Since CRC failed, we abort the rest of the loop and start over |
||
59 | } |
||
60 | Serial.println("Data is: "); // For the printout of the data |
||
61 | 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 |
||
62 | data[i] = ds.read(); // we store each read byte to a different position in the data array |
||
63 | Serial.print(data[i]); // printout in ASCII |
||
64 | Serial.print(" "); // blank space |
||
65 | } |
||
66 | Serial.println(); |
||
67 | delay(5000); // Delay so we don't saturate the serial output |
||
68 | } |
||
69 | else { // Nothing is connected in the bus |
||
70 | Serial.println("Nothing connected"); |
||
71 | delay(3000); |
||
72 | } |
||
73 | } |
||
74 | |||
75 | |||
76 | |||
77 | |||
78 | |||
79 | |||
80 | |||
81 | |||
82 | |||
83 | |||
84 | |||
85 | |||
86 | |||
87 | |||
88 | |||
89 | |||
90 |