Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
onewire.cpp
00001 /** 00002 * @file onewire.c 00003 * @brief library 1-Wire(www.maxim-ic.com) 00004 * @author Maciej Rajtar (Published 10 May 2010 www.mbed.org) 00005 * @author Frederic BLANC (Published 01/03/2012 www.mbed.org) 00006 */ 00007 00008 #include "mbed.h" 00009 #include "onewire.h" 00010 00011 DigitalInOut ow_pin(p21); 00012 DigitalInOut ow_0(p21); 00013 DigitalInOut ow_1(p22); 00014 DigitalInOut ow_2(p23); 00015 DigitalInOut ow_3(p24); 00016 DigitalInOut* t_ow[4]={&ow_0,&ow_1,&ow_2,&ow_3}; 00017 00018 //********************************************************************************************************** 00019 //* show_id 00020 //********************************************************************************************************** 00021 00022 00023 /** 00024 * @brief show_id 00025 * @param [in] id[] = rom_code 00026 * @param [out] text id 00027 * @date 02/12/2013 00028 */ 00029 char* ow_show_id( uint8_t id[],char *text) { 00030 00031 char hex[4]; 00032 sprintf(text,""); 00033 sprintf(hex,"%2.2X",id[0]); 00034 strcat(text,hex); 00035 for (int i = OW_ROMCODE_SIZE-2; i >=1 ; --i ) { 00036 sprintf(hex,"%2.2X",id[i]); 00037 strcat(text,hex); 00038 } 00039 sprintf(hex,"%2.2X",id[OW_ROMCODE_SIZE-1]); 00040 strcat(text,hex); 00041 return text; 00042 00043 } 00044 //********************************************************************************************************** 00045 //* show_id 00046 //********************************************************************************************************** 00047 00048 00049 /** 00050 * @brief uint64_id 00051 * @param [in] id[] = rom_code 00052 * @return [out] uint64_t id 00053 * @date 28/03/2011 00054 */ 00055 uint64_t uint64_id( uint8_t id[]) { 00056 00057 uint64_t *ptr; 00058 00059 ptr=(uint64_t *) &id[0]; 00060 00061 return *ptr; 00062 } 00063 //********************************************************************************************************** 00064 //* search_sensors 00065 //********************************************************************************************************** 00066 00067 /** 00068 * @brief search_sensors 00069 * @param [out] nSensors number of device onewire 00070 * @param [out] gSensorIDs[][] array of id romcode 00071 * @return OW_OK or OW_PRESENCE_ERR or OW_DATA_ERR 00072 * @date 20/06/2011 00073 */ 00074 uint8_t search_sensors(uint8_t *nSensors,uint8_t gSensorIDs[][OW_ROMCODE_SIZE] ) { 00075 00076 00077 uint8_t i; 00078 uint8_t id[OW_ROMCODE_SIZE]; 00079 uint8_t diff; 00080 printf( "Scanning Bus\r\n" ); 00081 diff = OW_SEARCH_FIRST; 00082 for (*nSensors = 0 ; (diff != OW_LAST_DEVICE) && (*nSensors < MAXSENSORS) ;++(*nSensors) ) { 00083 ow_find_sensor( &diff, &id[0] ); 00084 if ( diff == OW_PRESENCE_ERR ) { 00085 printf( "No Sensor found\r\n" ); 00086 return diff; 00087 } 00088 if ( diff == OW_DATA_ERR ) { 00089 printf( "Bus Error\r\n" ); 00090 return diff; 00091 } 00092 for (i=0;i<OW_ROMCODE_SIZE;i++) 00093 gSensorIDs[*nSensors][i]=id[i]; 00094 00095 } 00096 return OW_OK; 00097 } 00098 /** 00099 * @brief search_sensors 00100 * @param [in] n num bus onewire 00101 * @param [out] nSensors number of device onewire 00102 * @param [out] gSensorIDs[][][] array of id romcode 00103 * @return OW_OK or OW_PRESENCE_ERR or OW_DATA_ERR 00104 * @date 02/09/2011 00105 */ 00106 uint8_t search_sensors(uint8_t n,uint8_t *nSensors,uint8_t gSensorIDs[][MAXSENSORS][OW_ROMCODE_SIZE] ) { 00107 00108 00109 uint8_t i; 00110 uint8_t id[OW_ROMCODE_SIZE]; 00111 uint8_t diff; 00112 printf( "Scanning Bus %d\r\n",n ); 00113 diff = OW_SEARCH_FIRST; 00114 for (*nSensors = 0 ; (diff != OW_LAST_DEVICE) && (*nSensors < MAXSENSORS) ;++(*nSensors) ) { 00115 ow_find_sensor(n, &diff, &id[0] ); 00116 if ( diff == OW_PRESENCE_ERR ) { 00117 printf( "No Sensor found\r\n" ); 00118 return diff; 00119 } 00120 if ( diff == OW_DATA_ERR ) { 00121 printf( "Bus Error\r\n" ); 00122 return diff; 00123 } 00124 for (i=0;i<OW_ROMCODE_SIZE;i++){ 00125 gSensorIDs[n][*nSensors][i]=id[i]; 00126 //printf( "id[%d]=%d\r\n" ,i,id[i]); 00127 } 00128 00129 } 00130 return OW_OK; 00131 } 00132 //********************************************************************************************************** 00133 //* find Sensors on 1-Wire-Bus 00134 //********************************************************************************************************** 00135 00136 /** 00137 * @brief find Sensors on 1-Wire-Bus 00138 * @param [in/out] diff is the result of the last rom-search 00139 * @param [out] is the rom-code of the sensor found 00140 * @return OW_OK or OW_ERROR 00141 * @date 20/06/2011 00142 */ 00143 uint8_t ow_find_sensor(uint8_t *diff, uint8_t id[]) { 00144 for (;;) 00145 { 00146 *diff = ow_rom_search( *diff, &id[0] ); 00147 if ( *diff==OW_PRESENCE_ERR) 00148 return OW_ERROR; 00149 if ( *diff==OW_DATA_ERR ) 00150 return OW_ERROR; 00151 if ( *diff == OW_LAST_DEVICE ) 00152 return OW_OK ; 00153 if ( id[0] == DS18B20_ID || id[0] == DS18S20_ID ) 00154 return OW_OK ; 00155 if ( id[0] == DS2450_ID ) 00156 return OW_OK ; 00157 if ( id[0] == MAX31850_ID ) 00158 return OW_OK ; 00159 } 00160 } 00161 00162 /** 00163 * @brief find Sensors on 1-Wire-Bus 00164 * @param [in] num bus onewire 00165 * @param [in/out] diff is the result of the last rom-search 00166 * @param [out] is the rom-code of the sensor found 00167 * @return OW_OK or OW_ERROR 00168 * @date 30/08/2011 00169 */ 00170 uint8_t ow_find_sensor(uint8_t n,uint8_t *diff, uint8_t id[]) { 00171 for (;;) 00172 { 00173 *diff = ow_rom_search(n, *diff, &id[0] ); 00174 if ( *diff==OW_PRESENCE_ERR) 00175 return OW_ERROR; 00176 if ( *diff==OW_DATA_ERR ) 00177 return OW_ERROR; 00178 if ( *diff == OW_LAST_DEVICE ) 00179 return OW_OK ; 00180 if ( id[0] == DS18B20_ID || id[0] == DS18S20_ID ) 00181 return OW_OK ; 00182 if ( id[0] == DS2450_ID ) 00183 return OW_OK ; 00184 if ( id[0] == MAX31850_ID ) 00185 return OW_OK ; 00186 } 00187 } 00188 //********************************************************************************************************** 00189 //* search romcode 00190 //********************************************************************************************************** 00191 00192 /** 00193 * @brief search romcode 00194 * @param [in] uint8_t diff 00195 * @param [out] id romcode 00196 * @return next_diff or OW_LAST_DEVICE or OW_DATA_ERR or OW_PRESENCE_ERR 00197 * @date 20/06/2011 00198 */ 00199 uint8_t ow_rom_search( uint8_t diff, uint8_t id[] ) { 00200 uint8_t i, j, next_diff; 00201 uint8_t b; 00202 00203 if ( ow_reset() ) 00204 return OW_PRESENCE_ERR; // error, no device found 00205 ow_byte_wr( OW_SEARCH_ROM ); // ROM search command 00206 next_diff = OW_LAST_DEVICE; // unchanged on last device 00207 i = OW_ROMCODE_SIZE * 8; // 8 bytes 00208 do { 00209 j = 8; // 8 bits 00210 do { 00211 b = ow_bit_io( 1 ); // read bit 00212 if ( ow_bit_io( 1 ) ) { // read complement bit 00213 if ( b ) // 11 00214 return OW_DATA_ERR; // data error 00215 } else { 00216 if ( !b ) { // 00 = 2 devices 00217 if ( diff > i || ((*id & 1) && diff != i) ) { 00218 b = 1; // now 1 00219 next_diff = i; // next pass 0 00220 } 00221 } 00222 } 00223 ow_bit_io( b ); // write bit 00224 *id >>= 1; 00225 if ( b ) 00226 *id |= 0x80; // store bit 00227 --i; 00228 } while ( --j ); 00229 id++; // next byte 00230 } while ( i ); 00231 return next_diff; // to continue search 00232 } 00233 /** 00234 * @brief search romcode 00235 * @param [in]n num bus onewire 00236 * @param [in] uint8_t diff 00237 * @param [out] id romcode 00238 * @return next_diff or OW_LAST_DEVICE or OW_DATA_ERR or OW_PRESENCE_ERR 00239 * @date 30/08/2011 00240 */ 00241 uint8_t ow_rom_search(uint8_t n, uint8_t diff, uint8_t id[] ) { 00242 uint8_t i, j, next_diff; 00243 uint8_t b; 00244 if ( ow_reset(n) ) 00245 return OW_PRESENCE_ERR; // error, no device found 00246 ow_byte_wr(n, OW_SEARCH_ROM ); // ROM search command 00247 next_diff = OW_LAST_DEVICE; // unchanged on last device 00248 i = OW_ROMCODE_SIZE * 8; // 8 bytes 00249 do { 00250 j = 8; // 8 bits 00251 do { 00252 b = ow_bit_io(n, 1 ); // read bit 00253 if ( ow_bit_io(n, 1 ) ) { // read complement bit 00254 if ( b ) // 11 00255 { 00256 00257 return OW_DATA_ERR; // data error 00258 } 00259 } else { 00260 if ( !b ) { // 00 = 2 devices 00261 if ( diff > i || ((*id & 1) && diff != i) ) { 00262 b = 1; // now 1 00263 next_diff = i; // next pass 0 00264 } 00265 } 00266 } 00267 ow_bit_io(n, b ); // write bit 00268 00269 *id >>= 1; 00270 if ( b ) 00271 *id |= 0x80; // store bit 00272 --i; 00273 } while ( --j ); 00274 id++; // next byte 00275 00276 } while ( i ); 00277 00278 00279 return next_diff; // to continue search 00280 } 00281 00282 00283 //********************************************************************************************************** 00284 //* test pin onewire bus 00285 //********************************************************************************************************** 00286 /** 00287 * @brief test pin onewire bus 00288 * @return etat pin ow 00289 * @date 20/06/2011 00290 */ 00291 uint8_t ow_test_pin (void){ 00292 if (ow_pin) 00293 return 1; 00294 return 0; 00295 } 00296 00297 /** 00298 * @brief test pin onewire bus 00299 * @param [in] num bus one wire 00300 * @return etat pin ow 00301 * @date 30/08/2011 00302 */ 00303 uint8_t ow_test_pin (uint8_t n){ 00304 00305 if (*t_ow[n]) 00306 return 1; 00307 return 0; 00308 } 00309 00310 //********************************************************************************************************** 00311 //* onewire reset bus 00312 //********************************************************************************************************** 00313 /** 00314 * @brief onewire reset bus 00315 * @return pin ow or OW_SHORT_CIRCUIT 00316 * @date 20/06/2011 00317 */ 00318 uint8_t ow_reset(void) { // reset. Should improve to act as a presence pulse 00319 uint8_t err; 00320 00321 ow_pin.output(); 00322 ow_pin = 0; // bring low for 500 us 00323 wait_us(500); 00324 ow_pin.input(); 00325 wait_us(60); 00326 err = ow_pin; 00327 wait_us(240); 00328 if ( ow_pin == 0 ) { // short circuit 00329 err = OW_SHORT_CIRCUIT; 00330 } 00331 return err; 00332 } 00333 00334 /** 00335 * @brief onewire reset bus 00336 * @param [in] num bus onewire 00337 * @return pin ow or OW_SHORT_CIRCUIT 00338 * @date 30/08/2011 00339 */ 00340 uint8_t ow_reset(uint8_t n) { // reset. Should improve to act as a presence pulse 00341 uint8_t err; 00342 00343 t_ow[n]->output(); 00344 *t_ow[n] = 0; // bring low for 500 us 00345 wait_us(500); 00346 t_ow[n]->input(); 00347 wait_us(60); 00348 err = *t_ow[n]; 00349 wait_us(240); 00350 if ( *t_ow[n] == 0 ) { // short circuit 00351 err = OW_SHORT_CIRCUIT; 00352 00353 } 00354 return err; 00355 } 00356 //********************************************************************************************************** 00357 //* read write onewire 00358 //********************************************************************************************************** 00359 /** 00360 * @brief read write onewire 00361 * @param [in/out] b data 00362 * @return data 00363 * @date 20/06/2011 00364 */ 00365 uint8_t ow_bit_io( uint8_t b ) { 00366 00367 ow_pin.output(); // drive bus low 00368 ow_pin = 0; 00369 wait_us(1); // Recovery-Time wuffwuff was 1 00370 00371 if ( b ) 00372 ow_pin.input(); // if bit is 1 set bus high (by ext. pull-up) 00373 // delay was 15uS-1 see comment above 00374 wait_us(15-1); 00375 if ( ow_pin == 0 ) b = 0; // sample at end of read-timeslot 00376 wait_us(60-15); 00377 ow_pin.input(); 00378 return b; 00379 } 00380 /** 00381 * @brief read write onewire 00382 * @param [in] n num bus onewire 00383 * @param [in/out] b data 00384 * @return data 00385 * @date 30/08/2011 00386 */ 00387 uint8_t ow_bit_io(uint8_t n, uint8_t b ) { 00388 00389 t_ow[n]->output(); // drive bus low 00390 *t_ow[n] = 0; 00391 wait_us(1); // Recovery-Time wuffwuff was 1 00392 00393 if ( b ) 00394 t_ow[n]->input(); // if bit is 1 set bus high (by ext. pull-up) 00395 // delay was 15uS-1 see comment above 00396 wait_us(15-1); 00397 if ( *t_ow[n] == 0 ) b = 0; // sample at end of read-timeslot 00398 wait_us(60-15); 00399 t_ow[n]->input(); 00400 // printf("ow_bit_io n=%d b=%X\n",n,b); 00401 return b; 00402 } 00403 //********************************************************************************************************** 00404 //* byte write on onewire 00405 //********************************************************************************************************** 00406 /** 00407 * @brief byte write on onewire 00408 * @param [in] b data 00409 * @return data 00410 * @date 20/06/2011 00411 */ 00412 uint8_t ow_byte_wr( uint8_t b ) { 00413 uint8_t i = 8, j; 00414 00415 do { 00416 j = ow_bit_io( b & 1 ); 00417 b >>= 1; 00418 if ( j ) 00419 b |= 0x80; 00420 } while ( --i ); 00421 return b; 00422 } 00423 /** 00424 * @brief byte write on onewire 00425 * @param [in] n num bus onewire 00426 * @param [in] b data 00427 * @return data 00428 * @date 30/08/2011 00429 */ 00430 uint8_t ow_byte_wr(uint8_t n, uint8_t b ) { 00431 uint8_t i = 8, j; 00432 00433 do { 00434 j = ow_bit_io(n, b & 1 ); 00435 b >>= 1; 00436 if ( j ) 00437 b |= 0x80; 00438 } while ( --i ); 00439 return b; 00440 } 00441 //********************************************************************************************************** 00442 //* byte write on onewire 00443 //********************************************************************************************************** 00444 /** 00445 * @brief byte read on onewire 00446 * @return data 00447 * @date 20/06/2011 00448 */ 00449 uint8_t ow_byte_rd( void ) { 00450 // read by sending 0xff (a dontcare?) 00451 return ow_byte_wr( 0xFF ); 00452 } 00453 00454 /** 00455 * @brief byte read on onewire 00456 * @param [in] n num onewire 00457 * @return data 00458 * @date 30/08/2011 00459 */ 00460 uint8_t ow_byte_rd( uint8_t n) { 00461 // read by sending 0xff (a dontcare?) 00462 return ow_byte_wr(n, 0xFF ); 00463 } 00464 //********************************************************************************************************** 00465 //* byte write on onewire 00466 //********************************************************************************************************** 00467 /** 00468 * @brief write command 00469 * @param [in] command 00470 * @param [in] id romcode 00471 * @date 20/06/2011 00472 */ 00473 uint8_t ow_command( uint8_t command, uint8_t id[] ) { 00474 uint8_t i; 00475 00476 ow_reset(); 00477 if ( id ) { 00478 ow_byte_wr( OW_MATCH_ROM ); // to a single device 00479 i = OW_ROMCODE_SIZE; 00480 do { 00481 ow_byte_wr( *id ); 00482 ++id; 00483 } while ( --i ); 00484 } else { 00485 ow_byte_wr( OW_SKIP_ROM ); // to all devices 00486 } 00487 ow_byte_wr( command ); 00488 return 0; 00489 } 00490 /** 00491 * @brief write command 00492 * @param [in] n num bus onewire 00493 * @param [in] command 00494 * @param [in] id romcode 00495 * @date 30/08/2011 00496 */ 00497 uint8_t ow_command(uint8_t n, uint8_t command, uint8_t id[] ) { 00498 uint8_t i; 00499 00500 ow_reset(n); 00501 if ( id ) { 00502 ow_byte_wr( n,OW_MATCH_ROM ); // to a single device 00503 i = OW_ROMCODE_SIZE; 00504 do { 00505 ow_byte_wr(n, *id ); 00506 ++id; 00507 } while ( --i ); 00508 } else { 00509 ow_byte_wr(n, OW_SKIP_ROM ); // to all devices 00510 } 00511 ow_byte_wr(n, command ); 00512 return 0; 00513 } 00514 //********************************************************************************************************** 00515 //* ow mode 00516 //********************************************************************************************************** 00517 /** 00518 * @brief parasite enable 00519 * @date 20/06/2011 00520 */ 00521 uint8_t ow_parasite_enable(void) { 00522 ow_pin.output(); 00523 ow_pin = 1; 00524 return 0; 00525 } 00526 /** 00527 * @brief parasite disable 00528 * @date 20/06/2011 00529 */ 00530 uint8_t ow_parasite_disable(void) { 00531 00532 ow_pin.input(); 00533 return 0; 00534 } 00535 00536 /** 00537 * @brief parasite enable 00538 * @param [in] n num bus onewire 00539 * @date 30/08/2011 00540 */ 00541 uint8_t ow_parasite_enable(uint8_t n) { 00542 t_ow[n]->output(); 00543 *t_ow[n] = 1; 00544 return 0; 00545 } 00546 /** 00547 * @brief parasite disable 00548 * @param [in] n num bus onewire 00549 * @date 30/08/2011 00550 */ 00551 uint8_t ow_parasite_disable(uint8_t n) { 00552 t_ow[n]->input(); 00553 return 0; 00554 } 00555 /** 00556 * @brief PUL-UP bus OW 00557 * @return OW_OK 00558 * @date 20/06/2011 00559 */ 00560 uint8_t ow_PullUp(void) 00561 { 00562 ow_pin.mode(PullUp); //PULL-UP bus OW 00563 return OW_OK; 00564 } 00565 /** 00566 * @brief PUL-UP bus OW 00567 * @param [in] n num bus onewire 00568 * @return OW_OK 00569 * @date 30/08/2011 00570 */ 00571 uint8_t ow_PullUp(uint8_t n) 00572 { 00573 t_ow[n]->mode(PullUp); //PULL-UP bus OW 00574 return OW_OK; 00575 }
Generated on Mon Jul 18 2022 20:45:44 by
1.7.2