Fork of Erik's DS1820 library working on OS6

Dependencies:   LinkedList2

Dependents:   DS1820-example DS1820mitWebserver DS1820ohneWebserver

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers DS1820.cpp Source File

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