// 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
main.cpp@4:6ade7fcb2925, 2016-01-28 (annotated)
- Committer:
- RRacer
- Date:
- Thu Jan 28 09:05:50 2016 +0000
- Revision:
- 4:6ade7fcb2925
- Parent:
- 3:28b7b7fddede
- Child:
- 7:cf5affdab535
Modified/added info text.; No program changes.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
RRacer |
2:7ab1bf8540a3 | 1 | ////////////////////////////////////////////////////////////////////////////// |
RRacer |
2:7ab1bf8540a3 | 2 | ////////////////////////////////////////////////////////////////////////////// |
RRacer |
2:7ab1bf8540a3 | 3 | // Lots_of_DS18B20 |
RRacer |
2:7ab1bf8540a3 | 4 | // Demo on how to identify and read multiple DS18B20 connected to the same bus. |
RRacer |
2:7ab1bf8540a3 | 5 | // Parasitic power is not used in this example. |
RRacer |
2:7ab1bf8540a3 | 6 | // This program is based on the sample code from Maxim/Dallas application |
RRacer |
2:7ab1bf8540a3 | 7 | // note 162 (http://www.maxim-ic.com/app-notes/index.mvp/id/162). |
RRacer |
2:7ab1bf8540a3 | 8 | ////////////////////////////////////////////////////////////////////////////// |
RRacer |
2:7ab1bf8540a3 | 9 | ////////////////////////////////////////////////////////////////////////////// |
RRacer | 4:6ade7fcb2925 | 10 | // Program output should look like this: |
RRacer | 4:6ade7fcb2925 | 11 | // *** Test with multiple DS18B20 *** |
RRacer | 4:6ade7fcb2925 | 12 | // Memory allocated for 20 devices. |
RRacer | 4:6ade7fcb2925 | 13 | // Scanning for devices... |
RRacer | 4:6ade7fcb2925 | 14 | // ROM CODE =62:00:00:01:CB:25:CA:28 1 |
RRacer | 4:6ade7fcb2925 | 15 | // ROM CODE =B6:00:00:01:CB:1B:9E:28 2 |
RRacer | 4:6ade7fcb2925 | 16 | // ROM CODE =66:00:00:01:CB:28:59:28 3 |
RRacer | 4:6ade7fcb2925 | 17 | // 3 devices found. Scanning completed. |
RRacer | 4:6ade7fcb2925 | 18 | // Temp: 022.6875 Device: 000001CB25CA 001 |
RRacer | 4:6ade7fcb2925 | 19 | // Temp: 022.6875 Device: 000001CB1B9E 002 |
RRacer | 4:6ade7fcb2925 | 20 | // Temp: 027.6250 Device: 000001CB2859 003 |
RRacer | 4:6ade7fcb2925 | 21 | // Temp: 022.6250 Device: 000001CB25CA 001 |
RRacer | 4:6ade7fcb2925 | 22 | // Temp: 022.6875 Device: 000001CB1B9E 002 |
RRacer | 4:6ade7fcb2925 | 23 | // Temp: 025.3125 Device: 000001CB2859 003 |
RRacer | 4:6ade7fcb2925 | 24 | // Temp: 022.8125 Device: 000001CB25CA 001 |
RRacer | 4:6ade7fcb2925 | 25 | // Temp: 024.1875 Device: 000001CB1B9E 002 |
RRacer | 4:6ade7fcb2925 | 26 | // Temp: 023.7500 Device: 000001CB2859 003 |
RRacer | 4:6ade7fcb2925 | 27 | ////////////////////////////////////////////////////////////////////////////// |
RRacer | 4:6ade7fcb2925 | 28 | ////////////////////////////////////////////////////////////////////////////// |
RRacer | 4:6ade7fcb2925 | 29 | // I have found that adding a small capacitor (100 nF or so) over the supply |
RRacer | 4:6ade7fcb2925 | 30 | // lines on long cable runs significantly reduces the number of bad readings. |
RRacer | 4:6ade7fcb2925 | 31 | // If you make temperature conversions more often than every 4-5 seconds, the |
RRacer | 4:6ade7fcb2925 | 32 | // device(s) will self heat and produce a higher temperature reading. |
RRacer | 4:6ade7fcb2925 | 33 | ////////////////////////////////////////////////////////////////////////////// |
RRacer | 4:6ade7fcb2925 | 34 | ////////////////////////////////////////////////////////////////////////////// |
RRacer |
2:7ab1bf8540a3 | 35 | |
RRacer |
2:7ab1bf8540a3 | 36 | #include <mbed.h> |
RRacer |
2:7ab1bf8540a3 | 37 | Serial pc(USBTX, USBRX); |
RRacer |
2:7ab1bf8540a3 | 38 | |
RRacer |
2:7ab1bf8540a3 | 39 | #define FALSE 0 |
RRacer |
2:7ab1bf8540a3 | 40 | #define TRUE 1 |
RRacer |
2:7ab1bf8540a3 | 41 | #define MaxROMs 20 // Defines how many devices space is allocated for. |
RRacer |
2:7ab1bf8540a3 | 42 | |
RRacer | 4:6ade7fcb2925 | 43 | DigitalInOut DQ(p30); // Attach the DQ pin of your sensors to this mbed pin. |
RRacer |
2:7ab1bf8540a3 | 44 | |
RRacer |
2:7ab1bf8540a3 | 45 | unsigned char SPad[9]; // Scratchpad storage |
RRacer |
2:7ab1bf8540a3 | 46 | unsigned char ROM[8]; |
RRacer |
2:7ab1bf8540a3 | 47 | unsigned char lastDiscrep = 0; // last discrepancy |
RRacer |
2:7ab1bf8540a3 | 48 | unsigned char doneFlag = 0; // Done flag |
RRacer |
2:7ab1bf8540a3 | 49 | unsigned char FoundROM[MaxROMs][8]; // table of found ROM codes |
RRacer |
2:7ab1bf8540a3 | 50 | unsigned char numROMs; // Number of found devices. |
RRacer |
2:7ab1bf8540a3 | 51 | unsigned char dowcrc; |
RRacer |
2:7ab1bf8540a3 | 52 | unsigned char dscrc_table[] = { |
RRacer |
2:7ab1bf8540a3 | 53 | 0, 94,188,226, 97, 63,221,131,194,156,126, 32,163,253, 31, 65, |
RRacer |
2:7ab1bf8540a3 | 54 | 157,195, 33,127,252,162, 64, 30, 95, 1,227,189, 62, 96,130,220, |
RRacer |
2:7ab1bf8540a3 | 55 | 35,125,159,193, 66, 28,254,160,225,191, 93, 3,128,222, 60, 98, |
RRacer |
2:7ab1bf8540a3 | 56 | 190,224, 2, 92,223,129, 99, 61,124, 34,192,158, 29, 67,161,255, |
RRacer |
2:7ab1bf8540a3 | 57 | 70, 24,250,164, 39,121,155,197,132,218, 56,102,229,187, 89, 7, |
RRacer |
2:7ab1bf8540a3 | 58 | 219,133,103, 57,186,228, 6, 88, 25, 71,165,251,120, 38,196,154, |
RRacer |
2:7ab1bf8540a3 | 59 | 101, 59,217,135, 4, 90,184,230,167,249, 27, 69,198,152,122, 36, |
RRacer |
2:7ab1bf8540a3 | 60 | 248,166, 68, 26,153,199, 37,123, 58,100,134,216, 91, 5,231,185, |
RRacer |
2:7ab1bf8540a3 | 61 | 140,210, 48,110,237,179, 81, 15, 78, 16,242,172, 47,113,147,205, |
RRacer |
2:7ab1bf8540a3 | 62 | 17, 79,173,243,112, 46,204,146,211,141,111, 49,178,236, 14, 80, |
RRacer |
2:7ab1bf8540a3 | 63 | 175,241, 19, 77,206,144,114, 44,109, 51,209,143, 12, 82,176,238, |
RRacer |
2:7ab1bf8540a3 | 64 | 50,108,142,208, 83, 13,239,177,240,174, 76, 18,145,207, 45,115, |
RRacer |
2:7ab1bf8540a3 | 65 | 202,148,118, 40,171,245, 23, 73, 8, 86,180,234,105, 55,213,139, |
RRacer |
2:7ab1bf8540a3 | 66 | 87, 9,235,181, 54,104,138,212,149,203, 41,119,244,170, 72, 22, |
RRacer |
2:7ab1bf8540a3 | 67 | 233,183, 85, 11,136,214, 52,106, 43,117,151,201, 74, 20,246,168, |
RRacer |
2:7ab1bf8540a3 | 68 | 116, 42,200,150, 21, 75,169,247,182,232, 10, 84,215,137,107, 53}; |
RRacer |
2:7ab1bf8540a3 | 69 | |
RRacer |
2:7ab1bf8540a3 | 70 | ////////////////////////////////////////////////////////////////////////////// |
RRacer |
2:7ab1bf8540a3 | 71 | // OW_RESET - performs a reset on the 1-wire bus and returns the presence detect. |
RRacer |
2:7ab1bf8540a3 | 72 | unsigned char ow_reset(void) { |
RRacer |
2:7ab1bf8540a3 | 73 | unsigned char presence; |
RRacer |
2:7ab1bf8540a3 | 74 | DQ.output(); |
RRacer |
2:7ab1bf8540a3 | 75 | DQ = 0; //pull DQ line low |
RRacer |
2:7ab1bf8540a3 | 76 | wait_us(480); // leave it low for 480us |
RRacer |
2:7ab1bf8540a3 | 77 | DQ.input(); // allow line to return high |
RRacer |
2:7ab1bf8540a3 | 78 | wait_us(70); // wait for presence |
RRacer |
2:7ab1bf8540a3 | 79 | presence = DQ; // get presence signal |
RRacer |
2:7ab1bf8540a3 | 80 | wait_us(410); // wait for end of timeslot |
RRacer | 4:6ade7fcb2925 | 81 | return(presence); // presence signal returned, 0=presence, 1 = no sensor found. |
RRacer |
2:7ab1bf8540a3 | 82 | } |
RRacer |
2:7ab1bf8540a3 | 83 | |
RRacer |
2:7ab1bf8540a3 | 84 | ////////////////////////////////////////////////////////////////////////////// |
RRacer |
2:7ab1bf8540a3 | 85 | // READ_BIT - reads a bit from the one-wire bus. |
RRacer |
2:7ab1bf8540a3 | 86 | unsigned char read_bit(void) { |
RRacer |
2:7ab1bf8540a3 | 87 | unsigned char retval; |
RRacer |
2:7ab1bf8540a3 | 88 | wait_us(1); // Recovery time |
RRacer |
2:7ab1bf8540a3 | 89 | DQ.output(); |
RRacer |
2:7ab1bf8540a3 | 90 | DQ = 0; // pull DQ low to start timeslot |
RRacer |
2:7ab1bf8540a3 | 91 | wait_us(2); |
RRacer |
2:7ab1bf8540a3 | 92 | DQ.input(); // Tristate line |
RRacer |
2:7ab1bf8540a3 | 93 | wait_us(10); // delay 10 us from start of timeslot |
RRacer |
2:7ab1bf8540a3 | 94 | retval=DQ; |
RRacer |
2:7ab1bf8540a3 | 95 | wait_us(48); // minimum Read time slot: 60 us. |
RRacer |
2:7ab1bf8540a3 | 96 | return(retval); // return value of DQ line |
RRacer |
2:7ab1bf8540a3 | 97 | } |
RRacer |
2:7ab1bf8540a3 | 98 | |
RRacer |
2:7ab1bf8540a3 | 99 | ////////////////////////////////////////////////////////////////////////////// |
RRacer |
2:7ab1bf8540a3 | 100 | // WRITE_BIT - writes a bit to the one-wire bus, passed in bitval. |
RRacer |
2:7ab1bf8540a3 | 101 | void write_bit(unsigned char bitval) { |
RRacer |
2:7ab1bf8540a3 | 102 | wait_us(1); // Recovery time |
RRacer |
2:7ab1bf8540a3 | 103 | DQ.output(); |
RRacer |
2:7ab1bf8540a3 | 104 | DQ = 0; // pull DQ low to start timeslot |
RRacer |
2:7ab1bf8540a3 | 105 | wait_us(10); |
RRacer |
2:7ab1bf8540a3 | 106 | if(bitval==1) DQ =1; // return DQ high if write 1 |
RRacer |
2:7ab1bf8540a3 | 107 | wait_us(50); // hold value for remainder of timeslot |
RRacer |
2:7ab1bf8540a3 | 108 | DQ.input(); // Release line |
RRacer |
2:7ab1bf8540a3 | 109 | } |
RRacer |
2:7ab1bf8540a3 | 110 | |
RRacer |
2:7ab1bf8540a3 | 111 | ////////////////////////////////////////////////////////////////////////////// |
RRacer |
2:7ab1bf8540a3 | 112 | // READ_BYTE - reads a byte from the one-wire bus. |
RRacer |
2:7ab1bf8540a3 | 113 | unsigned char read_byte(void) { |
RRacer |
2:7ab1bf8540a3 | 114 | unsigned char i, value=0; |
RRacer |
2:7ab1bf8540a3 | 115 | for (i=0;i<8;i++) { |
RRacer |
2:7ab1bf8540a3 | 116 | if(read_bit()) value|=0x01<<i; // reads byte in, one byte at a time and then shifts it left |
RRacer |
2:7ab1bf8540a3 | 117 | } |
RRacer |
2:7ab1bf8540a3 | 118 | return(value); |
RRacer |
2:7ab1bf8540a3 | 119 | } |
RRacer |
2:7ab1bf8540a3 | 120 | |
RRacer |
2:7ab1bf8540a3 | 121 | ////////////////////////////////////////////////////////////////////////////// |
RRacer |
2:7ab1bf8540a3 | 122 | // WRITE_BYTE - writes a byte to the one-wire bus. |
RRacer |
2:7ab1bf8540a3 | 123 | void write_byte(char val) { |
RRacer |
2:7ab1bf8540a3 | 124 | unsigned char i; |
RRacer |
2:7ab1bf8540a3 | 125 | unsigned char temp; |
RRacer |
2:7ab1bf8540a3 | 126 | for (i=0; i<8; i++) {// writes byte, one bit at a time |
RRacer |
2:7ab1bf8540a3 | 127 | temp = val>>i; // shifts val right 'i' spaces |
RRacer |
2:7ab1bf8540a3 | 128 | temp &= 0x01; // copy that bit to temp |
RRacer | 4:6ade7fcb2925 | 129 | write_bit(temp); // write bit |
RRacer |
2:7ab1bf8540a3 | 130 | } |
RRacer |
2:7ab1bf8540a3 | 131 | } |
RRacer |
2:7ab1bf8540a3 | 132 | |
RRacer |
2:7ab1bf8540a3 | 133 | ////////////////////////////////////////////////////////////////////////////// |
RRacer |
2:7ab1bf8540a3 | 134 | // ONE WIRE CRC |
RRacer |
2:7ab1bf8540a3 | 135 | unsigned char ow_crc( unsigned char x) { |
RRacer |
2:7ab1bf8540a3 | 136 | dowcrc = dscrc_table[dowcrc^x]; |
RRacer |
2:7ab1bf8540a3 | 137 | return dowcrc; |
RRacer |
2:7ab1bf8540a3 | 138 | } |
RRacer |
2:7ab1bf8540a3 | 139 | |
RRacer |
2:7ab1bf8540a3 | 140 | ////////////////////////////////////////////////////////////////////////////// |
RRacer |
2:7ab1bf8540a3 | 141 | // NEXT |
RRacer |
2:7ab1bf8540a3 | 142 | // The Next function searches for the next device on the 1-Wire bus. If |
RRacer |
2:7ab1bf8540a3 | 143 | // there are no more devices on the 1-Wire then false is returned. |
RRacer |
2:7ab1bf8540a3 | 144 | unsigned char Next(void) { |
RRacer |
2:7ab1bf8540a3 | 145 | unsigned char m = 1; // ROM Bit index |
RRacer |
2:7ab1bf8540a3 | 146 | unsigned char n = 0; // ROM Byte index |
RRacer |
2:7ab1bf8540a3 | 147 | unsigned char k = 1; // bit mask |
RRacer |
2:7ab1bf8540a3 | 148 | unsigned char x = 0; |
RRacer |
2:7ab1bf8540a3 | 149 | unsigned char discrepMarker = 0; // discrepancy marker |
RRacer |
2:7ab1bf8540a3 | 150 | unsigned char g; // Output bit |
RRacer |
2:7ab1bf8540a3 | 151 | unsigned char nxt; // return value |
RRacer |
2:7ab1bf8540a3 | 152 | int flag; |
RRacer | 4:6ade7fcb2925 | 153 | nxt = FALSE; // set the nxt flag to false |
RRacer |
2:7ab1bf8540a3 | 154 | dowcrc = 0; // reset the dowcrc |
RRacer |
2:7ab1bf8540a3 | 155 | flag = ow_reset(); // reset the 1-Wire |
RRacer |
2:7ab1bf8540a3 | 156 | if(flag||doneFlag) { // no parts -> return false |
RRacer |
2:7ab1bf8540a3 | 157 | lastDiscrep = 0; // reset the search |
RRacer |
2:7ab1bf8540a3 | 158 | return FALSE; |
RRacer |
2:7ab1bf8540a3 | 159 | } |
RRacer |
2:7ab1bf8540a3 | 160 | write_byte(0xF0); // send SearchROM command |
RRacer |
2:7ab1bf8540a3 | 161 | do { // for all eight bytes |
RRacer |
2:7ab1bf8540a3 | 162 | x = 0; |
RRacer |
2:7ab1bf8540a3 | 163 | if(read_bit()==1) x = 2; |
RRacer |
2:7ab1bf8540a3 | 164 | wait_us(120); |
RRacer |
2:7ab1bf8540a3 | 165 | if(read_bit()==1) x |= 1; // and its complement |
RRacer |
2:7ab1bf8540a3 | 166 | if(x ==3) // there are no devices on the 1-Wire |
RRacer |
2:7ab1bf8540a3 | 167 | break; |
RRacer |
2:7ab1bf8540a3 | 168 | else { |
RRacer |
2:7ab1bf8540a3 | 169 | if(x>0) // all devices coupled have 0 or 1 |
RRacer |
2:7ab1bf8540a3 | 170 | g = x>>1; // bit write value for search |
RRacer |
2:7ab1bf8540a3 | 171 | else { |
RRacer |
2:7ab1bf8540a3 | 172 | // if this discrepancy is before the last discrepancy on a previous Next then pick the same as last time |
RRacer |
2:7ab1bf8540a3 | 173 | if(m<lastDiscrep) |
RRacer |
2:7ab1bf8540a3 | 174 | g = ((ROM[n]&k)>0); |
RRacer |
2:7ab1bf8540a3 | 175 | else // if equal to last pick 1 |
RRacer |
2:7ab1bf8540a3 | 176 | g = (m==lastDiscrep); // if not then pick 0 |
RRacer |
2:7ab1bf8540a3 | 177 | // if 0 was picked then record position with mask k |
RRacer |
2:7ab1bf8540a3 | 178 | if (g==0) discrepMarker = m; |
RRacer |
2:7ab1bf8540a3 | 179 | } |
RRacer |
2:7ab1bf8540a3 | 180 | if(g==1) // isolate bit in ROM[n] with mask k |
RRacer |
2:7ab1bf8540a3 | 181 | ROM[n] |= k; |
RRacer |
2:7ab1bf8540a3 | 182 | else |
RRacer |
2:7ab1bf8540a3 | 183 | ROM[n] &= ~k; |
RRacer |
2:7ab1bf8540a3 | 184 | write_bit(g); // ROM search write |
RRacer |
2:7ab1bf8540a3 | 185 | m++; // increment bit counter m |
RRacer |
2:7ab1bf8540a3 | 186 | k = k<<1; // and shift the bit mask k |
RRacer |
2:7ab1bf8540a3 | 187 | if(k==0) { // if the mask is 0 then go to new ROM // byte n and reset mask |
RRacer |
2:7ab1bf8540a3 | 188 | ow_crc(ROM[n]); // accumulate the CRC |
RRacer |
2:7ab1bf8540a3 | 189 | n++; k++; |
RRacer |
2:7ab1bf8540a3 | 190 | } |
RRacer |
2:7ab1bf8540a3 | 191 | } |
RRacer |
2:7ab1bf8540a3 | 192 | } |
RRacer |
2:7ab1bf8540a3 | 193 | while(n<8); //loop until through all ROM bytes 0-7 |
RRacer |
2:7ab1bf8540a3 | 194 | if(m<65||dowcrc) // if search was unsuccessful then |
RRacer |
2:7ab1bf8540a3 | 195 | lastDiscrep=0; // reset the last discrepancy to 0 |
RRacer |
2:7ab1bf8540a3 | 196 | else { // search was successful, so set lastDiscrep, lastOne, nxt |
RRacer |
2:7ab1bf8540a3 | 197 | lastDiscrep = discrepMarker; |
RRacer |
2:7ab1bf8540a3 | 198 | doneFlag = (lastDiscrep==0); |
RRacer |
2:7ab1bf8540a3 | 199 | nxt = TRUE; // indicates search is not complete yet, more parts remain |
RRacer |
2:7ab1bf8540a3 | 200 | } |
RRacer |
2:7ab1bf8540a3 | 201 | return nxt; |
RRacer |
2:7ab1bf8540a3 | 202 | } |
RRacer |
2:7ab1bf8540a3 | 203 | |
RRacer |
2:7ab1bf8540a3 | 204 | ////////////////////////////////////////////////////////////////////////////// |
RRacer |
2:7ab1bf8540a3 | 205 | // FIRST |
RRacer |
2:7ab1bf8540a3 | 206 | // The First function resets the current state of a ROM search and calls |
RRacer |
2:7ab1bf8540a3 | 207 | // Next to find the first device on the 1-Wire bus. |
RRacer |
2:7ab1bf8540a3 | 208 | unsigned char First(void) { |
RRacer |
2:7ab1bf8540a3 | 209 | lastDiscrep = 0; // reset the rom search last discrepancy global |
RRacer |
2:7ab1bf8540a3 | 210 | doneFlag = FALSE; |
RRacer |
2:7ab1bf8540a3 | 211 | return Next(); // call Next and return its return value |
RRacer |
2:7ab1bf8540a3 | 212 | } |
RRacer |
2:7ab1bf8540a3 | 213 | |
RRacer |
2:7ab1bf8540a3 | 214 | ////////////////////////////////////////////////////////////////////////////// |
RRacer |
2:7ab1bf8540a3 | 215 | // FIND DEVICES |
RRacer |
2:7ab1bf8540a3 | 216 | void FindDevices(void) { |
RRacer |
2:7ab1bf8540a3 | 217 | unsigned char m; |
RRacer |
2:7ab1bf8540a3 | 218 | if(!ow_reset()) { //Begins when a presence is detected |
RRacer |
2:7ab1bf8540a3 | 219 | if(First()) { //Begins when at least one part is found |
RRacer |
2:7ab1bf8540a3 | 220 | numROMs=0; |
RRacer |
2:7ab1bf8540a3 | 221 | do { |
RRacer |
2:7ab1bf8540a3 | 222 | numROMs++; |
RRacer |
2:7ab1bf8540a3 | 223 | for(m=0;m<8;m++) { |
RRacer |
2:7ab1bf8540a3 | 224 | FoundROM[numROMs][m]=ROM[m]; //Identifies ROM |
RRacer |
2:7ab1bf8540a3 | 225 | } |
RRacer |
2:7ab1bf8540a3 | 226 | pc.printf("ROM CODE =%02X:%02X:%02X:%02X:%02X:%02X:%02X:%02X %d\r\n", |
RRacer |
2:7ab1bf8540a3 | 227 | FoundROM[numROMs][7],FoundROM[numROMs][6],FoundROM[numROMs][5],FoundROM[numROMs][4], |
RRacer |
2:7ab1bf8540a3 | 228 | FoundROM[numROMs][3],FoundROM[numROMs][2],FoundROM[numROMs][1],FoundROM[numROMs][0],numROMs); |
RRacer |
2:7ab1bf8540a3 | 229 | } |
RRacer |
2:7ab1bf8540a3 | 230 | while (Next()&&(numROMs<MaxROMs)); //Continues until no additional devices are found |
RRacer |
2:7ab1bf8540a3 | 231 | } |
RRacer |
2:7ab1bf8540a3 | 232 | } |
RRacer |
2:7ab1bf8540a3 | 233 | pc.printf("\n%d devices found.\r\n\n",numROMs); |
RRacer |
2:7ab1bf8540a3 | 234 | } |
RRacer |
2:7ab1bf8540a3 | 235 | ////////////////////////////////////////////////////////////////////////////// |
RRacer |
2:7ab1bf8540a3 | 236 | void Read_ScratchPad(unsigned char n) { // Read the n first scratchpad bytes. Old data not wiped. |
RRacer |
2:7ab1bf8540a3 | 237 | n=n % 10; |
RRacer |
2:7ab1bf8540a3 | 238 | write_byte(0xBE); |
RRacer |
2:7ab1bf8540a3 | 239 | for (int j=1;j<=n;j++){SPad[j-1]=read_byte();} |
RRacer |
2:7ab1bf8540a3 | 240 | // CRC ********reserved******* Config Tl Th T MSB T LSB |
RRacer |
2:7ab1bf8540a3 | 241 | pc.printf("\n ScratchPAD: %X%X%X%X%X%X%X%X%X\n",SPad[8],SPad[7],SPad[6],SPad[5],SPad[4],SPad[3],SPad[2],SPad[1],SPad[0]); |
RRacer |
2:7ab1bf8540a3 | 242 | } |
RRacer |
2:7ab1bf8540a3 | 243 | |
RRacer |
2:7ab1bf8540a3 | 244 | ////////////////////////////////////////////////////////////////////////////// |
RRacer |
2:7ab1bf8540a3 | 245 | // Perform Match ROM |
RRacer |
2:7ab1bf8540a3 | 246 | unsigned char Send_MatchRom(unsigned char DeviceNo) { |
RRacer |
2:7ab1bf8540a3 | 247 | unsigned char i; |
RRacer |
2:7ab1bf8540a3 | 248 | if(ow_reset()) return false; |
RRacer |
2:7ab1bf8540a3 | 249 | write_byte(0x55); // match ROM |
RRacer |
2:7ab1bf8540a3 | 250 | for(i=0;i<8;i++) { |
RRacer |
2:7ab1bf8540a3 | 251 | write_byte(FoundROM[DeviceNo][i]); //send ROM code |
RRacer |
2:7ab1bf8540a3 | 252 | } |
RRacer |
2:7ab1bf8540a3 | 253 | return true; |
RRacer |
2:7ab1bf8540a3 | 254 | } |
RRacer |
2:7ab1bf8540a3 | 255 | |
RRacer |
2:7ab1bf8540a3 | 256 | ////////////////////////////////////////////////////////////////////////////// |
RRacer |
2:7ab1bf8540a3 | 257 | void ConvT() { // Make all devices on the bus start a temperature conversion. |
RRacer |
2:7ab1bf8540a3 | 258 | ow_reset(); |
RRacer |
2:7ab1bf8540a3 | 259 | write_byte( 0xcc); // Skip ROM command. |
RRacer |
2:7ab1bf8540a3 | 260 | write_byte( 0x44); // Convert T command. |
RRacer |
2:7ab1bf8540a3 | 261 | } |
RRacer |
2:7ab1bf8540a3 | 262 | |
RRacer |
2:7ab1bf8540a3 | 263 | ////////////////////////////////////////////////////////////////////////////// |
RRacer |
2:7ab1bf8540a3 | 264 | unsigned int ReadRawTemp(unsigned char device) { |
RRacer |
2:7ab1bf8540a3 | 265 | int HighByte, LowByte; |
RRacer |
2:7ab1bf8540a3 | 266 | Send_MatchRom(device); // Select device. |
RRacer |
2:7ab1bf8540a3 | 267 | write_byte( 0xbe); // Read Scratchpad command. |
RRacer |
2:7ab1bf8540a3 | 268 | LowByte=read_byte(); |
RRacer |
2:7ab1bf8540a3 | 269 | HighByte=read_byte(); |
RRacer |
2:7ab1bf8540a3 | 270 | return (HighByte << 8) + LowByte; |
RRacer |
2:7ab1bf8540a3 | 271 | } |
RRacer |
2:7ab1bf8540a3 | 272 | |
RRacer |
2:7ab1bf8540a3 | 273 | ////////////////////////////////////////////////////////////////////////////// |
RRacer |
2:7ab1bf8540a3 | 274 | float Get_Temp(unsigned char device) { |
RRacer |
3:28b7b7fddede | 275 | int Raw = ReadRawTemp(device); |
RRacer |
3:28b7b7fddede | 276 | if((Raw>>8) & 0x80) { // Check if temperature is negative. |
RRacer |
3:28b7b7fddede | 277 | Raw = (Raw ^ 0xFFFF) + 1; |
RRacer |
3:28b7b7fddede | 278 | Raw *= -1; |
RRacer |
3:28b7b7fddede | 279 | } |
RRacer |
3:28b7b7fddede | 280 | float temperature = (float)Raw / 16.0; |
RRacer |
2:7ab1bf8540a3 | 281 | return temperature; |
RRacer |
2:7ab1bf8540a3 | 282 | } |
RRacer |
2:7ab1bf8540a3 | 283 | |
RRacer |
2:7ab1bf8540a3 | 284 | ////////////////////////////////////////////////////////////////////////////// |
RRacer |
2:7ab1bf8540a3 | 285 | int main() { |
RRacer |
2:7ab1bf8540a3 | 286 | float temperature; |
RRacer |
2:7ab1bf8540a3 | 287 | DQ.output(); |
RRacer |
2:7ab1bf8540a3 | 288 | DQ = 0; |
RRacer |
2:7ab1bf8540a3 | 289 | DQ.input(); |
RRacer |
2:7ab1bf8540a3 | 290 | pc.baud(9600); |
RRacer |
2:7ab1bf8540a3 | 291 | pc.printf("\n\n*** Test with multiple DS18B20 ***\r\n\n"); |
RRacer |
2:7ab1bf8540a3 | 292 | pc.printf("Memory allocated for %d devices.\r\n",MaxROMs); |
RRacer |
2:7ab1bf8540a3 | 293 | pc.printf("Scanning for devices...\r\n"); |
RRacer |
2:7ab1bf8540a3 | 294 | ow_reset(); |
RRacer |
2:7ab1bf8540a3 | 295 | FindDevices(); |
RRacer |
2:7ab1bf8540a3 | 296 | pc.printf("Scanning completed.\r\n"); |
RRacer |
2:7ab1bf8540a3 | 297 | |
RRacer |
2:7ab1bf8540a3 | 298 | while (1) { |
RRacer |
2:7ab1bf8540a3 | 299 | ConvT(); // Issue Convert T command. |
RRacer |
2:7ab1bf8540a3 | 300 | wait_ms(750); // Minimum 12-bit conversion time. |
RRacer |
2:7ab1bf8540a3 | 301 | for(int i=1;i<=numROMs;i++) { // Cycle through found devices. |
RRacer |
2:7ab1bf8540a3 | 302 | temperature = Get_Temp(i); |
RRacer |
2:7ab1bf8540a3 | 303 | pc.printf("Temp: %08.4f Device: %02X%02X%02X%02X%02X%02X %03d\r\n",temperature,FoundROM[i][6],FoundROM[i][5],FoundROM[i][4],FoundROM[i][3],FoundROM[i][2],FoundROM[i][1],i); |
RRacer |
2:7ab1bf8540a3 | 304 | } |
RRacer |
2:7ab1bf8540a3 | 305 | pc.printf("\r\n"); |
RRacer |
2:7ab1bf8540a3 | 306 | wait(5); // Doing conversions more often will make devices self heat and produce a higher temperature reading. |
RRacer |
2:7ab1bf8540a3 | 307 | } |
RRacer |
2:7ab1bf8540a3 | 308 | } |