Library for reading temperature from DS1820, DS18B20 and DS1822
Dependents: heatmap BLE_Temperature BLE_Temperature_Exercise F334andDS18B20 ... more
Fork of DS1820 by
HelloWorld: http://mbed.org/users/Sissors/code/DS1820_HelloWorld/
Library should currently work on all mbed targets, let me know if there is an issue. First however make sure you have latest version of mbed library and this library.
Diff: DS1820.cpp
- Revision:
- 1:6a427f54e82c
- Parent:
- 0:61d83318f2d6
- Child:
- 2:ee820a991b95
--- a/DS1820.cpp Sun Dec 19 05:40:28 2010 +0000 +++ b/DS1820.cpp Wed Dec 14 20:22:16 2011 +0000 @@ -248,14 +248,26 @@ void DS1820::convert_temperature(devices device) { // Convert temperature into scratchpad RAM for all devices at once + int delay_time = 750; // Default delay time + char resolution; if (device==all_devices) skip_ROM(); // Skip ROM command, will convert for ALL devices - else + else { match_ROM(); + if (FAMILY_CODE == FAMILY_CODE_DS18B20 ) { + resolution = RAM[4] & 0x60; + if (resolution == 0x00) // 9 bits + delay_time = 94; + if (resolution == 0x20) // 10 bits + delay_time = 188; + if (resolution == 0x40) // 11 bits. Note 12bits uses the 750ms default + delay_time = 375; + } + } onewire_byte_out( 0x44); // perform temperature conversion if (_parasite_power) _parasitepin = 1; // Parasite power strong pullup - wait_ms(750); + wait_ms(delay_time); if (_parasite_power) _parasitepin = 0; } @@ -272,6 +284,19 @@ } } +bool DS1820::set_configuration_bits(unsigned int resolution) { + bool answer = false; + resolution = resolution - 9; + if (resolution < 4) { + resolution = resolution<<5; // align the bits + RAM[4] = (RAM[4] & 0x60) | resolution; // mask out old data, insert new + write_scratchpad ((RAM[2]<<8) + RAM[3]); +// store_scratchpad (DS1820::this_device); // Need to test if this is required + answer = true; + } + return answer; +} + int DS1820::read_scratchpad() { int answer; read_RAM(); @@ -286,6 +311,9 @@ onewire_byte_out(0x4E); // Copy scratchpad into DS1820 ram memory onewire_byte_out(RAM[2]); // T(H) onewire_byte_out(RAM[3]); // T(L) + if ( FAMILY_CODE == FAMILY_CODE_DS18B20 ) { + onewire_byte_out(RAM[4]); // Configuration register + } } void DS1820::store_scratchpad(devices device) { @@ -333,10 +361,15 @@ if (reading & 0x8000) { // negative degrees C reading = 0-((reading ^ 0xffff) + 1); // 2's comp then convert to signed int } - remaining_count = RAM[6]; - count_per_degree = RAM[7]; - answer = reading +0.0; - answer = answer - 0.25 + (count_per_degree - remaining_count) / count_per_degree; + answer = reading +0.0; // convert to floating point + if ( FAMILY_CODE == FAMILY_CODE_DS18B20 ) { + answer = answer / 8.0; + } + else { + remaining_count = RAM[6]; + count_per_degree = RAM[7]; + answer = answer - 0.25 + (count_per_degree - remaining_count) / count_per_degree; + } if (scale=='C' or scale=='c') answer = answer / 2.0; else