6°A PIO IX TP1. Ejercicio 1. ALMADA, Santiago MAVER, Francisco

Dependencies:   LinkedList

Dependents:   temp_prueba

Committer:
pairmand
Date:
Wed Aug 07 11:02:10 2013 +0000
Revision:
4:29264b0a2c9f
Parent:
3:8f2b7f4940b5
Child:
5:2cd4928e8147
Added check of CRC to end of search_ROM

Who changed what in which revision?

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