Library for MAX31850 thermocouple temperature sensor

Committer:
croberts21
Date:
Wed Jan 23 23:44:57 2019 +0000
Revision:
0:5530bf2d0e94
All files added

Who changed what in which revision?

UserRevisionLine numberNew contents of line
croberts21 0:5530bf2d0e94 1 #include "MAX31850.h"
croberts21 0:5530bf2d0e94 2
croberts21 0:5530bf2d0e94 3 Serial pc(USBTX, USBRX);
croberts21 0:5530bf2d0e94 4
croberts21 0:5530bf2d0e94 5 #define ONEWIRE_INPUT(pin) pin->input()
croberts21 0:5530bf2d0e94 6 #define ONEWIRE_OUTPUT(pin) pin->output()
croberts21 0:5530bf2d0e94 7 #define ONEWIRE_INIT(pin)
croberts21 0:5530bf2d0e94 8
croberts21 0:5530bf2d0e94 9 LinkedList<node> MAX31850::probes;
croberts21 0:5530bf2d0e94 10
croberts21 0:5530bf2d0e94 11
croberts21 0:5530bf2d0e94 12 MAX31850::MAX31850 (PinName data_pin, PinName power_pin, bool power_polarity) : _datapin(data_pin), _parasitepin(power_pin) {
croberts21 0:5530bf2d0e94 13 int byte_counter;
croberts21 0:5530bf2d0e94 14 _power_polarity = power_polarity;
croberts21 0:5530bf2d0e94 15
croberts21 0:5530bf2d0e94 16 _power_mosfet = power_pin != NC;
croberts21 0:5530bf2d0e94 17
croberts21 0:5530bf2d0e94 18 for(byte_counter=0;byte_counter<9;byte_counter++)
croberts21 0:5530bf2d0e94 19 scratchpad[byte_counter] = 0x00;
croberts21 0:5530bf2d0e94 20
croberts21 0:5530bf2d0e94 21 ONEWIRE_INIT((&_datapin));
croberts21 0:5530bf2d0e94 22
croberts21 0:5530bf2d0e94 23 if (!unassignedProbe(&_datapin, _ROM))
croberts21 0:5530bf2d0e94 24 error("No unassigned MAX31850 found!\n");
croberts21 0:5530bf2d0e94 25 else {
croberts21 0:5530bf2d0e94 26 _datapin.input();
croberts21 0:5530bf2d0e94 27 probes.append(this);
croberts21 0:5530bf2d0e94 28 _parasite_power = false; //only set up to use external power right now
croberts21 0:5530bf2d0e94 29 }
croberts21 0:5530bf2d0e94 30 }
croberts21 0:5530bf2d0e94 31
croberts21 0:5530bf2d0e94 32 MAX31850::~MAX31850 (void) {
croberts21 0:5530bf2d0e94 33 node *tmp;
croberts21 0:5530bf2d0e94 34 for(int i=1; i<=probes.length(); i++)
croberts21 0:5530bf2d0e94 35 {
croberts21 0:5530bf2d0e94 36 tmp = probes.pop(i);
croberts21 0:5530bf2d0e94 37 if (tmp->data == this)
croberts21 0:5530bf2d0e94 38 probes.remove(i);
croberts21 0:5530bf2d0e94 39 }
croberts21 0:5530bf2d0e94 40 }
croberts21 0:5530bf2d0e94 41
croberts21 0:5530bf2d0e94 42
croberts21 0:5530bf2d0e94 43 bool MAX31850::onewire_reset(DigitalInOut *pin) {
croberts21 0:5530bf2d0e94 44 // This will return false if no devices are present on the data bus
croberts21 0:5530bf2d0e94 45 bool presence=false;
croberts21 0:5530bf2d0e94 46 ONEWIRE_OUTPUT(pin);
croberts21 0:5530bf2d0e94 47 pin->write(0); // bring low for 500 us
croberts21 0:5530bf2d0e94 48 wait_us(500);
croberts21 0:5530bf2d0e94 49 ONEWIRE_INPUT(pin); // let the data line float high
croberts21 0:5530bf2d0e94 50 wait_us(90); // wait 90us
croberts21 0:5530bf2d0e94 51 if (pin->read()==0) // see if any devices are pulling the data line low
croberts21 0:5530bf2d0e94 52 presence=true;
croberts21 0:5530bf2d0e94 53 wait_us(410);
croberts21 0:5530bf2d0e94 54 return presence;
croberts21 0:5530bf2d0e94 55 }
croberts21 0:5530bf2d0e94 56
croberts21 0:5530bf2d0e94 57 void MAX31850::onewire_bit_out (DigitalInOut *pin, bool bit_data) {
croberts21 0:5530bf2d0e94 58 ONEWIRE_OUTPUT(pin);
croberts21 0:5530bf2d0e94 59 pin->write(0);
croberts21 0:5530bf2d0e94 60 wait_us(3); // DXP modified from 5
croberts21 0:5530bf2d0e94 61 if (bit_data) {
croberts21 0:5530bf2d0e94 62 pin->write(1); // bring data line high
croberts21 0:5530bf2d0e94 63 wait_us(55);
croberts21 0:5530bf2d0e94 64 } else {
croberts21 0:5530bf2d0e94 65 wait_us(55); // keep data line low
croberts21 0:5530bf2d0e94 66 pin->write(1);
croberts21 0:5530bf2d0e94 67 wait_us(10); // DXP added to allow bus to float high before next bit_out
croberts21 0:5530bf2d0e94 68 }
croberts21 0:5530bf2d0e94 69 }
croberts21 0:5530bf2d0e94 70
croberts21 0:5530bf2d0e94 71 void MAX31850::onewire_byte_out(char data) { // output data character (least sig bit first).
croberts21 0:5530bf2d0e94 72 int n;
croberts21 0:5530bf2d0e94 73 for (n=0; n<8; n++) {
croberts21 0:5530bf2d0e94 74 onewire_bit_out(&this->_datapin, data & 0x01);
croberts21 0:5530bf2d0e94 75 data = data >> 1; // now the next bit is in the least sig bit position.
croberts21 0:5530bf2d0e94 76 }
croberts21 0:5530bf2d0e94 77 }
croberts21 0:5530bf2d0e94 78
croberts21 0:5530bf2d0e94 79 bool MAX31850::onewire_bit_in(DigitalInOut *pin) {
croberts21 0:5530bf2d0e94 80 bool answer;
croberts21 0:5530bf2d0e94 81 ONEWIRE_OUTPUT(pin);
croberts21 0:5530bf2d0e94 82 pin->write(0);
croberts21 0:5530bf2d0e94 83 wait_us(3); // DXP modified from 5
croberts21 0:5530bf2d0e94 84 ONEWIRE_INPUT(pin);
croberts21 0:5530bf2d0e94 85 wait_us(10); // DXP modified from 5
croberts21 0:5530bf2d0e94 86 answer = pin->read();
croberts21 0:5530bf2d0e94 87 wait_us(45); // DXP modified from 50
croberts21 0:5530bf2d0e94 88 return answer;
croberts21 0:5530bf2d0e94 89 }
croberts21 0:5530bf2d0e94 90
croberts21 0:5530bf2d0e94 91 char MAX31850::onewire_byte_in() { // read byte, least sig byte first
croberts21 0:5530bf2d0e94 92 char answer = 0x00;
croberts21 0:5530bf2d0e94 93 int i;
croberts21 0:5530bf2d0e94 94 for (i=0; i<8; i++) {
croberts21 0:5530bf2d0e94 95 answer = answer >> 1; // shift over to make room for the next bit
croberts21 0:5530bf2d0e94 96 if (onewire_bit_in(&this->_datapin))
croberts21 0:5530bf2d0e94 97 answer = answer | 0x80; // if the data port is high, make this bit a 1
croberts21 0:5530bf2d0e94 98 }
croberts21 0:5530bf2d0e94 99 return answer;
croberts21 0:5530bf2d0e94 100 }
croberts21 0:5530bf2d0e94 101
croberts21 0:5530bf2d0e94 102 bool MAX31850::unassignedProbe(PinName pin) {
croberts21 0:5530bf2d0e94 103 DigitalInOut _pin(pin);
croberts21 0:5530bf2d0e94 104 ONEWIRE_INIT((&_pin));
croberts21 0:5530bf2d0e94 105 char ROM_address[8];
croberts21 0:5530bf2d0e94 106 return search_ROM_routine(&_pin, 0xF0, ROM_address);
croberts21 0:5530bf2d0e94 107 }
croberts21 0:5530bf2d0e94 108
croberts21 0:5530bf2d0e94 109 bool MAX31850::unassignedProbe(DigitalInOut *pin, char *ROM_address) {
croberts21 0:5530bf2d0e94 110 return search_ROM_routine(pin, 0xF0, ROM_address);
croberts21 0:5530bf2d0e94 111 }
croberts21 0:5530bf2d0e94 112
croberts21 0:5530bf2d0e94 113 bool MAX31850::search_ROM_routine(DigitalInOut *pin, char command, char *ROM_address) {
croberts21 0:5530bf2d0e94 114 bool DS1820_done_flag = false;
croberts21 0:5530bf2d0e94 115 int DS1820_last_discrepancy = 0;
croberts21 0:5530bf2d0e94 116 char DS1820_search_ROM[8] = {0, 0, 0, 0, 0, 0, 0, 0};
croberts21 0:5530bf2d0e94 117
croberts21 0:5530bf2d0e94 118 int discrepancy_marker, ROM_bit_index;
croberts21 0:5530bf2d0e94 119 bool return_value, Bit_A, Bit_B;
croberts21 0:5530bf2d0e94 120 char byte_counter, bit_mask;
croberts21 0:5530bf2d0e94 121
croberts21 0:5530bf2d0e94 122 return_value=false;
croberts21 0:5530bf2d0e94 123 while (!DS1820_done_flag) {
croberts21 0:5530bf2d0e94 124 if (!onewire_reset(pin)) {
croberts21 0:5530bf2d0e94 125 return false;
croberts21 0:5530bf2d0e94 126 } else {
croberts21 0:5530bf2d0e94 127 ROM_bit_index=1;
croberts21 0:5530bf2d0e94 128 discrepancy_marker=0;
croberts21 0:5530bf2d0e94 129 char command_shift = command;
croberts21 0:5530bf2d0e94 130 for (int n=0; n<8; n++) { // Search ROM command
croberts21 0:5530bf2d0e94 131 onewire_bit_out(pin, command_shift & 0x01);
croberts21 0:5530bf2d0e94 132 command_shift = command_shift >> 1; // now the next bit is in the least sig bit position.
croberts21 0:5530bf2d0e94 133 }
croberts21 0:5530bf2d0e94 134 byte_counter = 0;
croberts21 0:5530bf2d0e94 135 bit_mask = 0x01;
croberts21 0:5530bf2d0e94 136 while (ROM_bit_index<=64) {
croberts21 0:5530bf2d0e94 137 Bit_A = onewire_bit_in(pin);
croberts21 0:5530bf2d0e94 138 Bit_B = onewire_bit_in(pin);
croberts21 0:5530bf2d0e94 139 if (Bit_A & Bit_B) {
croberts21 0:5530bf2d0e94 140 discrepancy_marker = 0; // data read error, this should never happen
croberts21 0:5530bf2d0e94 141 ROM_bit_index = 0xFF;
croberts21 0:5530bf2d0e94 142 } else {
croberts21 0:5530bf2d0e94 143 if (Bit_A | Bit_B) {
croberts21 0:5530bf2d0e94 144 // Set ROM bit to Bit_A
croberts21 0:5530bf2d0e94 145 if (Bit_A) {
croberts21 0:5530bf2d0e94 146 DS1820_search_ROM[byte_counter] = DS1820_search_ROM[byte_counter] | bit_mask; // Set ROM bit to one
croberts21 0:5530bf2d0e94 147 } else {
croberts21 0:5530bf2d0e94 148 DS1820_search_ROM[byte_counter] = DS1820_search_ROM[byte_counter] & ~bit_mask; // Set ROM bit to zero
croberts21 0:5530bf2d0e94 149 }
croberts21 0:5530bf2d0e94 150 } else {
croberts21 0:5530bf2d0e94 151 // both bits A and B are low, so there are two or more devices present
croberts21 0:5530bf2d0e94 152 if ( ROM_bit_index == DS1820_last_discrepancy ) {
croberts21 0:5530bf2d0e94 153 DS1820_search_ROM[byte_counter] = DS1820_search_ROM[byte_counter] | bit_mask; // Set ROM bit to one
croberts21 0:5530bf2d0e94 154 } else {
croberts21 0:5530bf2d0e94 155 if ( ROM_bit_index > DS1820_last_discrepancy ) {
croberts21 0:5530bf2d0e94 156 DS1820_search_ROM[byte_counter] = DS1820_search_ROM[byte_counter] & ~bit_mask; // Set ROM bit to zero
croberts21 0:5530bf2d0e94 157 discrepancy_marker = ROM_bit_index;
croberts21 0:5530bf2d0e94 158 } else {
croberts21 0:5530bf2d0e94 159 if (( DS1820_search_ROM[byte_counter] & bit_mask) == 0x00 )
croberts21 0:5530bf2d0e94 160 discrepancy_marker = ROM_bit_index;
croberts21 0:5530bf2d0e94 161 }
croberts21 0:5530bf2d0e94 162 }
croberts21 0:5530bf2d0e94 163 }
croberts21 0:5530bf2d0e94 164 onewire_bit_out (pin, DS1820_search_ROM[byte_counter] & bit_mask);
croberts21 0:5530bf2d0e94 165 ROM_bit_index++;
croberts21 0:5530bf2d0e94 166 if (bit_mask & 0x80) {
croberts21 0:5530bf2d0e94 167 byte_counter++;
croberts21 0:5530bf2d0e94 168 bit_mask = 0x01;
croberts21 0:5530bf2d0e94 169 } else {
croberts21 0:5530bf2d0e94 170 bit_mask = bit_mask << 1;
croberts21 0:5530bf2d0e94 171 }
croberts21 0:5530bf2d0e94 172 }
croberts21 0:5530bf2d0e94 173 }
croberts21 0:5530bf2d0e94 174 DS1820_last_discrepancy = discrepancy_marker;
croberts21 0:5530bf2d0e94 175 if (ROM_bit_index != 0xFF) {
croberts21 0:5530bf2d0e94 176 int i = 1;
croberts21 0:5530bf2d0e94 177 node *list_container;
croberts21 0:5530bf2d0e94 178 while(1) {
croberts21 0:5530bf2d0e94 179 list_container = probes.pop(i);
croberts21 0:5530bf2d0e94 180 if (list_container == NULL) { //End of list, or empty list
croberts21 0:5530bf2d0e94 181 if (ROM_checksum_error(DS1820_search_ROM)) { // Check the CRC
croberts21 0:5530bf2d0e94 182 return false;
croberts21 0:5530bf2d0e94 183 }
croberts21 0:5530bf2d0e94 184 for(byte_counter=0;byte_counter<8;byte_counter++)
croberts21 0:5530bf2d0e94 185 ROM_address[byte_counter] = DS1820_search_ROM[byte_counter];
croberts21 0:5530bf2d0e94 186 return true;
croberts21 0:5530bf2d0e94 187 } else { //Otherwise, check if ROM is already known
croberts21 0:5530bf2d0e94 188 bool equal = true;
croberts21 0:5530bf2d0e94 189 MAX31850
croberts21 0:5530bf2d0e94 190 *pointer = (MAX31850
croberts21 0:5530bf2d0e94 191 *) list_container->data;
croberts21 0:5530bf2d0e94 192 char *ROM_compare = pointer->_ROM;
croberts21 0:5530bf2d0e94 193
croberts21 0:5530bf2d0e94 194 for(byte_counter=0;byte_counter<8;byte_counter++) {
croberts21 0:5530bf2d0e94 195 if ( ROM_compare[byte_counter] != DS1820_search_ROM[byte_counter])
croberts21 0:5530bf2d0e94 196 equal = false;
croberts21 0:5530bf2d0e94 197 }
croberts21 0:5530bf2d0e94 198 if (equal)
croberts21 0:5530bf2d0e94 199 break;
croberts21 0:5530bf2d0e94 200 else
croberts21 0:5530bf2d0e94 201 i++;
croberts21 0:5530bf2d0e94 202 }
croberts21 0:5530bf2d0e94 203 }
croberts21 0:5530bf2d0e94 204 }
croberts21 0:5530bf2d0e94 205 }
croberts21 0:5530bf2d0e94 206 if (DS1820_last_discrepancy == 0)
croberts21 0:5530bf2d0e94 207 DS1820_done_flag = true;
croberts21 0:5530bf2d0e94 208 }
croberts21 0:5530bf2d0e94 209 return return_value;
croberts21 0:5530bf2d0e94 210 }
croberts21 0:5530bf2d0e94 211
croberts21 0:5530bf2d0e94 212 void MAX31850::match_ROM() {
croberts21 0:5530bf2d0e94 213 // Used to select a specific device
croberts21 0:5530bf2d0e94 214 int i;
croberts21 0:5530bf2d0e94 215 onewire_reset(&this->_datapin);
croberts21 0:5530bf2d0e94 216 onewire_byte_out( 0x55); //Match ROM command
croberts21 0:5530bf2d0e94 217 for (i=0;i<8;i++) {
croberts21 0:5530bf2d0e94 218 onewire_byte_out(_ROM[i]);
croberts21 0:5530bf2d0e94 219 }
croberts21 0:5530bf2d0e94 220 }
croberts21 0:5530bf2d0e94 221
croberts21 0:5530bf2d0e94 222 void MAX31850::skip_ROM() {
croberts21 0:5530bf2d0e94 223 onewire_reset(&this->_datapin);
croberts21 0:5530bf2d0e94 224 onewire_byte_out(0xCC); // Skip ROM command
croberts21 0:5530bf2d0e94 225 }
croberts21 0:5530bf2d0e94 226
croberts21 0:5530bf2d0e94 227 bool MAX31850::ROM_checksum_error(char *_ROM_address) {
croberts21 0:5530bf2d0e94 228 char _CRC=0x00;
croberts21 0:5530bf2d0e94 229 int i;
croberts21 0:5530bf2d0e94 230 for(i=0;i<7;i++) // Only going to shift the lower 7 bytes
croberts21 0:5530bf2d0e94 231 _CRC = CRC_byte(_CRC, _ROM_address[i]);
croberts21 0:5530bf2d0e94 232 // After 7 bytes CRC should equal the 8th byte (ROM CRC)
croberts21 0:5530bf2d0e94 233 return (_CRC!=_ROM_address[7]); // will return true if there is a CRC checksum mis-match
croberts21 0:5530bf2d0e94 234 }
croberts21 0:5530bf2d0e94 235
croberts21 0:5530bf2d0e94 236 bool MAX31850::scratchpad_checksum_error() {
croberts21 0:5530bf2d0e94 237 char _CRC=0x00;
croberts21 0:5530bf2d0e94 238 int i;
croberts21 0:5530bf2d0e94 239 for(i=0;i<8;i++) // Only going to shift the lower 8 bytes
croberts21 0:5530bf2d0e94 240 _CRC = CRC_byte(_CRC, scratchpad[i]);
croberts21 0:5530bf2d0e94 241 // After 8 bytes CRC should equal the 9th byte (scratchpad CRC)
croberts21 0:5530bf2d0e94 242 return (_CRC!=scratchpad[8]); // will return true if there is a CRC checksum mis-match
croberts21 0:5530bf2d0e94 243 }
croberts21 0:5530bf2d0e94 244
croberts21 0:5530bf2d0e94 245 char MAX31850::CRC_byte (char _CRC, char byte ) {
croberts21 0:5530bf2d0e94 246 int j;
croberts21 0:5530bf2d0e94 247 for(j=0;j<8;j++) {
croberts21 0:5530bf2d0e94 248 if ((byte & 0x01 ) ^ (_CRC & 0x01)) {
croberts21 0:5530bf2d0e94 249 // DATA ^ LSB CRC = 1
croberts21 0:5530bf2d0e94 250 _CRC = _CRC>>1;
croberts21 0:5530bf2d0e94 251 // Set the MSB to 1
croberts21 0:5530bf2d0e94 252 _CRC = _CRC | 0x80;
croberts21 0:5530bf2d0e94 253 // Check bit 3
croberts21 0:5530bf2d0e94 254 if (_CRC & 0x04) {
croberts21 0:5530bf2d0e94 255 _CRC = _CRC & 0xFB; // Bit 3 is set, so clear it
croberts21 0:5530bf2d0e94 256 } else {
croberts21 0:5530bf2d0e94 257 _CRC = _CRC | 0x04; // Bit 3 is clear, so set it
croberts21 0:5530bf2d0e94 258 }
croberts21 0:5530bf2d0e94 259 // Check bit 4
croberts21 0:5530bf2d0e94 260 if (_CRC & 0x08) {
croberts21 0:5530bf2d0e94 261 _CRC = _CRC & 0xF7; // Bit 4 is set, so clear it
croberts21 0:5530bf2d0e94 262 } else {
croberts21 0:5530bf2d0e94 263 _CRC = _CRC | 0x08; // Bit 4 is clear, so set it
croberts21 0:5530bf2d0e94 264 }
croberts21 0:5530bf2d0e94 265 } else {
croberts21 0:5530bf2d0e94 266 // DATA ^ LSB CRC = 0
croberts21 0:5530bf2d0e94 267 _CRC = _CRC>>1;
croberts21 0:5530bf2d0e94 268 // clear MSB
croberts21 0:5530bf2d0e94 269 _CRC = _CRC & 0x7F;
croberts21 0:5530bf2d0e94 270 // No need to check bits, with DATA ^ LSB CRC = 0, they will remain unchanged
croberts21 0:5530bf2d0e94 271 }
croberts21 0:5530bf2d0e94 272 byte = byte>>1;
croberts21 0:5530bf2d0e94 273 }
croberts21 0:5530bf2d0e94 274 return _CRC;
croberts21 0:5530bf2d0e94 275 }
croberts21 0:5530bf2d0e94 276
croberts21 0:5530bf2d0e94 277 void MAX31850::readAll() {
croberts21 0:5530bf2d0e94 278 // Convert temperature into scratchpad for all devices at once
croberts21 0:5530bf2d0e94 279 int delay_time = 75; //default delay time
croberts21 0:5530bf2d0e94 280
croberts21 0:5530bf2d0e94 281 skip_ROM(); //Skip ROM command, will convert for all devices
croberts21 0:5530bf2d0e94 282
croberts21 0:5530bf2d0e94 283 onewire_byte_out( 0x44); //perform temperature conversion
croberts21 0:5530bf2d0e94 284
croberts21 0:5530bf2d0e94 285 wait_ms(delay_time);
croberts21 0:5530bf2d0e94 286 }
croberts21 0:5530bf2d0e94 287
croberts21 0:5530bf2d0e94 288 void MAX31850::read_scratchpad() {
croberts21 0:5530bf2d0e94 289 // This will copy the MAX31850's 9 bytes of scratchpad data
croberts21 0:5530bf2d0e94 290 // into the objects scratchpad array. Functions that use
croberts21 0:5530bf2d0e94 291 // scratchpad values will automaticaly call this procedure.
croberts21 0:5530bf2d0e94 292 int i;
croberts21 0:5530bf2d0e94 293 match_ROM(); // Select this device
croberts21 0:5530bf2d0e94 294 onewire_byte_out( 0xBE); //Read Scratchpad command
croberts21 0:5530bf2d0e94 295 for(i=0;i<9;i++) {
croberts21 0:5530bf2d0e94 296 scratchpad[i] = onewire_byte_in();
croberts21 0:5530bf2d0e94 297 }
croberts21 0:5530bf2d0e94 298 }
croberts21 0:5530bf2d0e94 299
croberts21 0:5530bf2d0e94 300
croberts21 0:5530bf2d0e94 301 float MAX31850::getTemp(int scale) {
croberts21 0:5530bf2d0e94 302 float answer;
croberts21 0:5530bf2d0e94 303 int reading;
croberts21 0:5530bf2d0e94 304 read_scratchpad();
croberts21 0:5530bf2d0e94 305 if (scratchpad_checksum_error()) {
croberts21 0:5530bf2d0e94 306 //indicate we got a CRC error
croberts21 0:5530bf2d0e94 307 answer = invalid_conversion;
croberts21 0:5530bf2d0e94 308 pc.printf("CRC Error\r\n");
croberts21 0:5530bf2d0e94 309 }
croberts21 0:5530bf2d0e94 310 else {
croberts21 0:5530bf2d0e94 311 reading = (scratchpad[1] << 8) + scratchpad[0]; //combine first two bytes of scratchpad
croberts21 0:5530bf2d0e94 312 reading = reading >> 2; //shift right two to drop unused bits
croberts21 0:5530bf2d0e94 313 if (reading & 0x2000) { //check MSB to determin sign
croberts21 0:5530bf2d0e94 314 reading = 0-((reading ^ 0x3fff) + 1); //use 2's compliment to convert back to positive number and make it a signed int
croberts21 0:5530bf2d0e94 315 }
croberts21 0:5530bf2d0e94 316 answer = reading +0.0; //convert to floating point
croberts21 0:5530bf2d0e94 317 answer = answer / 4.0; //shift decimal place over two places
croberts21 0:5530bf2d0e94 318
croberts21 0:5530bf2d0e94 319 //unit convertion
croberts21 0:5530bf2d0e94 320 //checks for passed in value first and if there is none it sets the units baced on class units variable
croberts21 0:5530bf2d0e94 321 switch(scale) {
croberts21 0:5530bf2d0e94 322 case C:
croberts21 0:5530bf2d0e94 323 break;
croberts21 0:5530bf2d0e94 324 case K:
croberts21 0:5530bf2d0e94 325 answer = answer + 273.15f;
croberts21 0:5530bf2d0e94 326 break;
croberts21 0:5530bf2d0e94 327 case F:
croberts21 0:5530bf2d0e94 328 answer = answer * 1.8f + 32.0f;
croberts21 0:5530bf2d0e94 329 break;
croberts21 0:5530bf2d0e94 330 case R:
croberts21 0:5530bf2d0e94 331 answer = answer * 1.8f + 491.67f;
croberts21 0:5530bf2d0e94 332 break;
croberts21 0:5530bf2d0e94 333 default:
croberts21 0:5530bf2d0e94 334 switch(_units) {
croberts21 0:5530bf2d0e94 335 case C:
croberts21 0:5530bf2d0e94 336 break;
croberts21 0:5530bf2d0e94 337 case K:
croberts21 0:5530bf2d0e94 338 answer = answer + 273.15f;
croberts21 0:5530bf2d0e94 339 break;
croberts21 0:5530bf2d0e94 340 case F:
croberts21 0:5530bf2d0e94 341 answer = answer * 1.8f + 32.0f;
croberts21 0:5530bf2d0e94 342 break;
croberts21 0:5530bf2d0e94 343 case R:
croberts21 0:5530bf2d0e94 344 answer = answer * 1.8f + 491.67f;
croberts21 0:5530bf2d0e94 345 break;
croberts21 0:5530bf2d0e94 346 }
croberts21 0:5530bf2d0e94 347 }
croberts21 0:5530bf2d0e94 348 }
croberts21 0:5530bf2d0e94 349 return answer;
croberts21 0:5530bf2d0e94 350 }
croberts21 0:5530bf2d0e94 351
croberts21 0:5530bf2d0e94 352 // Contributed by John M. Larkin (jlarkin@whitworth.edu)
croberts21 0:5530bf2d0e94 353 unsigned long long MAX31850::getId() {
croberts21 0:5530bf2d0e94 354 // This will return the 64 bit address of the connected MAX31850
croberts21 0:5530bf2d0e94 355 unsigned long long myName = 0;
croberts21 0:5530bf2d0e94 356 for (int i = 0; i<8; i++) {
croberts21 0:5530bf2d0e94 357 myName = (myName<<8) | _ROM[i];
croberts21 0:5530bf2d0e94 358 }
croberts21 0:5530bf2d0e94 359 return myName;
croberts21 0:5530bf2d0e94 360 }
croberts21 0:5530bf2d0e94 361
croberts21 0:5530bf2d0e94 362 void MAX31850::setUnits(int units) {
croberts21 0:5530bf2d0e94 363 _units = units;
croberts21 0:5530bf2d0e94 364 }