ds18b20 for senior design

Dependencies:   mbed

Fork of ds18b20_test by Tuan PM

Committer:
jchari
Date:
Tue Nov 17 21:55:04 2015 +0000
Revision:
2:b369cd116bf1
Parent:
0:6cb99f312e0e
So committed

Who changed what in which revision?

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