![](/media/cache/profiles/32cfdce9631d8c7906e8e9d6e68b514b.50x50_q85.jpg)
// Demo on how to identify and read multiple DS18B20 connected to the same bus. // Parasitic power is not used in this example. // This program is based on the sample code from Maxim/Dallas application // note 162 (http://www.maxim-ic.com/app-notes/index.mvp/id/162). Program output should look like this: *** Test with multiple DS18B20 *** Memory allocated for 20 devices. Scanning for devices... ROM CODE =62:00:00:01:CB:25:CA:28 1 ROM CODE =B6:00:00:01:CB:1B:9E:28 2 ROM CODE =66:00:00:01:CB:28:59:28 3 3 devices found. Scanning completed. Temp: 022.6875 Device: 000001CB25CA 001 Temp: 022.6875 Device: 000001CB1B9E 002 Temp: 027.6250 Device: 000001CB2859 003 Temp: 022.6250 Device: 000001CB25CA 001 Temp: 022.6875 Device: 000001CB1B9E 002 Temp: 025.3125 Device: 000001CB2859 003 Temp: 022.8125 Device: 000001CB25CA 001 Temp: 024.1875 Device: 000001CB1B9E 002 Temp: 023.7500 Device: 000001CB2859 003
Diff: main.cpp
- Revision:
- 4:6ade7fcb2925
- Parent:
- 3:28b7b7fddede
- Child:
- 7:cf5affdab535
diff -r 28b7b7fddede -r 6ade7fcb2925 main.cpp --- a/main.cpp Mon Jan 23 10:13:57 2012 +0000 +++ b/main.cpp Thu Jan 28 09:05:50 2016 +0000 @@ -7,6 +7,31 @@ // note 162 (http://www.maxim-ic.com/app-notes/index.mvp/id/162). ////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////// +// Program output should look like this: +// *** Test with multiple DS18B20 *** +// Memory allocated for 20 devices. +// Scanning for devices... +// ROM CODE =62:00:00:01:CB:25:CA:28 1 +// ROM CODE =B6:00:00:01:CB:1B:9E:28 2 +// ROM CODE =66:00:00:01:CB:28:59:28 3 +// 3 devices found. Scanning completed. +// Temp: 022.6875 Device: 000001CB25CA 001 +// Temp: 022.6875 Device: 000001CB1B9E 002 +// Temp: 027.6250 Device: 000001CB2859 003 +// Temp: 022.6250 Device: 000001CB25CA 001 +// Temp: 022.6875 Device: 000001CB1B9E 002 +// Temp: 025.3125 Device: 000001CB2859 003 +// Temp: 022.8125 Device: 000001CB25CA 001 +// Temp: 024.1875 Device: 000001CB1B9E 002 +// Temp: 023.7500 Device: 000001CB2859 003 +////////////////////////////////////////////////////////////////////////////// +////////////////////////////////////////////////////////////////////////////// +// I have found that adding a small capacitor (100 nF or so) over the supply +// lines on long cable runs significantly reduces the number of bad readings. +// If you make temperature conversions more often than every 4-5 seconds, the +// device(s) will self heat and produce a higher temperature reading. +////////////////////////////////////////////////////////////////////////////// +////////////////////////////////////////////////////////////////////////////// #include <mbed.h> Serial pc(USBTX, USBRX); @@ -15,7 +40,7 @@ #define TRUE 1 #define MaxROMs 20 // Defines how many devices space is allocated for. -DigitalInOut DQ(p30); +DigitalInOut DQ(p30); // Attach the DQ pin of your sensors to this mbed pin. unsigned char SPad[9]; // Scratchpad storage unsigned char ROM[8]; @@ -53,7 +78,7 @@ wait_us(70); // wait for presence presence = DQ; // get presence signal wait_us(410); // wait for end of timeslot - return(presence); // presence signal returned, 0=presence, 1 = no part + return(presence); // presence signal returned, 0=presence, 1 = no sensor found. } ////////////////////////////////////////////////////////////////////////////// @@ -101,7 +126,7 @@ for (i=0; i<8; i++) {// writes byte, one bit at a time temp = val>>i; // shifts val right 'i' spaces temp &= 0x01; // copy that bit to temp - write_bit(temp); // write bit in temp into + write_bit(temp); // write bit } } @@ -125,7 +150,7 @@ unsigned char g; // Output bit unsigned char nxt; // return value int flag; - nxt = FALSE; // set the next flag to false + nxt = FALSE; // set the nxt flag to false dowcrc = 0; // reset the dowcrc flag = ow_reset(); // reset the 1-Wire if(flag||doneFlag) { // no parts -> return false