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