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