6°A PIO IX TP1. Ejercicio 1. ALMADA, Santiago MAVER, Francisco
Dependencies: LinkedList
DS1820.cpp@8:d87e11e8d012, 2014-07-08 (annotated)
- Committer:
- Sissors
- Date:
- Tue Jul 08 18:55:54 2014 +0000
- Revision:
- 8:d87e11e8d012
- Parent:
- 7:58b61681818f
- Child:
- 9:3821ca0b7f14
Instead of open drain use CMOS output for writing. Not according to standard, but can help for long busses. And helps with parasite powered via data pin (no external mosfet)
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; |
Sissors | 5:2cd4928e8147 | 9 | if (power_pin != NC) |
Sissors | 5:2cd4928e8147 | 10 | _power_mosfet = true; |
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 | 5:2cd4928e8147 | 120 | for (int n=0; n<8; n++) { // Search ROM command or Search Alarm command |
Sissors | 5:2cd4928e8147 | 121 | onewire_bit_out(pin, command & 0x01); |
Sissors | 5:2cd4928e8147 | 122 | command = command >> 1; // now the next bit is in the least sig bit position. |
Sissors | 5:2cd4928e8147 | 123 | } |
pairmand | 3:8f2b7f4940b5 | 124 | byte_counter = 0; |
pairmand | 3:8f2b7f4940b5 | 125 | bit_mask = 0x01; |
pairmand | 3:8f2b7f4940b5 | 126 | while (ROM_bit_index<=64) { |
Sissors | 5:2cd4928e8147 | 127 | Bit_A = onewire_bit_in(pin); |
Sissors | 5:2cd4928e8147 | 128 | Bit_B = onewire_bit_in(pin); |
pairmand | 3:8f2b7f4940b5 | 129 | if (Bit_A & Bit_B) { |
pairmand | 3:8f2b7f4940b5 | 130 | descrepancy_marker = 0; // data read error, this should never happen |
pairmand | 3:8f2b7f4940b5 | 131 | ROM_bit_index = 0xFF; |
pairmand | 3:8f2b7f4940b5 | 132 | } else { |
pairmand | 3:8f2b7f4940b5 | 133 | if (Bit_A | Bit_B) { |
pairmand | 3:8f2b7f4940b5 | 134 | // Set ROM bit to Bit_A |
pairmand | 3:8f2b7f4940b5 | 135 | if (Bit_A) { |
pairmand | 3:8f2b7f4940b5 | 136 | DS1820_search_ROM[byte_counter] = DS1820_search_ROM[byte_counter] | bit_mask; // Set ROM bit to one |
pairmand | 3:8f2b7f4940b5 | 137 | } else { |
pairmand | 3:8f2b7f4940b5 | 138 | DS1820_search_ROM[byte_counter] = DS1820_search_ROM[byte_counter] & ~bit_mask; // Set ROM bit to zero |
pairmand | 3:8f2b7f4940b5 | 139 | } |
pairmand | 3:8f2b7f4940b5 | 140 | } else { |
pairmand | 3:8f2b7f4940b5 | 141 | // both bits A and B are low, so there are two or more devices present |
pairmand | 3:8f2b7f4940b5 | 142 | if ( ROM_bit_index == DS1820_last_descrepancy ) { |
pairmand | 3:8f2b7f4940b5 | 143 | DS1820_search_ROM[byte_counter] = DS1820_search_ROM[byte_counter] | bit_mask; // Set ROM bit to one |
pairmand | 3:8f2b7f4940b5 | 144 | } else { |
pairmand | 3:8f2b7f4940b5 | 145 | if ( ROM_bit_index > DS1820_last_descrepancy ) { |
pairmand | 3:8f2b7f4940b5 | 146 | DS1820_search_ROM[byte_counter] = DS1820_search_ROM[byte_counter] & ~bit_mask; // Set ROM bit to zero |
pairmand | 3:8f2b7f4940b5 | 147 | descrepancy_marker = ROM_bit_index; |
pairmand | 3:8f2b7f4940b5 | 148 | } else { |
pairmand | 3:8f2b7f4940b5 | 149 | if (( DS1820_search_ROM[byte_counter] & bit_mask) == 0x00 ) |
pairmand | 3:8f2b7f4940b5 | 150 | descrepancy_marker = ROM_bit_index; |
pairmand | 3:8f2b7f4940b5 | 151 | } |
pairmand | 3:8f2b7f4940b5 | 152 | } |
pairmand | 3:8f2b7f4940b5 | 153 | } |
Sissors | 5:2cd4928e8147 | 154 | onewire_bit_out (pin, DS1820_search_ROM[byte_counter] & bit_mask); |
pairmand | 3:8f2b7f4940b5 | 155 | ROM_bit_index++; |
pairmand | 3:8f2b7f4940b5 | 156 | if (bit_mask & 0x80) { |
pairmand | 3:8f2b7f4940b5 | 157 | byte_counter++; |
pairmand | 3:8f2b7f4940b5 | 158 | bit_mask = 0x01; |
pairmand | 3:8f2b7f4940b5 | 159 | } else { |
pairmand | 3:8f2b7f4940b5 | 160 | bit_mask = bit_mask << 1; |
pairmand | 3:8f2b7f4940b5 | 161 | } |
pairmand | 3:8f2b7f4940b5 | 162 | } |
pairmand | 3:8f2b7f4940b5 | 163 | } |
pairmand | 3:8f2b7f4940b5 | 164 | DS1820_last_descrepancy = descrepancy_marker; |
pairmand | 3:8f2b7f4940b5 | 165 | if (ROM_bit_index != 0xFF) { |
Sissors | 5:2cd4928e8147 | 166 | int i = 1; |
Sissors | 5:2cd4928e8147 | 167 | node *list_container; |
Sissors | 5:2cd4928e8147 | 168 | while(1) { |
Sissors | 5:2cd4928e8147 | 169 | list_container = probes.pop(i); |
Sissors | 5:2cd4928e8147 | 170 | if (list_container == NULL) { //End of list, or empty list |
Sissors | 5:2cd4928e8147 | 171 | if (ROM_checksum_error(DS1820_search_ROM)) { // Check the CRC |
Sissors | 5:2cd4928e8147 | 172 | return false; |
Sissors | 5:2cd4928e8147 | 173 | } |
Sissors | 5:2cd4928e8147 | 174 | for(byte_counter=0;byte_counter<8;byte_counter++) |
Sissors | 5:2cd4928e8147 | 175 | ROM_address[byte_counter] = DS1820_search_ROM[byte_counter]; |
Sissors | 5:2cd4928e8147 | 176 | return true; |
Sissors | 5:2cd4928e8147 | 177 | } else { //Otherwise, check if ROM is already known |
Sissors | 5:2cd4928e8147 | 178 | bool equal = true; |
Sissors | 5:2cd4928e8147 | 179 | DS1820 *pointer = (DS1820*) list_container->data; |
Sissors | 5:2cd4928e8147 | 180 | char *ROM_compare = pointer->_ROM; |
Sissors | 5:2cd4928e8147 | 181 | |
Sissors | 5:2cd4928e8147 | 182 | for(byte_counter=0;byte_counter<8;byte_counter++) { |
Sissors | 5:2cd4928e8147 | 183 | if ( ROM_compare[byte_counter] != DS1820_search_ROM[byte_counter]) |
Sissors | 5:2cd4928e8147 | 184 | equal = false; |
Sissors | 5:2cd4928e8147 | 185 | } |
Sissors | 5:2cd4928e8147 | 186 | if (equal) |
Sissors | 5:2cd4928e8147 | 187 | break; |
Sissors | 5:2cd4928e8147 | 188 | else |
Sissors | 5:2cd4928e8147 | 189 | i++; |
Sissors | 5:2cd4928e8147 | 190 | } |
Sissors | 5:2cd4928e8147 | 191 | } |
pairmand | 3:8f2b7f4940b5 | 192 | } |
pairmand | 3:8f2b7f4940b5 | 193 | } |
pairmand | 3:8f2b7f4940b5 | 194 | if (DS1820_last_descrepancy == 0) |
pairmand | 3:8f2b7f4940b5 | 195 | DS1820_done_flag = true; |
pairmand | 3:8f2b7f4940b5 | 196 | } |
pairmand | 3:8f2b7f4940b5 | 197 | return return_value; |
pairmand | 3:8f2b7f4940b5 | 198 | } |
pairmand | 3:8f2b7f4940b5 | 199 | |
pairmand | 3:8f2b7f4940b5 | 200 | void DS1820::match_ROM() { |
pairmand | 3:8f2b7f4940b5 | 201 | // Used to select a specific device |
pairmand | 3:8f2b7f4940b5 | 202 | int i; |
Sissors | 5:2cd4928e8147 | 203 | onewire_reset(&this->_datapin); |
pairmand | 3:8f2b7f4940b5 | 204 | onewire_byte_out( 0x55); //Match ROM command |
Sissors | 5:2cd4928e8147 | 205 | for (i=0;i<8;i++) { |
Sissors | 5:2cd4928e8147 | 206 | onewire_byte_out(_ROM[i]); |
Sissors | 5:2cd4928e8147 | 207 | } |
pairmand | 3:8f2b7f4940b5 | 208 | } |
pairmand | 3:8f2b7f4940b5 | 209 | |
pairmand | 3:8f2b7f4940b5 | 210 | void DS1820::skip_ROM() { |
Sissors | 5:2cd4928e8147 | 211 | onewire_reset(&this->_datapin); |
pairmand | 3:8f2b7f4940b5 | 212 | onewire_byte_out(0xCC); // Skip ROM command |
pairmand | 3:8f2b7f4940b5 | 213 | } |
pairmand | 3:8f2b7f4940b5 | 214 | |
Sissors | 5:2cd4928e8147 | 215 | bool DS1820::ROM_checksum_error(char *_ROM_address) { |
pairmand | 3:8f2b7f4940b5 | 216 | char CRC=0x00; |
pairmand | 3:8f2b7f4940b5 | 217 | int i; |
pairmand | 3:8f2b7f4940b5 | 218 | for(i=0;i<7;i++) // Only going to shift the lower 7 bytes |
Sissors | 5:2cd4928e8147 | 219 | CRC = CRC_byte(CRC, _ROM_address[i]); |
pairmand | 3:8f2b7f4940b5 | 220 | // After 7 bytes CRC should equal the 8th byte (ROM CRC) |
Sissors | 5:2cd4928e8147 | 221 | return (CRC!=_ROM_address[7]); // will return true if there is a CRC checksum mis-match |
pairmand | 3:8f2b7f4940b5 | 222 | } |
pairmand | 3:8f2b7f4940b5 | 223 | |
pairmand | 3:8f2b7f4940b5 | 224 | bool DS1820::RAM_checksum_error() { |
pairmand | 3:8f2b7f4940b5 | 225 | char CRC=0x00; |
pairmand | 3:8f2b7f4940b5 | 226 | int i; |
pairmand | 3:8f2b7f4940b5 | 227 | for(i=0;i<8;i++) // Only going to shift the lower 8 bytes |
pairmand | 3:8f2b7f4940b5 | 228 | CRC = CRC_byte(CRC, RAM[i]); |
pairmand | 3:8f2b7f4940b5 | 229 | // After 8 bytes CRC should equal the 9th byte (RAM CRC) |
pairmand | 3:8f2b7f4940b5 | 230 | return (CRC!=RAM[8]); // will return true if there is a CRC checksum mis-match |
pairmand | 3:8f2b7f4940b5 | 231 | } |
pairmand | 3:8f2b7f4940b5 | 232 | |
pairmand | 3:8f2b7f4940b5 | 233 | char DS1820::CRC_byte (char CRC, char byte ) { |
pairmand | 3:8f2b7f4940b5 | 234 | int j; |
pairmand | 3:8f2b7f4940b5 | 235 | for(j=0;j<8;j++) { |
pairmand | 3:8f2b7f4940b5 | 236 | if ((byte & 0x01 ) ^ (CRC & 0x01)) { |
pairmand | 3:8f2b7f4940b5 | 237 | // DATA ^ LSB CRC = 1 |
pairmand | 3:8f2b7f4940b5 | 238 | CRC = CRC>>1; |
pairmand | 3:8f2b7f4940b5 | 239 | // Set the MSB to 1 |
pairmand | 3:8f2b7f4940b5 | 240 | CRC = CRC | 0x80; |
pairmand | 3:8f2b7f4940b5 | 241 | // Check bit 3 |
pairmand | 3:8f2b7f4940b5 | 242 | if (CRC & 0x04) { |
pairmand | 3:8f2b7f4940b5 | 243 | CRC = CRC & 0xFB; // Bit 3 is set, so clear it |
pairmand | 3:8f2b7f4940b5 | 244 | } else { |
pairmand | 3:8f2b7f4940b5 | 245 | CRC = CRC | 0x04; // Bit 3 is clear, so set it |
pairmand | 3:8f2b7f4940b5 | 246 | } |
pairmand | 3:8f2b7f4940b5 | 247 | // Check bit 4 |
pairmand | 3:8f2b7f4940b5 | 248 | if (CRC & 0x08) { |
pairmand | 3:8f2b7f4940b5 | 249 | CRC = CRC & 0xF7; // Bit 4 is set, so clear it |
pairmand | 3:8f2b7f4940b5 | 250 | } else { |
pairmand | 3:8f2b7f4940b5 | 251 | CRC = CRC | 0x08; // Bit 4 is clear, so set it |
pairmand | 3:8f2b7f4940b5 | 252 | } |
pairmand | 3:8f2b7f4940b5 | 253 | } else { |
pairmand | 3:8f2b7f4940b5 | 254 | // DATA ^ LSB CRC = 0 |
pairmand | 3:8f2b7f4940b5 | 255 | CRC = CRC>>1; |
pairmand | 3:8f2b7f4940b5 | 256 | // clear MSB |
pairmand | 3:8f2b7f4940b5 | 257 | CRC = CRC & 0x7F; |
pairmand | 3:8f2b7f4940b5 | 258 | // No need to check bits, with DATA ^ LSB CRC = 0, they will remain unchanged |
pairmand | 3:8f2b7f4940b5 | 259 | } |
pairmand | 3:8f2b7f4940b5 | 260 | byte = byte>>1; |
pairmand | 3:8f2b7f4940b5 | 261 | } |
pairmand | 3:8f2b7f4940b5 | 262 | return CRC; |
pairmand | 3:8f2b7f4940b5 | 263 | } |
pairmand | 3:8f2b7f4940b5 | 264 | |
Sissors | 5:2cd4928e8147 | 265 | int DS1820::convertTemperature(bool wait, devices device) { |
pairmand | 3:8f2b7f4940b5 | 266 | // Convert temperature into scratchpad RAM for all devices at once |
pairmand | 3:8f2b7f4940b5 | 267 | int delay_time = 750; // Default delay time |
pairmand | 3:8f2b7f4940b5 | 268 | char resolution; |
pairmand | 3:8f2b7f4940b5 | 269 | if (device==all_devices) |
pairmand | 3:8f2b7f4940b5 | 270 | skip_ROM(); // Skip ROM command, will convert for ALL devices |
pairmand | 3:8f2b7f4940b5 | 271 | else { |
pairmand | 3:8f2b7f4940b5 | 272 | match_ROM(); |
Sissors | 5:2cd4928e8147 | 273 | if ((FAMILY_CODE == FAMILY_CODE_DS18B20 ) || (FAMILY_CODE == FAMILY_CODE_DS1822 )) { |
pairmand | 3:8f2b7f4940b5 | 274 | resolution = RAM[4] & 0x60; |
pairmand | 3:8f2b7f4940b5 | 275 | if (resolution == 0x00) // 9 bits |
pairmand | 3:8f2b7f4940b5 | 276 | delay_time = 94; |
pairmand | 3:8f2b7f4940b5 | 277 | if (resolution == 0x20) // 10 bits |
pairmand | 3:8f2b7f4940b5 | 278 | delay_time = 188; |
pairmand | 3:8f2b7f4940b5 | 279 | if (resolution == 0x40) // 11 bits. Note 12bits uses the 750ms default |
pairmand | 3:8f2b7f4940b5 | 280 | delay_time = 375; |
pairmand | 3:8f2b7f4940b5 | 281 | } |
pairmand | 3:8f2b7f4940b5 | 282 | } |
Sissors | 5:2cd4928e8147 | 283 | |
pairmand | 3:8f2b7f4940b5 | 284 | onewire_byte_out( 0x44); // perform temperature conversion |
pairmand | 3:8f2b7f4940b5 | 285 | if (_parasite_power) { |
Sissors | 5:2cd4928e8147 | 286 | if (_power_mosfet) { |
Sissors | 5:2cd4928e8147 | 287 | _parasitepin = _power_polarity; // Parasite power strong pullup |
Sissors | 5:2cd4928e8147 | 288 | wait_ms(delay_time); |
Sissors | 5:2cd4928e8147 | 289 | _parasitepin = !_power_polarity; |
Sissors | 5:2cd4928e8147 | 290 | delay_time = 0; |
Sissors | 5:2cd4928e8147 | 291 | } else { |
Sissors | 5:2cd4928e8147 | 292 | _datapin.output(); |
Sissors | 5:2cd4928e8147 | 293 | _datapin.write(1); |
Sissors | 5:2cd4928e8147 | 294 | wait_ms(delay_time); |
Sissors | 5:2cd4928e8147 | 295 | _datapin.input(); |
Sissors | 5:2cd4928e8147 | 296 | } |
pairmand | 3:8f2b7f4940b5 | 297 | } else { |
pairmand | 3:8f2b7f4940b5 | 298 | if (wait) { |
pairmand | 3:8f2b7f4940b5 | 299 | wait_ms(delay_time); |
pairmand | 3:8f2b7f4940b5 | 300 | delay_time = 0; |
pairmand | 3:8f2b7f4940b5 | 301 | } |
pairmand | 3:8f2b7f4940b5 | 302 | } |
pairmand | 3:8f2b7f4940b5 | 303 | return delay_time; |
pairmand | 3:8f2b7f4940b5 | 304 | } |
pairmand | 3:8f2b7f4940b5 | 305 | |
pairmand | 3:8f2b7f4940b5 | 306 | void DS1820::read_RAM() { |
pairmand | 3:8f2b7f4940b5 | 307 | // This will copy the DS1820's 9 bytes of RAM data |
pairmand | 3:8f2b7f4940b5 | 308 | // into the objects RAM array. Functions that use |
pairmand | 3:8f2b7f4940b5 | 309 | // RAM values will automaticly call this procedure. |
pairmand | 3:8f2b7f4940b5 | 310 | int i; |
pairmand | 3:8f2b7f4940b5 | 311 | match_ROM(); // Select this device |
pairmand | 3:8f2b7f4940b5 | 312 | onewire_byte_out( 0xBE); //Read Scratchpad command |
pairmand | 3:8f2b7f4940b5 | 313 | for(i=0;i<9;i++) { |
pairmand | 3:8f2b7f4940b5 | 314 | RAM[i] = onewire_byte_in(); |
pairmand | 3:8f2b7f4940b5 | 315 | } |
pairmand | 3:8f2b7f4940b5 | 316 | // if (!RAM_checksum_error()) |
pairmand | 3:8f2b7f4940b5 | 317 | // crcerr = 1; |
pairmand | 3:8f2b7f4940b5 | 318 | } |
pairmand | 3:8f2b7f4940b5 | 319 | |
Sissors | 5:2cd4928e8147 | 320 | bool DS1820::setResolution(unsigned int resolution) { |
pairmand | 3:8f2b7f4940b5 | 321 | bool answer = false; |
pairmand | 3:8f2b7f4940b5 | 322 | resolution = resolution - 9; |
pairmand | 3:8f2b7f4940b5 | 323 | if (resolution < 4) { |
pairmand | 3:8f2b7f4940b5 | 324 | resolution = resolution<<5; // align the bits |
pairmand | 3:8f2b7f4940b5 | 325 | RAM[4] = (RAM[4] & 0x60) | resolution; // mask out old data, insert new |
pairmand | 3:8f2b7f4940b5 | 326 | write_scratchpad ((RAM[2]<<8) + RAM[3]); |
pairmand | 3:8f2b7f4940b5 | 327 | // store_scratchpad (DS1820::this_device); // Need to test if this is required |
pairmand | 3:8f2b7f4940b5 | 328 | answer = true; |
pairmand | 3:8f2b7f4940b5 | 329 | } |
pairmand | 3:8f2b7f4940b5 | 330 | return answer; |
pairmand | 3:8f2b7f4940b5 | 331 | } |
pairmand | 3:8f2b7f4940b5 | 332 | |
pairmand | 3:8f2b7f4940b5 | 333 | void DS1820::write_scratchpad(int data) { |
pairmand | 3:8f2b7f4940b5 | 334 | RAM[3] = data; |
pairmand | 3:8f2b7f4940b5 | 335 | RAM[2] = data>>8; |
pairmand | 3:8f2b7f4940b5 | 336 | match_ROM(); |
pairmand | 3:8f2b7f4940b5 | 337 | onewire_byte_out(0x4E); // Copy scratchpad into DS1820 ram memory |
pairmand | 3:8f2b7f4940b5 | 338 | onewire_byte_out(RAM[2]); // T(H) |
pairmand | 3:8f2b7f4940b5 | 339 | onewire_byte_out(RAM[3]); // T(L) |
Sissors | 5:2cd4928e8147 | 340 | if ((FAMILY_CODE == FAMILY_CODE_DS18B20 ) || (FAMILY_CODE == FAMILY_CODE_DS1822 )) { |
pairmand | 3:8f2b7f4940b5 | 341 | onewire_byte_out(RAM[4]); // Configuration register |
pairmand | 3:8f2b7f4940b5 | 342 | } |
pairmand | 3:8f2b7f4940b5 | 343 | } |
pairmand | 3:8f2b7f4940b5 | 344 | |
pairmand | 3:8f2b7f4940b5 | 345 | float DS1820::temperature(char scale) { |
pairmand | 3:8f2b7f4940b5 | 346 | // The data specs state that count_per_degree should be 0x10 (16), I found my devices |
pairmand | 3:8f2b7f4940b5 | 347 | // to have a count_per_degree of 0x4B (75). With the standard resolution of 1/2 deg C |
pairmand | 3:8f2b7f4940b5 | 348 | // this allowed an expanded resolution of 1/150th of a deg C. I wouldn't rely on this |
pairmand | 3:8f2b7f4940b5 | 349 | // being super acurate, but it does allow for a smooth display in the 1/10ths of a |
pairmand | 3:8f2b7f4940b5 | 350 | // deg C or F scales. |
pairmand | 3:8f2b7f4940b5 | 351 | float answer, remaining_count, count_per_degree; |
pairmand | 3:8f2b7f4940b5 | 352 | int reading; |
pairmand | 3:8f2b7f4940b5 | 353 | read_RAM(); |
pairmand | 3:8f2b7f4940b5 | 354 | if (RAM_checksum_error()) |
pairmand | 3:8f2b7f4940b5 | 355 | // Indicate we got a CRC error |
Sissors | 7:58b61681818f | 356 | answer = invalid_conversion; |
pairmand | 3:8f2b7f4940b5 | 357 | else { |
pairmand | 3:8f2b7f4940b5 | 358 | reading = (RAM[1] << 8) + RAM[0]; |
pairmand | 3:8f2b7f4940b5 | 359 | if (reading & 0x8000) { // negative degrees C |
pairmand | 3:8f2b7f4940b5 | 360 | reading = 0-((reading ^ 0xffff) + 1); // 2's comp then convert to signed int |
pairmand | 3:8f2b7f4940b5 | 361 | } |
pairmand | 3:8f2b7f4940b5 | 362 | answer = reading +0.0; // convert to floating point |
Sissors | 5:2cd4928e8147 | 363 | if ((FAMILY_CODE == FAMILY_CODE_DS18B20 ) || (FAMILY_CODE == FAMILY_CODE_DS1822 )) { |
pairmand | 3:8f2b7f4940b5 | 364 | answer = answer / 8.0; |
pairmand | 3:8f2b7f4940b5 | 365 | } |
pairmand | 3:8f2b7f4940b5 | 366 | else { |
pairmand | 3:8f2b7f4940b5 | 367 | remaining_count = RAM[6]; |
pairmand | 3:8f2b7f4940b5 | 368 | count_per_degree = RAM[7]; |
pairmand | 3:8f2b7f4940b5 | 369 | answer = answer - 0.25 + (count_per_degree - remaining_count) / count_per_degree; |
pairmand | 3:8f2b7f4940b5 | 370 | } |
pairmand | 3:8f2b7f4940b5 | 371 | if (scale=='C' or scale=='c') |
pairmand | 3:8f2b7f4940b5 | 372 | answer = answer / 2.0; |
pairmand | 3:8f2b7f4940b5 | 373 | else |
pairmand | 3:8f2b7f4940b5 | 374 | // Convert to deg F |
pairmand | 3:8f2b7f4940b5 | 375 | answer = answer * 9.0 / 10.0 + 32.0; |
pairmand | 3:8f2b7f4940b5 | 376 | } |
pairmand | 3:8f2b7f4940b5 | 377 | return answer; |
pairmand | 3:8f2b7f4940b5 | 378 | } |
pairmand | 3:8f2b7f4940b5 | 379 | |
pairmand | 3:8f2b7f4940b5 | 380 | bool DS1820::read_power_supply(devices device) { |
pairmand | 3:8f2b7f4940b5 | 381 | // This will return true if the device (or all devices) are Vcc powered |
pairmand | 3:8f2b7f4940b5 | 382 | // This will return false if the device (or ANY device) is parasite powered |
pairmand | 3:8f2b7f4940b5 | 383 | if (device==all_devices) |
pairmand | 3:8f2b7f4940b5 | 384 | skip_ROM(); // Skip ROM command, will poll for any device using parasite power |
pairmand | 3:8f2b7f4940b5 | 385 | else |
pairmand | 3:8f2b7f4940b5 | 386 | match_ROM(); |
pairmand | 3:8f2b7f4940b5 | 387 | onewire_byte_out(0xB4); // Read power supply command |
Sissors | 5:2cd4928e8147 | 388 | return onewire_bit_in(&this->_datapin); |
pairmand | 3:8f2b7f4940b5 | 389 | } |
pairmand | 3:8f2b7f4940b5 | 390 | |
pairmand | 3:8f2b7f4940b5 | 391 |