Fork to fix compiler warnings.

Dependencies:   LinkedList

Fork of DS1820 by Erik -

Committer:
Tejas Kale
Date:
Sat Mar 24 04:00:34 2018 +0100
Revision:
19:cabdd07a57e7
Parent:
17:eeb0edf0d2d5
Add an isValid function to re-search for sensors that error out.

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