Fork of Erik's DS1820 library working on OS6
Dependents: DS1820-example DS1820mitWebserver DS1820ohneWebserver
DS1820.cpp@1:ce70453af3db, 2020-12-29 (annotated)
- Committer:
- star297
- Date:
- Tue Dec 29 13:11:12 2020 +0000
- Revision:
- 1:ce70453af3db
- Parent:
- 0:a43dcf6ca539
Initial commit.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
star297 | 0:a43dcf6ca539 | 1 | #include "DS1820.h" |
star297 | 0:a43dcf6ca539 | 2 | |
star297 | 0:a43dcf6ca539 | 3 | #ifdef TARGET_STM |
star297 | 0:a43dcf6ca539 | 4 | //STM targets use opendrain mode since their switching between input and output is slow |
star297 | 0:a43dcf6ca539 | 5 | #define ONEWIRE_INPUT(pin) pin->write(1) |
star297 | 0:a43dcf6ca539 | 6 | #define ONEWIRE_OUTPUT(pin) |
star297 | 0:a43dcf6ca539 | 7 | #define ONEWIRE_INIT(pin) pin->output(); pin->mode(OpenDrain) |
star297 | 0:a43dcf6ca539 | 8 | #else |
star297 | 0:a43dcf6ca539 | 9 | #define ONEWIRE_INPUT(pin) pin->input() |
star297 | 0:a43dcf6ca539 | 10 | #define ONEWIRE_OUTPUT(pin) pin->output() |
star297 | 0:a43dcf6ca539 | 11 | #define ONEWIRE_INIT(pin) |
star297 | 0:a43dcf6ca539 | 12 | #endif |
star297 | 0:a43dcf6ca539 | 13 | |
star297 | 0:a43dcf6ca539 | 14 | #ifdef TARGET_NORDIC |
star297 | 0:a43dcf6ca539 | 15 | //NORDIC targets (NRF) use software delays since their ticker uses a 32kHz clock |
star297 | 0:a43dcf6ca539 | 16 | static uint32_t loops_per_us = 0; |
star297 | 0:a43dcf6ca539 | 17 | |
star297 | 0:a43dcf6ca539 | 18 | #define INIT_DELAY init_soft_delay() |
star297 | 0:a43dcf6ca539 | 19 | #define ONEWIRE_DELAY_US(value) for(int cnt = 0; cnt < (value * loops_per_us) >> 5; cnt++) {__NOP(); __NOP(); __NOP();} |
star297 | 0:a43dcf6ca539 | 20 | |
star297 | 0:a43dcf6ca539 | 21 | void init_soft_delay( void ) { |
star297 | 0:a43dcf6ca539 | 22 | if (loops_per_us == 0) { |
star297 | 0:a43dcf6ca539 | 23 | loops_per_us = 1; |
star297 | 0:a43dcf6ca539 | 24 | Timer timey; |
star297 | 0:a43dcf6ca539 | 25 | timey.start(); |
star297 | 0:a43dcf6ca539 | 26 | ONEWIRE_DELAY_US(320000); |
star297 | 0:a43dcf6ca539 | 27 | timey.stop(); |
star297 | 0:a43dcf6ca539 | 28 | loops_per_us = (320000 + timey.read_us() / 2) / timey.read_us(); |
star297 | 0:a43dcf6ca539 | 29 | } |
star297 | 0:a43dcf6ca539 | 30 | } |
star297 | 0:a43dcf6ca539 | 31 | #else |
star297 | 0:a43dcf6ca539 | 32 | #define INIT_DELAY |
star297 | 0:a43dcf6ca539 | 33 | #define ONEWIRE_DELAY_US(value) wait_us(value) |
star297 | 0:a43dcf6ca539 | 34 | #endif |
star297 | 0:a43dcf6ca539 | 35 | |
star297 | 0:a43dcf6ca539 | 36 | LinkedList2<node> DS1820::probes; |
star297 | 0:a43dcf6ca539 | 37 | |
star297 | 0:a43dcf6ca539 | 38 | |
star297 | 0:a43dcf6ca539 | 39 | DS1820::DS1820 (PinName data_pin, PinName power_pin, bool power_polarity) : _datapin(data_pin), _parasitepin(power_pin) { |
star297 | 0:a43dcf6ca539 | 40 | int byte_counter; |
star297 | 0:a43dcf6ca539 | 41 | _power_polarity = power_polarity; |
star297 | 0:a43dcf6ca539 | 42 | |
star297 | 0:a43dcf6ca539 | 43 | _power_mosfet = power_pin != NC; |
star297 | 0:a43dcf6ca539 | 44 | |
star297 | 0:a43dcf6ca539 | 45 | for(byte_counter=0;byte_counter<9;byte_counter++) |
star297 | 0:a43dcf6ca539 | 46 | RAM[byte_counter] = 0x00; |
star297 | 0:a43dcf6ca539 | 47 | |
star297 | 0:a43dcf6ca539 | 48 | ONEWIRE_INIT((&_datapin)); |
star297 | 0:a43dcf6ca539 | 49 | INIT_DELAY; |
star297 | 0:a43dcf6ca539 | 50 | |
star297 | 0:a43dcf6ca539 | 51 | if (!unassignedProbe(&_datapin, _ROM)) |
star297 | 0:a43dcf6ca539 | 52 | error("No unassigned DS1820 found!\n"); |
star297 | 0:a43dcf6ca539 | 53 | else { |
star297 | 0:a43dcf6ca539 | 54 | _datapin.input(); |
star297 | 0:a43dcf6ca539 | 55 | probes.append(this); |
star297 | 0:a43dcf6ca539 | 56 | _parasite_power = !read_power_supply(); |
star297 | 0:a43dcf6ca539 | 57 | } |
star297 | 0:a43dcf6ca539 | 58 | } |
star297 | 0:a43dcf6ca539 | 59 | |
star297 | 0:a43dcf6ca539 | 60 | DS1820::~DS1820 (void) { |
star297 | 0:a43dcf6ca539 | 61 | node *tmp; |
star297 | 0:a43dcf6ca539 | 62 | for(int i=1; i<=probes.length(); i++) |
star297 | 0:a43dcf6ca539 | 63 | { |
star297 | 0:a43dcf6ca539 | 64 | tmp = probes.pop(i); |
star297 | 0:a43dcf6ca539 | 65 | if (tmp->data == this) |
star297 | 0:a43dcf6ca539 | 66 | probes.remove(i); |
star297 | 0:a43dcf6ca539 | 67 | } |
star297 | 0:a43dcf6ca539 | 68 | } |
star297 | 0:a43dcf6ca539 | 69 | |
star297 | 0:a43dcf6ca539 | 70 | bool DS1820::onewire_reset(DigitalInOut *pin) { |
star297 | 0:a43dcf6ca539 | 71 | // This will return false if no devices are present on the data bus |
star297 | 0:a43dcf6ca539 | 72 | bool presence=false; |
star297 | 0:a43dcf6ca539 | 73 | ONEWIRE_OUTPUT(pin); |
star297 | 0:a43dcf6ca539 | 74 | pin->write(0); // bring low for 500 us |
star297 | 0:a43dcf6ca539 | 75 | ONEWIRE_DELAY_US(500); |
star297 | 0:a43dcf6ca539 | 76 | ONEWIRE_INPUT(pin); // let the data line float high |
star297 | 0:a43dcf6ca539 | 77 | ONEWIRE_DELAY_US(90); // wait 90us |
star297 | 0:a43dcf6ca539 | 78 | if (pin->read()==0) // see if any devices are pulling the data line low |
star297 | 0:a43dcf6ca539 | 79 | presence=true; |
star297 | 0:a43dcf6ca539 | 80 | ONEWIRE_DELAY_US(410); |
star297 | 0:a43dcf6ca539 | 81 | return presence; |
star297 | 0:a43dcf6ca539 | 82 | } |
star297 | 0:a43dcf6ca539 | 83 | |
star297 | 0:a43dcf6ca539 | 84 | void DS1820::onewire_bit_out (DigitalInOut *pin, bool bit_data) { |
star297 | 0:a43dcf6ca539 | 85 | ONEWIRE_OUTPUT(pin); |
star297 | 0:a43dcf6ca539 | 86 | pin->write(0); |
star297 | 0:a43dcf6ca539 | 87 | ONEWIRE_DELAY_US(3); // DXP modified from 5 |
star297 | 0:a43dcf6ca539 | 88 | if (bit_data) { |
star297 | 0:a43dcf6ca539 | 89 | pin->write(1); // bring data line high |
star297 | 0:a43dcf6ca539 | 90 | ONEWIRE_DELAY_US(55); |
star297 | 0:a43dcf6ca539 | 91 | } else { |
star297 | 0:a43dcf6ca539 | 92 | ONEWIRE_DELAY_US(55); // keep data line low |
star297 | 0:a43dcf6ca539 | 93 | pin->write(1); |
star297 | 0:a43dcf6ca539 | 94 | ONEWIRE_DELAY_US(10); // DXP added to allow bus to float high before next bit_out |
star297 | 0:a43dcf6ca539 | 95 | } |
star297 | 0:a43dcf6ca539 | 96 | } |
star297 | 0:a43dcf6ca539 | 97 | |
star297 | 0:a43dcf6ca539 | 98 | void DS1820::onewire_byte_out(char data) { // output data character (least sig bit first). |
star297 | 0:a43dcf6ca539 | 99 | int n; |
star297 | 0:a43dcf6ca539 | 100 | for (n=0; n<8; n++) { |
star297 | 0:a43dcf6ca539 | 101 | onewire_bit_out(&this->_datapin, data & 0x01); |
star297 | 0:a43dcf6ca539 | 102 | data = data >> 1; // now the next bit is in the least sig bit position. |
star297 | 0:a43dcf6ca539 | 103 | } |
star297 | 0:a43dcf6ca539 | 104 | } |
star297 | 0:a43dcf6ca539 | 105 | |
star297 | 0:a43dcf6ca539 | 106 | bool DS1820::onewire_bit_in(DigitalInOut *pin) { |
star297 | 0:a43dcf6ca539 | 107 | bool answer; |
star297 | 0:a43dcf6ca539 | 108 | ONEWIRE_OUTPUT(pin); |
star297 | 0:a43dcf6ca539 | 109 | pin->write(0); |
star297 | 0:a43dcf6ca539 | 110 | ONEWIRE_DELAY_US(3); // DXP modofied from 5 |
star297 | 0:a43dcf6ca539 | 111 | ONEWIRE_INPUT(pin); |
star297 | 0:a43dcf6ca539 | 112 | ONEWIRE_DELAY_US(6); // DXP modified from 5 |
star297 | 0:a43dcf6ca539 | 113 | answer = pin->read(); |
star297 | 0:a43dcf6ca539 | 114 | ONEWIRE_DELAY_US(45); // DXP modified from 50 |
star297 | 0:a43dcf6ca539 | 115 | return answer; |
star297 | 0:a43dcf6ca539 | 116 | } |
star297 | 0:a43dcf6ca539 | 117 | |
star297 | 0:a43dcf6ca539 | 118 | char DS1820::onewire_byte_in() { // read byte, least sig byte first |
star297 | 0:a43dcf6ca539 | 119 | char answer = 0x00; |
star297 | 0:a43dcf6ca539 | 120 | int i; |
star297 | 0:a43dcf6ca539 | 121 | for (i=0; i<8; i++) { |
star297 | 0:a43dcf6ca539 | 122 | answer = answer >> 1; // shift over to make room for the next bit |
star297 | 0:a43dcf6ca539 | 123 | if (onewire_bit_in(&this->_datapin)) |
star297 | 0:a43dcf6ca539 | 124 | answer = answer | 0x80; // if the data port is high, make this bit a 1 |
star297 | 0:a43dcf6ca539 | 125 | } |
star297 | 0:a43dcf6ca539 | 126 | return answer; |
star297 | 0:a43dcf6ca539 | 127 | } |
star297 | 0:a43dcf6ca539 | 128 | |
star297 | 0:a43dcf6ca539 | 129 | bool DS1820::unassignedProbe(PinName pin) { |
star297 | 0:a43dcf6ca539 | 130 | DigitalInOut _pin(pin); |
star297 | 0:a43dcf6ca539 | 131 | ONEWIRE_INIT((&_pin)); |
star297 | 0:a43dcf6ca539 | 132 | INIT_DELAY; |
star297 | 0:a43dcf6ca539 | 133 | char ROM_address[8]; |
star297 | 0:a43dcf6ca539 | 134 | return search_ROM_routine(&_pin, 0xF0, ROM_address); |
star297 | 0:a43dcf6ca539 | 135 | } |
star297 | 0:a43dcf6ca539 | 136 | |
star297 | 0:a43dcf6ca539 | 137 | bool DS1820::unassignedProbe(DigitalInOut *pin, char *ROM_address) { |
star297 | 0:a43dcf6ca539 | 138 | return search_ROM_routine(pin, 0xF0, ROM_address); |
star297 | 0:a43dcf6ca539 | 139 | } |
star297 | 0:a43dcf6ca539 | 140 | |
star297 | 0:a43dcf6ca539 | 141 | bool DS1820::search_ROM_routine(DigitalInOut *pin, char command, char *ROM_address) { |
star297 | 0:a43dcf6ca539 | 142 | bool DS1820_done_flag = false; |
star297 | 0:a43dcf6ca539 | 143 | int DS1820_last_descrepancy = 0; |
star297 | 0:a43dcf6ca539 | 144 | char DS1820_search_ROM[8] = {0, 0, 0, 0, 0, 0, 0, 0}; |
star297 | 0:a43dcf6ca539 | 145 | |
star297 | 0:a43dcf6ca539 | 146 | int descrepancy_marker, ROM_bit_index; |
star297 | 0:a43dcf6ca539 | 147 | bool return_value, Bit_A, Bit_B; |
star297 | 0:a43dcf6ca539 | 148 | char byte_counter, bit_mask; |
star297 | 0:a43dcf6ca539 | 149 | |
star297 | 0:a43dcf6ca539 | 150 | return_value=false; |
star297 | 0:a43dcf6ca539 | 151 | while (!DS1820_done_flag) { |
star297 | 0:a43dcf6ca539 | 152 | if (!onewire_reset(pin)) { |
star297 | 0:a43dcf6ca539 | 153 | return false; |
star297 | 0:a43dcf6ca539 | 154 | } else { |
star297 | 0:a43dcf6ca539 | 155 | ROM_bit_index=1; |
star297 | 0:a43dcf6ca539 | 156 | descrepancy_marker=0; |
star297 | 0:a43dcf6ca539 | 157 | char command_shift = command; |
star297 | 0:a43dcf6ca539 | 158 | for (int n=0; n<8; n++) { // Search ROM command or Search Alarm command |
star297 | 0:a43dcf6ca539 | 159 | onewire_bit_out(pin, command_shift & 0x01); |
star297 | 0:a43dcf6ca539 | 160 | command_shift = command_shift >> 1; // now the next bit is in the least sig bit position. |
star297 | 0:a43dcf6ca539 | 161 | } |
star297 | 0:a43dcf6ca539 | 162 | byte_counter = 0; |
star297 | 0:a43dcf6ca539 | 163 | bit_mask = 0x01; |
star297 | 0:a43dcf6ca539 | 164 | while (ROM_bit_index<=64) { |
star297 | 0:a43dcf6ca539 | 165 | Bit_A = onewire_bit_in(pin); |
star297 | 0:a43dcf6ca539 | 166 | Bit_B = onewire_bit_in(pin); |
star297 | 0:a43dcf6ca539 | 167 | if (Bit_A & Bit_B) { |
star297 | 0:a43dcf6ca539 | 168 | descrepancy_marker = 0; // data read error, this should never happen |
star297 | 0:a43dcf6ca539 | 169 | ROM_bit_index = 0xFF; |
star297 | 0:a43dcf6ca539 | 170 | } else { |
star297 | 0:a43dcf6ca539 | 171 | if (Bit_A | Bit_B) { |
star297 | 0:a43dcf6ca539 | 172 | // Set ROM bit to Bit_A |
star297 | 0:a43dcf6ca539 | 173 | if (Bit_A) { |
star297 | 0:a43dcf6ca539 | 174 | DS1820_search_ROM[byte_counter] = DS1820_search_ROM[byte_counter] | bit_mask; // Set ROM bit to one |
star297 | 0:a43dcf6ca539 | 175 | } else { |
star297 | 0:a43dcf6ca539 | 176 | DS1820_search_ROM[byte_counter] = DS1820_search_ROM[byte_counter] & ~bit_mask; // Set ROM bit to zero |
star297 | 0:a43dcf6ca539 | 177 | } |
star297 | 0:a43dcf6ca539 | 178 | } else { |
star297 | 0:a43dcf6ca539 | 179 | // both bits A and B are low, so there are two or more devices present |
star297 | 0:a43dcf6ca539 | 180 | if ( ROM_bit_index == DS1820_last_descrepancy ) { |
star297 | 0:a43dcf6ca539 | 181 | DS1820_search_ROM[byte_counter] = DS1820_search_ROM[byte_counter] | bit_mask; // Set ROM bit to one |
star297 | 0:a43dcf6ca539 | 182 | } else { |
star297 | 0:a43dcf6ca539 | 183 | if ( ROM_bit_index > DS1820_last_descrepancy ) { |
star297 | 0:a43dcf6ca539 | 184 | DS1820_search_ROM[byte_counter] = DS1820_search_ROM[byte_counter] & ~bit_mask; // Set ROM bit to zero |
star297 | 0:a43dcf6ca539 | 185 | descrepancy_marker = ROM_bit_index; |
star297 | 0:a43dcf6ca539 | 186 | } else { |
star297 | 0:a43dcf6ca539 | 187 | if (( DS1820_search_ROM[byte_counter] & bit_mask) == 0x00 ) |
star297 | 0:a43dcf6ca539 | 188 | descrepancy_marker = ROM_bit_index; |
star297 | 0:a43dcf6ca539 | 189 | } |
star297 | 0:a43dcf6ca539 | 190 | } |
star297 | 0:a43dcf6ca539 | 191 | } |
star297 | 0:a43dcf6ca539 | 192 | onewire_bit_out (pin, DS1820_search_ROM[byte_counter] & bit_mask); |
star297 | 0:a43dcf6ca539 | 193 | ROM_bit_index++; |
star297 | 0:a43dcf6ca539 | 194 | if (bit_mask & 0x80) { |
star297 | 0:a43dcf6ca539 | 195 | byte_counter++; |
star297 | 0:a43dcf6ca539 | 196 | bit_mask = 0x01; |
star297 | 0:a43dcf6ca539 | 197 | } else { |
star297 | 0:a43dcf6ca539 | 198 | bit_mask = bit_mask << 1; |
star297 | 0:a43dcf6ca539 | 199 | } |
star297 | 0:a43dcf6ca539 | 200 | } |
star297 | 0:a43dcf6ca539 | 201 | } |
star297 | 0:a43dcf6ca539 | 202 | DS1820_last_descrepancy = descrepancy_marker; |
star297 | 0:a43dcf6ca539 | 203 | if (ROM_bit_index != 0xFF) { |
star297 | 0:a43dcf6ca539 | 204 | int i = 1; |
star297 | 0:a43dcf6ca539 | 205 | node *list_container; |
star297 | 0:a43dcf6ca539 | 206 | while(1) { |
star297 | 0:a43dcf6ca539 | 207 | list_container = probes.pop(i); |
star297 | 0:a43dcf6ca539 | 208 | if (list_container == NULL) { //End of list, or empty list |
star297 | 0:a43dcf6ca539 | 209 | if (ROM_checksum_error(DS1820_search_ROM)) { // Check the CRC |
star297 | 0:a43dcf6ca539 | 210 | return false; |
star297 | 0:a43dcf6ca539 | 211 | } |
star297 | 0:a43dcf6ca539 | 212 | for(byte_counter=0;byte_counter<8;byte_counter++) |
star297 | 0:a43dcf6ca539 | 213 | ROM_address[byte_counter] = DS1820_search_ROM[byte_counter]; |
star297 | 0:a43dcf6ca539 | 214 | return true; |
star297 | 0:a43dcf6ca539 | 215 | } else { //Otherwise, check if ROM is already known |
star297 | 0:a43dcf6ca539 | 216 | bool equal = true; |
star297 | 0:a43dcf6ca539 | 217 | DS1820 *pointer = (DS1820*) list_container->data; |
star297 | 0:a43dcf6ca539 | 218 | char *ROM_compare = pointer->_ROM; |
star297 | 0:a43dcf6ca539 | 219 | |
star297 | 0:a43dcf6ca539 | 220 | for(byte_counter=0;byte_counter<8;byte_counter++) { |
star297 | 0:a43dcf6ca539 | 221 | if ( ROM_compare[byte_counter] != DS1820_search_ROM[byte_counter]) |
star297 | 0:a43dcf6ca539 | 222 | equal = false; |
star297 | 0:a43dcf6ca539 | 223 | } |
star297 | 0:a43dcf6ca539 | 224 | if (equal) |
star297 | 0:a43dcf6ca539 | 225 | break; |
star297 | 0:a43dcf6ca539 | 226 | else |
star297 | 0:a43dcf6ca539 | 227 | i++; |
star297 | 0:a43dcf6ca539 | 228 | } |
star297 | 0:a43dcf6ca539 | 229 | } |
star297 | 0:a43dcf6ca539 | 230 | } |
star297 | 0:a43dcf6ca539 | 231 | } |
star297 | 0:a43dcf6ca539 | 232 | if (DS1820_last_descrepancy == 0) |
star297 | 0:a43dcf6ca539 | 233 | DS1820_done_flag = true; |
star297 | 0:a43dcf6ca539 | 234 | } |
star297 | 0:a43dcf6ca539 | 235 | return return_value; |
star297 | 0:a43dcf6ca539 | 236 | } |
star297 | 0:a43dcf6ca539 | 237 | |
star297 | 0:a43dcf6ca539 | 238 | void DS1820::match_ROM() { |
star297 | 0:a43dcf6ca539 | 239 | // Used to select a specific device |
star297 | 0:a43dcf6ca539 | 240 | int i; |
star297 | 0:a43dcf6ca539 | 241 | onewire_reset(&this->_datapin); |
star297 | 0:a43dcf6ca539 | 242 | onewire_byte_out( 0x55); //Match ROM command |
star297 | 0:a43dcf6ca539 | 243 | for (i=0;i<8;i++) { |
star297 | 0:a43dcf6ca539 | 244 | onewire_byte_out(_ROM[i]); |
star297 | 0:a43dcf6ca539 | 245 | } |
star297 | 0:a43dcf6ca539 | 246 | } |
star297 | 0:a43dcf6ca539 | 247 | |
star297 | 0:a43dcf6ca539 | 248 | void DS1820::skip_ROM() { |
star297 | 0:a43dcf6ca539 | 249 | onewire_reset(&this->_datapin); |
star297 | 0:a43dcf6ca539 | 250 | onewire_byte_out(0xCC); // Skip ROM command |
star297 | 0:a43dcf6ca539 | 251 | } |
star297 | 0:a43dcf6ca539 | 252 | |
star297 | 0:a43dcf6ca539 | 253 | bool DS1820::ROM_checksum_error(char *_ROM_address) { |
star297 | 0:a43dcf6ca539 | 254 | char _CRC=0x00; |
star297 | 0:a43dcf6ca539 | 255 | int i; |
star297 | 0:a43dcf6ca539 | 256 | for(i=0;i<7;i++) // Only going to shift the lower 7 bytes |
star297 | 0:a43dcf6ca539 | 257 | _CRC = CRC_byte(_CRC, _ROM_address[i]); |
star297 | 0:a43dcf6ca539 | 258 | // After 7 bytes CRC should equal the 8th byte (ROM CRC) |
star297 | 0:a43dcf6ca539 | 259 | return (_CRC!=_ROM_address[7]); // will return true if there is a CRC checksum mis-match |
star297 | 0:a43dcf6ca539 | 260 | } |
star297 | 0:a43dcf6ca539 | 261 | |
star297 | 0:a43dcf6ca539 | 262 | bool DS1820::RAM_checksum_error() { |
star297 | 0:a43dcf6ca539 | 263 | char _CRC=0x00; |
star297 | 0:a43dcf6ca539 | 264 | int i; |
star297 | 0:a43dcf6ca539 | 265 | for(i=0;i<8;i++) // Only going to shift the lower 8 bytes |
star297 | 0:a43dcf6ca539 | 266 | _CRC = CRC_byte(_CRC, RAM[i]); |
star297 | 0:a43dcf6ca539 | 267 | // After 8 bytes CRC should equal the 9th byte (RAM CRC) |
star297 | 0:a43dcf6ca539 | 268 | return (_CRC!=RAM[8]); // will return true if there is a CRC checksum mis-match |
star297 | 0:a43dcf6ca539 | 269 | } |
star297 | 0:a43dcf6ca539 | 270 | |
star297 | 0:a43dcf6ca539 | 271 | char DS1820::CRC_byte (char _CRC, char byte ) { |
star297 | 0:a43dcf6ca539 | 272 | int j; |
star297 | 0:a43dcf6ca539 | 273 | for(j=0;j<8;j++) { |
star297 | 0:a43dcf6ca539 | 274 | if ((byte & 0x01 ) ^ (_CRC & 0x01)) { |
star297 | 0:a43dcf6ca539 | 275 | // DATA ^ LSB CRC = 1 |
star297 | 0:a43dcf6ca539 | 276 | _CRC = _CRC>>1; |
star297 | 0:a43dcf6ca539 | 277 | // Set the MSB to 1 |
star297 | 0:a43dcf6ca539 | 278 | _CRC = _CRC | 0x80; |
star297 | 0:a43dcf6ca539 | 279 | // Check bit 3 |
star297 | 0:a43dcf6ca539 | 280 | if (_CRC & 0x04) { |
star297 | 0:a43dcf6ca539 | 281 | _CRC = _CRC & 0xFB; // Bit 3 is set, so clear it |
star297 | 0:a43dcf6ca539 | 282 | } else { |
star297 | 0:a43dcf6ca539 | 283 | _CRC = _CRC | 0x04; // Bit 3 is clear, so set it |
star297 | 0:a43dcf6ca539 | 284 | } |
star297 | 0:a43dcf6ca539 | 285 | // Check bit 4 |
star297 | 0:a43dcf6ca539 | 286 | if (_CRC & 0x08) { |
star297 | 0:a43dcf6ca539 | 287 | _CRC = _CRC & 0xF7; // Bit 4 is set, so clear it |
star297 | 0:a43dcf6ca539 | 288 | } else { |
star297 | 0:a43dcf6ca539 | 289 | _CRC = _CRC | 0x08; // Bit 4 is clear, so set it |
star297 | 0:a43dcf6ca539 | 290 | } |
star297 | 0:a43dcf6ca539 | 291 | } else { |
star297 | 0:a43dcf6ca539 | 292 | // DATA ^ LSB CRC = 0 |
star297 | 0:a43dcf6ca539 | 293 | _CRC = _CRC>>1; |
star297 | 0:a43dcf6ca539 | 294 | // clear MSB |
star297 | 0:a43dcf6ca539 | 295 | _CRC = _CRC & 0x7F; |
star297 | 0:a43dcf6ca539 | 296 | // No need to check bits, with DATA ^ LSB CRC = 0, they will remain unchanged |
star297 | 0:a43dcf6ca539 | 297 | } |
star297 | 0:a43dcf6ca539 | 298 | byte = byte>>1; |
star297 | 0:a43dcf6ca539 | 299 | } |
star297 | 0:a43dcf6ca539 | 300 | return _CRC; |
star297 | 0:a43dcf6ca539 | 301 | } |
star297 | 0:a43dcf6ca539 | 302 | |
star297 | 0:a43dcf6ca539 | 303 | int DS1820::convertTemperature(bool wait, devices device) { |
star297 | 0:a43dcf6ca539 | 304 | // Convert temperature into scratchpad RAM for all devices at once |
star297 | 0:a43dcf6ca539 | 305 | int delay_time = 750000; // Default delay time |
star297 | 0:a43dcf6ca539 | 306 | char resolution; |
star297 | 0:a43dcf6ca539 | 307 | if (device==all_devices) |
star297 | 0:a43dcf6ca539 | 308 | skip_ROM(); // Skip ROM command, will convert for ALL devices |
star297 | 0:a43dcf6ca539 | 309 | else { |
star297 | 0:a43dcf6ca539 | 310 | match_ROM(); |
star297 | 0:a43dcf6ca539 | 311 | if ((FAMILY_CODE == FAMILY_CODE_DS18B20 ) || (FAMILY_CODE == FAMILY_CODE_DS1822 )) { |
star297 | 0:a43dcf6ca539 | 312 | resolution = RAM[4] & 0x60; |
star297 | 0:a43dcf6ca539 | 313 | if (resolution == 0x00) // 9 bits |
star297 | 0:a43dcf6ca539 | 314 | delay_time = 94000; |
star297 | 0:a43dcf6ca539 | 315 | if (resolution == 0x20) // 10 bits |
star297 | 0:a43dcf6ca539 | 316 | delay_time = 188000; |
star297 | 0:a43dcf6ca539 | 317 | if (resolution == 0x40) // 11 bits. Note 12bits uses the 750ms default |
star297 | 0:a43dcf6ca539 | 318 | delay_time = 375000; |
star297 | 0:a43dcf6ca539 | 319 | } |
star297 | 0:a43dcf6ca539 | 320 | } |
star297 | 0:a43dcf6ca539 | 321 | |
star297 | 0:a43dcf6ca539 | 322 | onewire_byte_out( 0x44); // perform temperature conversion |
star297 | 0:a43dcf6ca539 | 323 | if (_parasite_power) { |
star297 | 0:a43dcf6ca539 | 324 | if (_power_mosfet) { |
star297 | 0:a43dcf6ca539 | 325 | _parasitepin = _power_polarity; // Parasite power strong pullup |
star297 | 0:a43dcf6ca539 | 326 | wait_us(delay_time); |
star297 | 0:a43dcf6ca539 | 327 | _parasitepin = !_power_polarity; |
star297 | 0:a43dcf6ca539 | 328 | delay_time = 0; |
star297 | 0:a43dcf6ca539 | 329 | } else { |
star297 | 0:a43dcf6ca539 | 330 | _datapin.output(); |
star297 | 0:a43dcf6ca539 | 331 | _datapin.write(1); |
star297 | 0:a43dcf6ca539 | 332 | wait_us(delay_time); |
star297 | 0:a43dcf6ca539 | 333 | _datapin.input(); |
star297 | 0:a43dcf6ca539 | 334 | } |
star297 | 0:a43dcf6ca539 | 335 | } else { |
star297 | 0:a43dcf6ca539 | 336 | if (wait) { |
star297 | 0:a43dcf6ca539 | 337 | wait_us(delay_time); |
star297 | 0:a43dcf6ca539 | 338 | delay_time = 0; |
star297 | 0:a43dcf6ca539 | 339 | } |
star297 | 0:a43dcf6ca539 | 340 | } |
star297 | 0:a43dcf6ca539 | 341 | return delay_time; |
star297 | 0:a43dcf6ca539 | 342 | } |
star297 | 0:a43dcf6ca539 | 343 | |
star297 | 0:a43dcf6ca539 | 344 | void DS1820::read_RAM() { |
star297 | 0:a43dcf6ca539 | 345 | // This will copy the DS1820's 9 bytes of RAM data |
star297 | 0:a43dcf6ca539 | 346 | // into the objects RAM array. Functions that use |
star297 | 0:a43dcf6ca539 | 347 | // RAM values will automaticly call this procedure. |
star297 | 0:a43dcf6ca539 | 348 | int i; |
star297 | 0:a43dcf6ca539 | 349 | match_ROM(); // Select this device |
star297 | 0:a43dcf6ca539 | 350 | onewire_byte_out( 0xBE); //Read Scratchpad command |
star297 | 0:a43dcf6ca539 | 351 | for(i=0;i<9;i++) { |
star297 | 0:a43dcf6ca539 | 352 | RAM[i] = onewire_byte_in(); |
star297 | 0:a43dcf6ca539 | 353 | } |
star297 | 0:a43dcf6ca539 | 354 | // if (!RAM_checksum_error()) |
star297 | 0:a43dcf6ca539 | 355 | // crcerr = 1; |
star297 | 0:a43dcf6ca539 | 356 | } |
star297 | 0:a43dcf6ca539 | 357 | |
star297 | 0:a43dcf6ca539 | 358 | bool DS1820::setResolution(unsigned int resolution) { |
star297 | 0:a43dcf6ca539 | 359 | bool answer = false; |
star297 | 0:a43dcf6ca539 | 360 | resolution = resolution - 9; |
star297 | 0:a43dcf6ca539 | 361 | if (resolution < 4) { |
star297 | 0:a43dcf6ca539 | 362 | resolution = resolution<<5; // align the bits |
star297 | 0:a43dcf6ca539 | 363 | RAM[4] = (RAM[4] & 0x60) | resolution; // mask out old data, insert new |
star297 | 0:a43dcf6ca539 | 364 | write_scratchpad ((RAM[2]<<8) + RAM[3]); |
star297 | 0:a43dcf6ca539 | 365 | write_scratchpad (DS1820::this_device); // Need to test if this is required |
star297 | 0:a43dcf6ca539 | 366 | answer = true; |
star297 | 0:a43dcf6ca539 | 367 | } |
star297 | 0:a43dcf6ca539 | 368 | return answer; |
star297 | 0:a43dcf6ca539 | 369 | } |
star297 | 0:a43dcf6ca539 | 370 | |
star297 | 0:a43dcf6ca539 | 371 | void DS1820::write_scratchpad(int data) { |
star297 | 0:a43dcf6ca539 | 372 | RAM[3] = data; |
star297 | 0:a43dcf6ca539 | 373 | RAM[2] = data>>8; |
star297 | 0:a43dcf6ca539 | 374 | match_ROM(); |
star297 | 0:a43dcf6ca539 | 375 | onewire_byte_out(0x4E); // Copy scratchpad into DS1820 ram memory |
star297 | 0:a43dcf6ca539 | 376 | onewire_byte_out(RAM[2]); // T(H) |
star297 | 0:a43dcf6ca539 | 377 | onewire_byte_out(RAM[3]); // T(L) |
star297 | 0:a43dcf6ca539 | 378 | if ((FAMILY_CODE == FAMILY_CODE_DS18B20 ) || (FAMILY_CODE == FAMILY_CODE_DS1822 )) { |
star297 | 0:a43dcf6ca539 | 379 | onewire_byte_out(RAM[4]); // Configuration register |
star297 | 0:a43dcf6ca539 | 380 | } |
star297 | 0:a43dcf6ca539 | 381 | } |
star297 | 0:a43dcf6ca539 | 382 | |
star297 | 0:a43dcf6ca539 | 383 | float DS1820::temperature(char scale) { |
star297 | 0:a43dcf6ca539 | 384 | // The data specs state that count_per_degree should be 0x10 (16), I found my devices |
star297 | 0:a43dcf6ca539 | 385 | // to have a count_per_degree of 0x4B (75). With the standard resolution of 1/2 deg C |
star297 | 0:a43dcf6ca539 | 386 | // this allowed an expanded resolution of 1/150th of a deg C. I wouldn't rely on this |
star297 | 0:a43dcf6ca539 | 387 | // being super acurate, but it does allow for a smooth display in the 1/10ths of a |
star297 | 0:a43dcf6ca539 | 388 | // deg C or F scales. |
star297 | 0:a43dcf6ca539 | 389 | float answer, remaining_count, count_per_degree; |
star297 | 0:a43dcf6ca539 | 390 | int reading; |
star297 | 0:a43dcf6ca539 | 391 | read_RAM(); |
star297 | 0:a43dcf6ca539 | 392 | if (RAM_checksum_error()) |
star297 | 0:a43dcf6ca539 | 393 | // Indicate we got a CRC error |
star297 | 0:a43dcf6ca539 | 394 | answer = invalid_conversion; |
star297 | 0:a43dcf6ca539 | 395 | else { |
star297 | 0:a43dcf6ca539 | 396 | reading = (RAM[1] << 8) + RAM[0]; |
star297 | 0:a43dcf6ca539 | 397 | if (reading & 0x8000) { // negative degrees C |
star297 | 0:a43dcf6ca539 | 398 | reading = 0-((reading ^ 0xffff) + 1); // 2's comp then convert to signed int |
star297 | 0:a43dcf6ca539 | 399 | } |
star297 | 0:a43dcf6ca539 | 400 | answer = reading +0.0; // convert to floating point |
star297 | 0:a43dcf6ca539 | 401 | if ((FAMILY_CODE == FAMILY_CODE_DS18B20 ) || (FAMILY_CODE == FAMILY_CODE_DS1822 )) { |
star297 | 0:a43dcf6ca539 | 402 | answer = answer / 16.0f; |
star297 | 0:a43dcf6ca539 | 403 | } |
star297 | 0:a43dcf6ca539 | 404 | else { |
star297 | 0:a43dcf6ca539 | 405 | remaining_count = RAM[6]; |
star297 | 0:a43dcf6ca539 | 406 | count_per_degree = RAM[7]; |
star297 | 0:a43dcf6ca539 | 407 | answer = floor(answer/2.0f) - 0.25f + (count_per_degree - remaining_count) / count_per_degree; |
star297 | 0:a43dcf6ca539 | 408 | } |
star297 | 0:a43dcf6ca539 | 409 | if (scale=='F' or scale=='f') |
star297 | 0:a43dcf6ca539 | 410 | // Convert to deg F |
star297 | 0:a43dcf6ca539 | 411 | answer = answer * 9.0f / 5.0f + 32.0f; |
star297 | 0:a43dcf6ca539 | 412 | } |
star297 | 0:a43dcf6ca539 | 413 | return answer; |
star297 | 0:a43dcf6ca539 | 414 | } |
star297 | 0:a43dcf6ca539 | 415 | |
star297 | 0:a43dcf6ca539 | 416 | bool DS1820::read_power_supply(devices device) { |
star297 | 0:a43dcf6ca539 | 417 | // This will return true if the device (or all devices) are Vcc powered |
star297 | 0:a43dcf6ca539 | 418 | // This will return false if the device (or ANY device) is parasite powered |
star297 | 0:a43dcf6ca539 | 419 | if (device==all_devices) |
star297 | 0:a43dcf6ca539 | 420 | skip_ROM(); // Skip ROM command, will poll for any device using parasite power |
star297 | 0:a43dcf6ca539 | 421 | else |
star297 | 0:a43dcf6ca539 | 422 | match_ROM(); |
star297 | 0:a43dcf6ca539 | 423 | onewire_byte_out(0xB4); // Read power supply command |
star297 | 0:a43dcf6ca539 | 424 | return onewire_bit_in(&this->_datapin); |
star297 | 0:a43dcf6ca539 | 425 | } |
star297 | 0:a43dcf6ca539 | 426 |