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