Fork to fix compiler warnings.
Fork of DS1820 by
Embed:
(wiki syntax)
Show/hide line numbers
DS1820.cpp
00001 #include "DS1820.h" 00002 00003 #ifdef TARGET_STM 00004 //STM targets use opendrain mode since their switching between input and output is slow 00005 #define ONEWIRE_INPUT(pin) pin->write(1) 00006 #define ONEWIRE_OUTPUT(pin) 00007 #define ONEWIRE_INIT(pin) pin->output(); pin->mode(OpenDrain) 00008 #else 00009 #define ONEWIRE_INPUT(pin) pin->input() 00010 #define ONEWIRE_OUTPUT(pin) pin->output() 00011 #define ONEWIRE_INIT(pin) 00012 #endif 00013 00014 #ifdef TARGET_NORDIC 00015 //NORDIC targets (NRF) use software delays since their ticker uses a 32kHz clock 00016 static uint32_t loops_per_us = 0; 00017 00018 #define INIT_DELAY init_soft_delay() 00019 #define ONEWIRE_DELAY_US(value) for(int cnt = 0; cnt < (value * loops_per_us) >> 5; cnt++) {__NOP(); __NOP(); __NOP();} 00020 00021 void init_soft_delay( void ) { 00022 if (loops_per_us == 0) { 00023 loops_per_us = 1; 00024 Timer timey; 00025 timey.start(); 00026 ONEWIRE_DELAY_US(320000); 00027 timey.stop(); 00028 loops_per_us = (320000 + timey.read_us() / 2) / timey.read_us(); 00029 } 00030 } 00031 #else 00032 #define INIT_DELAY 00033 #define ONEWIRE_DELAY_US(value) wait_us(value) 00034 #endif 00035 00036 LinkedList<node> DS1820::probes; 00037 00038 00039 DS1820::DS1820 (PinName data_pin, PinName power_pin, bool power_polarity) : _datapin(data_pin), _parasitepin(power_pin) { 00040 int byte_counter; 00041 _power_polarity = power_polarity; 00042 00043 _power_mosfet = power_pin != NC; 00044 00045 for(byte_counter=0;byte_counter<9;byte_counter++) 00046 RAM[byte_counter] = 0x00; 00047 00048 ONEWIRE_INIT((&_datapin)); 00049 INIT_DELAY; 00050 00051 if (!unassignedProbe(&_datapin, _ROM)) { 00052 printf("DS18B20:No unassigned DS1820 found!\r\n"); 00053 valid = false; 00054 } 00055 else { 00056 _datapin.input(); 00057 probes.append(this); 00058 _parasite_power = !read_power_supply(); 00059 valid = true; 00060 } 00061 } 00062 00063 DS1820::~DS1820 (void) { 00064 node *tmp; 00065 for(uint32_t i=1; i<=probes.length(); i++) 00066 { 00067 tmp = probes.pop(i); 00068 if (tmp->data == this) 00069 probes.remove(i); 00070 } 00071 } 00072 00073 00074 bool DS1820::onewire_reset(DigitalInOut *pin) { 00075 // This will return false if no devices are present on the data bus 00076 bool presence=false; 00077 ONEWIRE_OUTPUT(pin); 00078 pin->write(0); // bring low for 500 us 00079 ONEWIRE_DELAY_US(500); 00080 ONEWIRE_INPUT(pin); // let the data line float high 00081 ONEWIRE_DELAY_US(90); // wait 90us 00082 if (pin->read()==0) // see if any devices are pulling the data line low 00083 presence=true; 00084 ONEWIRE_DELAY_US(410); 00085 return presence; 00086 } 00087 00088 void DS1820::onewire_bit_out (DigitalInOut *pin, bool bit_data) { 00089 ONEWIRE_OUTPUT(pin); 00090 pin->write(0); 00091 ONEWIRE_DELAY_US(3); // DXP modified from 5 00092 if (bit_data) { 00093 pin->write(1); // bring data line high 00094 ONEWIRE_DELAY_US(55); 00095 } else { 00096 ONEWIRE_DELAY_US(55); // keep data line low 00097 pin->write(1); 00098 ONEWIRE_DELAY_US(10); // DXP added to allow bus to float high before next bit_out 00099 } 00100 } 00101 00102 void DS1820::onewire_byte_out(char data) { // output data character (least sig bit first). 00103 int n; 00104 for (n=0; n<8; n++) { 00105 onewire_bit_out(&this->_datapin, data & 0x01); 00106 data = data >> 1; // now the next bit is in the least sig bit position. 00107 } 00108 } 00109 00110 bool DS1820::onewire_bit_in(DigitalInOut *pin) { 00111 bool answer; 00112 ONEWIRE_OUTPUT(pin); 00113 pin->write(0); 00114 ONEWIRE_DELAY_US(3); // DXP modofied from 5 00115 ONEWIRE_INPUT(pin); 00116 ONEWIRE_DELAY_US(10); // DXP modified from 5 00117 answer = pin->read(); 00118 ONEWIRE_DELAY_US(45); // DXP modified from 50 00119 return answer; 00120 } 00121 00122 char DS1820::onewire_byte_in() { // read byte, least sig byte first 00123 char answer = 0x00; 00124 int i; 00125 for (i=0; i<8; i++) { 00126 answer = answer >> 1; // shift over to make room for the next bit 00127 if (onewire_bit_in(&this->_datapin)) 00128 answer = answer | 0x80; // if the data port is high, make this bit a 1 00129 } 00130 return answer; 00131 } 00132 00133 bool DS1820::unassignedProbe(PinName pin) { 00134 DigitalInOut _pin(pin); 00135 ONEWIRE_INIT((&_pin)); 00136 INIT_DELAY; 00137 char ROM_address[8]; 00138 return search_ROM_routine(&_pin, 0xF0, ROM_address); 00139 } 00140 00141 bool DS1820::unassignedProbe(DigitalInOut *pin, char *ROM_address) { 00142 return search_ROM_routine(pin, 0xF0, ROM_address); 00143 } 00144 00145 bool DS1820::search_ROM_routine(DigitalInOut *pin, char command, char *ROM_address) { 00146 bool DS1820_done_flag = false; 00147 int DS1820_last_descrepancy = 0; 00148 char DS1820_search_ROM[8] = {0, 0, 0, 0, 0, 0, 0, 0}; 00149 00150 int descrepancy_marker, ROM_bit_index; 00151 bool return_value, Bit_A, Bit_B; 00152 char bit_mask; 00153 int byte_counter; 00154 00155 return_value=false; 00156 while (!DS1820_done_flag) { 00157 if (!onewire_reset(pin)) { 00158 return false; 00159 } else { 00160 ROM_bit_index=1; 00161 descrepancy_marker=0; 00162 char command_shift = command; 00163 for (int n=0; n<8; n++) { // Search ROM command or Search Alarm command 00164 onewire_bit_out(pin, command_shift & 0x01); 00165 command_shift = command_shift >> 1; // now the next bit is in the least sig bit position. 00166 } 00167 byte_counter = 0; 00168 bit_mask = 0x01; 00169 while (ROM_bit_index<=64) { 00170 Bit_A = onewire_bit_in(pin); 00171 Bit_B = onewire_bit_in(pin); 00172 if (Bit_A & Bit_B) { 00173 descrepancy_marker = 0; // data read error, this should never happen 00174 ROM_bit_index = 0xFF; 00175 } else { 00176 if (Bit_A | Bit_B) { 00177 // Set ROM bit to Bit_A 00178 if (Bit_A) { 00179 DS1820_search_ROM[byte_counter] = DS1820_search_ROM[byte_counter] | bit_mask; // Set ROM bit to one 00180 } else { 00181 DS1820_search_ROM[byte_counter] = DS1820_search_ROM[byte_counter] & ~bit_mask; // Set ROM bit to zero 00182 } 00183 } else { 00184 // both bits A and B are low, so there are two or more devices present 00185 if ( ROM_bit_index == DS1820_last_descrepancy ) { 00186 DS1820_search_ROM[byte_counter] = DS1820_search_ROM[byte_counter] | bit_mask; // Set ROM bit to one 00187 } else { 00188 if ( ROM_bit_index > DS1820_last_descrepancy ) { 00189 DS1820_search_ROM[byte_counter] = DS1820_search_ROM[byte_counter] & ~bit_mask; // Set ROM bit to zero 00190 descrepancy_marker = ROM_bit_index; 00191 } else { 00192 if (( DS1820_search_ROM[byte_counter] & bit_mask) == 0x00 ) 00193 descrepancy_marker = ROM_bit_index; 00194 } 00195 } 00196 } 00197 onewire_bit_out (pin, DS1820_search_ROM[byte_counter] & bit_mask); 00198 ROM_bit_index++; 00199 if (bit_mask & 0x80) { 00200 byte_counter++; 00201 bit_mask = 0x01; 00202 } else { 00203 bit_mask = bit_mask << 1; 00204 } 00205 } 00206 } 00207 DS1820_last_descrepancy = descrepancy_marker; 00208 if (ROM_bit_index != 0xFF) { 00209 int i = 1; 00210 node *list_container; 00211 while(1) { 00212 list_container = probes.pop(i); 00213 if (list_container == NULL) { //End of list, or empty list 00214 if (ROM_checksum_error(DS1820_search_ROM)) { // Check the CRC 00215 return false; 00216 } 00217 for(byte_counter=0;byte_counter<8;byte_counter++) 00218 ROM_address[byte_counter] = DS1820_search_ROM[byte_counter]; 00219 return true; 00220 } else { //Otherwise, check if ROM is already known 00221 bool equal = true; 00222 DS1820 *pointer = (DS1820*) list_container->data; 00223 char *ROM_compare = pointer->_ROM; 00224 00225 for(byte_counter=0;byte_counter<8;byte_counter++) { 00226 if ( ROM_compare[byte_counter] != DS1820_search_ROM[byte_counter]) 00227 equal = false; 00228 } 00229 if (equal) 00230 break; 00231 else 00232 i++; 00233 } 00234 } 00235 } 00236 } 00237 if (DS1820_last_descrepancy == 0) 00238 DS1820_done_flag = true; 00239 } 00240 return return_value; 00241 } 00242 00243 void DS1820::match_ROM() { 00244 // Used to select a specific device 00245 int i; 00246 onewire_reset(&this->_datapin); 00247 onewire_byte_out( 0x55); //Match ROM command 00248 for (i=0;i<8;i++) { 00249 onewire_byte_out(_ROM[i]); 00250 } 00251 } 00252 00253 void DS1820::skip_ROM() { 00254 onewire_reset(&this->_datapin); 00255 onewire_byte_out(0xCC); // Skip ROM command 00256 } 00257 00258 bool DS1820::ROM_checksum_error(char *_ROM_address) { 00259 char _CRC=0x00; 00260 int i; 00261 for(i=0;i<7;i++) // Only going to shift the lower 7 bytes 00262 _CRC = CRC_byte(_CRC, _ROM_address[i]); 00263 // After 7 bytes CRC should equal the 8th byte (ROM CRC) 00264 return (_CRC!=_ROM_address[7]); // will return true if there is a CRC checksum mis-match 00265 } 00266 00267 bool DS1820::RAM_checksum_error() { 00268 char _CRC=0x00; 00269 int i; 00270 for(i=0;i<8;i++) // Only going to shift the lower 8 bytes 00271 _CRC = CRC_byte(_CRC, RAM[i]); 00272 // After 8 bytes CRC should equal the 9th byte (RAM CRC) 00273 return (_CRC!=RAM[8]); // will return true if there is a CRC checksum mis-match 00274 } 00275 00276 char DS1820::CRC_byte (char _CRC, char byte ) { 00277 int j; 00278 for(j=0;j<8;j++) { 00279 if ((byte & 0x01 ) ^ (_CRC & 0x01)) { 00280 // DATA ^ LSB CRC = 1 00281 _CRC = _CRC>>1; 00282 // Set the MSB to 1 00283 _CRC = _CRC | 0x80; 00284 // Check bit 3 00285 if (_CRC & 0x04) { 00286 _CRC = _CRC & 0xFB; // Bit 3 is set, so clear it 00287 } else { 00288 _CRC = _CRC | 0x04; // Bit 3 is clear, so set it 00289 } 00290 // Check bit 4 00291 if (_CRC & 0x08) { 00292 _CRC = _CRC & 0xF7; // Bit 4 is set, so clear it 00293 } else { 00294 _CRC = _CRC | 0x08; // Bit 4 is clear, so set it 00295 } 00296 } else { 00297 // DATA ^ LSB CRC = 0 00298 _CRC = _CRC>>1; 00299 // clear MSB 00300 _CRC = _CRC & 0x7F; 00301 // No need to check bits, with DATA ^ LSB CRC = 0, they will remain unchanged 00302 } 00303 byte = byte>>1; 00304 } 00305 return _CRC; 00306 } 00307 00308 int DS1820::convertTemperature(bool wait, devices device) { 00309 // Convert temperature into scratchpad RAM for all devices at once 00310 int delay_time = 750; // Default delay time 00311 char resolution; 00312 if (device==all_devices) 00313 skip_ROM(); // Skip ROM command, will convert for ALL devices 00314 else { 00315 match_ROM(); 00316 if ((FAMILY_CODE == FAMILY_CODE_DS18B20 ) || (FAMILY_CODE == FAMILY_CODE_DS1822 )) { 00317 resolution = RAM[4] & 0x60; 00318 if (resolution == 0x00) // 9 bits 00319 delay_time = 94; 00320 if (resolution == 0x20) // 10 bits 00321 delay_time = 188; 00322 if (resolution == 0x40) // 11 bits. Note 12bits uses the 750ms default 00323 delay_time = 375; 00324 } 00325 } 00326 00327 onewire_byte_out( 0x44); // perform temperature conversion 00328 if (_parasite_power) { 00329 if (_power_mosfet) { 00330 _parasitepin = _power_polarity; // Parasite power strong pullup 00331 wait_ms(delay_time); 00332 _parasitepin = !_power_polarity; 00333 delay_time = 0; 00334 } else { 00335 _datapin.output(); 00336 _datapin.write(1); 00337 wait_ms(delay_time); 00338 _datapin.input(); 00339 } 00340 } else { 00341 if (wait) { 00342 wait_ms(delay_time); 00343 delay_time = 0; 00344 } 00345 } 00346 return delay_time; 00347 } 00348 00349 void DS1820::read_RAM() { 00350 // This will copy the DS1820's 9 bytes of RAM data 00351 // into the objects RAM array. Functions that use 00352 // RAM values will automaticly call this procedure. 00353 int i; 00354 match_ROM(); // Select this device 00355 onewire_byte_out( 0xBE); //Read Scratchpad command 00356 for(i=0;i<9;i++) { 00357 RAM[i] = onewire_byte_in(); 00358 } 00359 // if (!RAM_checksum_error()) 00360 // crcerr = 1; 00361 } 00362 00363 bool DS1820::setResolution(unsigned int resolution) { 00364 bool answer = false; 00365 resolution = resolution - 9; 00366 if (resolution < 4) { 00367 resolution = resolution<<5; // align the bits 00368 RAM[4] = (RAM[4] & 0x60) | resolution; // mask out old data, insert new 00369 write_scratchpad ((RAM[2]<<8) + RAM[3]); 00370 // store_scratchpad (DS1820::this_device); // Need to test if this is required 00371 answer = true; 00372 } 00373 return answer; 00374 } 00375 00376 void DS1820::write_scratchpad(int data) { 00377 RAM[3] = data; 00378 RAM[2] = data>>8; 00379 match_ROM(); 00380 onewire_byte_out(0x4E); // Copy scratchpad into DS1820 ram memory 00381 onewire_byte_out(RAM[2]); // T(H) 00382 onewire_byte_out(RAM[3]); // T(L) 00383 if ((FAMILY_CODE == FAMILY_CODE_DS18B20 ) || (FAMILY_CODE == FAMILY_CODE_DS1822 )) { 00384 onewire_byte_out(RAM[4]); // Configuration register 00385 } 00386 } 00387 00388 float DS1820::temperature(char scale) { 00389 // The data specs state that count_per_degree should be 0x10 (16), I found my devices 00390 // to have a count_per_degree of 0x4B (75). With the standard resolution of 1/2 deg C 00391 // this allowed an expanded resolution of 1/150th of a deg C. I wouldn't rely on this 00392 // being super acurate, but it does allow for a smooth display in the 1/10ths of a 00393 // deg C or F scales. 00394 float answer, remaining_count, count_per_degree; 00395 int reading; 00396 read_RAM(); 00397 if (RAM_checksum_error()) 00398 // Indicate we got a CRC error 00399 answer = invalid_conversion; 00400 else { 00401 reading = (RAM[1] << 8) + RAM[0]; 00402 if (reading & 0x8000) { // negative degrees C 00403 reading = 0-((reading ^ 0xffff) + 1); // 2's comp then convert to signed int 00404 } 00405 answer = reading +0.0; // convert to floating point 00406 if ((FAMILY_CODE == FAMILY_CODE_DS18B20 ) || (FAMILY_CODE == FAMILY_CODE_DS1822 )) { 00407 answer = answer / 16.0f; 00408 } 00409 else { 00410 remaining_count = RAM[6]; 00411 count_per_degree = RAM[7]; 00412 answer = floor(answer/2.0f) - 0.25f + (count_per_degree - remaining_count) / count_per_degree; 00413 } 00414 if (scale=='F' or scale=='f') 00415 // Convert to deg F 00416 answer = answer * 9.0f / 5.0f + 32.0f; 00417 } 00418 return answer; 00419 } 00420 00421 bool DS1820::read_power_supply(devices device) { 00422 // This will return true if the device (or all devices) are Vcc powered 00423 // This will return false if the device (or ANY device) is parasite powered 00424 if (device==all_devices) 00425 skip_ROM(); // Skip ROM command, will poll for any device using parasite power 00426 else 00427 match_ROM(); 00428 onewire_byte_out(0xB4); // Read power supply command 00429 return onewire_bit_in(&this->_datapin); 00430 } 00431 00432 bool DS1820::isValid() { 00433 return valid; 00434 } 00435
Generated on Sun Jul 17 2022 02:58:51 by 1.7.2