no chance

Dependencies:   LinkedList

Dependents:   10_ETH_SD_JPG

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     if (power_pin != NC)
00010         _power_mosfet = true;
00011     
00012     for(byte_counter=0;byte_counter<9;byte_counter++)
00013         RAM[byte_counter] = 0x00;
00014     
00015     if (!unassignedProbe(&_datapin, _ROM))
00016         error("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->input(); // bring data line high
00055         wait_us(55);
00056     } else {
00057         wait_us(55);            // keep data line low
00058         pin->input();
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             for (int n=0; n<8; n++) {           // Search ROM command or Search Alarm command
00121                 onewire_bit_out(pin, command & 0x01);
00122                 command = command >> 1; // now the next bit is in the least sig bit position.
00123             } 
00124             byte_counter = 0;
00125             bit_mask = 0x01;
00126             while (ROM_bit_index<=64) {
00127                 Bit_A = onewire_bit_in(pin);
00128                 Bit_B = onewire_bit_in(pin);
00129                 if (Bit_A & Bit_B) {
00130                     descrepancy_marker = 0; // data read error, this should never happen
00131                     ROM_bit_index = 0xFF;
00132                 } else {
00133                     if (Bit_A | Bit_B) {
00134                         // Set ROM bit to Bit_A
00135                         if (Bit_A) {
00136                             DS1820_search_ROM[byte_counter] = DS1820_search_ROM[byte_counter] | bit_mask; // Set ROM bit to one
00137                         } else {
00138                             DS1820_search_ROM[byte_counter] = DS1820_search_ROM[byte_counter] & ~bit_mask; // Set ROM bit to zero
00139                         }
00140                     } else {
00141                         // both bits A and B are low, so there are two or more devices present
00142                         if ( ROM_bit_index == DS1820_last_descrepancy ) {
00143                             DS1820_search_ROM[byte_counter] = DS1820_search_ROM[byte_counter] | bit_mask; // Set ROM bit to one
00144                         } else {
00145                             if ( ROM_bit_index > DS1820_last_descrepancy ) {
00146                                 DS1820_search_ROM[byte_counter] = DS1820_search_ROM[byte_counter] & ~bit_mask; // Set ROM bit to zero
00147                                 descrepancy_marker = ROM_bit_index;
00148                             } else {
00149                                 if (( DS1820_search_ROM[byte_counter] & bit_mask) == 0x00 )
00150                                     descrepancy_marker = ROM_bit_index;
00151                             }
00152                         }
00153                     }
00154                     onewire_bit_out (pin, DS1820_search_ROM[byte_counter] & bit_mask);
00155                     ROM_bit_index++;
00156                     if (bit_mask & 0x80) {
00157                         byte_counter++;
00158                         bit_mask = 0x01;
00159                     } else {
00160                         bit_mask = bit_mask << 1;
00161                     }
00162                 }
00163             }
00164             DS1820_last_descrepancy = descrepancy_marker;
00165             if (ROM_bit_index != 0xFF) {
00166                 int i = 1;
00167                 node *list_container;
00168                 while(1) {
00169                     list_container = probes.pop(i);
00170                     if (list_container == NULL) {                             //End of list, or empty list
00171                         if (ROM_checksum_error(DS1820_search_ROM)) {          // Check the CRC
00172                             return false;
00173                         }
00174                         for(byte_counter=0;byte_counter<8;byte_counter++)
00175                             ROM_address[byte_counter] = DS1820_search_ROM[byte_counter];
00176                         return true;
00177                     } else {                    //Otherwise, check if ROM is already known
00178                         bool equal = true;
00179                         DS1820 *pointer = (DS1820*) list_container->data;
00180                         char *ROM_compare = pointer->_ROM;
00181                         
00182                         for(byte_counter=0;byte_counter<8;byte_counter++) {
00183                             if ( ROM_compare[byte_counter] != DS1820_search_ROM[byte_counter])
00184                                 equal = false;
00185                         }
00186                         if (equal)
00187                             break;
00188                         else
00189                             i++;
00190                     }
00191                 }                        
00192             }
00193         }
00194         if (DS1820_last_descrepancy == 0)
00195             DS1820_done_flag = true;
00196     }
00197     return return_value;
00198 }
00199  
00200 void DS1820::match_ROM() {
00201 // Used to select a specific device
00202     int i;
00203     onewire_reset(&this->_datapin);
00204     onewire_byte_out( 0x55);  //Match ROM command
00205     for (i=0;i<8;i++) {
00206         onewire_byte_out(_ROM[i]);
00207     }
00208 }
00209  
00210 void DS1820::skip_ROM() {
00211     onewire_reset(&this->_datapin);
00212     onewire_byte_out(0xCC);   // Skip ROM command
00213 }
00214  
00215 bool DS1820::ROM_checksum_error(char *_ROM_address) {
00216     char CRC=0x00;
00217     int i;
00218     for(i=0;i<7;i++) // Only going to shift the lower 7 bytes
00219         CRC = CRC_byte(CRC, _ROM_address[i]);
00220     // After 7 bytes CRC should equal the 8th byte (ROM CRC)
00221     return (CRC!=_ROM_address[7]); // will return true if there is a CRC checksum mis-match         
00222 }
00223  
00224 bool DS1820::RAM_checksum_error() {
00225     char CRC=0x00;
00226     int i;
00227     for(i=0;i<8;i++) // Only going to shift the lower 8 bytes
00228         CRC = CRC_byte(CRC, RAM[i]);
00229     // After 8 bytes CRC should equal the 9th byte (RAM CRC)
00230     return (CRC!=RAM[8]); // will return true if there is a CRC checksum mis-match        
00231 }
00232  
00233 char DS1820::CRC_byte (char CRC, char byte ) {
00234     int j;
00235     for(j=0;j<8;j++) {
00236         if ((byte & 0x01 ) ^ (CRC & 0x01)) {
00237             // DATA ^ LSB CRC = 1
00238             CRC = CRC>>1;
00239             // Set the MSB to 1
00240             CRC = CRC | 0x80;
00241             // Check bit 3
00242             if (CRC & 0x04) {
00243                 CRC = CRC & 0xFB; // Bit 3 is set, so clear it
00244             } else {
00245                 CRC = CRC | 0x04; // Bit 3 is clear, so set it
00246             }
00247             // Check bit 4
00248             if (CRC & 0x08) {
00249                 CRC = CRC & 0xF7; // Bit 4 is set, so clear it
00250             } else {
00251                 CRC = CRC | 0x08; // Bit 4 is clear, so set it
00252             }
00253         } else {
00254             // DATA ^ LSB CRC = 0
00255             CRC = CRC>>1;
00256             // clear MSB
00257             CRC = CRC & 0x7F;
00258             // No need to check bits, with DATA ^ LSB CRC = 0, they will remain unchanged
00259         }
00260         byte = byte>>1;
00261     }
00262 return CRC;
00263 }
00264  
00265 int DS1820::convertTemperature(bool wait, devices device) {
00266     // Convert temperature into scratchpad RAM for all devices at once
00267     int delay_time = 100; // Default delay time
00268     char resolution;
00269     if (device==all_devices)
00270         skip_ROM();          // Skip ROM command, will convert for ALL devices
00271     else {
00272         match_ROM();
00273         if ((FAMILY_CODE == FAMILY_CODE_DS18B20 ) || (FAMILY_CODE == FAMILY_CODE_DS1822 )) {
00274             resolution = RAM[4] & 0x60;
00275             if (resolution == 0x00) // 9 bits
00276                 delay_time = 94;
00277             if (resolution == 0x20) // 10 bits
00278                 delay_time = 188;
00279             if (resolution == 0x40) // 11 bits. Note 12bits uses the 750ms default
00280                 delay_time = 375;
00281         }
00282     }
00283     
00284     onewire_byte_out( 0x44);  // perform temperature conversion
00285     if (_parasite_power) {
00286         if (_power_mosfet) {
00287             _parasitepin = _power_polarity;     // Parasite power strong pullup
00288             wait_ms(delay_time);
00289             _parasitepin = !_power_polarity;
00290             delay_time = 0;
00291         } else {
00292             _datapin.output();
00293             _datapin.write(1);
00294             wait_ms(delay_time);
00295             _datapin.input();
00296         }
00297     } else {
00298         if (wait) {
00299             wait_ms(delay_time);
00300             delay_time = 0;
00301         }
00302     }
00303     return delay_time;
00304 }
00305  
00306 void DS1820::read_RAM() {
00307     // This will copy the DS1820's 9 bytes of RAM data
00308     // into the objects RAM array. Functions that use
00309     // RAM values will automaticly call this procedure.
00310     int i;
00311     match_ROM();             // Select this device
00312     onewire_byte_out( 0xBE);   //Read Scratchpad command
00313     for(i=0;i<9;i++) {
00314         RAM[i] = onewire_byte_in();
00315     }
00316 //    if (!RAM_checksum_error())
00317 //       crcerr = 1;
00318 }
00319 
00320 bool DS1820::setResolution(unsigned int resolution) {
00321     bool answer = false;
00322     resolution = resolution - 9;
00323     if (resolution < 4) {
00324         resolution = resolution<<5; // align the bits
00325         RAM[4] = (RAM[4] & 0x60) | resolution; // mask out old data, insert new
00326         write_scratchpad ((RAM[2]<<8) + RAM[3]);
00327 //        store_scratchpad (DS1820::this_device); // Need to test if this is required
00328         answer = true;
00329     }
00330     return answer;
00331 }
00332  
00333 void DS1820::write_scratchpad(int data) {
00334     RAM[3] = data;
00335     RAM[2] = data>>8;
00336     match_ROM();
00337     onewire_byte_out(0x4E);   // Copy scratchpad into DS1820 ram memory
00338     onewire_byte_out(RAM[2]); // T(H)
00339     onewire_byte_out(RAM[3]); // T(L)
00340     if ((FAMILY_CODE == FAMILY_CODE_DS18B20 ) || (FAMILY_CODE == FAMILY_CODE_DS1822 )) {
00341         onewire_byte_out(RAM[4]); // Configuration register
00342     }
00343 }
00344  
00345 float DS1820::temperature(char scale) {
00346 // The data specs state that count_per_degree should be 0x10 (16), I found my devices
00347 // to have a count_per_degree of 0x4B (75). With the standard resolution of 1/2 deg C
00348 // this allowed an expanded resolution of 1/150th of a deg C. I wouldn't rely on this
00349 // being super acurate, but it does allow for a smooth display in the 1/10ths of a
00350 // deg C or F scales.
00351     float answer, remaining_count, count_per_degree;
00352     int reading;
00353     read_RAM();
00354     if (RAM_checksum_error())
00355         // Indicate we got a CRC error
00356         answer = -1000.0;
00357     else {
00358         reading = (RAM[1] << 8) + RAM[0];
00359         if (reading & 0x8000) { // negative degrees C
00360             reading = 0-((reading ^ 0xffff) + 1); // 2's comp then convert to signed int
00361         }
00362         answer = reading +0.0; // convert to floating point
00363         if ((FAMILY_CODE == FAMILY_CODE_DS18B20 ) || (FAMILY_CODE == FAMILY_CODE_DS1822 )) {
00364             answer = answer / 8.0;
00365         }
00366         else {
00367             remaining_count = RAM[6];
00368             count_per_degree = RAM[7];
00369             answer = answer - 0.25 + (count_per_degree - remaining_count) / count_per_degree;
00370         }
00371         if (scale=='C' or scale=='c')
00372             answer = answer / 2.0;
00373         else
00374             // Convert to deg F
00375             answer = answer * 9.0 / 10.0 + 32.0;
00376     }
00377     return answer;
00378 }
00379  
00380 bool DS1820::read_power_supply(devices device) {
00381 // This will return true if the device (or all devices) are Vcc powered
00382 // This will return false if the device (or ANY device) is parasite powered
00383     if (device==all_devices)
00384         skip_ROM();          // Skip ROM command, will poll for any device using parasite power
00385     else
00386         match_ROM();
00387     onewire_byte_out(0xB4);   // Read power supply command
00388     return onewire_bit_in(&this->_datapin);
00389 }
00390 
00391