c

Dependencies:   LinkedList

Committer:
SomeRandomBloke
Date:
Sun Jan 10 22:35:20 2016 +0000
Revision:
14:4760669f5f5e
Parent:
12:196e9e54b033
Updates to expose address

Who changed what in which revision?

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