// 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

Dependencies:   mbed

Committer:
RRacer
Date:
Mon Jan 23 10:18:02 2012 +0000
Revision:
1:0777f93ef466
Parent:
0:770734d973b0
Child:
4:6ade7fcb2925
Added check for negative temperatures, as it was missing in the Dallas app note.

Who changed what in which revision?

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