Dependents:   BMS_BMUCore_Max_DummyData

Committer:
yoonghm
Date:
Tue Feb 28 07:42:07 2012 +0000
Revision:
0:245af9360f0d

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
yoonghm 0:245af9360f0d 1 #include "DS1820.h"
yoonghm 0:245af9360f0d 2 #include "mbed.h"
yoonghm 0:245af9360f0d 3
yoonghm 0:245af9360f0d 4 // Global variables shared between all DS1820 objects
yoonghm 0:245af9360f0d 5 bool DS1820_done_flag;
yoonghm 0:245af9360f0d 6 int DS1820_last_descrepancy;
yoonghm 0:245af9360f0d 7 char DS1820_search_ROM[8];
yoonghm 0:245af9360f0d 8
yoonghm 0:245af9360f0d 9
yoonghm 0:245af9360f0d 10 DS1820::DS1820 (PinName data_pin, PinName power_pin) : _datapin(data_pin), _parasitepin(power_pin) {
yoonghm 0:245af9360f0d 11 int byte_counter;
yoonghm 0:245af9360f0d 12 _parasite_power = true;
yoonghm 0:245af9360f0d 13 for(byte_counter=0;byte_counter<8;byte_counter++)
yoonghm 0:245af9360f0d 14 ROM[byte_counter] = 0xFF;
yoonghm 0:245af9360f0d 15 for(byte_counter=0;byte_counter<9;byte_counter++)
yoonghm 0:245af9360f0d 16 RAM[byte_counter] = 0x00;
yoonghm 0:245af9360f0d 17 }
yoonghm 0:245af9360f0d 18 DS1820::DS1820 (PinName data_pin) : _datapin(data_pin), _parasitepin(NC) {
yoonghm 0:245af9360f0d 19 int byte_counter;
yoonghm 0:245af9360f0d 20 _parasite_power = false;
yoonghm 0:245af9360f0d 21 for(byte_counter=0;byte_counter<8;byte_counter++)
yoonghm 0:245af9360f0d 22 ROM[byte_counter] = 0xFF;
yoonghm 0:245af9360f0d 23 for(byte_counter=0;byte_counter<9;byte_counter++)
yoonghm 0:245af9360f0d 24 RAM[byte_counter] = 0x00;
yoonghm 0:245af9360f0d 25 }
yoonghm 0:245af9360f0d 26
yoonghm 0:245af9360f0d 27 bool DS1820::onewire_reset() {
yoonghm 0:245af9360f0d 28 // This will return false if no devices are present on the data bus
yoonghm 0:245af9360f0d 29 bool presence=false;
yoonghm 0:245af9360f0d 30 _datapin.output();
yoonghm 0:245af9360f0d 31 _datapin = 0; // bring low for 500 us
yoonghm 0:245af9360f0d 32 wait_us(500);
yoonghm 0:245af9360f0d 33 _datapin.input(); // let the data line float high
yoonghm 0:245af9360f0d 34 wait_us(90); // wait 90us
yoonghm 0:245af9360f0d 35 if (_datapin.read()==0) // see if any devices are pulling the data line low
yoonghm 0:245af9360f0d 36 presence=true;
yoonghm 0:245af9360f0d 37 wait_us(410);
yoonghm 0:245af9360f0d 38 return presence;
yoonghm 0:245af9360f0d 39 }
yoonghm 0:245af9360f0d 40
yoonghm 0:245af9360f0d 41 void DS1820::onewire_bit_out (bool bit_data) {
yoonghm 0:245af9360f0d 42 _datapin.output();
yoonghm 0:245af9360f0d 43 _datapin = 0;
yoonghm 0:245af9360f0d 44 wait_us(5);
yoonghm 0:245af9360f0d 45 if (bit_data) {
yoonghm 0:245af9360f0d 46 _datapin.input(); // bring data line high
yoonghm 0:245af9360f0d 47 wait_us(55);
yoonghm 0:245af9360f0d 48 } else {
yoonghm 0:245af9360f0d 49 wait_us(55); // keep data line low
yoonghm 0:245af9360f0d 50 _datapin.input();
yoonghm 0:245af9360f0d 51 }
yoonghm 0:245af9360f0d 52 }
yoonghm 0:245af9360f0d 53
yoonghm 0:245af9360f0d 54 void DS1820::onewire_byte_out(char data) { // output data character (least sig bit first).
yoonghm 0:245af9360f0d 55 int n;
yoonghm 0:245af9360f0d 56 for (n=0; n<8; n++) {
yoonghm 0:245af9360f0d 57 onewire_bit_out(data & 0x01);
yoonghm 0:245af9360f0d 58 data = data >> 1; // now the next bit is in the least sig bit position.
yoonghm 0:245af9360f0d 59 }
yoonghm 0:245af9360f0d 60 }
yoonghm 0:245af9360f0d 61
yoonghm 0:245af9360f0d 62 bool DS1820::onewire_bit_in() {
yoonghm 0:245af9360f0d 63 bool answer;
yoonghm 0:245af9360f0d 64 _datapin.output();
yoonghm 0:245af9360f0d 65 _datapin = 0;
yoonghm 0:245af9360f0d 66 wait_us(5);
yoonghm 0:245af9360f0d 67 _datapin.input();
yoonghm 0:245af9360f0d 68 wait_us(5);
yoonghm 0:245af9360f0d 69 answer = _datapin.read();
yoonghm 0:245af9360f0d 70 wait_us(50);
yoonghm 0:245af9360f0d 71 return answer;
yoonghm 0:245af9360f0d 72 }
yoonghm 0:245af9360f0d 73
yoonghm 0:245af9360f0d 74 char DS1820::onewire_byte_in() { // read byte, least sig byte first
yoonghm 0:245af9360f0d 75 char answer = 0x00;
yoonghm 0:245af9360f0d 76 int i;
yoonghm 0:245af9360f0d 77 for (i=0; i<8; i++) {
yoonghm 0:245af9360f0d 78 answer = answer >> 1; // shift over to make room for the next bit
yoonghm 0:245af9360f0d 79 if (onewire_bit_in())
yoonghm 0:245af9360f0d 80 answer = answer | 0x80; // if the data port is high, make this bit a 1
yoonghm 0:245af9360f0d 81 }
yoonghm 0:245af9360f0d 82 return answer;
yoonghm 0:245af9360f0d 83 }
yoonghm 0:245af9360f0d 84
yoonghm 0:245af9360f0d 85 bool DS1820::search_ROM() {
yoonghm 0:245af9360f0d 86 return search_ROM_routine(0xF0); // Search ROM command
yoonghm 0:245af9360f0d 87 }
yoonghm 0:245af9360f0d 88
yoonghm 0:245af9360f0d 89 bool DS1820::search_alarm() {
yoonghm 0:245af9360f0d 90 return search_ROM_routine(0xEC); // Search Alarm command
yoonghm 0:245af9360f0d 91 }
yoonghm 0:245af9360f0d 92
yoonghm 0:245af9360f0d 93 bool DS1820::search_ROM_routine(char command) {
yoonghm 0:245af9360f0d 94 extern bool DS1820_done_flag;
yoonghm 0:245af9360f0d 95 extern int DS1820_last_descrepancy;
yoonghm 0:245af9360f0d 96 extern char DS1820_search_ROM[8];
yoonghm 0:245af9360f0d 97 int descrepancy_marker, ROM_bit_index;
yoonghm 0:245af9360f0d 98 bool return_value, Bit_A, Bit_B;
yoonghm 0:245af9360f0d 99 char byte_counter, bit_mask;
yoonghm 0:245af9360f0d 100
yoonghm 0:245af9360f0d 101 return_value=false;
yoonghm 0:245af9360f0d 102 if (!DS1820_done_flag) {
yoonghm 0:245af9360f0d 103 if (!onewire_reset()) {
yoonghm 0:245af9360f0d 104 DS1820_last_descrepancy = 0; // no devices present
yoonghm 0:245af9360f0d 105 } else {
yoonghm 0:245af9360f0d 106 ROM_bit_index=1;
yoonghm 0:245af9360f0d 107 descrepancy_marker=0;
yoonghm 0:245af9360f0d 108 onewire_byte_out(command); // Search ROM command or Search Alarm command
yoonghm 0:245af9360f0d 109 byte_counter = 0;
yoonghm 0:245af9360f0d 110 bit_mask = 0x01;
yoonghm 0:245af9360f0d 111 while (ROM_bit_index<=64) {
yoonghm 0:245af9360f0d 112 Bit_A = onewire_bit_in();
yoonghm 0:245af9360f0d 113 Bit_B = onewire_bit_in();
yoonghm 0:245af9360f0d 114 if (Bit_A & Bit_B) {
yoonghm 0:245af9360f0d 115 descrepancy_marker = 0; // data read error, this should never happen
yoonghm 0:245af9360f0d 116 ROM_bit_index = 0xFF;
yoonghm 0:245af9360f0d 117 } else {
yoonghm 0:245af9360f0d 118 if (Bit_A | Bit_B) {
yoonghm 0:245af9360f0d 119 // Set ROM bit to Bit_A
yoonghm 0:245af9360f0d 120 if (Bit_A) {
yoonghm 0:245af9360f0d 121 DS1820_search_ROM[byte_counter] = DS1820_search_ROM[byte_counter] | bit_mask; // Set ROM bit to one
yoonghm 0:245af9360f0d 122 } else {
yoonghm 0:245af9360f0d 123 DS1820_search_ROM[byte_counter] = DS1820_search_ROM[byte_counter] & ~bit_mask; // Set ROM bit to zero
yoonghm 0:245af9360f0d 124 }
yoonghm 0:245af9360f0d 125 } else {
yoonghm 0:245af9360f0d 126 // both bits A and B are low, so there are two or more devices present
yoonghm 0:245af9360f0d 127 if ( ROM_bit_index == DS1820_last_descrepancy ) {
yoonghm 0:245af9360f0d 128 DS1820_search_ROM[byte_counter] = DS1820_search_ROM[byte_counter] | bit_mask; // Set ROM bit to one
yoonghm 0:245af9360f0d 129 } else {
yoonghm 0:245af9360f0d 130 if ( ROM_bit_index > DS1820_last_descrepancy ) {
yoonghm 0:245af9360f0d 131 DS1820_search_ROM[byte_counter] = DS1820_search_ROM[byte_counter] & ~bit_mask; // Set ROM bit to zero
yoonghm 0:245af9360f0d 132 descrepancy_marker = ROM_bit_index;
yoonghm 0:245af9360f0d 133 } else {
yoonghm 0:245af9360f0d 134 if (( DS1820_search_ROM[byte_counter] & bit_mask) == 0x00 )
yoonghm 0:245af9360f0d 135 descrepancy_marker = ROM_bit_index;
yoonghm 0:245af9360f0d 136 }
yoonghm 0:245af9360f0d 137 }
yoonghm 0:245af9360f0d 138 }
yoonghm 0:245af9360f0d 139 onewire_bit_out (DS1820_search_ROM[byte_counter] & bit_mask);
yoonghm 0:245af9360f0d 140 ROM_bit_index++;
yoonghm 0:245af9360f0d 141 if (bit_mask & 0x80) {
yoonghm 0:245af9360f0d 142 byte_counter++;
yoonghm 0:245af9360f0d 143 bit_mask = 0x01;
yoonghm 0:245af9360f0d 144 } else {
yoonghm 0:245af9360f0d 145 bit_mask = bit_mask << 1;
yoonghm 0:245af9360f0d 146 }
yoonghm 0:245af9360f0d 147 }
yoonghm 0:245af9360f0d 148 }
yoonghm 0:245af9360f0d 149 DS1820_last_descrepancy = descrepancy_marker;
yoonghm 0:245af9360f0d 150 if (ROM_bit_index != 0xFF) {
yoonghm 0:245af9360f0d 151 for(byte_counter=0;byte_counter<8;byte_counter++)
yoonghm 0:245af9360f0d 152 ROM[byte_counter] = DS1820_search_ROM[byte_counter];
yoonghm 0:245af9360f0d 153 return_value = true;
yoonghm 0:245af9360f0d 154 }
yoonghm 0:245af9360f0d 155 }
yoonghm 0:245af9360f0d 156 if (DS1820_last_descrepancy == 0)
yoonghm 0:245af9360f0d 157 DS1820_done_flag = true;
yoonghm 0:245af9360f0d 158 }
yoonghm 0:245af9360f0d 159 return return_value;
yoonghm 0:245af9360f0d 160 }
yoonghm 0:245af9360f0d 161
yoonghm 0:245af9360f0d 162 void DS1820::search_ROM_setup() {
yoonghm 0:245af9360f0d 163 extern bool DS1820_done_flag;
yoonghm 0:245af9360f0d 164 extern int DS1820_last_descrepancy;
yoonghm 0:245af9360f0d 165 extern char DS1820_search_ROM[8];
yoonghm 0:245af9360f0d 166 DS1820_done_flag = false;
yoonghm 0:245af9360f0d 167 DS1820_last_descrepancy = 0;
yoonghm 0:245af9360f0d 168 int i;
yoonghm 0:245af9360f0d 169 for (i=0; i<8; i++)
yoonghm 0:245af9360f0d 170 DS1820_search_ROM[i]=0x00;
yoonghm 0:245af9360f0d 171 }
yoonghm 0:245af9360f0d 172
yoonghm 0:245af9360f0d 173 void DS1820::read_ROM() {
yoonghm 0:245af9360f0d 174 // NOTE: This command can only be used when there is one DS1820 on the bus. If this command
yoonghm 0:245af9360f0d 175 // is used when there is more than one slave present on the bus, a data collision will occur
yoonghm 0:245af9360f0d 176 // when all the DS1820s attempt to respond at the same time.
yoonghm 0:245af9360f0d 177 int i;
yoonghm 0:245af9360f0d 178 onewire_reset();
yoonghm 0:245af9360f0d 179 onewire_byte_out(0x33); // Read ROM id
yoonghm 0:245af9360f0d 180 for (i=0; i<8; i++)
yoonghm 0:245af9360f0d 181 ROM[i]=onewire_byte_in();
yoonghm 0:245af9360f0d 182 }
yoonghm 0:245af9360f0d 183
yoonghm 0:245af9360f0d 184 void DS1820::match_ROM() {
yoonghm 0:245af9360f0d 185 // Used to select a specific device
yoonghm 0:245af9360f0d 186 int i;
yoonghm 0:245af9360f0d 187 onewire_reset();
yoonghm 0:245af9360f0d 188 onewire_byte_out( 0x55); //Match ROM command
yoonghm 0:245af9360f0d 189 for (i=0;i<8;i++)
yoonghm 0:245af9360f0d 190 onewire_byte_out(ROM[i]);
yoonghm 0:245af9360f0d 191 }
yoonghm 0:245af9360f0d 192
yoonghm 0:245af9360f0d 193 void DS1820::skip_ROM() {
yoonghm 0:245af9360f0d 194 onewire_reset();
yoonghm 0:245af9360f0d 195 onewire_byte_out(0xCC); // Skip ROM command
yoonghm 0:245af9360f0d 196 }
yoonghm 0:245af9360f0d 197
yoonghm 0:245af9360f0d 198 bool DS1820::ROM_checksum_error() {
yoonghm 0:245af9360f0d 199 char CRC=0x00;
yoonghm 0:245af9360f0d 200 int i;
yoonghm 0:245af9360f0d 201 for(i=0;i<7;i++) // Only going to shift the lower 7 bytes
yoonghm 0:245af9360f0d 202 CRC = CRC_byte(CRC, ROM[i]);
yoonghm 0:245af9360f0d 203 // After 7 bytes CRC should equal the 8th byte (ROM CRC)
yoonghm 0:245af9360f0d 204 return (CRC!=ROM[7]); // will return true if there is a CRC checksum error
yoonghm 0:245af9360f0d 205 }
yoonghm 0:245af9360f0d 206
yoonghm 0:245af9360f0d 207 bool DS1820::RAM_checksum_error() {
yoonghm 0:245af9360f0d 208 char CRC=0x00;
yoonghm 0:245af9360f0d 209 int i;
yoonghm 0:245af9360f0d 210 read_RAM();
yoonghm 0:245af9360f0d 211 for(i=0;i<8;i++) // Only going to shift the lower 8 bytes
yoonghm 0:245af9360f0d 212 CRC = CRC_byte(CRC, RAM[i]);
yoonghm 0:245af9360f0d 213 // After 8 bytes CRC should equal the 9th byte (RAM CRC)
yoonghm 0:245af9360f0d 214 return (CRC!=RAM[8]); // will return true if there is a CRC checksum error
yoonghm 0:245af9360f0d 215 }
yoonghm 0:245af9360f0d 216
yoonghm 0:245af9360f0d 217 char DS1820::CRC_byte (char CRC, char byte ) {
yoonghm 0:245af9360f0d 218 int j;
yoonghm 0:245af9360f0d 219 for(j=0;j<8;j++) {
yoonghm 0:245af9360f0d 220 if ((byte & 0x01 ) ^ (CRC & 0x01)) {
yoonghm 0:245af9360f0d 221 // DATA ^ LSB CRC = 1
yoonghm 0:245af9360f0d 222 CRC = CRC>>1;
yoonghm 0:245af9360f0d 223 // Set the MSB to 1
yoonghm 0:245af9360f0d 224 CRC = CRC | 0x80;
yoonghm 0:245af9360f0d 225 // Check bit 3
yoonghm 0:245af9360f0d 226 if (CRC & 0x04) {
yoonghm 0:245af9360f0d 227 CRC = CRC & 0xFB; // Bit 3 is set, so clear it
yoonghm 0:245af9360f0d 228 } else {
yoonghm 0:245af9360f0d 229 CRC = CRC | 0x04; // Bit 3 is clear, so set it
yoonghm 0:245af9360f0d 230 }
yoonghm 0:245af9360f0d 231 // Check bit 4
yoonghm 0:245af9360f0d 232 if (CRC & 0x08) {
yoonghm 0:245af9360f0d 233 CRC = CRC & 0xF7; // Bit 4 is set, so clear it
yoonghm 0:245af9360f0d 234 } else {
yoonghm 0:245af9360f0d 235 CRC = CRC | 0x08; // Bit 4 is clear, so set it
yoonghm 0:245af9360f0d 236 }
yoonghm 0:245af9360f0d 237 } else {
yoonghm 0:245af9360f0d 238 // DATA ^ LSB CRC = 0
yoonghm 0:245af9360f0d 239 CRC = CRC>>1;
yoonghm 0:245af9360f0d 240 // clear MSB
yoonghm 0:245af9360f0d 241 CRC = CRC & 0x7F;
yoonghm 0:245af9360f0d 242 // No need to check bits, with DATA ^ LSB CRC = 0, they will remain unchanged
yoonghm 0:245af9360f0d 243 }
yoonghm 0:245af9360f0d 244 byte = byte>>1;
yoonghm 0:245af9360f0d 245 }
yoonghm 0:245af9360f0d 246 return CRC;
yoonghm 0:245af9360f0d 247 }
yoonghm 0:245af9360f0d 248
yoonghm 0:245af9360f0d 249 void DS1820::convert_temperature(devices device) {
yoonghm 0:245af9360f0d 250 // Convert temperature into scratchpad RAM for all devices at once
yoonghm 0:245af9360f0d 251 if (device==all_devices)
yoonghm 0:245af9360f0d 252 skip_ROM(); // Skip ROM command, will convert for ALL devices
yoonghm 0:245af9360f0d 253 else
yoonghm 0:245af9360f0d 254 match_ROM();
yoonghm 0:245af9360f0d 255 onewire_byte_out( 0x44); // perform temperature conversion
yoonghm 0:245af9360f0d 256 if (_parasite_power)
yoonghm 0:245af9360f0d 257 _parasitepin = 1; // Parasite power strong pullup
yoonghm 0:245af9360f0d 258 wait_ms(750);
yoonghm 0:245af9360f0d 259 if (_parasite_power)
yoonghm 0:245af9360f0d 260 _parasitepin = 0;
yoonghm 0:245af9360f0d 261 }
yoonghm 0:245af9360f0d 262
yoonghm 0:245af9360f0d 263 void DS1820::read_RAM() {
yoonghm 0:245af9360f0d 264 // This will copy the DS1820's 9 bytes of RAM data
yoonghm 0:245af9360f0d 265 // into the objects RAM array. Functions that use
yoonghm 0:245af9360f0d 266 // RAM values will automaticly call this procedure.
yoonghm 0:245af9360f0d 267 int i;
yoonghm 0:245af9360f0d 268 match_ROM(); // Select this device
yoonghm 0:245af9360f0d 269 onewire_byte_out( 0xBE); //Read Scratchpad command
yoonghm 0:245af9360f0d 270 for(i=0;i<9;i++) {
yoonghm 0:245af9360f0d 271 RAM[i] = onewire_byte_in();
yoonghm 0:245af9360f0d 272 }
yoonghm 0:245af9360f0d 273 }
yoonghm 0:245af9360f0d 274
yoonghm 0:245af9360f0d 275 int DS1820::read_scratchpad() {
yoonghm 0:245af9360f0d 276 int answer;
yoonghm 0:245af9360f0d 277 read_RAM();
yoonghm 0:245af9360f0d 278 answer = (RAM[2]<<8) + RAM[3];
yoonghm 0:245af9360f0d 279 return answer;
yoonghm 0:245af9360f0d 280 }
yoonghm 0:245af9360f0d 281
yoonghm 0:245af9360f0d 282 void DS1820::write_scratchpad(int data) {
yoonghm 0:245af9360f0d 283 RAM[3] = data;
yoonghm 0:245af9360f0d 284 RAM[2] = data>>8;
yoonghm 0:245af9360f0d 285 match_ROM();
yoonghm 0:245af9360f0d 286 onewire_byte_out(0x4E); // Copy scratchpad into DS1820 ram memory
yoonghm 0:245af9360f0d 287 onewire_byte_out(RAM[2]); // T(H)
yoonghm 0:245af9360f0d 288 onewire_byte_out(RAM[3]); // T(L)
yoonghm 0:245af9360f0d 289 }
yoonghm 0:245af9360f0d 290
yoonghm 0:245af9360f0d 291 void DS1820::store_scratchpad(devices device) {
yoonghm 0:245af9360f0d 292 if (device==all_devices)
yoonghm 0:245af9360f0d 293 skip_ROM(); // Skip ROM command, will store for ALL devices
yoonghm 0:245af9360f0d 294 else
yoonghm 0:245af9360f0d 295 match_ROM();
yoonghm 0:245af9360f0d 296 onewire_byte_out(0x48); // Write scratchpad into E2 command
yoonghm 0:245af9360f0d 297 if (_parasite_power)
yoonghm 0:245af9360f0d 298 _parasitepin=1;
yoonghm 0:245af9360f0d 299 wait_ms(10); // Parasite power strong pullup for 10ms
yoonghm 0:245af9360f0d 300 if (_parasite_power)
yoonghm 0:245af9360f0d 301 _parasitepin=0;
yoonghm 0:245af9360f0d 302 }
yoonghm 0:245af9360f0d 303
yoonghm 0:245af9360f0d 304 int DS1820::recall_scratchpad(devices device) {
yoonghm 0:245af9360f0d 305 // This copies the E2 values into the DS1820's memory.
yoonghm 0:245af9360f0d 306 // If you specify all_devices this will return zero, otherwise
yoonghm 0:245af9360f0d 307 // it will return the value of the scratchpad memory.
yoonghm 0:245af9360f0d 308 int answer=0;
yoonghm 0:245af9360f0d 309 if (device==all_devices)
yoonghm 0:245af9360f0d 310 skip_ROM(); // Skip ROM command, will recall for ALL devices
yoonghm 0:245af9360f0d 311 else
yoonghm 0:245af9360f0d 312 match_ROM();
yoonghm 0:245af9360f0d 313 onewire_byte_out(0xB8); // Recall E2 data to scratchpad command
yoonghm 0:245af9360f0d 314 wait_ms(10); // not sure I like polling for completion
yoonghm 0:245af9360f0d 315 // it could cause an infinite loop
yoonghm 0:245af9360f0d 316 if (device==DS1820::this_device) {
yoonghm 0:245af9360f0d 317 read_RAM();
yoonghm 0:245af9360f0d 318 answer = read_scratchpad();
yoonghm 0:245af9360f0d 319 }
yoonghm 0:245af9360f0d 320 return answer;
yoonghm 0:245af9360f0d 321 }
yoonghm 0:245af9360f0d 322
yoonghm 0:245af9360f0d 323 float DS1820::temperature(char scale) {
yoonghm 0:245af9360f0d 324 // The data specs state that count_per_degree should be 0x10 (16), I found my devices
yoonghm 0:245af9360f0d 325 // to have a count_per_degree of 0x4B (75). With the standard resolution of 1/2 deg C
yoonghm 0:245af9360f0d 326 // this allowed an expanded resolution of 1/150th of a deg C. I wouldn't rely on this
yoonghm 0:245af9360f0d 327 // being super acurate, but it does allow for a smooth display in the 1/10ths of a
yoonghm 0:245af9360f0d 328 // deg C or F scales.
yoonghm 0:245af9360f0d 329 float answer, remaining_count, count_per_degree;
yoonghm 0:245af9360f0d 330 int reading;
yoonghm 0:245af9360f0d 331 read_RAM();
yoonghm 0:245af9360f0d 332 reading = (RAM[1] << 8) + RAM[0];
yoonghm 0:245af9360f0d 333 if (reading & 0x8000) { // negative degrees C
yoonghm 0:245af9360f0d 334 reading = 0-((reading ^ 0xffff) + 1); // 2's comp then convert to signed int
yoonghm 0:245af9360f0d 335 }
yoonghm 0:245af9360f0d 336 remaining_count = RAM[6];
yoonghm 0:245af9360f0d 337 count_per_degree = RAM[7];
yoonghm 0:245af9360f0d 338 answer = reading +0.0;
yoonghm 0:245af9360f0d 339 answer = answer - 0.25 + (count_per_degree - remaining_count) / count_per_degree;
yoonghm 0:245af9360f0d 340 if (scale=='C' or scale=='c')
yoonghm 0:245af9360f0d 341 answer = answer / 2.0;
yoonghm 0:245af9360f0d 342 else
yoonghm 0:245af9360f0d 343 // Convert to deg F
yoonghm 0:245af9360f0d 344 answer = answer * 9.0 / 10.0 + 32.0;
yoonghm 0:245af9360f0d 345 return answer;
yoonghm 0:245af9360f0d 346 }
yoonghm 0:245af9360f0d 347
yoonghm 0:245af9360f0d 348 bool DS1820::read_power_supply(devices device) {
yoonghm 0:245af9360f0d 349 // This will return true if the device (or all devices) are Vcc powered
yoonghm 0:245af9360f0d 350 // This will return false if the device (or ANY device) is parasite powered
yoonghm 0:245af9360f0d 351 if (device==all_devices)
yoonghm 0:245af9360f0d 352 skip_ROM(); // Skip ROM command, will poll for any device using parasite power
yoonghm 0:245af9360f0d 353 else
yoonghm 0:245af9360f0d 354 match_ROM();
yoonghm 0:245af9360f0d 355 onewire_byte_out(0xB4); // Read power supply command
yoonghm 0:245af9360f0d 356 return onewire_bit_in();
yoonghm 0:245af9360f0d 357 }