Based on DS1820. Only put 3 defines in comment to allow operations with Nucleo board

Dependencies:   LinkedList

Fork of DS1820 by Erik -

Committer:
YROY2004
Date:
Thu Mar 23 19:41:28 2017 +0000
Revision:
16:37956dcaa5c0
Parent:
15:236eb8f8e73a
Modified DS1820 librairy (3 defines removed to avoid problem with Nucleo board.

Who changed what in which revision?

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