For st-conect

Dependencies:   LinkedList

Fork of DS1820 by Erik -

Committer:
raminou
Date:
Mon Oct 15 07:35:26 2018 +0000
Revision:
16:178194eadd46
Parent:
15:236eb8f8e73a
modified for st_connect

Who changed what in which revision?

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