A cut-down version of https://os.mbed.com/users/Sissors/code/DS1820/ tweaked for use with the STM32F103. It is all generic Mbed operations though, so should be usable anywhere. Non-essential functions have been removed, as this is intended for use within a tutorial.

Dependencies:   LinkedList

Dependents:   STM32F103C8T6_DS18B20 stm32f103c8t6-ds18b20

Fork of DS1820 by Erik -

Committer:
deece
Date:
Thu Jan 25 07:33:48 2018 +0000
Revision:
19:b9d69bad8185
Parent:
17:045f96704cc6
Fix timings

Who changed what in which revision?

UserRevisionLine numberNew contents of line
pairmand 3:8f2b7f4940b5 1 #include "DS1820.h"
Sissors 5:2cd4928e8147 2
deece 16:d490e11c466d 3 static inline void onewire_input(DigitalInOut &pin) {
deece 16:d490e11c466d 4 pin.input();
deece 16:d490e11c466d 5 }
Sissors 14:c591209285e9 6
deece 16:d490e11c466d 7 static inline void onewire_output(DigitalInOut &pin) {
deece 16:d490e11c466d 8 pin.output();
deece 16:d490e11c466d 9 pin.write(0);
deece 16:d490e11c466d 10 }
Sissors 14:c591209285e9 11
Sissors 5:2cd4928e8147 12 LinkedList<node> DS1820::probes;
pairmand 3:8f2b7f4940b5 13
pairmand 3:8f2b7f4940b5 14
deece 16:d490e11c466d 15 DS1820::DS1820 (PinName data_pin) : _datapin(data_pin) {
pairmand 3:8f2b7f4940b5 16 int byte_counter;
deece 16:d490e11c466d 17
pairmand 3:8f2b7f4940b5 18 for(byte_counter=0;byte_counter<9;byte_counter++)
pairmand 3:8f2b7f4940b5 19 RAM[byte_counter] = 0x00;
Sissors 5:2cd4928e8147 20
deece 16:d490e11c466d 21 onewire_input(_datapin);
Sissors 14:c591209285e9 22
deece 16:d490e11c466d 23 if (!unassignedProbe(_ROM))
deece 16:d490e11c466d 24 error("No unassigned DS1820 found!\r\n");
Sissors 5:2cd4928e8147 25 else {
deece 16:d490e11c466d 26 onewire_input(_datapin);
Sissors 5:2cd4928e8147 27 probes.append(this);
Sissors 5:2cd4928e8147 28 }
pairmand 3:8f2b7f4940b5 29 }
Sissors 5:2cd4928e8147 30
Sissors 5:2cd4928e8147 31 DS1820::~DS1820 (void) {
Sissors 5:2cd4928e8147 32 node *tmp;
Sissors 5:2cd4928e8147 33 for(int i=1; i<=probes.length(); i++)
Sissors 5:2cd4928e8147 34 {
Sissors 5:2cd4928e8147 35 tmp = probes.pop(i);
Sissors 5:2cd4928e8147 36 if (tmp->data == this)
Sissors 5:2cd4928e8147 37 probes.remove(i);
Sissors 5:2cd4928e8147 38 }
pairmand 3:8f2b7f4940b5 39 }
Sissors 5:2cd4928e8147 40
pairmand 3:8f2b7f4940b5 41
deece 16:d490e11c466d 42 bool DS1820::onewire_reset() {
pairmand 3:8f2b7f4940b5 43 // This will return false if no devices are present on the data bus
pairmand 3:8f2b7f4940b5 44 bool presence=false;
deece 16:d490e11c466d 45 onewire_output(_datapin); // bring low for 500 us
pairmand 3:8f2b7f4940b5 46 wait_us(500);
deece 16:d490e11c466d 47 onewire_input(_datapin); // let the data line float high
pairmand 3:8f2b7f4940b5 48 wait_us(90); // wait 90us
deece 16:d490e11c466d 49 if (_datapin.read()==0) // see if any devices are pulling the data line low
pairmand 3:8f2b7f4940b5 50 presence=true;
pairmand 3:8f2b7f4940b5 51 wait_us(410);
pairmand 3:8f2b7f4940b5 52 return presence;
pairmand 3:8f2b7f4940b5 53 }
pairmand 3:8f2b7f4940b5 54
deece 16:d490e11c466d 55 void DS1820::onewire_bit_out (bool bit_data) {
deece 16:d490e11c466d 56 onewire_output(_datapin);
deece 16:d490e11c466d 57 wait_us(1); // DXP modified from 5
deece 17:045f96704cc6 58 if (!bit_data) {
deece 17:045f96704cc6 59 wait_us(57); // keep data line low
pairmand 3:8f2b7f4940b5 60 }
deece 17:045f96704cc6 61 onewire_input(_datapin);
deece 19:b9d69bad8185 62 wait_us(60);
pairmand 3:8f2b7f4940b5 63 }
pairmand 3:8f2b7f4940b5 64
pairmand 3:8f2b7f4940b5 65 void DS1820::onewire_byte_out(char data) { // output data character (least sig bit first).
deece 17:045f96704cc6 66 for (int n=0; n<8; n++) {
deece 16:d490e11c466d 67 onewire_bit_out(data & 0x01);
pairmand 3:8f2b7f4940b5 68 data = data >> 1; // now the next bit is in the least sig bit position.
pairmand 3:8f2b7f4940b5 69 }
deece 16:d490e11c466d 70 wait_us(100);
pairmand 3:8f2b7f4940b5 71 }
pairmand 3:8f2b7f4940b5 72
deece 16:d490e11c466d 73 bool DS1820::onewire_bit_in() {
pairmand 3:8f2b7f4940b5 74 bool answer;
deece 16:d490e11c466d 75 onewire_output(_datapin);
deece 16:d490e11c466d 76 wait_us(1); // DXP modified from 5
deece 16:d490e11c466d 77 onewire_input(_datapin);
pairmand 3:8f2b7f4940b5 78 wait_us(10); // DXP modified from 5
deece 16:d490e11c466d 79 answer = _datapin.read();
deece 16:d490e11c466d 80 wait_us(49); // DXP modified from 50
pairmand 3:8f2b7f4940b5 81 return answer;
pairmand 3:8f2b7f4940b5 82 }
pairmand 3:8f2b7f4940b5 83
pairmand 3:8f2b7f4940b5 84 char DS1820::onewire_byte_in() { // read byte, least sig byte first
pairmand 3:8f2b7f4940b5 85 char answer = 0x00;
pairmand 3:8f2b7f4940b5 86 int i;
pairmand 3:8f2b7f4940b5 87 for (i=0; i<8; i++) {
pairmand 3:8f2b7f4940b5 88 answer = answer >> 1; // shift over to make room for the next bit
deece 16:d490e11c466d 89 if (onewire_bit_in())
pairmand 3:8f2b7f4940b5 90 answer = answer | 0x80; // if the data port is high, make this bit a 1
pairmand 3:8f2b7f4940b5 91 }
pairmand 3:8f2b7f4940b5 92 return answer;
pairmand 3:8f2b7f4940b5 93 }
Sissors 5:2cd4928e8147 94
deece 16:d490e11c466d 95
deece 16:d490e11c466d 96 bool DS1820::unassignedProbe(char *ROM_address) {
deece 16:d490e11c466d 97 return search_ROM_routine(0xF0, ROM_address);
pairmand 3:8f2b7f4940b5 98 }
pairmand 3:8f2b7f4940b5 99
deece 16:d490e11c466d 100 bool DS1820::search_ROM_routine(char command, char *ROM_address) {
Sissors 5:2cd4928e8147 101 bool DS1820_done_flag = false;
Sissors 5:2cd4928e8147 102 int DS1820_last_descrepancy = 0;
Sissors 5:2cd4928e8147 103 char DS1820_search_ROM[8] = {0, 0, 0, 0, 0, 0, 0, 0};
Sissors 5:2cd4928e8147 104
pairmand 3:8f2b7f4940b5 105 int descrepancy_marker, ROM_bit_index;
pairmand 3:8f2b7f4940b5 106 bool return_value, Bit_A, Bit_B;
pairmand 3:8f2b7f4940b5 107 char byte_counter, bit_mask;
pairmand 3:8f2b7f4940b5 108
pairmand 3:8f2b7f4940b5 109 return_value=false;
Sissors 5:2cd4928e8147 110 while (!DS1820_done_flag) {
deece 16:d490e11c466d 111 if (!onewire_reset()) {
Sissors 5:2cd4928e8147 112 return false;
pairmand 3:8f2b7f4940b5 113 } else {
pairmand 3:8f2b7f4940b5 114 ROM_bit_index=1;
pairmand 3:8f2b7f4940b5 115 descrepancy_marker=0;
Sissors 12:196e9e54b033 116 char command_shift = command;
Sissors 5:2cd4928e8147 117 for (int n=0; n<8; n++) { // Search ROM command or Search Alarm command
deece 16:d490e11c466d 118 onewire_bit_out(command_shift & 0x01);
Sissors 12:196e9e54b033 119 command_shift = command_shift >> 1; // now the next bit is in the least sig bit position.
Sissors 5:2cd4928e8147 120 }
deece 16:d490e11c466d 121 wait_us(100);
deece 16:d490e11c466d 122
pairmand 3:8f2b7f4940b5 123 byte_counter = 0;
pairmand 3:8f2b7f4940b5 124 bit_mask = 0x01;
pairmand 3:8f2b7f4940b5 125 while (ROM_bit_index<=64) {
deece 16:d490e11c466d 126 Bit_A = onewire_bit_in();
deece 16:d490e11c466d 127 Bit_B = onewire_bit_in();
pairmand 3:8f2b7f4940b5 128 if (Bit_A & Bit_B) {
pairmand 3:8f2b7f4940b5 129 descrepancy_marker = 0; // data read error, this should never happen
pairmand 3:8f2b7f4940b5 130 ROM_bit_index = 0xFF;
pairmand 3:8f2b7f4940b5 131 } else {
pairmand 3:8f2b7f4940b5 132 if (Bit_A | Bit_B) {
pairmand 3:8f2b7f4940b5 133 // Set ROM bit to Bit_A
pairmand 3:8f2b7f4940b5 134 if (Bit_A) {
pairmand 3:8f2b7f4940b5 135 DS1820_search_ROM[byte_counter] = DS1820_search_ROM[byte_counter] | bit_mask; // Set ROM bit to one
pairmand 3:8f2b7f4940b5 136 } else {
pairmand 3:8f2b7f4940b5 137 DS1820_search_ROM[byte_counter] = DS1820_search_ROM[byte_counter] & ~bit_mask; // Set ROM bit to zero
pairmand 3:8f2b7f4940b5 138 }
pairmand 3:8f2b7f4940b5 139 } else {
pairmand 3:8f2b7f4940b5 140 // both bits A and B are low, so there are two or more devices present
pairmand 3:8f2b7f4940b5 141 if ( ROM_bit_index == DS1820_last_descrepancy ) {
pairmand 3:8f2b7f4940b5 142 DS1820_search_ROM[byte_counter] = DS1820_search_ROM[byte_counter] | bit_mask; // Set ROM bit to one
pairmand 3:8f2b7f4940b5 143 } else {
pairmand 3:8f2b7f4940b5 144 if ( ROM_bit_index > DS1820_last_descrepancy ) {
pairmand 3:8f2b7f4940b5 145 DS1820_search_ROM[byte_counter] = DS1820_search_ROM[byte_counter] & ~bit_mask; // Set ROM bit to zero
pairmand 3:8f2b7f4940b5 146 descrepancy_marker = ROM_bit_index;
pairmand 3:8f2b7f4940b5 147 } else {
pairmand 3:8f2b7f4940b5 148 if (( DS1820_search_ROM[byte_counter] & bit_mask) == 0x00 )
pairmand 3:8f2b7f4940b5 149 descrepancy_marker = ROM_bit_index;
pairmand 3:8f2b7f4940b5 150 }
pairmand 3:8f2b7f4940b5 151 }
pairmand 3:8f2b7f4940b5 152 }
deece 16:d490e11c466d 153 onewire_bit_out (DS1820_search_ROM[byte_counter] & bit_mask);
pairmand 3:8f2b7f4940b5 154 ROM_bit_index++;
pairmand 3:8f2b7f4940b5 155 if (bit_mask & 0x80) {
pairmand 3:8f2b7f4940b5 156 byte_counter++;
pairmand 3:8f2b7f4940b5 157 bit_mask = 0x01;
pairmand 3:8f2b7f4940b5 158 } else {
pairmand 3:8f2b7f4940b5 159 bit_mask = bit_mask << 1;
pairmand 3:8f2b7f4940b5 160 }
pairmand 3:8f2b7f4940b5 161 }
deece 16:d490e11c466d 162
deece 16:d490e11c466d 163 wait_us(100);
pairmand 3:8f2b7f4940b5 164 }
pairmand 3:8f2b7f4940b5 165 DS1820_last_descrepancy = descrepancy_marker;
pairmand 3:8f2b7f4940b5 166 if (ROM_bit_index != 0xFF) {
Sissors 5:2cd4928e8147 167 int i = 1;
Sissors 5:2cd4928e8147 168 node *list_container;
Sissors 5:2cd4928e8147 169 while(1) {
Sissors 5:2cd4928e8147 170 list_container = probes.pop(i);
Sissors 5:2cd4928e8147 171 if (list_container == NULL) { //End of list, or empty list
Sissors 5:2cd4928e8147 172 if (ROM_checksum_error(DS1820_search_ROM)) { // Check the CRC
Sissors 5:2cd4928e8147 173 return false;
Sissors 5:2cd4928e8147 174 }
Sissors 5:2cd4928e8147 175 for(byte_counter=0;byte_counter<8;byte_counter++)
Sissors 5:2cd4928e8147 176 ROM_address[byte_counter] = DS1820_search_ROM[byte_counter];
deece 16:d490e11c466d 177
deece 16:d490e11c466d 178 printf("Found DS18B20: %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x\r\n",
deece 16:d490e11c466d 179 ROM_address[0], ROM_address[1], ROM_address[2], ROM_address[3], ROM_address[4], ROM_address[5], ROM_address[6], ROM_address[7]);
Sissors 5:2cd4928e8147 180 return true;
Sissors 5:2cd4928e8147 181 } else { //Otherwise, check if ROM is already known
Sissors 5:2cd4928e8147 182 bool equal = true;
Sissors 5:2cd4928e8147 183 DS1820 *pointer = (DS1820*) list_container->data;
Sissors 5:2cd4928e8147 184 char *ROM_compare = pointer->_ROM;
Sissors 5:2cd4928e8147 185
Sissors 5:2cd4928e8147 186 for(byte_counter=0;byte_counter<8;byte_counter++) {
Sissors 5:2cd4928e8147 187 if ( ROM_compare[byte_counter] != DS1820_search_ROM[byte_counter])
Sissors 5:2cd4928e8147 188 equal = false;
Sissors 5:2cd4928e8147 189 }
Sissors 5:2cd4928e8147 190 if (equal)
Sissors 5:2cd4928e8147 191 break;
Sissors 5:2cd4928e8147 192 else
Sissors 5:2cd4928e8147 193 i++;
Sissors 5:2cd4928e8147 194 }
Sissors 5:2cd4928e8147 195 }
pairmand 3:8f2b7f4940b5 196 }
pairmand 3:8f2b7f4940b5 197 }
pairmand 3:8f2b7f4940b5 198 if (DS1820_last_descrepancy == 0)
pairmand 3:8f2b7f4940b5 199 DS1820_done_flag = true;
pairmand 3:8f2b7f4940b5 200 }
pairmand 3:8f2b7f4940b5 201 return return_value;
pairmand 3:8f2b7f4940b5 202 }
pairmand 3:8f2b7f4940b5 203
pairmand 3:8f2b7f4940b5 204 void DS1820::match_ROM() {
pairmand 3:8f2b7f4940b5 205 // Used to select a specific device
pairmand 3:8f2b7f4940b5 206 int i;
deece 16:d490e11c466d 207 onewire_reset();
deece 16:d490e11c466d 208 onewire_byte_out(0x55); //Match ROM command
Sissors 5:2cd4928e8147 209 for (i=0;i<8;i++) {
Sissors 5:2cd4928e8147 210 onewire_byte_out(_ROM[i]);
Sissors 5:2cd4928e8147 211 }
pairmand 3:8f2b7f4940b5 212 }
pairmand 3:8f2b7f4940b5 213
pairmand 3:8f2b7f4940b5 214 void DS1820::skip_ROM() {
deece 16:d490e11c466d 215 onewire_reset();
pairmand 3:8f2b7f4940b5 216 onewire_byte_out(0xCC); // Skip ROM command
pairmand 3:8f2b7f4940b5 217 }
pairmand 3:8f2b7f4940b5 218
Sissors 5:2cd4928e8147 219 bool DS1820::ROM_checksum_error(char *_ROM_address) {
Sissors 11:1a3c3002b50c 220 char _CRC=0x00;
pairmand 3:8f2b7f4940b5 221 int i;
pairmand 3:8f2b7f4940b5 222 for(i=0;i<7;i++) // Only going to shift the lower 7 bytes
Sissors 11:1a3c3002b50c 223 _CRC = CRC_byte(_CRC, _ROM_address[i]);
pairmand 3:8f2b7f4940b5 224 // After 7 bytes CRC should equal the 8th byte (ROM CRC)
Sissors 11:1a3c3002b50c 225 return (_CRC!=_ROM_address[7]); // will return true if there is a CRC checksum mis-match
pairmand 3:8f2b7f4940b5 226 }
pairmand 3:8f2b7f4940b5 227
pairmand 3:8f2b7f4940b5 228 bool DS1820::RAM_checksum_error() {
Sissors 11:1a3c3002b50c 229 char _CRC=0x00;
pairmand 3:8f2b7f4940b5 230 int i;
pairmand 3:8f2b7f4940b5 231 for(i=0;i<8;i++) // Only going to shift the lower 8 bytes
Sissors 11:1a3c3002b50c 232 _CRC = CRC_byte(_CRC, RAM[i]);
pairmand 3:8f2b7f4940b5 233 // After 8 bytes CRC should equal the 9th byte (RAM CRC)
Sissors 11:1a3c3002b50c 234 return (_CRC!=RAM[8]); // will return true if there is a CRC checksum mis-match
pairmand 3:8f2b7f4940b5 235 }
pairmand 3:8f2b7f4940b5 236
Sissors 11:1a3c3002b50c 237 char DS1820::CRC_byte (char _CRC, char byte ) {
pairmand 3:8f2b7f4940b5 238 int j;
pairmand 3:8f2b7f4940b5 239 for(j=0;j<8;j++) {
Sissors 11:1a3c3002b50c 240 if ((byte & 0x01 ) ^ (_CRC & 0x01)) {
pairmand 3:8f2b7f4940b5 241 // DATA ^ LSB CRC = 1
Sissors 11:1a3c3002b50c 242 _CRC = _CRC>>1;
pairmand 3:8f2b7f4940b5 243 // Set the MSB to 1
Sissors 11:1a3c3002b50c 244 _CRC = _CRC | 0x80;
pairmand 3:8f2b7f4940b5 245 // Check bit 3
Sissors 11:1a3c3002b50c 246 if (_CRC & 0x04) {
Sissors 11:1a3c3002b50c 247 _CRC = _CRC & 0xFB; // Bit 3 is set, so clear it
pairmand 3:8f2b7f4940b5 248 } else {
Sissors 11:1a3c3002b50c 249 _CRC = _CRC | 0x04; // Bit 3 is clear, so set it
pairmand 3:8f2b7f4940b5 250 }
pairmand 3:8f2b7f4940b5 251 // Check bit 4
Sissors 11:1a3c3002b50c 252 if (_CRC & 0x08) {
Sissors 11:1a3c3002b50c 253 _CRC = _CRC & 0xF7; // Bit 4 is set, so clear it
pairmand 3:8f2b7f4940b5 254 } else {
Sissors 11:1a3c3002b50c 255 _CRC = _CRC | 0x08; // Bit 4 is clear, so set it
pairmand 3:8f2b7f4940b5 256 }
pairmand 3:8f2b7f4940b5 257 } else {
pairmand 3:8f2b7f4940b5 258 // DATA ^ LSB CRC = 0
Sissors 11:1a3c3002b50c 259 _CRC = _CRC>>1;
pairmand 3:8f2b7f4940b5 260 // clear MSB
Sissors 11:1a3c3002b50c 261 _CRC = _CRC & 0x7F;
pairmand 3:8f2b7f4940b5 262 // No need to check bits, with DATA ^ LSB CRC = 0, they will remain unchanged
pairmand 3:8f2b7f4940b5 263 }
pairmand 3:8f2b7f4940b5 264 byte = byte>>1;
pairmand 3:8f2b7f4940b5 265 }
Sissors 11:1a3c3002b50c 266 return _CRC;
pairmand 3:8f2b7f4940b5 267 }
pairmand 3:8f2b7f4940b5 268
Sissors 5:2cd4928e8147 269 int DS1820::convertTemperature(bool wait, devices device) {
pairmand 3:8f2b7f4940b5 270 // Convert temperature into scratchpad RAM for all devices at once
deece 17:045f96704cc6 271 int delay_time = 850; // Default delay time
pairmand 3:8f2b7f4940b5 272 if (device==all_devices)
pairmand 3:8f2b7f4940b5 273 skip_ROM(); // Skip ROM command, will convert for ALL devices
pairmand 3:8f2b7f4940b5 274 else {
pairmand 3:8f2b7f4940b5 275 match_ROM();
pairmand 3:8f2b7f4940b5 276 }
Sissors 5:2cd4928e8147 277
deece 17:045f96704cc6 278 onewire_byte_out(0x44); // perform temperature conversion
deece 16:d490e11c466d 279
deece 16:d490e11c466d 280 if (wait) {
deece 16:d490e11c466d 281 wait_ms(delay_time);
pairmand 3:8f2b7f4940b5 282 }
pairmand 3:8f2b7f4940b5 283 return delay_time;
pairmand 3:8f2b7f4940b5 284 }
pairmand 3:8f2b7f4940b5 285
pairmand 3:8f2b7f4940b5 286 void DS1820::read_RAM() {
pairmand 3:8f2b7f4940b5 287 // This will copy the DS1820's 9 bytes of RAM data
pairmand 3:8f2b7f4940b5 288 // into the objects RAM array. Functions that use
pairmand 3:8f2b7f4940b5 289 // RAM values will automaticly call this procedure.
pairmand 3:8f2b7f4940b5 290 int i;
pairmand 3:8f2b7f4940b5 291 match_ROM(); // Select this device
pairmand 3:8f2b7f4940b5 292 onewire_byte_out( 0xBE); //Read Scratchpad command
pairmand 3:8f2b7f4940b5 293 for(i=0;i<9;i++) {
pairmand 3:8f2b7f4940b5 294 RAM[i] = onewire_byte_in();
pairmand 3:8f2b7f4940b5 295 }
deece 17:045f96704cc6 296 printf("Scratchpad=%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x\r\n",
deece 17:045f96704cc6 297 RAM[8], RAM[7], RAM[6], RAM[5], RAM[4], RAM[3], RAM[2], RAM[1], RAM[0]);
deece 17:045f96704cc6 298 printf("Checksum is %s\r\n", RAM_checksum_error() ? "bad" : "good");
pairmand 3:8f2b7f4940b5 299 }
pairmand 3:8f2b7f4940b5 300
Sissors 5:2cd4928e8147 301 bool DS1820::setResolution(unsigned int resolution) {
pairmand 3:8f2b7f4940b5 302 bool answer = false;
pairmand 3:8f2b7f4940b5 303 resolution = resolution - 9;
pairmand 3:8f2b7f4940b5 304 if (resolution < 4) {
pairmand 3:8f2b7f4940b5 305 resolution = resolution<<5; // align the bits
pairmand 3:8f2b7f4940b5 306 RAM[4] = (RAM[4] & 0x60) | resolution; // mask out old data, insert new
pairmand 3:8f2b7f4940b5 307 write_scratchpad ((RAM[2]<<8) + RAM[3]);
pairmand 3:8f2b7f4940b5 308 // store_scratchpad (DS1820::this_device); // Need to test if this is required
pairmand 3:8f2b7f4940b5 309 answer = true;
pairmand 3:8f2b7f4940b5 310 }
pairmand 3:8f2b7f4940b5 311 return answer;
pairmand 3:8f2b7f4940b5 312 }
pairmand 3:8f2b7f4940b5 313
pairmand 3:8f2b7f4940b5 314 void DS1820::write_scratchpad(int data) {
pairmand 3:8f2b7f4940b5 315 RAM[3] = data;
pairmand 3:8f2b7f4940b5 316 RAM[2] = data>>8;
pairmand 3:8f2b7f4940b5 317 match_ROM();
pairmand 3:8f2b7f4940b5 318 onewire_byte_out(0x4E); // Copy scratchpad into DS1820 ram memory
pairmand 3:8f2b7f4940b5 319 onewire_byte_out(RAM[2]); // T(H)
pairmand 3:8f2b7f4940b5 320 onewire_byte_out(RAM[3]); // T(L)
Sissors 5:2cd4928e8147 321 if ((FAMILY_CODE == FAMILY_CODE_DS18B20 ) || (FAMILY_CODE == FAMILY_CODE_DS1822 )) {
pairmand 3:8f2b7f4940b5 322 onewire_byte_out(RAM[4]); // Configuration register
pairmand 3:8f2b7f4940b5 323 }
pairmand 3:8f2b7f4940b5 324 }
pairmand 3:8f2b7f4940b5 325
pairmand 3:8f2b7f4940b5 326 float DS1820::temperature(char scale) {
pairmand 3:8f2b7f4940b5 327 // The data specs state that count_per_degree should be 0x10 (16), I found my devices
pairmand 3:8f2b7f4940b5 328 // to have a count_per_degree of 0x4B (75). With the standard resolution of 1/2 deg C
pairmand 3:8f2b7f4940b5 329 // this allowed an expanded resolution of 1/150th of a deg C. I wouldn't rely on this
pairmand 3:8f2b7f4940b5 330 // being super acurate, but it does allow for a smooth display in the 1/10ths of a
pairmand 3:8f2b7f4940b5 331 // deg C or F scales.
pairmand 3:8f2b7f4940b5 332 float answer, remaining_count, count_per_degree;
pairmand 3:8f2b7f4940b5 333 int reading;
pairmand 3:8f2b7f4940b5 334 read_RAM();
pairmand 3:8f2b7f4940b5 335 if (RAM_checksum_error())
pairmand 3:8f2b7f4940b5 336 // Indicate we got a CRC error
Sissors 7:58b61681818f 337 answer = invalid_conversion;
pairmand 3:8f2b7f4940b5 338 else {
pairmand 3:8f2b7f4940b5 339 reading = (RAM[1] << 8) + RAM[0];
pairmand 3:8f2b7f4940b5 340 if (reading & 0x8000) { // negative degrees C
pairmand 3:8f2b7f4940b5 341 reading = 0-((reading ^ 0xffff) + 1); // 2's comp then convert to signed int
pairmand 3:8f2b7f4940b5 342 }
pairmand 3:8f2b7f4940b5 343 answer = reading +0.0; // convert to floating point
Sissors 5:2cd4928e8147 344 if ((FAMILY_CODE == FAMILY_CODE_DS18B20 ) || (FAMILY_CODE == FAMILY_CODE_DS1822 )) {
Sissors 11:1a3c3002b50c 345 answer = answer / 16.0f;
pairmand 3:8f2b7f4940b5 346 }
pairmand 3:8f2b7f4940b5 347 else {
pairmand 3:8f2b7f4940b5 348 remaining_count = RAM[6];
pairmand 3:8f2b7f4940b5 349 count_per_degree = RAM[7];
Sissors 11:1a3c3002b50c 350 answer = floor(answer/2.0f) - 0.25f + (count_per_degree - remaining_count) / count_per_degree;
pairmand 3:8f2b7f4940b5 351 }
florian 10:d297ce9ce422 352 if (scale=='F' or scale=='f')
pairmand 3:8f2b7f4940b5 353 // Convert to deg F
Sissors 11:1a3c3002b50c 354 answer = answer * 9.0f / 5.0f + 32.0f;
pairmand 3:8f2b7f4940b5 355 }
pairmand 3:8f2b7f4940b5 356 return answer;
pairmand 3:8f2b7f4940b5 357 }
pairmand 3:8f2b7f4940b5 358
pairmand 3:8f2b7f4940b5 359