CUER version of the DS1820 library
Fork of DS1820 by
Diff: DS1820.cpp
- Revision:
- 1:91aa74f0cb0e
- Parent:
- 0:245af9360f0d
- Child:
- 2:4c7277e9f267
--- a/DS1820.cpp Tue Feb 28 07:42:07 2012 +0000 +++ b/DS1820.cpp Sun Jul 02 15:10:13 2017 +0000 @@ -1,15 +1,16 @@ #include "DS1820.h" #include "mbed.h" - + // Global variables shared between all DS1820 objects bool DS1820_done_flag; int DS1820_last_descrepancy; char DS1820_search_ROM[8]; - - + + DS1820::DS1820 (PinName data_pin, PinName power_pin) : _datapin(data_pin), _parasitepin(power_pin) { int byte_counter; _parasite_power = true; + _parasitepin = 1; //Initial value set high, P-channel mosfet is off so data can transmit for(byte_counter=0;byte_counter<8;byte_counter++) ROM[byte_counter] = 0xFF; for(byte_counter=0;byte_counter<9;byte_counter++) @@ -23,7 +24,7 @@ for(byte_counter=0;byte_counter<9;byte_counter++) RAM[byte_counter] = 0x00; } - + bool DS1820::onewire_reset() { // This will return false if no devices are present on the data bus bool presence=false; @@ -37,7 +38,7 @@ wait_us(410); return presence; } - + void DS1820::onewire_bit_out (bool bit_data) { _datapin.output(); _datapin = 0; @@ -50,7 +51,7 @@ _datapin.input(); } } - + void DS1820::onewire_byte_out(char data) { // output data character (least sig bit first). int n; for (n=0; n<8; n++) { @@ -58,7 +59,7 @@ data = data >> 1; // now the next bit is in the least sig bit position. } } - + bool DS1820::onewire_bit_in() { bool answer; _datapin.output(); @@ -70,7 +71,7 @@ wait_us(50); return answer; } - + char DS1820::onewire_byte_in() { // read byte, least sig byte first char answer = 0x00; int i; @@ -81,15 +82,15 @@ } return answer; } - + bool DS1820::search_ROM() { return search_ROM_routine(0xF0); // Search ROM command } - + bool DS1820::search_alarm() { return search_ROM_routine(0xEC); // Search Alarm command } - + bool DS1820::search_ROM_routine(char command) { extern bool DS1820_done_flag; extern int DS1820_last_descrepancy; @@ -97,7 +98,7 @@ int descrepancy_marker, ROM_bit_index; bool return_value, Bit_A, Bit_B; char byte_counter, bit_mask; - + return_value=false; if (!DS1820_done_flag) { if (!onewire_reset()) { @@ -158,7 +159,7 @@ } return return_value; } - + void DS1820::search_ROM_setup() { extern bool DS1820_done_flag; extern int DS1820_last_descrepancy; @@ -169,7 +170,7 @@ for (i=0; i<8; i++) DS1820_search_ROM[i]=0x00; } - + void DS1820::read_ROM() { // NOTE: This command can only be used when there is one DS1820 on the bus. If this command // is used when there is more than one slave present on the bus, a data collision will occur @@ -180,7 +181,7 @@ for (i=0; i<8; i++) ROM[i]=onewire_byte_in(); } - + void DS1820::match_ROM() { // Used to select a specific device int i; @@ -189,77 +190,89 @@ for (i=0;i<8;i++) onewire_byte_out(ROM[i]); } - + void DS1820::skip_ROM() { onewire_reset(); onewire_byte_out(0xCC); // Skip ROM command } - + bool DS1820::ROM_checksum_error() { - char CRC=0x00; + char crc=0x00; int i; for(i=0;i<7;i++) // Only going to shift the lower 7 bytes - CRC = CRC_byte(CRC, ROM[i]); - // After 7 bytes CRC should equal the 8th byte (ROM CRC) - return (CRC!=ROM[7]); // will return true if there is a CRC checksum error + crc = CRC_byte(crc, ROM[i]); + // After 7 bytes crc should equal the 8th byte (ROM crc) + return (crc!=ROM[7]); // will return true if there is a crc checksum error } - + bool DS1820::RAM_checksum_error() { - char CRC=0x00; + char crc=0x00; int i; read_RAM(); for(i=0;i<8;i++) // Only going to shift the lower 8 bytes - CRC = CRC_byte(CRC, RAM[i]); - // After 8 bytes CRC should equal the 9th byte (RAM CRC) - return (CRC!=RAM[8]); // will return true if there is a CRC checksum error + crc = CRC_byte(crc, RAM[i]); + // After 8 bytes crc should equal the 9th byte (RAM crc) + return (crc!=RAM[8]); // will return true if there is a crc checksum error } - -char DS1820::CRC_byte (char CRC, char byte ) { + +char DS1820::CRC_byte (char crc, char byte ) { int j; for(j=0;j<8;j++) { - if ((byte & 0x01 ) ^ (CRC & 0x01)) { - // DATA ^ LSB CRC = 1 - CRC = CRC>>1; + if ((byte & 0x01 ) ^ (crc & 0x01)) { + // DATA ^ LSB crc = 1 + crc = crc>>1; // Set the MSB to 1 - CRC = CRC | 0x80; + crc = crc | 0x80; // Check bit 3 - if (CRC & 0x04) { - CRC = CRC & 0xFB; // Bit 3 is set, so clear it + if (crc & 0x04) { + crc = crc & 0xFB; // Bit 3 is set, so clear it } else { - CRC = CRC | 0x04; // Bit 3 is clear, so set it + crc = crc | 0x04; // Bit 3 is clear, so set it } // Check bit 4 - if (CRC & 0x08) { - CRC = CRC & 0xF7; // Bit 4 is set, so clear it + if (crc & 0x08) { + crc = crc & 0xF7; // Bit 4 is set, so clear it } else { - CRC = CRC | 0x08; // Bit 4 is clear, so set it + crc = crc | 0x08; // Bit 4 is clear, so set it } } else { - // DATA ^ LSB CRC = 0 - CRC = CRC>>1; + // DATA ^ LSB crc = 0 + crc = crc>>1; // clear MSB - CRC = CRC & 0x7F; - // No need to check bits, with DATA ^ LSB CRC = 0, they will remain unchanged + crc = crc & 0x7F; + // No need to check bits, with DATA ^ LSB crc = 0, they will remain unchanged } byte = byte>>1; } -return CRC; +return crc; } - + 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); + _parasitepin = 0; // Parasite power strong pullup + wait_ms(delay_time); if (_parasite_power) - _parasitepin = 0; + _parasitepin = 1; } - + void DS1820::read_RAM() { // This will copy the DS1820's 9 bytes of RAM data // into the objects RAM array. Functions that use @@ -271,14 +284,27 @@ RAM[i] = onewire_byte_in(); } } - + +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(); answer = (RAM[2]<<8) + RAM[3]; return answer; } - + void DS1820::write_scratchpad(int data) { RAM[3] = data; RAM[2] = data>>8; @@ -286,8 +312,11 @@ 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) { if (device==all_devices) skip_ROM(); // Skip ROM command, will store for ALL devices @@ -295,12 +324,12 @@ match_ROM(); onewire_byte_out(0x48); // Write scratchpad into E2 command if (_parasite_power) - _parasitepin=1; + _parasitepin=0; wait_ms(10); // Parasite power strong pullup for 10ms if (_parasite_power) - _parasitepin=0; + _parasitepin=1; } - + int DS1820::recall_scratchpad(devices device) { // This copies the E2 values into the DS1820's memory. // If you specify all_devices this will return zero, otherwise @@ -319,7 +348,7 @@ } return answer; } - + float DS1820::temperature(char scale) { // The data specs state that count_per_degree should be 0x10 (16), I found my devices // to have a count_per_degree of 0x4B (75). With the standard resolution of 1/2 deg C @@ -333,10 +362,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 @@ -344,7 +378,7 @@ answer = answer * 9.0 / 10.0 + 32.0; return answer; } - + bool DS1820::read_power_supply(devices device) { // This will return true if the device (or all devices) are Vcc powered // This will return false if the device (or ANY device) is parasite powered @@ -355,3 +389,5 @@ onewire_byte_out(0xB4); // Read power supply command return onewire_bit_in(); } + + \ No newline at end of file