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.
Revision 1:6a427f54e82c, committed 2011-12-14
- Comitter:
- Michael_
- Date:
- Wed Dec 14 20:22:16 2011 +0000
- Parent:
- 0:61d83318f2d6
- Child:
- 2:ee820a991b95
- Commit message:
- Added code to support the DS18B20
Changed in this revision
DS1820.cpp | Show annotated file Show diff for this revision Revisions of this file |
DS1820.h | Show annotated file Show diff for this revision Revisions of this file |
--- 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
--- a/DS1820.h Sun Dec 19 05:40:28 2010 +0000 +++ b/DS1820.h Wed Dec 14 20:22:16 2011 +0000 @@ -106,6 +106,10 @@ * ROM[7] is the device CRC */ char ROM[8]; + #define FAMILY_CODE ROM[0] + #define FAMILY_CODE_DS1820 0x10 + #define FAMILY_CODE_DS18S20 0x10 + #define FAMILY_CODE_DS18B20 0x28 /** RAM is a copy of the internal DS1820's RAM * It's updated during the read_RAM() command @@ -189,11 +193,19 @@ */ bool RAM_checksum_error(); - /** This funtion returns the values stored in the temperature + /** This function returns the values stored in the temperature * alarm registers. * * @returns a 16 bit integer of TH (upper byte) and TL (lower byte). */ + bool set_configuration_bits(unsigned int resolution); + + /** This function sets the temperature resolution for the DS18B20 + * in the configuration register. + * + * @param a number between 9 and 12 to specify the resolution + * @returns true if successful + */ int read_scratchpad(); /** This function will store the passed data into the DS1820's RAM. @@ -217,7 +229,7 @@ /** This function will copy the stored values from the EEPROM * into the DS1820's RAM locations for TH and TL. * - * @param allows the fnction to apply to a specific device or + * @param allows the function to apply to a specific device or * to all devices on the 1-Wire bus. */ int recall_scratchpad(devices device=this_device);