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