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