HM Yoong / DS1820

Dependents:   BMS_BMUCore_Max_DummyData

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     if (device==all_devices)
00252         skip_ROM();          // Skip ROM command, will convert for ALL devices
00253     else
00254         match_ROM();
00255     onewire_byte_out( 0x44);  // perform temperature conversion
00256     if (_parasite_power)
00257         _parasitepin = 1;       // Parasite power strong pullup
00258     wait_ms(750);
00259     if (_parasite_power)
00260         _parasitepin = 0;
00261 }
00262 
00263 void DS1820::read_RAM() {
00264     // This will copy the DS1820's 9 bytes of RAM data
00265     // into the objects RAM array. Functions that use
00266     // RAM values will automaticly call this procedure.
00267     int i;
00268     match_ROM();             // Select this device
00269     onewire_byte_out( 0xBE);   //Read Scratchpad command
00270     for(i=0;i<9;i++) {
00271         RAM[i] = onewire_byte_in();
00272     }
00273 }
00274 
00275 int DS1820::read_scratchpad() {
00276     int answer;
00277     read_RAM();
00278     answer = (RAM[2]<<8) + RAM[3];
00279     return answer;
00280 }
00281 
00282 void DS1820::write_scratchpad(int data) {
00283     RAM[3] = data;
00284     RAM[2] = data>>8;
00285     match_ROM();
00286     onewire_byte_out(0x4E);   // Copy scratchpad into DS1820 ram memory
00287     onewire_byte_out(RAM[2]); // T(H)
00288     onewire_byte_out(RAM[3]); // T(L)
00289 }
00290 
00291 void DS1820::store_scratchpad(devices device) {
00292     if (device==all_devices)
00293         skip_ROM();          // Skip ROM command, will store for ALL devices
00294     else
00295         match_ROM();
00296     onewire_byte_out(0x48);   // Write scratchpad into E2 command
00297     if (_parasite_power)
00298         _parasitepin=1;
00299     wait_ms(10);            // Parasite power strong pullup for 10ms
00300     if (_parasite_power)
00301         _parasitepin=0;
00302 }
00303 
00304 int DS1820::recall_scratchpad(devices device) {
00305 // This copies the E2 values into the DS1820's memory.
00306 // If you specify all_devices this will return zero, otherwise
00307 // it will return the value of the scratchpad memory.
00308     int answer=0;
00309     if (device==all_devices)
00310         skip_ROM();          // Skip ROM command, will recall for ALL devices
00311     else
00312         match_ROM();
00313     onewire_byte_out(0xB8);   // Recall E2 data to scratchpad command
00314     wait_ms(10); // not sure I like polling for completion
00315                  // it could cause an infinite loop
00316     if (device==DS1820::this_device) {
00317         read_RAM();
00318         answer = read_scratchpad();
00319     }
00320     return answer;
00321 }    
00322 
00323 float DS1820::temperature(char scale) {
00324 // The data specs state that count_per_degree should be 0x10 (16), I found my devices
00325 // to have a count_per_degree of 0x4B (75). With the standard resolution of 1/2 deg C
00326 // this allowed an expanded resolution of 1/150th of a deg C. I wouldn't rely on this
00327 // being super acurate, but it does allow for a smooth display in the 1/10ths of a
00328 // deg C or F scales.
00329     float answer, remaining_count, count_per_degree;
00330     int reading;
00331     read_RAM();
00332     reading = (RAM[1] << 8) + RAM[0];
00333     if (reading & 0x8000) { // negative degrees C
00334         reading = 0-((reading ^ 0xffff) + 1); // 2's comp then convert to signed int
00335     }
00336     remaining_count = RAM[6];
00337     count_per_degree = RAM[7];
00338     answer = reading +0.0;
00339     answer = answer - 0.25 + (count_per_degree - remaining_count) / count_per_degree;
00340     if (scale=='C' or scale=='c')
00341         answer = answer / 2.0;
00342     else
00343         // Convert to deg F
00344         answer = answer * 9.0 / 10.0 + 32.0;
00345     return answer;
00346 }
00347 
00348 bool DS1820::read_power_supply(devices device) {
00349 // This will return true if the device (or all devices) are Vcc powered
00350 // This will return false if the device (or ANY device) is parasite powered
00351     if (device==all_devices)
00352         skip_ROM();          // Skip ROM command, will poll for any device using parasite power
00353     else
00354         match_ROM();
00355     onewire_byte_out(0xB4);   // Read power supply command
00356     return onewire_bit_in();
00357 }