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