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