Fork of DS1820 that can function with the data and parasite power pin being the same.

Dependents:   AutonomousDAQ AutonomousDAQ

Fork of DS1820 by David Pairman

Committer:
uci1
Date:
Thu Nov 30 05:57:26 2017 +0000
Revision:
8:7f64569563ea
Parent:
7:aceafdd78b28
explicitly set data pin high instead of input() which requires a pull-up resistor

Who changed what in which revision?

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