DS1820

Dependencies:   LinkedList

Fork of DS1820 by Erik -

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers DS1820.cpp Source File

DS1820.cpp

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