A cut-down version of https://os.mbed.com/users/Sissors/code/DS1820/ tweaked for use with the STM32F103. It is all generic Mbed operations though, so should be usable anywhere. Non-essential functions have been removed, as this is intended for use within a tutorial.

Dependencies:   LinkedList

Dependents:   STM32F103C8T6_DS18B20 stm32f103c8t6-ds18b20

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