teste de publish
Fork of DS1820 by
DS1820.cpp@12:196e9e54b033, 2015-02-26 (annotated)
- Committer:
- Sissors
- Date:
- Thu Feb 26 22:20:24 2015 +0000
- Revision:
- 12:196e9e54b033
- Parent:
- 11:1a3c3002b50c
- Child:
- 14:01fb493e5d2f
Fixed issue with multplie probes
;
; The command byte got garbled after first sent, resulting in other runs no probes responding.
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 | |
Sissors | 5:2cd4928e8147 | 3 | LinkedList<node> DS1820::probes; |
pairmand | 3:8f2b7f4940b5 | 4 | |
pairmand | 3:8f2b7f4940b5 | 5 | |
Sissors | 5:2cd4928e8147 | 6 | DS1820::DS1820 (PinName data_pin, PinName power_pin, bool power_polarity) : _datapin(data_pin), _parasitepin(power_pin) { |
pairmand | 3:8f2b7f4940b5 | 7 | int byte_counter; |
Sissors | 5:2cd4928e8147 | 8 | _power_polarity = power_polarity; |
florian | 9:3821ca0b7f14 | 9 | |
florian | 9:3821ca0b7f14 | 10 | _power_mosfet = power_pin != NC; |
Sissors | 5:2cd4928e8147 | 11 | |
pairmand | 3:8f2b7f4940b5 | 12 | for(byte_counter=0;byte_counter<9;byte_counter++) |
pairmand | 3:8f2b7f4940b5 | 13 | RAM[byte_counter] = 0x00; |
Sissors | 5:2cd4928e8147 | 14 | |
Sissors | 5:2cd4928e8147 | 15 | if (!unassignedProbe(&_datapin, _ROM)) |
Sissors | 5:2cd4928e8147 | 16 | error("No unassigned DS1820 found!\n"); |
Sissors | 5:2cd4928e8147 | 17 | else { |
Sissors | 5:2cd4928e8147 | 18 | _datapin.input(); |
Sissors | 5:2cd4928e8147 | 19 | probes.append(this); |
Sissors | 5:2cd4928e8147 | 20 | _parasite_power = !read_power_supply(); |
Sissors | 5:2cd4928e8147 | 21 | } |
pairmand | 3:8f2b7f4940b5 | 22 | } |
Sissors | 5:2cd4928e8147 | 23 | |
Sissors | 5:2cd4928e8147 | 24 | DS1820::~DS1820 (void) { |
Sissors | 5:2cd4928e8147 | 25 | node *tmp; |
Sissors | 5:2cd4928e8147 | 26 | for(int i=1; i<=probes.length(); i++) |
Sissors | 5:2cd4928e8147 | 27 | { |
Sissors | 5:2cd4928e8147 | 28 | tmp = probes.pop(i); |
Sissors | 5:2cd4928e8147 | 29 | if (tmp->data == this) |
Sissors | 5:2cd4928e8147 | 30 | probes.remove(i); |
Sissors | 5:2cd4928e8147 | 31 | } |
pairmand | 3:8f2b7f4940b5 | 32 | } |
Sissors | 5:2cd4928e8147 | 33 | |
pairmand | 3:8f2b7f4940b5 | 34 | |
Sissors | 5:2cd4928e8147 | 35 | bool DS1820::onewire_reset(DigitalInOut *pin) { |
pairmand | 3:8f2b7f4940b5 | 36 | // This will return false if no devices are present on the data bus |
pairmand | 3:8f2b7f4940b5 | 37 | bool presence=false; |
Sissors | 5:2cd4928e8147 | 38 | pin->output(); |
Sissors | 5:2cd4928e8147 | 39 | pin->write(0); // bring low for 500 us |
pairmand | 3:8f2b7f4940b5 | 40 | wait_us(500); |
Sissors | 5:2cd4928e8147 | 41 | pin->input(); // let the data line float high |
pairmand | 3:8f2b7f4940b5 | 42 | wait_us(90); // wait 90us |
Sissors | 5:2cd4928e8147 | 43 | if (pin->read()==0) // see if any devices are pulling the data line low |
pairmand | 3:8f2b7f4940b5 | 44 | presence=true; |
pairmand | 3:8f2b7f4940b5 | 45 | wait_us(410); |
pairmand | 3:8f2b7f4940b5 | 46 | return presence; |
pairmand | 3:8f2b7f4940b5 | 47 | } |
pairmand | 3:8f2b7f4940b5 | 48 | |
Sissors | 5:2cd4928e8147 | 49 | void DS1820::onewire_bit_out (DigitalInOut *pin, bool bit_data) { |
Sissors | 5:2cd4928e8147 | 50 | pin->output(); |
Sissors | 5:2cd4928e8147 | 51 | pin->write(0); |
pairmand | 3:8f2b7f4940b5 | 52 | wait_us(3); // DXP modified from 5 |
pairmand | 3:8f2b7f4940b5 | 53 | if (bit_data) { |
Sissors | 8:d87e11e8d012 | 54 | pin->write(1); // bring data line high |
pairmand | 3:8f2b7f4940b5 | 55 | wait_us(55); |
pairmand | 3:8f2b7f4940b5 | 56 | } else { |
pairmand | 3:8f2b7f4940b5 | 57 | wait_us(55); // keep data line low |
Sissors | 8:d87e11e8d012 | 58 | pin->write(1); |
pairmand | 3:8f2b7f4940b5 | 59 | wait_us(10); // DXP added to allow bus to float high before next bit_out |
pairmand | 3:8f2b7f4940b5 | 60 | } |
pairmand | 3:8f2b7f4940b5 | 61 | } |
pairmand | 3:8f2b7f4940b5 | 62 | |
pairmand | 3:8f2b7f4940b5 | 63 | void DS1820::onewire_byte_out(char data) { // output data character (least sig bit first). |
pairmand | 3:8f2b7f4940b5 | 64 | int n; |
pairmand | 3:8f2b7f4940b5 | 65 | for (n=0; n<8; n++) { |
Sissors | 5:2cd4928e8147 | 66 | onewire_bit_out(&this->_datapin, data & 0x01); |
pairmand | 3:8f2b7f4940b5 | 67 | data = data >> 1; // now the next bit is in the least sig bit position. |
pairmand | 3:8f2b7f4940b5 | 68 | } |
pairmand | 3:8f2b7f4940b5 | 69 | } |
pairmand | 3:8f2b7f4940b5 | 70 | |
Sissors | 5:2cd4928e8147 | 71 | bool DS1820::onewire_bit_in(DigitalInOut *pin) { |
pairmand | 3:8f2b7f4940b5 | 72 | bool answer; |
Sissors | 5:2cd4928e8147 | 73 | pin->output(); |
Sissors | 5:2cd4928e8147 | 74 | pin->write(0); |
pairmand | 3:8f2b7f4940b5 | 75 | wait_us(3); // DXP modofied from 5 |
Sissors | 5:2cd4928e8147 | 76 | pin->input(); |
pairmand | 3:8f2b7f4940b5 | 77 | wait_us(10); // DXP modified from 5 |
Sissors | 5:2cd4928e8147 | 78 | answer = pin->read(); |
pairmand | 3:8f2b7f4940b5 | 79 | wait_us(45); // DXP modified from 50 |
pairmand | 3:8f2b7f4940b5 | 80 | return answer; |
pairmand | 3:8f2b7f4940b5 | 81 | } |
pairmand | 3:8f2b7f4940b5 | 82 | |
pairmand | 3:8f2b7f4940b5 | 83 | char DS1820::onewire_byte_in() { // read byte, least sig byte first |
pairmand | 3:8f2b7f4940b5 | 84 | char answer = 0x00; |
pairmand | 3:8f2b7f4940b5 | 85 | int i; |
pairmand | 3:8f2b7f4940b5 | 86 | for (i=0; i<8; i++) { |
pairmand | 3:8f2b7f4940b5 | 87 | answer = answer >> 1; // shift over to make room for the next bit |
Sissors | 5:2cd4928e8147 | 88 | if (onewire_bit_in(&this->_datapin)) |
pairmand | 3:8f2b7f4940b5 | 89 | answer = answer | 0x80; // if the data port is high, make this bit a 1 |
pairmand | 3:8f2b7f4940b5 | 90 | } |
pairmand | 3:8f2b7f4940b5 | 91 | return answer; |
pairmand | 3:8f2b7f4940b5 | 92 | } |
Sissors | 5:2cd4928e8147 | 93 | |
Sissors | 5:2cd4928e8147 | 94 | bool DS1820::unassignedProbe(PinName pin) { |
Sissors | 5:2cd4928e8147 | 95 | DigitalInOut _pin(pin); |
Sissors | 5:2cd4928e8147 | 96 | char ROM_address[8]; |
Sissors | 5:2cd4928e8147 | 97 | return search_ROM_routine(&_pin, 0xF0, ROM_address); |
pairmand | 3:8f2b7f4940b5 | 98 | } |
pairmand | 3:8f2b7f4940b5 | 99 | |
Sissors | 5:2cd4928e8147 | 100 | bool DS1820::unassignedProbe(DigitalInOut *pin, char *ROM_address) { |
Sissors | 5:2cd4928e8147 | 101 | return search_ROM_routine(pin, 0xF0, ROM_address); |
pairmand | 3:8f2b7f4940b5 | 102 | } |
pairmand | 3:8f2b7f4940b5 | 103 | |
Sissors | 5:2cd4928e8147 | 104 | bool DS1820::search_ROM_routine(DigitalInOut *pin, char command, char *ROM_address) { |
Sissors | 5:2cd4928e8147 | 105 | bool DS1820_done_flag = false; |
Sissors | 5:2cd4928e8147 | 106 | int DS1820_last_descrepancy = 0; |
Sissors | 5:2cd4928e8147 | 107 | char DS1820_search_ROM[8] = {0, 0, 0, 0, 0, 0, 0, 0}; |
Sissors | 5:2cd4928e8147 | 108 | |
pairmand | 3:8f2b7f4940b5 | 109 | int descrepancy_marker, ROM_bit_index; |
pairmand | 3:8f2b7f4940b5 | 110 | bool return_value, Bit_A, Bit_B; |
pairmand | 3:8f2b7f4940b5 | 111 | char byte_counter, bit_mask; |
pairmand | 3:8f2b7f4940b5 | 112 | |
pairmand | 3:8f2b7f4940b5 | 113 | return_value=false; |
Sissors | 5:2cd4928e8147 | 114 | while (!DS1820_done_flag) { |
Sissors | 5:2cd4928e8147 | 115 | if (!onewire_reset(pin)) { |
Sissors | 5:2cd4928e8147 | 116 | return false; |
pairmand | 3:8f2b7f4940b5 | 117 | } else { |
pairmand | 3:8f2b7f4940b5 | 118 | ROM_bit_index=1; |
pairmand | 3:8f2b7f4940b5 | 119 | descrepancy_marker=0; |
Sissors | 12:196e9e54b033 | 120 | char command_shift = command; |
Sissors | 5:2cd4928e8147 | 121 | for (int n=0; n<8; n++) { // Search ROM command or Search Alarm command |
Sissors | 12:196e9e54b033 | 122 | onewire_bit_out(pin, command_shift & 0x01); |
Sissors | 12:196e9e54b033 | 123 | command_shift = command_shift >> 1; // now the next bit is in the least sig bit position. |
Sissors | 5:2cd4928e8147 | 124 | } |
pairmand | 3:8f2b7f4940b5 | 125 | byte_counter = 0; |
pairmand | 3:8f2b7f4940b5 | 126 | bit_mask = 0x01; |
pairmand | 3:8f2b7f4940b5 | 127 | while (ROM_bit_index<=64) { |
Sissors | 5:2cd4928e8147 | 128 | Bit_A = onewire_bit_in(pin); |
Sissors | 5:2cd4928e8147 | 129 | Bit_B = onewire_bit_in(pin); |
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 | } |
Sissors | 5:2cd4928e8147 | 155 | onewire_bit_out (pin, 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 | } |
pairmand | 3:8f2b7f4940b5 | 164 | } |
pairmand | 3:8f2b7f4940b5 | 165 | DS1820_last_descrepancy = descrepancy_marker; |
pairmand | 3:8f2b7f4940b5 | 166 | if (ROM_bit_index != 0xFF) { |
Sissors | 5:2cd4928e8147 | 167 | int i = 1; |
Sissors | 5:2cd4928e8147 | 168 | node *list_container; |
Sissors | 5:2cd4928e8147 | 169 | while(1) { |
Sissors | 5:2cd4928e8147 | 170 | list_container = probes.pop(i); |
Sissors | 5:2cd4928e8147 | 171 | if (list_container == NULL) { //End of list, or empty list |
Sissors | 5:2cd4928e8147 | 172 | if (ROM_checksum_error(DS1820_search_ROM)) { // Check the CRC |
Sissors | 5:2cd4928e8147 | 173 | return false; |
Sissors | 5:2cd4928e8147 | 174 | } |
Sissors | 5:2cd4928e8147 | 175 | for(byte_counter=0;byte_counter<8;byte_counter++) |
Sissors | 5:2cd4928e8147 | 176 | ROM_address[byte_counter] = DS1820_search_ROM[byte_counter]; |
Sissors | 5:2cd4928e8147 | 177 | return true; |
Sissors | 5:2cd4928e8147 | 178 | } else { //Otherwise, check if ROM is already known |
Sissors | 5:2cd4928e8147 | 179 | bool equal = true; |
Sissors | 5:2cd4928e8147 | 180 | DS1820 *pointer = (DS1820*) list_container->data; |
Sissors | 5:2cd4928e8147 | 181 | char *ROM_compare = pointer->_ROM; |
Sissors | 5:2cd4928e8147 | 182 | |
Sissors | 5:2cd4928e8147 | 183 | for(byte_counter=0;byte_counter<8;byte_counter++) { |
Sissors | 5:2cd4928e8147 | 184 | if ( ROM_compare[byte_counter] != DS1820_search_ROM[byte_counter]) |
Sissors | 5:2cd4928e8147 | 185 | equal = false; |
Sissors | 5:2cd4928e8147 | 186 | } |
Sissors | 5:2cd4928e8147 | 187 | if (equal) |
Sissors | 5:2cd4928e8147 | 188 | break; |
Sissors | 5:2cd4928e8147 | 189 | else |
Sissors | 5:2cd4928e8147 | 190 | i++; |
Sissors | 5:2cd4928e8147 | 191 | } |
Sissors | 5:2cd4928e8147 | 192 | } |
pairmand | 3:8f2b7f4940b5 | 193 | } |
pairmand | 3:8f2b7f4940b5 | 194 | } |
pairmand | 3:8f2b7f4940b5 | 195 | if (DS1820_last_descrepancy == 0) |
pairmand | 3:8f2b7f4940b5 | 196 | DS1820_done_flag = true; |
pairmand | 3:8f2b7f4940b5 | 197 | } |
pairmand | 3:8f2b7f4940b5 | 198 | return return_value; |
pairmand | 3:8f2b7f4940b5 | 199 | } |
pairmand | 3:8f2b7f4940b5 | 200 | |
pairmand | 3:8f2b7f4940b5 | 201 | void DS1820::match_ROM() { |
pairmand | 3:8f2b7f4940b5 | 202 | // Used to select a specific device |
pairmand | 3:8f2b7f4940b5 | 203 | int i; |
Sissors | 5:2cd4928e8147 | 204 | onewire_reset(&this->_datapin); |
pairmand | 3:8f2b7f4940b5 | 205 | onewire_byte_out( 0x55); //Match ROM command |
Sissors | 5:2cd4928e8147 | 206 | for (i=0;i<8;i++) { |
Sissors | 5:2cd4928e8147 | 207 | onewire_byte_out(_ROM[i]); |
Sissors | 5:2cd4928e8147 | 208 | } |
pairmand | 3:8f2b7f4940b5 | 209 | } |
pairmand | 3:8f2b7f4940b5 | 210 | |
pairmand | 3:8f2b7f4940b5 | 211 | void DS1820::skip_ROM() { |
Sissors | 5:2cd4928e8147 | 212 | onewire_reset(&this->_datapin); |
pairmand | 3:8f2b7f4940b5 | 213 | onewire_byte_out(0xCC); // Skip ROM command |
pairmand | 3:8f2b7f4940b5 | 214 | } |
pairmand | 3:8f2b7f4940b5 | 215 | |
Sissors | 5:2cd4928e8147 | 216 | bool DS1820::ROM_checksum_error(char *_ROM_address) { |
Sissors | 11:1a3c3002b50c | 217 | char _CRC=0x00; |
pairmand | 3:8f2b7f4940b5 | 218 | int i; |
pairmand | 3:8f2b7f4940b5 | 219 | for(i=0;i<7;i++) // Only going to shift the lower 7 bytes |
Sissors | 11:1a3c3002b50c | 220 | _CRC = CRC_byte(_CRC, _ROM_address[i]); |
pairmand | 3:8f2b7f4940b5 | 221 | // After 7 bytes CRC should equal the 8th byte (ROM CRC) |
Sissors | 11:1a3c3002b50c | 222 | return (_CRC!=_ROM_address[7]); // will return true if there is a CRC checksum mis-match |
pairmand | 3:8f2b7f4940b5 | 223 | } |
pairmand | 3:8f2b7f4940b5 | 224 | |
pairmand | 3:8f2b7f4940b5 | 225 | bool DS1820::RAM_checksum_error() { |
Sissors | 11:1a3c3002b50c | 226 | char _CRC=0x00; |
pairmand | 3:8f2b7f4940b5 | 227 | int i; |
pairmand | 3:8f2b7f4940b5 | 228 | for(i=0;i<8;i++) // Only going to shift the lower 8 bytes |
Sissors | 11:1a3c3002b50c | 229 | _CRC = CRC_byte(_CRC, RAM[i]); |
pairmand | 3:8f2b7f4940b5 | 230 | // After 8 bytes CRC should equal the 9th byte (RAM CRC) |
Sissors | 11:1a3c3002b50c | 231 | return (_CRC!=RAM[8]); // will return true if there is a CRC checksum mis-match |
pairmand | 3:8f2b7f4940b5 | 232 | } |
pairmand | 3:8f2b7f4940b5 | 233 | |
Sissors | 11:1a3c3002b50c | 234 | char DS1820::CRC_byte (char _CRC, char byte ) { |
pairmand | 3:8f2b7f4940b5 | 235 | int j; |
pairmand | 3:8f2b7f4940b5 | 236 | for(j=0;j<8;j++) { |
Sissors | 11:1a3c3002b50c | 237 | if ((byte & 0x01 ) ^ (_CRC & 0x01)) { |
pairmand | 3:8f2b7f4940b5 | 238 | // DATA ^ LSB CRC = 1 |
Sissors | 11:1a3c3002b50c | 239 | _CRC = _CRC>>1; |
pairmand | 3:8f2b7f4940b5 | 240 | // Set the MSB to 1 |
Sissors | 11:1a3c3002b50c | 241 | _CRC = _CRC | 0x80; |
pairmand | 3:8f2b7f4940b5 | 242 | // Check bit 3 |
Sissors | 11:1a3c3002b50c | 243 | if (_CRC & 0x04) { |
Sissors | 11:1a3c3002b50c | 244 | _CRC = _CRC & 0xFB; // Bit 3 is set, so clear it |
pairmand | 3:8f2b7f4940b5 | 245 | } else { |
Sissors | 11:1a3c3002b50c | 246 | _CRC = _CRC | 0x04; // Bit 3 is clear, so set it |
pairmand | 3:8f2b7f4940b5 | 247 | } |
pairmand | 3:8f2b7f4940b5 | 248 | // Check bit 4 |
Sissors | 11:1a3c3002b50c | 249 | if (_CRC & 0x08) { |
Sissors | 11:1a3c3002b50c | 250 | _CRC = _CRC & 0xF7; // Bit 4 is set, so clear it |
pairmand | 3:8f2b7f4940b5 | 251 | } else { |
Sissors | 11:1a3c3002b50c | 252 | _CRC = _CRC | 0x08; // Bit 4 is clear, so set it |
pairmand | 3:8f2b7f4940b5 | 253 | } |
pairmand | 3:8f2b7f4940b5 | 254 | } else { |
pairmand | 3:8f2b7f4940b5 | 255 | // DATA ^ LSB CRC = 0 |
Sissors | 11:1a3c3002b50c | 256 | _CRC = _CRC>>1; |
pairmand | 3:8f2b7f4940b5 | 257 | // clear MSB |
Sissors | 11:1a3c3002b50c | 258 | _CRC = _CRC & 0x7F; |
pairmand | 3:8f2b7f4940b5 | 259 | // No need to check bits, with DATA ^ LSB CRC = 0, they will remain unchanged |
pairmand | 3:8f2b7f4940b5 | 260 | } |
pairmand | 3:8f2b7f4940b5 | 261 | byte = byte>>1; |
pairmand | 3:8f2b7f4940b5 | 262 | } |
Sissors | 11:1a3c3002b50c | 263 | return _CRC; |
pairmand | 3:8f2b7f4940b5 | 264 | } |
pairmand | 3:8f2b7f4940b5 | 265 | |
Sissors | 5:2cd4928e8147 | 266 | int DS1820::convertTemperature(bool wait, devices device) { |
pairmand | 3:8f2b7f4940b5 | 267 | // Convert temperature into scratchpad RAM for all devices at once |
pairmand | 3:8f2b7f4940b5 | 268 | int delay_time = 750; // Default delay time |
pairmand | 3:8f2b7f4940b5 | 269 | char resolution; |
pairmand | 3:8f2b7f4940b5 | 270 | if (device==all_devices) |
pairmand | 3:8f2b7f4940b5 | 271 | skip_ROM(); // Skip ROM command, will convert for ALL devices |
pairmand | 3:8f2b7f4940b5 | 272 | else { |
pairmand | 3:8f2b7f4940b5 | 273 | match_ROM(); |
Sissors | 5:2cd4928e8147 | 274 | if ((FAMILY_CODE == FAMILY_CODE_DS18B20 ) || (FAMILY_CODE == FAMILY_CODE_DS1822 )) { |
pairmand | 3:8f2b7f4940b5 | 275 | resolution = RAM[4] & 0x60; |
pairmand | 3:8f2b7f4940b5 | 276 | if (resolution == 0x00) // 9 bits |
pairmand | 3:8f2b7f4940b5 | 277 | delay_time = 94; |
pairmand | 3:8f2b7f4940b5 | 278 | if (resolution == 0x20) // 10 bits |
pairmand | 3:8f2b7f4940b5 | 279 | delay_time = 188; |
pairmand | 3:8f2b7f4940b5 | 280 | if (resolution == 0x40) // 11 bits. Note 12bits uses the 750ms default |
pairmand | 3:8f2b7f4940b5 | 281 | delay_time = 375; |
pairmand | 3:8f2b7f4940b5 | 282 | } |
pairmand | 3:8f2b7f4940b5 | 283 | } |
Sissors | 5:2cd4928e8147 | 284 | |
pairmand | 3:8f2b7f4940b5 | 285 | onewire_byte_out( 0x44); // perform temperature conversion |
pairmand | 3:8f2b7f4940b5 | 286 | if (_parasite_power) { |
Sissors | 5:2cd4928e8147 | 287 | if (_power_mosfet) { |
Sissors | 5:2cd4928e8147 | 288 | _parasitepin = _power_polarity; // Parasite power strong pullup |
Sissors | 5:2cd4928e8147 | 289 | wait_ms(delay_time); |
Sissors | 5:2cd4928e8147 | 290 | _parasitepin = !_power_polarity; |
Sissors | 5:2cd4928e8147 | 291 | delay_time = 0; |
Sissors | 5:2cd4928e8147 | 292 | } else { |
Sissors | 5:2cd4928e8147 | 293 | _datapin.output(); |
Sissors | 5:2cd4928e8147 | 294 | _datapin.write(1); |
Sissors | 5:2cd4928e8147 | 295 | wait_ms(delay_time); |
Sissors | 5:2cd4928e8147 | 296 | _datapin.input(); |
Sissors | 5:2cd4928e8147 | 297 | } |
pairmand | 3:8f2b7f4940b5 | 298 | } else { |
pairmand | 3:8f2b7f4940b5 | 299 | if (wait) { |
pairmand | 3:8f2b7f4940b5 | 300 | wait_ms(delay_time); |
pairmand | 3:8f2b7f4940b5 | 301 | delay_time = 0; |
pairmand | 3:8f2b7f4940b5 | 302 | } |
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 | bool DS1820::read_power_supply(devices device) { |
pairmand | 3:8f2b7f4940b5 | 380 | // This will return true if the device (or all devices) are Vcc powered |
pairmand | 3:8f2b7f4940b5 | 381 | // This will return false if the device (or ANY device) is parasite powered |
pairmand | 3:8f2b7f4940b5 | 382 | if (device==all_devices) |
pairmand | 3:8f2b7f4940b5 | 383 | skip_ROM(); // Skip ROM command, will poll for any device using parasite power |
pairmand | 3:8f2b7f4940b5 | 384 | else |
pairmand | 3:8f2b7f4940b5 | 385 | match_ROM(); |
pairmand | 3:8f2b7f4940b5 | 386 | onewire_byte_out(0xB4); // Read power supply command |
Sissors | 5:2cd4928e8147 | 387 | return onewire_bit_in(&this->_datapin); |
pairmand | 3:8f2b7f4940b5 | 388 | } |
pairmand | 3:8f2b7f4940b5 | 389 | |
pairmand | 3:8f2b7f4940b5 | 390 |