Dallas / Maxim DS1820 1-Wire library. For communication with multiple DS1820 on a single 1-Wire bus. Also supports DS18S20 and DS18B20.

Committer:
lamell
Date:
Mon Jan 29 03:39:38 2018 +0000
Revision:
3:3e89eafb60c2
Parent:
2:ee820a991b95
Child:
4:c2b93dbd6a9e
No changes

Who changed what in which revision?

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