Interface Driver for Maxim DS2482 1Wire-to-I2C bridge IC. Includes access functions for DS1820 temperature sensors. Can easily be ported to other hardware by using hardware abstraction layer.
ds2482.cpp
00001 #include "ds2482.h" 00002 00003 /* ================================================ 00004 Reads Status Register of given DS2482 IC 00005 Parameter: Pointer to DS2482 object to work on 00006 Returns: register content 00007 (negative value on error) 00008 ------------------------------------------------ */ 00009 int16_t i16DS2482GetStatus(struct sDS2482_t *dev) 00010 { 00011 uint8_t u8Data[2]; 00012 u8Data[0] = SRP; 00013 u8Data[1] = 0xF0; // Status register 00014 if(_i8I2CWrite(dev->u8Addr, u8Data, 2) != 0) 00015 return -1; 00016 if(_i8I2CRead(dev->u8Addr, u8Data, 1, 0) != 0) 00017 return -1; 00018 00019 return u8Data[0]; 00020 } 00021 00022 /* ================================================ 00023 Issues reset to given DS2482 IC 00024 Parameter: 1 Pointer to DS2482 object to work on 00025 Returns: zero 00026 (negative value on error) 00027 ------------------------------------------------ */ 00028 int8_t i8DS2482Reset(struct sDS2482_t *dev) 00029 { 00030 uint8_t u8Data; 00031 u8Data = DRST; 00032 if(_i8I2CWrite(dev->u8Addr, &u8Data, 1) != 0) 00033 return -DS2482_ERR_I2CWRITE; 00034 00035 return 0; 00036 } 00037 00038 /* ================================================ 00039 Writes into Config Register of given DS2482 IC 00040 Parameter: 1 Pointer to DS2482 object to work on 00041 2 Byte register value 00042 Returns: zero 00043 (negative value on error) 00044 ------------------------------------------------ */ 00045 int8_t i8DS2482SetControlBits(struct sDS2482_t *dev, uint8_t u8Flags) 00046 { 00047 uint8_t u8Data[2]; 00048 00049 u8Data[0] = WCFG; 00050 u8Data[1] = (~u8Flags)<<4 | u8Flags; // active pullup 00051 if(_i8I2CWrite(dev->u8Addr, u8Data, 2) != 0) 00052 return -DS2482_ERR_I2CWRITE; 00053 00054 00055 00056 u8Data[0] = SRP; 00057 u8Data[1] = 0xC3; // Config 00058 00059 if(_i8I2CWrite(dev->u8Addr, u8Data, 2) != 0) 00060 return -DS2482_ERR_I2CWRITE; 00061 00062 if(_i8I2CRead(dev->u8Addr, u8Data, 1, 0) != 0) 00063 return -DS2482_ERR_I2CREAD; 00064 00065 00066 00067 if(u8Data[0] != u8Flags) 00068 return -DS2482_ERR_CONFIGMISSMATCH; 00069 00070 return 0; 00071 } 00072 00073 /* ================================================ 00074 Issue 1Wire Reset on given DS2482 IC 00075 Parameter: 1 Pointer to DS2482 object to work on 00076 Returns: zero if a device is present 00077 (negative value on error) 00078 ------------------------------------------------ */ 00079 int8_t i8DS2482_OWReset(struct sDS2482_t *dev) 00080 { 00081 unsigned char status; 00082 int poll_count = 0; 00083 00084 // 1-Wire reset (Case B) 00085 // S AD,0 [A] 1WRS [A] Sr AD,1 [A] [Status] A [Status] A\ P 00086 // \--------/ 00087 // Repeat until 1WB bit has changed to 0 00088 // [] indicates from slave 00089 uint8_t u8Data[2]; 00090 u8Data[0] = OWRS; 00091 00092 _i8I2CWrite(dev->u8Addr, u8Data, 1); 00093 00094 // loop checking 1WB bit for completion of 1-Wire operation 00095 // abort if poll limit reached 00096 _i8I2CRead(dev->u8Addr, &status, 1, 0); 00097 do 00098 { 00099 _i8I2CRead(dev->u8Addr, &status, 1 ,status & STATUS_1WB); 00100 } 00101 while ((status & STATUS_1WB) && (poll_count++ < POLL_LIMIT)); 00102 00103 00104 00105 // check for failure due to poll limit reached 00106 if (poll_count >= POLL_LIMIT) 00107 { 00108 // handle error 00109 // ... 00110 i8DS2482Reset(dev); 00111 return -DS2482_ERR_TIMEOUT; 00112 } 00113 00114 // check for short condition 00115 if (status & STATUS_SD) 00116 dev->u32Flags &= ~FLAG_SHORT; 00117 else 00118 dev->u32Flags |= FLAG_SHORT; 00119 00120 // check for presence detect 00121 if (status & STATUS_PPD) 00122 return 0; 00123 else 00124 return -DS2482_ERR_NOPRESENCE; 00125 } 00126 00127 //-------------------------------------------------------------------------- 00128 // Send 1 bit of communication to the 1-Wire Net. 00129 // The parameter 'sendbit' least significant bit is used. 00130 // 00131 // 'sendbit' - 1 bit to send (least significant byte) 00132 // 00133 /* ================================================ 00134 Sends 1 bit to 1Wire network of given DS2482 IC 00135 Parameter: 1 Pointer to DS2482 object to work on 00136 2 Information 00137 Returns: zero 00138 (negative value on error) 00139 ------------------------------------------------ */ 00140 int8_t i8DS2482_OWWriteBit(struct sDS2482_t *dev, uint8_t sendbit) 00141 { 00142 return i8DS2482_OWTouchBit(dev, sendbit); 00143 } 00144 00145 //-------------------------------------------------------------------------- 00146 // Reads 1 bit of communication from the 1-Wire Net and returns the 00147 // result 00148 // 00149 // Returns: 1 bit read from 1-Wire Net 00150 // 00151 /* ================================================ 00152 Reads one bit from 1Wire network of given DS2482 IC 00153 Parameter: 1 Pointer to DS2482 object to work on 00154 Returns: Information 00155 (negative value on error) 00156 ------------------------------------------------ */ 00157 int8_t i8DS2482_OWReadBit(struct sDS2482_t *dev) 00158 { 00159 return i8DS2482_OWTouchBit(dev, 0x01); 00160 } 00161 00162 //-------------------------------------------------------------------------- 00163 // Send 1 bit of communication to the 1-Wire Net and return the 00164 // result 1 bit read from the 1-Wire Net. The parameter 'sendbit' 00165 // least significant bit is used and the least significant bit 00166 // of the result is the return bit. 00167 // 00168 // 'sendbit' - the least significant bit is the bit to send 00169 // 00170 // Returns: 0: 0 bit read from sendbit 00171 // 1: 1 bit read from sendbit 00172 // 00173 00174 int8_t i8DS2482_OWTouchBit(struct sDS2482_t *dev, uint8_t sendbit) 00175 { 00176 uint8_t status; 00177 int poll_count = 0; 00178 00179 // 1-Wire bit (Case B) 00180 // S AD,0 [A] 1WSB [A] BB [A] Sr AD,1 [A] [Status] A [Status] A\ P 00181 // \--------/ 00182 // Repeat until 1WB bit has changed to 0 00183 // [] indicates from slave 00184 // BB indicates byte containing bit value in msbit 00185 00186 uint8_t u8Data[2]; 00187 u8Data[0] = OWSB; 00188 u8Data[1] = sendbit?0x80:0x00; 00189 00190 if(_i8I2CWrite(dev->u8Addr, u8Data, 2) != 0) 00191 return -DS2482_ERR_I2CWRITE; 00192 00193 // loop checking 1WB bit for completion of 1-Wire operation 00194 // abort if poll limit reached 00195 _i8I2CRead(dev->u8Addr, &status, 1, 0); 00196 do 00197 { 00198 00199 _i8I2CRead(dev->u8Addr, &status, 1, status & STATUS_1WB); 00200 } 00201 while ((status & STATUS_1WB) && (poll_count++ < POLL_LIMIT)); 00202 00203 // check for failure due to poll limit reached 00204 if (poll_count >= POLL_LIMIT) 00205 { 00206 i8DS2482Reset(dev); 00207 return -DS2482_ERR_TIMEOUT; 00208 } 00209 00210 // return bit state 00211 if (status & STATUS_SBR) 00212 return 1; 00213 else 00214 return 0; 00215 } 00216 00217 /* ================================================ 00218 Sends 1 byte to 1Wire network of given DS2482 IC 00219 Parameter: 1 Pointer to DS2482 object to work on 00220 2 Data 00221 Returns: zero 00222 (negative value on error) 00223 ------------------------------------------------ */ 00224 int8_t i8DS2482_OWWriteByte(struct sDS2482_t *dev, uint8_t sendbyte) 00225 { 00226 // 1-Wire Write Byte (Case B) 00227 // S AD,0 [A] 1WWB [A] DD [A] Sr AD,1 [A] [Status] A [Status] A\ P 00228 // \--------/ 00229 // Repeat until 1WB bit has changed to 0 00230 // [] indicates from slave 00231 // DD data to write 00232 00233 uint8_t u8Data[2]; 00234 u8Data[0] = OWWB; 00235 u8Data[1] = sendbyte; 00236 00237 if(_i8I2CWrite(dev->u8Addr, u8Data, 2) != 0) 00238 return -DS2482_ERR_I2CWRITE; 00239 00240 int8_t i8Tmp = i8DS2482_OWWait(dev); 00241 if(i8Tmp != 0) 00242 return i8Tmp; 00243 00244 return 0; // all went good 00245 } 00246 00247 /* ================================================ 00248 Reads 1 byte from 1Wire network of given DS2482 IC 00249 Parameter: 1 Pointer to DS2482 object to work on 00250 Returns: Read byte 00251 (negative value on error) 00252 ------------------------------------------------ */ 00253 int16_t i16DS2482_OWReadByte(struct sDS2482_t *dev) 00254 { 00255 // 1-Wire Read Bytes (Case C) 00256 // S AD,0 [A] 1WRB [A] Sr AD,1 [A] [Status] A [Status] A\ 00257 // \--------/ 00258 // Repeat until 1WB bit has changed to 0 00259 // Sr AD,0 [A] SRP [A] E1 [A] Sr AD,1 [A] DD A\ P 00260 // 00261 // [] indicates from slave 00262 // DD data read 00263 uint8_t u8Data[2]; 00264 uint8_t data = 0; 00265 u8Data[0] = OWRB; 00266 00267 if(_i8I2CWrite(dev->u8Addr, u8Data, 1) != 0) 00268 return -DS2482_ERR_I2CWRITE; 00269 00270 i8DS2482_OWWait(dev); 00271 00272 u8Data[0] = SRP; 00273 u8Data[1] = 0xE1; 00274 00275 if(_i8I2CWrite(dev->u8Addr, u8Data, 2) != 0) 00276 return -DS2482_ERR_I2CWRITE; 00277 00278 if(_i8I2CRead(dev->u8Addr, &data, 1, 0) != 0) 00279 return -DS2482_ERR_I2CREAD; 00280 00281 return data; 00282 } 00283 00284 /* ================================================ 00285 Write-and-read block of data (byte-alligned) 00286 to/from 1Wire network of given DS2482 00287 Parameter: 1 Pointer to DS2482 object to work on 00288 2 Pointer to Data Buffer 00289 3 Length (in bytes) of Data Buffer 00290 Returns: zero (Data Buffer now contains read data) 00291 (negative value on error) 00292 ------------------------------------------------ */ 00293 int8_t i8DS2482_OWBlock(struct sDS2482_t *dev, uint8_t *tran_buf, uint8_t tran_len) 00294 { 00295 int i; 00296 int16_t i16Ret; 00297 00298 for (i = 0; i < tran_len; i++) 00299 { 00300 00301 i16Ret = i16DS2482_OWTouchByte(dev, tran_buf[i]); 00302 00303 if(i16Ret >= 0) 00304 tran_buf[i] = (uint8_t)i16Ret; 00305 else 00306 return i16Ret; 00307 } 00308 return 0; 00309 } 00310 00311 /* ================================================ 00312 Waits for 1Wire transmission ends on given DS2482 IC 00313 Parameter: 1 Pointer to DS2482 object to work on 00314 Returns: zero 00315 (negative value on error) 00316 ------------------------------------------------ */ 00317 int8_t i8DS2482_OWWait(struct sDS2482_t *dev) 00318 { 00319 uint8_t status, poll_count; 00320 poll_count = 0; 00321 status = 0; 00322 // loop checking 1WB bit for completion of 1-Wire operation 00323 // abort if poll limit reached 00324 _i8I2CRead(dev->u8Addr, &status, 1, 0); 00325 do 00326 { 00327 _i8I2CRead(dev->u8Addr, &status, 1 ,status & STATUS_1WB); 00328 } 00329 while ((status & STATUS_1WB) && (poll_count++ < POLL_LIMIT)); 00330 00331 // check for failure due to poll limit reached 00332 if (poll_count >= POLL_LIMIT) 00333 { 00334 i8DS2482Reset(dev); 00335 return -DS2482_ERR_TIMEOUT; 00336 } 00337 return 0; 00338 } 00339 00340 /* ================================================ 00341 Write-and-Reads a Byte to/from 1Wire network 00342 of given DS2482 00343 Parameter: 1 Pointer to DS2482 object to work on 00344 2 Byte to send 00345 Returns: Read data 00346 (negative value on error) 00347 ------------------------------------------------ */ 00348 int16_t i16DS2482_OWTouchByte(struct sDS2482_t *dev, uint8_t sendbyte) 00349 { 00350 if (sendbyte == 0xFF) 00351 { 00352 return i16DS2482_OWReadByte(dev); 00353 } 00354 else 00355 { 00356 return i8DS2482_OWWriteByte(dev, sendbyte); 00357 } 00358 } 00359 00360 //-------------------------------------------------------------------------- 00361 // Find the 'first' devices on the 1-Wire network 00362 // Return TRUE : device found, ROM number in ROM_NO buffer 00363 // FALSE : no device present 00364 // 00365 00366 int16_t i16DS2482_OWFirst(struct sDS2482_t *dev) 00367 { 00368 // reset the search state 00369 dev->i16LastDiscrepancy = 0; 00370 dev->i16LastDeviceFlag = 0; 00371 dev->i16LastFamilyDiscrepancy = 0; 00372 00373 return i16DS2482_OWSearch(dev); 00374 } 00375 00376 //-------------------------------------------------------------------------- 00377 // Find the 'next' devices on the 1-Wire network 00378 // Return TRUE : device found, ROM number in ROM_NO buffer 00379 // FALSE : device not found, end of search 00380 // 00381 int16_t i16DS2482_OWNext(struct sDS2482_t *dev) 00382 { 00383 // leave the search state alone 00384 return i16DS2482_OWSearch(dev); 00385 } 00386 00387 00388 //-------------------------------------------------------------------------- 00389 // The 'OWSearch' function does a general search. This function 00390 // continues from the previous search state. The search state 00391 // can be reset by using the 'OWFirst' function. 00392 // This function contains one parameter 'alarm_only'. 00393 // When 'alarm_only' is TRUE (1) the find alarm command 00394 // 0xEC is sent instead of the normal search command 0xF0. 00395 // Using the find alarm command 0xEC will limit the search to only 00396 // 1-Wire devices that are in an 'alarm' state. 00397 // 00398 // Returns: TRUE (1) : when a 1-Wire device was found and its 00399 // Serial Number placed in the global ROM 00400 // FALSE (0): when no new device was found. Either the 00401 // last search was the last device or there 00402 // are no devices on the 1-Wire Net. 00403 // 00404 int16_t i16DS2482_OWSearch(struct sDS2482_t *dev) 00405 { 00406 int id_bit_number; 00407 int last_zero, rom_byte_number, search_result; 00408 int id_bit, cmp_id_bit; 00409 unsigned char rom_byte_mask, search_direction, status; 00410 00411 00412 00413 // initialize for search 00414 id_bit_number = 1; 00415 last_zero = 0; 00416 rom_byte_number = 0; 00417 rom_byte_mask = 1; 00418 search_result = 0; 00419 dev->u8CRC8 = 0; 00420 00421 // if the last call was not the last one 00422 if (!dev->i16LastDeviceFlag) 00423 { 00424 // 1-Wire reset 00425 00426 int8_t i8Ret = i8DS2482_OWReset(dev); 00427 if(i8Ret != 0) 00428 { 00429 // reset the search 00430 dev->i16LastDiscrepancy = 0; 00431 dev->i16LastDeviceFlag = 0; 00432 dev->i16LastFamilyDiscrepancy = 0; 00433 return i8Ret; 00434 } 00435 00436 // issue the search command 00437 00438 i8Ret = i8DS2482_OWWriteByte(dev, 0xF0); 00439 if(i8Ret != 0) 00440 return i8Ret; 00441 00442 // loop to do the search 00443 00444 do 00445 { 00446 00447 // if this discrepancy if before the Last Discrepancy 00448 // on a previous next then pick the same as last time 00449 if (id_bit_number < dev->i16LastDiscrepancy) 00450 { 00451 if ((dev->u8RomNr[rom_byte_number] & rom_byte_mask) > 0) 00452 search_direction = 1; 00453 else 00454 search_direction = 0; 00455 } 00456 else 00457 { 00458 // if equal to last pick 1, if not then pick 0 00459 if (id_bit_number == dev->i16LastDiscrepancy) 00460 search_direction = 1; 00461 else 00462 search_direction = 0; 00463 } 00464 00465 // Perform a triple operation on the DS2482 which will perform 00466 // 2 read bits and 1 write bit 00467 status = i16DS2482_search_triplet(dev, search_direction); 00468 00469 // check bit results in status byte 00470 id_bit = ((status & STATUS_SBR) == STATUS_SBR); 00471 cmp_id_bit = ((status & STATUS_TSB) == STATUS_TSB); 00472 search_direction = 00473 ((status & STATUS_DIR) == STATUS_DIR) ? 1 : 0; 00474 00475 // check for no devices on 1-Wire 00476 if ((id_bit) && (cmp_id_bit)) 00477 break; 00478 else 00479 { 00480 if ((!id_bit) && (!cmp_id_bit) && (search_direction == 0)) 00481 { 00482 last_zero = id_bit_number; 00483 00484 // check for Last discrepancy in family 00485 if (last_zero < 9) 00486 dev->i16LastFamilyDiscrepancy = last_zero; 00487 } 00488 00489 // set or clear the bit in the ROM byte rom_byte_number 00490 // with mask rom_byte_mask 00491 if (search_direction == 1) 00492 dev->u8RomNr[rom_byte_number] |= rom_byte_mask; 00493 else 00494 dev->u8RomNr[rom_byte_number] &= ~rom_byte_mask; 00495 00496 // increment the byte counter id_bit_number 00497 // and shift the mask rom_byte_mask 00498 id_bit_number++; 00499 rom_byte_mask <<= 1; 00500 00501 // if the mask is 0 then go to new SerialNum byte rom_byte_number 00502 // and reset mask 00503 if (rom_byte_mask == 0) 00504 { 00505 dev->u8CRC8 += calc_crc8(&dev->u8RomNr[rom_byte_number], 1); // accumulate the CRC 00506 rom_byte_number++; 00507 rom_byte_mask = 1; 00508 } 00509 } 00510 } 00511 while(rom_byte_number < 8); // loop until through all ROM bytes 0-7 00512 00513 // do CRC check 00514 dev->u8CRC8 = calc_crc8(dev->u8RomNr, 7); 00515 if(dev->u8CRC8 != dev->u8RomNr[7]) 00516 return -DS2482_ERR_CHECKSUM; 00517 00518 00519 if(!(id_bit_number < 65 )) 00520 { 00521 // search successful so set LastDiscrepancy,LastDeviceFlag 00522 // search_result 00523 dev->i16LastDiscrepancy = last_zero; 00524 00525 // check for last device 00526 if (dev->i16LastDiscrepancy == 0) 00527 dev->i16LastDeviceFlag = 1; 00528 00529 search_result = 1; 00530 } 00531 else 00532 { 00533 return -DS2482_ERR_CHECKSUM; 00534 } 00535 } 00536 00537 // if no device found then reset counters so next 00538 // 'search' will be like a first 00539 00540 if (!search_result || (dev->u8RomNr[0] == 0)) 00541 { 00542 dev->i16LastDiscrepancy = 0; 00543 dev->i16LastDeviceFlag = 0; 00544 dev->i16LastFamilyDiscrepancy = 0; 00545 search_result = 0; 00546 } 00547 00548 return search_result; 00549 } 00550 00551 //-------------------------------------------------------------------------- 00552 // Use the DS2482 help command '1-Wire triplet' to perform one bit of a 00553 //1-Wire search. 00554 //This command does two read bits and one write bit. The write bit 00555 // is either the default direction (all device have same bit) or in case of 00556 // a discrepancy, the 'search_direction' parameter is used. 00557 // 00558 // Returns – The DS2482 status byte result from the triplet command 00559 // 00560 int16_t i16DS2482_search_triplet(struct sDS2482_t *dev, int search_direction) 00561 { 00562 unsigned char status; 00563 int poll_count = 0; 00564 00565 // 1-Wire Triplet (Case B) 00566 // S AD,0 [A] 1WT [A] SS [A] Sr AD,1 [A] [Status] A [Status] A\ P 00567 // \--------/ 00568 // Repeat until 1WB bit has changed to 0 00569 // [] indicates from slave 00570 // SS indicates byte containing search direction bit value in msbit 00571 00572 uint8_t u8Data[2]; 00573 u8Data[0] = OWT; 00574 u8Data[1] = search_direction ? 0x80 : 0x00; 00575 00576 if(_i8I2CWrite(dev->u8Addr, u8Data, 2) != 0) 00577 return -DS2482_ERR_I2CWRITE; 00578 00579 // loop checking 1WB bit for completion of 1-Wire operation 00580 // abort if poll limit reached 00581 _i8I2CRead(dev->u8Addr, &status, 1, 0); 00582 do 00583 { 00584 _i8I2CRead(dev->u8Addr, &status, 1 ,status & STATUS_1WB); 00585 } 00586 while ((status & STATUS_1WB) && (poll_count++ < POLL_LIMIT)); 00587 00588 // check for failure due to poll limit reached 00589 if (poll_count >= POLL_LIMIT) 00590 { 00591 i8DS2482Reset(dev); 00592 return -DS2482_ERR_TIMEOUT; 00593 } 00594 00595 // return status byte 00596 return status; 00597 } 00598 00599 uint8_t calc_crc8 ( uint8_t *data_in, int number_of_bytes_to_read ) 00600 { 00601 char crc; 00602 int loop_count; 00603 char bit_counter; 00604 char data; 00605 char feedback_bit; 00606 00607 crc = CRC8INIT; 00608 00609 for (loop_count = 0; loop_count != number_of_bytes_to_read; loop_count++) 00610 { 00611 data = data_in[loop_count]; 00612 00613 bit_counter = 8; 00614 do { 00615 feedback_bit = (crc ^ data) & 0x01; 00616 00617 if ( feedback_bit == 0x01 ) { 00618 crc = crc ^ CRC8POLY; 00619 } 00620 crc = (crc >> 1) & 0x7F; 00621 if ( feedback_bit == 0x01 ) { 00622 crc = crc | 0x80; 00623 } 00624 00625 data = data >> 1; 00626 bit_counter--; 00627 00628 } while (bit_counter > 0); 00629 } 00630 00631 return crc; 00632 } 00633 00634 int8_t i8DS2482_OWSelectDevice(struct sDS2482_t *dev, uint8_t *u8SN) 00635 { 00636 int8_t i8Tmp; 00637 00638 // 1. reset 1Wire bus 00639 i8Tmp = i8DS2482_OWReset(dev); 00640 if(i8Tmp != 0) 00641 return i8Tmp; 00642 00643 // 2. issue command 0x55 00644 i8Tmp = i8DS2482_OWWriteByte(dev, 0x55); 00645 //i8Tmp = i8DS2482_OWWriteByte(dev, 0x69); 00646 if(i8Tmp != 0) 00647 return i8Tmp; 00648 //i8DS2482SetControlBits(dev, APU | OWS ); 00649 00650 // 3. wait for 1wire transaction 00651 i8Tmp = i8DS2482_OWWait(dev); 00652 if(i8Tmp != 0) 00653 return i8Tmp; 00654 00655 // 4. send rom code to select device 00656 for(int x=0; x<8; x++) 00657 i8DS2482_OWWriteByte(dev, u8SN[x]); 00658 00659 // 5. wait for 1wire transaction 00660 i8Tmp = i8DS2482_OWWait(dev); 00661 if(i8Tmp != 0) 00662 return i8Tmp; 00663 00664 return 0; 00665 } 00666 00667 int8_t i8DS2482_OWWaitForDevice(struct sDS2482_t *dev) 00668 { 00669 int8_t i8Tmp; 00670 00671 do // wait for completion 00672 { 00673 i8Tmp = i8DS2482_OWReadBit(dev); 00674 if(i8Tmp<0) 00675 return i8Tmp; 00676 } while(i8Tmp == 0); 00677 00678 return 0; 00679 } 00680 00681 int8_t i8DS2482_OWStartAllDS1820(struct sDS2482_t *dev, uint8_t u8WaitForCompletion) 00682 { 00683 int8_t i8Tmp; 00684 i8DS2482_OWReset(dev); 00685 00686 i8Tmp = i8DS2482_OWWriteByte(dev, 0xCC); // skip rom 00687 //i8Tmp = i8DS2482_OWWriteByte(dev, 0x3C); // skip rom 00688 if(i8Tmp) return i8Tmp; 00689 //i8DS2482SetControlBits(dev, APU | OWS ); 00690 00691 i8Tmp = i8DS2482_OWWriteByte(dev, 0x44); // start conversion 00692 if(i8Tmp) return i8Tmp; 00693 00694 if(u8WaitForCompletion) 00695 { 00696 i8Tmp = i8DS2482_OWWaitForDevice(dev); // wait for device 00697 if(i8Tmp) return i8Tmp; 00698 } 00699 00700 return 0; 00701 } 00702 00703 int8_t i8DS2482_OWCheckDeviceReady(struct sDS2482_t *dev) 00704 { 00705 00706 return i8DS2482_OWReadBit(dev); 00707 00708 } 00709 00710 int16_t i16DS2482_OWReadDS1820(struct sDS2482_t *dev, uint8_t *u8SN, uint8_t u8ManualStart) 00711 { 00712 int8_t i8Tmp; 00713 int16_t i16Tmp; 00714 uint8_t u8Data[9]; 00715 uint8_t u8CRC8 = 0; 00716 00717 if(u8ManualStart) 00718 { 00719 i8DS2482_OWReset(dev); 00720 00721 i8Tmp = i8DS2482_OWSelectDevice(dev, u8SN); 00722 if(i8Tmp) return i8Tmp; 00723 00724 i8Tmp = i8DS2482_OWWriteByte(dev, 0x44); 00725 if(i8Tmp) return i8Tmp; 00726 00727 do 00728 { 00729 i8Tmp = i8DS2482_OWReadBit(dev); 00730 if(i8Tmp<0) 00731 return i8Tmp; 00732 } while(i8Tmp == 0); 00733 } 00734 00735 i8DS2482_OWReset(dev); 00736 00737 i8Tmp = i8DS2482_OWSelectDevice(dev, u8SN); 00738 if(i8Tmp) return i8Tmp; 00739 00740 i8Tmp = i8DS2482_OWWriteByte(dev, 0xBE); 00741 if(i8Tmp) return i8Tmp; 00742 00743 for(int x=0; x<9; x++) 00744 { 00745 00746 i16Tmp = i16DS2482_OWReadByte(dev); 00747 00748 if(i16Tmp<0) 00749 return i16Tmp; 00750 else 00751 u8Data[x] = i16Tmp; 00752 } 00753 // check CRC 00754 u8CRC8 = calc_crc8(u8Data, 8); 00755 if(u8CRC8 != u8Data[8]) 00756 return -DS2482_ERR_CHECKSUM; 00757 00758 // calculate temperature 00759 // check sign 00760 if(u8Data[1]) 00761 u8Data[0] = ~u8Data[0]; 00762 00763 i16Tmp = u8Data[0]; 00764 00765 if(!u8Data[1]) 00766 i16Tmp+=109; 00767 else 00768 i16Tmp=110-i16Tmp; 00769 00770 return i16Tmp; 00771 } 00772 00773 00774 int8_t i8DS2482_OWReadDS1820Precise(struct sDS2482_t *dev, uint8_t *u8SN, uint8_t u8ManualStart, int16_t *i16Temperature) 00775 { 00776 int8_t i8Tmp; 00777 int16_t i16Tmp; 00778 uint8_t u8Data[9]; 00779 uint8_t u8CRC8 = 0; 00780 00781 if(u8ManualStart) 00782 { 00783 i8DS2482_OWReset(dev); 00784 00785 i8Tmp = i8DS2482_OWSelectDevice(dev, u8SN); 00786 if(i8Tmp) return i8Tmp; 00787 00788 i8Tmp = i8DS2482_OWWriteByte(dev, 0x44); 00789 if(i8Tmp) return i8Tmp; 00790 00791 do 00792 { 00793 i8Tmp = i8DS2482_OWReadBit(dev); 00794 if(i8Tmp<0) 00795 return i8Tmp; 00796 } while(i8Tmp == 0); 00797 } 00798 00799 i8DS2482_OWReset(dev); 00800 00801 i8Tmp = i8DS2482_OWSelectDevice(dev, u8SN); 00802 if(i8Tmp) return i8Tmp; 00803 00804 i8Tmp = i8DS2482_OWWriteByte(dev, 0xBE); 00805 if(i8Tmp) return i8Tmp; 00806 00807 for(int x=0; x<9; x++) 00808 { 00809 00810 i16Tmp = i16DS2482_OWReadByte(dev); 00811 00812 if(i16Tmp<0) 00813 return i16Tmp; 00814 else 00815 u8Data[x] = i16Tmp; 00816 } 00817 // check CRC 00818 u8CRC8 = calc_crc8(u8Data, 8); 00819 if(u8CRC8 != u8Data[8]) 00820 return -DS2482_ERR_CHECKSUM; 00821 00822 // calculate temperature 00823 *i16Temperature = u8Data[0]&0xFE; 00824 if(u8Data[1]) 00825 { 00826 *i16Temperature = ~*i16Temperature; 00827 *i16Temperature -= 1; 00828 } 00829 00830 *i16Temperature*=100; 00831 *i16Temperature -= 250; 00832 int16_t countPerC, countRemain; 00833 countPerC = u8Data[7]; 00834 countRemain = u8Data[6]; 00835 00836 *i16Temperature += ((100*(countPerC-countRemain)))/countPerC; 00837 00838 return 0; 00839 }
Generated on Sun Jul 17 2022 22:35:34 by
1.7.2