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