DS18B20 library

Dependencies:   LinkedList

Fork of DS1820 by Erik -

Committer:
SomeRandomBloke
Date:
Tue Nov 22 16:19:22 2016 +0000
Revision:
14:206d620315cf
Parent:
12:196e9e54b033
Updates

Who changed what in which revision?

UserRevisionLine numberNew contents of line
pairmand 3:8f2b7f4940b5 1 #include "DS1820.h"
Sissors 5:2cd4928e8147 2
Sissors 5:2cd4928e8147 3 LinkedList<node> DS1820::probes;
pairmand 3:8f2b7f4940b5 4
pairmand 3:8f2b7f4940b5 5
Sissors 5:2cd4928e8147 6 DS1820::DS1820 (PinName data_pin, PinName power_pin, bool power_polarity) : _datapin(data_pin), _parasitepin(power_pin) {
pairmand 3:8f2b7f4940b5 7 int byte_counter;
Sissors 5:2cd4928e8147 8 _power_polarity = power_polarity;
florian 9:3821ca0b7f14 9
florian 9:3821ca0b7f14 10 _power_mosfet = power_pin != NC;
Sissors 5:2cd4928e8147 11
pairmand 3:8f2b7f4940b5 12 for(byte_counter=0;byte_counter<9;byte_counter++)
pairmand 3:8f2b7f4940b5 13 RAM[byte_counter] = 0x00;
Sissors 5:2cd4928e8147 14
Sissors 5:2cd4928e8147 15 if (!unassignedProbe(&_datapin, _ROM))
SomeRandomBloke 14:206d620315cf 16 // error("No unassigned DS1820 found!\n");
SomeRandomBloke 14:206d620315cf 17 _hasProbes = false;
Sissors 5:2cd4928e8147 18 else {
SomeRandomBloke 14:206d620315cf 19 _hasProbes = true;
Sissors 5:2cd4928e8147 20 _datapin.input();
Sissors 5:2cd4928e8147 21 probes.append(this);
Sissors 5:2cd4928e8147 22 _parasite_power = !read_power_supply();
Sissors 5:2cd4928e8147 23 }
pairmand 3:8f2b7f4940b5 24 }
Sissors 5:2cd4928e8147 25
Sissors 5:2cd4928e8147 26 DS1820::~DS1820 (void) {
Sissors 5:2cd4928e8147 27 node *tmp;
Sissors 5:2cd4928e8147 28 for(int i=1; i<=probes.length(); i++)
Sissors 5:2cd4928e8147 29 {
Sissors 5:2cd4928e8147 30 tmp = probes.pop(i);
Sissors 5:2cd4928e8147 31 if (tmp->data == this)
Sissors 5:2cd4928e8147 32 probes.remove(i);
Sissors 5:2cd4928e8147 33 }
pairmand 3:8f2b7f4940b5 34 }
Sissors 5:2cd4928e8147 35
pairmand 3:8f2b7f4940b5 36
Sissors 5:2cd4928e8147 37 bool DS1820::onewire_reset(DigitalInOut *pin) {
pairmand 3:8f2b7f4940b5 38 // This will return false if no devices are present on the data bus
pairmand 3:8f2b7f4940b5 39 bool presence=false;
Sissors 5:2cd4928e8147 40 pin->output();
Sissors 5:2cd4928e8147 41 pin->write(0); // bring low for 500 us
pairmand 3:8f2b7f4940b5 42 wait_us(500);
Sissors 5:2cd4928e8147 43 pin->input(); // let the data line float high
pairmand 3:8f2b7f4940b5 44 wait_us(90); // wait 90us
Sissors 5:2cd4928e8147 45 if (pin->read()==0) // see if any devices are pulling the data line low
pairmand 3:8f2b7f4940b5 46 presence=true;
pairmand 3:8f2b7f4940b5 47 wait_us(410);
pairmand 3:8f2b7f4940b5 48 return presence;
pairmand 3:8f2b7f4940b5 49 }
pairmand 3:8f2b7f4940b5 50
Sissors 5:2cd4928e8147 51 void DS1820::onewire_bit_out (DigitalInOut *pin, bool bit_data) {
Sissors 5:2cd4928e8147 52 pin->output();
Sissors 5:2cd4928e8147 53 pin->write(0);
pairmand 3:8f2b7f4940b5 54 wait_us(3); // DXP modified from 5
pairmand 3:8f2b7f4940b5 55 if (bit_data) {
Sissors 8:d87e11e8d012 56 pin->write(1); // bring data line high
pairmand 3:8f2b7f4940b5 57 wait_us(55);
pairmand 3:8f2b7f4940b5 58 } else {
pairmand 3:8f2b7f4940b5 59 wait_us(55); // keep data line low
Sissors 8:d87e11e8d012 60 pin->write(1);
pairmand 3:8f2b7f4940b5 61 wait_us(10); // DXP added to allow bus to float high before next bit_out
pairmand 3:8f2b7f4940b5 62 }
pairmand 3:8f2b7f4940b5 63 }
pairmand 3:8f2b7f4940b5 64
pairmand 3:8f2b7f4940b5 65 void DS1820::onewire_byte_out(char data) { // output data character (least sig bit first).
pairmand 3:8f2b7f4940b5 66 int n;
pairmand 3:8f2b7f4940b5 67 for (n=0; n<8; n++) {
Sissors 5:2cd4928e8147 68 onewire_bit_out(&this->_datapin, data & 0x01);
pairmand 3:8f2b7f4940b5 69 data = data >> 1; // now the next bit is in the least sig bit position.
pairmand 3:8f2b7f4940b5 70 }
pairmand 3:8f2b7f4940b5 71 }
pairmand 3:8f2b7f4940b5 72
Sissors 5:2cd4928e8147 73 bool DS1820::onewire_bit_in(DigitalInOut *pin) {
pairmand 3:8f2b7f4940b5 74 bool answer;
Sissors 5:2cd4928e8147 75 pin->output();
Sissors 5:2cd4928e8147 76 pin->write(0);
pairmand 3:8f2b7f4940b5 77 wait_us(3); // DXP modofied from 5
Sissors 5:2cd4928e8147 78 pin->input();
pairmand 3:8f2b7f4940b5 79 wait_us(10); // DXP modified from 5
Sissors 5:2cd4928e8147 80 answer = pin->read();
pairmand 3:8f2b7f4940b5 81 wait_us(45); // DXP modified from 50
pairmand 3:8f2b7f4940b5 82 return answer;
pairmand 3:8f2b7f4940b5 83 }
pairmand 3:8f2b7f4940b5 84
pairmand 3:8f2b7f4940b5 85 char DS1820::onewire_byte_in() { // read byte, least sig byte first
pairmand 3:8f2b7f4940b5 86 char answer = 0x00;
pairmand 3:8f2b7f4940b5 87 int i;
pairmand 3:8f2b7f4940b5 88 for (i=0; i<8; i++) {
pairmand 3:8f2b7f4940b5 89 answer = answer >> 1; // shift over to make room for the next bit
Sissors 5:2cd4928e8147 90 if (onewire_bit_in(&this->_datapin))
pairmand 3:8f2b7f4940b5 91 answer = answer | 0x80; // if the data port is high, make this bit a 1
pairmand 3:8f2b7f4940b5 92 }
pairmand 3:8f2b7f4940b5 93 return answer;
pairmand 3:8f2b7f4940b5 94 }
Sissors 5:2cd4928e8147 95
Sissors 5:2cd4928e8147 96 bool DS1820::unassignedProbe(PinName pin) {
Sissors 5:2cd4928e8147 97 DigitalInOut _pin(pin);
Sissors 5:2cd4928e8147 98 char ROM_address[8];
Sissors 5:2cd4928e8147 99 return search_ROM_routine(&_pin, 0xF0, ROM_address);
pairmand 3:8f2b7f4940b5 100 }
pairmand 3:8f2b7f4940b5 101
Sissors 5:2cd4928e8147 102 bool DS1820::unassignedProbe(DigitalInOut *pin, char *ROM_address) {
Sissors 5:2cd4928e8147 103 return search_ROM_routine(pin, 0xF0, ROM_address);
pairmand 3:8f2b7f4940b5 104 }
pairmand 3:8f2b7f4940b5 105
Sissors 5:2cd4928e8147 106 bool DS1820::search_ROM_routine(DigitalInOut *pin, char command, char *ROM_address) {
Sissors 5:2cd4928e8147 107 bool DS1820_done_flag = false;
Sissors 5:2cd4928e8147 108 int DS1820_last_descrepancy = 0;
Sissors 5:2cd4928e8147 109 char DS1820_search_ROM[8] = {0, 0, 0, 0, 0, 0, 0, 0};
Sissors 5:2cd4928e8147 110
pairmand 3:8f2b7f4940b5 111 int descrepancy_marker, ROM_bit_index;
pairmand 3:8f2b7f4940b5 112 bool return_value, Bit_A, Bit_B;
pairmand 3:8f2b7f4940b5 113 char byte_counter, bit_mask;
pairmand 3:8f2b7f4940b5 114
SomeRandomBloke 14:206d620315cf 115 // if( !_hasProbes ) return false;
SomeRandomBloke 14:206d620315cf 116
pairmand 3:8f2b7f4940b5 117 return_value=false;
Sissors 5:2cd4928e8147 118 while (!DS1820_done_flag) {
Sissors 5:2cd4928e8147 119 if (!onewire_reset(pin)) {
Sissors 5:2cd4928e8147 120 return false;
pairmand 3:8f2b7f4940b5 121 } else {
pairmand 3:8f2b7f4940b5 122 ROM_bit_index=1;
pairmand 3:8f2b7f4940b5 123 descrepancy_marker=0;
Sissors 12:196e9e54b033 124 char command_shift = command;
Sissors 5:2cd4928e8147 125 for (int n=0; n<8; n++) { // Search ROM command or Search Alarm command
Sissors 12:196e9e54b033 126 onewire_bit_out(pin, command_shift & 0x01);
Sissors 12:196e9e54b033 127 command_shift = command_shift >> 1; // now the next bit is in the least sig bit position.
Sissors 5:2cd4928e8147 128 }
pairmand 3:8f2b7f4940b5 129 byte_counter = 0;
pairmand 3:8f2b7f4940b5 130 bit_mask = 0x01;
pairmand 3:8f2b7f4940b5 131 while (ROM_bit_index<=64) {
Sissors 5:2cd4928e8147 132 Bit_A = onewire_bit_in(pin);
Sissors 5:2cd4928e8147 133 Bit_B = onewire_bit_in(pin);
pairmand 3:8f2b7f4940b5 134 if (Bit_A & Bit_B) {
pairmand 3:8f2b7f4940b5 135 descrepancy_marker = 0; // data read error, this should never happen
pairmand 3:8f2b7f4940b5 136 ROM_bit_index = 0xFF;
pairmand 3:8f2b7f4940b5 137 } else {
pairmand 3:8f2b7f4940b5 138 if (Bit_A | Bit_B) {
pairmand 3:8f2b7f4940b5 139 // Set ROM bit to Bit_A
pairmand 3:8f2b7f4940b5 140 if (Bit_A) {
pairmand 3:8f2b7f4940b5 141 DS1820_search_ROM[byte_counter] = DS1820_search_ROM[byte_counter] | bit_mask; // Set ROM bit to one
pairmand 3:8f2b7f4940b5 142 } else {
pairmand 3:8f2b7f4940b5 143 DS1820_search_ROM[byte_counter] = DS1820_search_ROM[byte_counter] & ~bit_mask; // Set ROM bit to zero
pairmand 3:8f2b7f4940b5 144 }
pairmand 3:8f2b7f4940b5 145 } else {
pairmand 3:8f2b7f4940b5 146 // both bits A and B are low, so there are two or more devices present
pairmand 3:8f2b7f4940b5 147 if ( ROM_bit_index == DS1820_last_descrepancy ) {
pairmand 3:8f2b7f4940b5 148 DS1820_search_ROM[byte_counter] = DS1820_search_ROM[byte_counter] | bit_mask; // Set ROM bit to one
pairmand 3:8f2b7f4940b5 149 } else {
pairmand 3:8f2b7f4940b5 150 if ( ROM_bit_index > DS1820_last_descrepancy ) {
pairmand 3:8f2b7f4940b5 151 DS1820_search_ROM[byte_counter] = DS1820_search_ROM[byte_counter] & ~bit_mask; // Set ROM bit to zero
pairmand 3:8f2b7f4940b5 152 descrepancy_marker = ROM_bit_index;
pairmand 3:8f2b7f4940b5 153 } else {
pairmand 3:8f2b7f4940b5 154 if (( DS1820_search_ROM[byte_counter] & bit_mask) == 0x00 )
pairmand 3:8f2b7f4940b5 155 descrepancy_marker = ROM_bit_index;
pairmand 3:8f2b7f4940b5 156 }
pairmand 3:8f2b7f4940b5 157 }
pairmand 3:8f2b7f4940b5 158 }
Sissors 5:2cd4928e8147 159 onewire_bit_out (pin, DS1820_search_ROM[byte_counter] & bit_mask);
pairmand 3:8f2b7f4940b5 160 ROM_bit_index++;
pairmand 3:8f2b7f4940b5 161 if (bit_mask & 0x80) {
pairmand 3:8f2b7f4940b5 162 byte_counter++;
pairmand 3:8f2b7f4940b5 163 bit_mask = 0x01;
pairmand 3:8f2b7f4940b5 164 } else {
pairmand 3:8f2b7f4940b5 165 bit_mask = bit_mask << 1;
pairmand 3:8f2b7f4940b5 166 }
pairmand 3:8f2b7f4940b5 167 }
pairmand 3:8f2b7f4940b5 168 }
pairmand 3:8f2b7f4940b5 169 DS1820_last_descrepancy = descrepancy_marker;
pairmand 3:8f2b7f4940b5 170 if (ROM_bit_index != 0xFF) {
Sissors 5:2cd4928e8147 171 int i = 1;
Sissors 5:2cd4928e8147 172 node *list_container;
Sissors 5:2cd4928e8147 173 while(1) {
Sissors 5:2cd4928e8147 174 list_container = probes.pop(i);
Sissors 5:2cd4928e8147 175 if (list_container == NULL) { //End of list, or empty list
Sissors 5:2cd4928e8147 176 if (ROM_checksum_error(DS1820_search_ROM)) { // Check the CRC
Sissors 5:2cd4928e8147 177 return false;
Sissors 5:2cd4928e8147 178 }
Sissors 5:2cd4928e8147 179 for(byte_counter=0;byte_counter<8;byte_counter++)
Sissors 5:2cd4928e8147 180 ROM_address[byte_counter] = DS1820_search_ROM[byte_counter];
Sissors 5:2cd4928e8147 181 return true;
Sissors 5:2cd4928e8147 182 } else { //Otherwise, check if ROM is already known
Sissors 5:2cd4928e8147 183 bool equal = true;
Sissors 5:2cd4928e8147 184 DS1820 *pointer = (DS1820*) list_container->data;
Sissors 5:2cd4928e8147 185 char *ROM_compare = pointer->_ROM;
Sissors 5:2cd4928e8147 186
Sissors 5:2cd4928e8147 187 for(byte_counter=0;byte_counter<8;byte_counter++) {
Sissors 5:2cd4928e8147 188 if ( ROM_compare[byte_counter] != DS1820_search_ROM[byte_counter])
Sissors 5:2cd4928e8147 189 equal = false;
Sissors 5:2cd4928e8147 190 }
Sissors 5:2cd4928e8147 191 if (equal)
Sissors 5:2cd4928e8147 192 break;
Sissors 5:2cd4928e8147 193 else
Sissors 5:2cd4928e8147 194 i++;
Sissors 5:2cd4928e8147 195 }
Sissors 5:2cd4928e8147 196 }
pairmand 3:8f2b7f4940b5 197 }
pairmand 3:8f2b7f4940b5 198 }
pairmand 3:8f2b7f4940b5 199 if (DS1820_last_descrepancy == 0)
pairmand 3:8f2b7f4940b5 200 DS1820_done_flag = true;
pairmand 3:8f2b7f4940b5 201 }
pairmand 3:8f2b7f4940b5 202 return return_value;
pairmand 3:8f2b7f4940b5 203 }
pairmand 3:8f2b7f4940b5 204
pairmand 3:8f2b7f4940b5 205 void DS1820::match_ROM() {
pairmand 3:8f2b7f4940b5 206 // Used to select a specific device
pairmand 3:8f2b7f4940b5 207 int i;
SomeRandomBloke 14:206d620315cf 208 if( !_hasProbes ) return;
SomeRandomBloke 14:206d620315cf 209
Sissors 5:2cd4928e8147 210 onewire_reset(&this->_datapin);
pairmand 3:8f2b7f4940b5 211 onewire_byte_out( 0x55); //Match ROM command
Sissors 5:2cd4928e8147 212 for (i=0;i<8;i++) {
Sissors 5:2cd4928e8147 213 onewire_byte_out(_ROM[i]);
Sissors 5:2cd4928e8147 214 }
pairmand 3:8f2b7f4940b5 215 }
pairmand 3:8f2b7f4940b5 216
pairmand 3:8f2b7f4940b5 217 void DS1820::skip_ROM() {
Sissors 5:2cd4928e8147 218 onewire_reset(&this->_datapin);
pairmand 3:8f2b7f4940b5 219 onewire_byte_out(0xCC); // Skip ROM command
pairmand 3:8f2b7f4940b5 220 }
pairmand 3:8f2b7f4940b5 221
Sissors 5:2cd4928e8147 222 bool DS1820::ROM_checksum_error(char *_ROM_address) {
Sissors 11:1a3c3002b50c 223 char _CRC=0x00;
pairmand 3:8f2b7f4940b5 224 int i;
pairmand 3:8f2b7f4940b5 225 for(i=0;i<7;i++) // Only going to shift the lower 7 bytes
Sissors 11:1a3c3002b50c 226 _CRC = CRC_byte(_CRC, _ROM_address[i]);
pairmand 3:8f2b7f4940b5 227 // After 7 bytes CRC should equal the 8th byte (ROM CRC)
Sissors 11:1a3c3002b50c 228 return (_CRC!=_ROM_address[7]); // will return true if there is a CRC checksum mis-match
pairmand 3:8f2b7f4940b5 229 }
pairmand 3:8f2b7f4940b5 230
pairmand 3:8f2b7f4940b5 231 bool DS1820::RAM_checksum_error() {
Sissors 11:1a3c3002b50c 232 char _CRC=0x00;
pairmand 3:8f2b7f4940b5 233 int i;
pairmand 3:8f2b7f4940b5 234 for(i=0;i<8;i++) // Only going to shift the lower 8 bytes
Sissors 11:1a3c3002b50c 235 _CRC = CRC_byte(_CRC, RAM[i]);
pairmand 3:8f2b7f4940b5 236 // After 8 bytes CRC should equal the 9th byte (RAM CRC)
Sissors 11:1a3c3002b50c 237 return (_CRC!=RAM[8]); // will return true if there is a CRC checksum mis-match
pairmand 3:8f2b7f4940b5 238 }
pairmand 3:8f2b7f4940b5 239
Sissors 11:1a3c3002b50c 240 char DS1820::CRC_byte (char _CRC, char byte ) {
pairmand 3:8f2b7f4940b5 241 int j;
pairmand 3:8f2b7f4940b5 242 for(j=0;j<8;j++) {
Sissors 11:1a3c3002b50c 243 if ((byte & 0x01 ) ^ (_CRC & 0x01)) {
pairmand 3:8f2b7f4940b5 244 // DATA ^ LSB CRC = 1
Sissors 11:1a3c3002b50c 245 _CRC = _CRC>>1;
pairmand 3:8f2b7f4940b5 246 // Set the MSB to 1
Sissors 11:1a3c3002b50c 247 _CRC = _CRC | 0x80;
pairmand 3:8f2b7f4940b5 248 // Check bit 3
Sissors 11:1a3c3002b50c 249 if (_CRC & 0x04) {
Sissors 11:1a3c3002b50c 250 _CRC = _CRC & 0xFB; // Bit 3 is set, so clear it
pairmand 3:8f2b7f4940b5 251 } else {
Sissors 11:1a3c3002b50c 252 _CRC = _CRC | 0x04; // Bit 3 is clear, so set it
pairmand 3:8f2b7f4940b5 253 }
pairmand 3:8f2b7f4940b5 254 // Check bit 4
Sissors 11:1a3c3002b50c 255 if (_CRC & 0x08) {
Sissors 11:1a3c3002b50c 256 _CRC = _CRC & 0xF7; // Bit 4 is set, so clear it
pairmand 3:8f2b7f4940b5 257 } else {
Sissors 11:1a3c3002b50c 258 _CRC = _CRC | 0x08; // Bit 4 is clear, so set it
pairmand 3:8f2b7f4940b5 259 }
pairmand 3:8f2b7f4940b5 260 } else {
pairmand 3:8f2b7f4940b5 261 // DATA ^ LSB CRC = 0
Sissors 11:1a3c3002b50c 262 _CRC = _CRC>>1;
pairmand 3:8f2b7f4940b5 263 // clear MSB
Sissors 11:1a3c3002b50c 264 _CRC = _CRC & 0x7F;
pairmand 3:8f2b7f4940b5 265 // No need to check bits, with DATA ^ LSB CRC = 0, they will remain unchanged
pairmand 3:8f2b7f4940b5 266 }
pairmand 3:8f2b7f4940b5 267 byte = byte>>1;
pairmand 3:8f2b7f4940b5 268 }
Sissors 11:1a3c3002b50c 269 return _CRC;
pairmand 3:8f2b7f4940b5 270 }
pairmand 3:8f2b7f4940b5 271
Sissors 5:2cd4928e8147 272 int DS1820::convertTemperature(bool wait, devices device) {
pairmand 3:8f2b7f4940b5 273 // Convert temperature into scratchpad RAM for all devices at once
pairmand 3:8f2b7f4940b5 274 int delay_time = 750; // Default delay time
SomeRandomBloke 14:206d620315cf 275 if( !_hasProbes ) return delay_time;
SomeRandomBloke 14:206d620315cf 276
pairmand 3:8f2b7f4940b5 277 char resolution;
pairmand 3:8f2b7f4940b5 278 if (device==all_devices)
pairmand 3:8f2b7f4940b5 279 skip_ROM(); // Skip ROM command, will convert for ALL devices
pairmand 3:8f2b7f4940b5 280 else {
pairmand 3:8f2b7f4940b5 281 match_ROM();
Sissors 5:2cd4928e8147 282 if ((FAMILY_CODE == FAMILY_CODE_DS18B20 ) || (FAMILY_CODE == FAMILY_CODE_DS1822 )) {
pairmand 3:8f2b7f4940b5 283 resolution = RAM[4] & 0x60;
pairmand 3:8f2b7f4940b5 284 if (resolution == 0x00) // 9 bits
pairmand 3:8f2b7f4940b5 285 delay_time = 94;
pairmand 3:8f2b7f4940b5 286 if (resolution == 0x20) // 10 bits
pairmand 3:8f2b7f4940b5 287 delay_time = 188;
pairmand 3:8f2b7f4940b5 288 if (resolution == 0x40) // 11 bits. Note 12bits uses the 750ms default
pairmand 3:8f2b7f4940b5 289 delay_time = 375;
pairmand 3:8f2b7f4940b5 290 }
pairmand 3:8f2b7f4940b5 291 }
Sissors 5:2cd4928e8147 292
pairmand 3:8f2b7f4940b5 293 onewire_byte_out( 0x44); // perform temperature conversion
pairmand 3:8f2b7f4940b5 294 if (_parasite_power) {
Sissors 5:2cd4928e8147 295 if (_power_mosfet) {
Sissors 5:2cd4928e8147 296 _parasitepin = _power_polarity; // Parasite power strong pullup
Sissors 5:2cd4928e8147 297 wait_ms(delay_time);
Sissors 5:2cd4928e8147 298 _parasitepin = !_power_polarity;
Sissors 5:2cd4928e8147 299 delay_time = 0;
Sissors 5:2cd4928e8147 300 } else {
Sissors 5:2cd4928e8147 301 _datapin.output();
Sissors 5:2cd4928e8147 302 _datapin.write(1);
Sissors 5:2cd4928e8147 303 wait_ms(delay_time);
Sissors 5:2cd4928e8147 304 _datapin.input();
Sissors 5:2cd4928e8147 305 }
pairmand 3:8f2b7f4940b5 306 } else {
pairmand 3:8f2b7f4940b5 307 if (wait) {
pairmand 3:8f2b7f4940b5 308 wait_ms(delay_time);
pairmand 3:8f2b7f4940b5 309 delay_time = 0;
pairmand 3:8f2b7f4940b5 310 }
pairmand 3:8f2b7f4940b5 311 }
pairmand 3:8f2b7f4940b5 312 return delay_time;
pairmand 3:8f2b7f4940b5 313 }
pairmand 3:8f2b7f4940b5 314
pairmand 3:8f2b7f4940b5 315 void DS1820::read_RAM() {
pairmand 3:8f2b7f4940b5 316 // This will copy the DS1820's 9 bytes of RAM data
pairmand 3:8f2b7f4940b5 317 // into the objects RAM array. Functions that use
pairmand 3:8f2b7f4940b5 318 // RAM values will automaticly call this procedure.
pairmand 3:8f2b7f4940b5 319 int i;
pairmand 3:8f2b7f4940b5 320 match_ROM(); // Select this device
pairmand 3:8f2b7f4940b5 321 onewire_byte_out( 0xBE); //Read Scratchpad command
pairmand 3:8f2b7f4940b5 322 for(i=0;i<9;i++) {
pairmand 3:8f2b7f4940b5 323 RAM[i] = onewire_byte_in();
pairmand 3:8f2b7f4940b5 324 }
pairmand 3:8f2b7f4940b5 325 // if (!RAM_checksum_error())
pairmand 3:8f2b7f4940b5 326 // crcerr = 1;
pairmand 3:8f2b7f4940b5 327 }
pairmand 3:8f2b7f4940b5 328
Sissors 5:2cd4928e8147 329 bool DS1820::setResolution(unsigned int resolution) {
pairmand 3:8f2b7f4940b5 330 bool answer = false;
SomeRandomBloke 14:206d620315cf 331 if( !_hasProbes ) return false;
SomeRandomBloke 14:206d620315cf 332
pairmand 3:8f2b7f4940b5 333 resolution = resolution - 9;
pairmand 3:8f2b7f4940b5 334 if (resolution < 4) {
pairmand 3:8f2b7f4940b5 335 resolution = resolution<<5; // align the bits
pairmand 3:8f2b7f4940b5 336 RAM[4] = (RAM[4] & 0x60) | resolution; // mask out old data, insert new
pairmand 3:8f2b7f4940b5 337 write_scratchpad ((RAM[2]<<8) + RAM[3]);
pairmand 3:8f2b7f4940b5 338 // store_scratchpad (DS1820::this_device); // Need to test if this is required
pairmand 3:8f2b7f4940b5 339 answer = true;
pairmand 3:8f2b7f4940b5 340 }
pairmand 3:8f2b7f4940b5 341 return answer;
pairmand 3:8f2b7f4940b5 342 }
pairmand 3:8f2b7f4940b5 343
pairmand 3:8f2b7f4940b5 344 void DS1820::write_scratchpad(int data) {
pairmand 3:8f2b7f4940b5 345 RAM[3] = data;
pairmand 3:8f2b7f4940b5 346 RAM[2] = data>>8;
pairmand 3:8f2b7f4940b5 347 match_ROM();
pairmand 3:8f2b7f4940b5 348 onewire_byte_out(0x4E); // Copy scratchpad into DS1820 ram memory
pairmand 3:8f2b7f4940b5 349 onewire_byte_out(RAM[2]); // T(H)
pairmand 3:8f2b7f4940b5 350 onewire_byte_out(RAM[3]); // T(L)
Sissors 5:2cd4928e8147 351 if ((FAMILY_CODE == FAMILY_CODE_DS18B20 ) || (FAMILY_CODE == FAMILY_CODE_DS1822 )) {
pairmand 3:8f2b7f4940b5 352 onewire_byte_out(RAM[4]); // Configuration register
pairmand 3:8f2b7f4940b5 353 }
pairmand 3:8f2b7f4940b5 354 }
pairmand 3:8f2b7f4940b5 355
pairmand 3:8f2b7f4940b5 356 float DS1820::temperature(char scale) {
pairmand 3:8f2b7f4940b5 357 // The data specs state that count_per_degree should be 0x10 (16), I found my devices
pairmand 3:8f2b7f4940b5 358 // to have a count_per_degree of 0x4B (75). With the standard resolution of 1/2 deg C
pairmand 3:8f2b7f4940b5 359 // this allowed an expanded resolution of 1/150th of a deg C. I wouldn't rely on this
pairmand 3:8f2b7f4940b5 360 // being super acurate, but it does allow for a smooth display in the 1/10ths of a
pairmand 3:8f2b7f4940b5 361 // deg C or F scales.
pairmand 3:8f2b7f4940b5 362 float answer, remaining_count, count_per_degree;
pairmand 3:8f2b7f4940b5 363 int reading;
pairmand 3:8f2b7f4940b5 364 read_RAM();
pairmand 3:8f2b7f4940b5 365 if (RAM_checksum_error())
pairmand 3:8f2b7f4940b5 366 // Indicate we got a CRC error
Sissors 7:58b61681818f 367 answer = invalid_conversion;
pairmand 3:8f2b7f4940b5 368 else {
pairmand 3:8f2b7f4940b5 369 reading = (RAM[1] << 8) + RAM[0];
pairmand 3:8f2b7f4940b5 370 if (reading & 0x8000) { // negative degrees C
pairmand 3:8f2b7f4940b5 371 reading = 0-((reading ^ 0xffff) + 1); // 2's comp then convert to signed int
pairmand 3:8f2b7f4940b5 372 }
pairmand 3:8f2b7f4940b5 373 answer = reading +0.0; // convert to floating point
Sissors 5:2cd4928e8147 374 if ((FAMILY_CODE == FAMILY_CODE_DS18B20 ) || (FAMILY_CODE == FAMILY_CODE_DS1822 )) {
Sissors 11:1a3c3002b50c 375 answer = answer / 16.0f;
pairmand 3:8f2b7f4940b5 376 }
pairmand 3:8f2b7f4940b5 377 else {
pairmand 3:8f2b7f4940b5 378 remaining_count = RAM[6];
pairmand 3:8f2b7f4940b5 379 count_per_degree = RAM[7];
Sissors 11:1a3c3002b50c 380 answer = floor(answer/2.0f) - 0.25f + (count_per_degree - remaining_count) / count_per_degree;
pairmand 3:8f2b7f4940b5 381 }
florian 10:d297ce9ce422 382 if (scale=='F' or scale=='f')
pairmand 3:8f2b7f4940b5 383 // Convert to deg F
Sissors 11:1a3c3002b50c 384 answer = answer * 9.0f / 5.0f + 32.0f;
pairmand 3:8f2b7f4940b5 385 }
pairmand 3:8f2b7f4940b5 386 return answer;
pairmand 3:8f2b7f4940b5 387 }
pairmand 3:8f2b7f4940b5 388
pairmand 3:8f2b7f4940b5 389 bool DS1820::read_power_supply(devices device) {
pairmand 3:8f2b7f4940b5 390 // This will return true if the device (or all devices) are Vcc powered
pairmand 3:8f2b7f4940b5 391 // This will return false if the device (or ANY device) is parasite powered
pairmand 3:8f2b7f4940b5 392 if (device==all_devices)
pairmand 3:8f2b7f4940b5 393 skip_ROM(); // Skip ROM command, will poll for any device using parasite power
pairmand 3:8f2b7f4940b5 394 else
pairmand 3:8f2b7f4940b5 395 match_ROM();
pairmand 3:8f2b7f4940b5 396 onewire_byte_out(0xB4); // Read power supply command
Sissors 5:2cd4928e8147 397 return onewire_bit_in(&this->_datapin);
pairmand 3:8f2b7f4940b5 398 }
pairmand 3:8f2b7f4940b5 399
pairmand 3:8f2b7f4940b5 400