'SmOuse' / DS1820

Dependents:   ACandLightsandWifi ACandLightsandWifi

Fork of DS1820 by Michael Hagberg

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers DS1820.cpp Source File

DS1820.cpp

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