| Linduino 1.3.0 Linear Technology Arduino-Compatible Demonstration Board

support_functions_LTC2984.cpp
Go to the documentation of this file.
1 /*!
2 LTC2984: Multi-Sensor High Accuracy Digital Temperature Measurement System.
3 @verbatim
4
5 support_functions_LTC2984.cpp:
6 This file contains all the support functions used in the main program.
7 @endverbatim
8
9 http://www.linear.com/product/LTC2984
10
11 http://www.linear.com/product/LTC2984#demoboards
12
13
14 Copyright 2018(c) Analog Devices, Inc.
15
16 All rights reserved.
17
18 Redistribution and use in source and binary forms, with or without
19 modification, are permitted provided that the following conditions are met:
20 - Redistributions of source code must retain the above copyright
21 notice, this list of conditions and the following disclaimer.
22 - Redistributions in binary form must reproduce the above copyright
23 notice, this list of conditions and the following disclaimer in
24 the documentation and/or other materials provided with the
25 distribution.
26 - Neither the name of Analog Devices, Inc. nor the names of its
27 contributors may be used to endorse or promote products derived
28 from this software without specific prior written permission.
29 - The use of this software may or may not infringe the patent rights
30 of one or more patent holders. This license does not release you
31 from the requirement that you obtain separate licenses from these
32 patent holders to use this software.
33 - Use of the software either in source or binary form, must be run
34 on or directly connected to an Analog Devices Inc. component.
35
36 THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES "AS IS" AND ANY EXPRESS OR
37 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT,
38 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
39 IN NO EVENT SHALL ANALOG DEVICES BE LIABLE FOR ANY DIRECT, INDIRECT,
40 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41 LIMITED TO, INTELLECTUAL PROPERTY RIGHTS, PROCUREMENT OF SUBSTITUTE GOODS OR
42 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
43 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
44 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
45 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
46 */
47
48
49 //! @ingroup Temperature_Monitors
50 //! @{
51 //! @defgroup LTC2984 LTC2984: Multi-Sensor High Accuracy Digital Temperature Measurement System
52 //! @}
53
54 /*! @file
55 @ingroup LTC2984
56 Library for LTC2984: Multi-Sensor High Accuracy Digital Temperature Measurement System
57 */
58
59
60
61 #include <Arduino.h>
62 #include <stdint.h>
63 #include "Linduino.h"
64 #include "LT_SPI.h"
65 #include <SPI.h>
66 #include "UserInterface.h"
67 #include "LT_I2C.h"
68 #include "QuikEval_EEPROM.h"
69 #include "configuration_constants_LTC2984.h"
70 #include "table_coeffs_LTC2984.h"
71 #include "support_functions_LTC2984.h"
72
73 //! Prints the title block when program first starts.
74 void print_title()
75 {
76 Serial.print(F("\n******************************************************************\n"));
77 Serial.print(F(" LTC2984 Demonstration Program \n"));
78 Serial.print(F(" \n"));
79 Serial.print(F(" This program demonstrates how to read the multi-sensor \n"));
80 Serial.print(F(" temperature measurement system. \n"));
81 Serial.print(F(" \n"));
82 Serial.print(F(" Set the baud rate to 115200 and select the newline terminator. \n"));
83 Serial.print(F("******************************************************************\n"));
84 }
85
86 // ***********************
87 // Program the part
88 // ***********************
89 void assign_channel(uint8_t chip_select, uint8_t channel_number, uint32_t channel_assignment_data)
90 {
91 uint16_t start_address = get_start_address(CH_ADDRESS_BASE, channel_number);
92 transfer_four_bytes(chip_select, WRITE_TO_RAM, start_address, channel_assignment_data);
93 }
94
95
96 void write_custom_table(uint8_t chip_select, struct table_coeffs coefficients[64], uint16_t start_address, uint8_t table_length)
97 {
98 int8_t i;
99 uint32_t coeff;
100
101 output_low(chip_select);
102
103 SPI.transfer(WRITE_TO_RAM);
104 SPI.transfer(highByte(start_address));
105 SPI.transfer(lowByte(start_address));
106
107 for (i=0; i< table_length; i++)
108 {
109 coeff = coefficients[i].measurement;
110 SPI.transfer((uint8_t)(coeff >> 16));
111 SPI.transfer((uint8_t)(coeff >> 8));
112 SPI.transfer((uint8_t)coeff);
113
114 coeff = coefficients[i].temperature;
115 SPI.transfer((uint8_t)(coeff >> 16));
116 SPI.transfer((uint8_t)(coeff >> 8));
117 SPI.transfer((uint8_t)coeff);
118 }
119 output_high(chip_select);
120 }
121
122
123 void write_custom_steinhart_hart(uint8_t chip_select, uint32_t steinhart_hart_coeffs[6], uint16_t start_address)
124 {
125 int8_t i;
126 uint32_t coeff;
127
128 output_low(chip_select);
129
130 SPI.transfer(WRITE_TO_RAM);
131 SPI.transfer(highByte(start_address));
132 SPI.transfer(lowByte(start_address));
133
134 for (i = 0; i < 6; i++)
135 {
136 coeff = steinhart_hart_coeffs[i];
137 SPI.transfer((uint8_t)(coeff >> 24));
138 SPI.transfer((uint8_t)(coeff >> 16));
139 SPI.transfer((uint8_t)(coeff >> 8));
140 SPI.transfer((uint8_t)coeff);
141 }
142 output_high(chip_select);
143 }
144
145
146 // ******************************
147 // EEPROM transfer
148 // ******************************
149 void eeprom_transfer(uint8_t chip_select, uint8_t eeprom_read_or_write)
150 // Read from or write to the EEPROM.
151 // To read from the EEPROM, pass READ_FROM_EEPROM into eeprom_read_or_write.
152 // To write to the EEPROM, pass WRITE_TO_EEPROM into eeprom_read_or_write.
153 {
154 uint8_t eeprom_status;
155 Serial.println("** EEPROM transfer started ** ");
156
157 // Set EEPROM key
158 transfer_four_bytes(chip_select, WRITE_TO_RAM, EEPROM_START_ADDRESS, EEPROM_KEY);
159
160 // Set EEPROM read/write
161 transfer_byte(chip_select, WRITE_TO_RAM, COMMAND_STATUS_REGISTER, eeprom_read_or_write);
162
163 // Wait for read/write to finish
164 wait_for_process_to_finish(chip_select);
165
166 // Check for success
167 eeprom_status = transfer_byte(chip_select, READ_FROM_RAM, EEPROM_STATUS_REGISTER, 0);
168 if (eeprom_status == 0)
169 {
170 Serial.println("** EEPROM transfer succeeded ** ");
171 }
172 else
173 {
174 Serial.print(F("** EEPROM transfer had a problem. Status byte ="));
175 Serial.println(eeprom_status);
176 }
177 }
178
179
180
181 // *****************
182 // Measure channel
183 // *****************
184 void measure_channel(uint8_t chip_select, uint8_t channel_number, uint8_t channel_output)
185 {
186 convert_channel(chip_select, channel_number);
187 get_result(chip_select, channel_number, channel_output);
188 }
189
190
191 void convert_channel(uint8_t chip_select, uint8_t channel_number)
192 {
193 // Start conversion
194 transfer_byte(chip_select, WRITE_TO_RAM, COMMAND_STATUS_REGISTER, CONVERSION_CONTROL_BYTE | channel_number);
195
196 wait_for_process_to_finish(chip_select);
197 }
198
199
200 void wait_for_process_to_finish(uint8_t chip_select)
201 {
202 uint8_t process_finished = 0;
203 uint8_t data;
204 while (process_finished == 0)
205 {
206 data = transfer_byte(chip_select, READ_FROM_RAM, COMMAND_STATUS_REGISTER, 0);
207 process_finished = data & 0x40;
208 }
209 }
210
211
212 // *********************************
213 // Get results
214 // *********************************
215 void get_result(uint8_t chip_select, uint8_t channel_number, uint8_t channel_output)
216 {
217 uint32_t raw_data;
218 uint8_t fault_data;
219 uint16_t start_address = get_start_address(CONVERSION_RESULT_MEMORY_BASE, channel_number);
220 uint32_t raw_conversion_result;
221
222 raw_data = transfer_four_bytes(chip_select, READ_FROM_RAM, start_address, 0);
223
224 Serial.print(F("\nChannel "));
225 Serial.println(channel_number);
226
227 // 24 LSB's are conversion result
228 raw_conversion_result = raw_data & 0xFFFFFF;
229 print_conversion_result(raw_conversion_result, channel_output);
230
231 // If you're interested in the raw voltage or resistance, use the following
232 if (channel_output != VOLTAGE)
233 {
234 read_voltage_or_resistance_results(chip_select, channel_number);
235 }
236
237 // 8 MSB's show the fault data
238 fault_data = raw_data >> 24;
239 print_fault_data(fault_data);
240 }
241
242
243 void print_conversion_result(uint32_t raw_conversion_result, uint8_t channel_output)
244 {
245 int32_t signed_data = raw_conversion_result;
246 float scaled_result;
247
248 // Convert the 24 LSB's into a signed 32-bit integer
249 if (signed_data & 0x800000)
250 signed_data = signed_data | 0xFF000000;
251
252 // Translate and print result
253 if (channel_output == TEMPERATURE)
254 {
255 scaled_result = float(signed_data) / 1024;
256 Serial.print(F(" Temperature = "));
257 Serial.println(scaled_result);
258 }
259 else if (channel_output == VOLTAGE)
260 {
261 scaled_result = float(signed_data) / 2097152;
262 Serial.print(F(" Direct ADC reading in V = "));
263 Serial.println(scaled_result);
264 }
265
266 }
267
268
269 void read_voltage_or_resistance_results(uint8_t chip_select, uint8_t channel_number)
270 {
271 int32_t raw_data;
272 float voltage_or_resistance_result;
273 uint16_t start_address = get_start_address(VOUT_CH_BASE, channel_number);
274
275 raw_data = transfer_four_bytes(chip_select, READ_FROM_RAM, start_address, 0);
276 voltage_or_resistance_result = (float)raw_data/1024;
277 Serial.print(F(" Voltage or resistance = "));
278 Serial.println(voltage_or_resistance_result);
279 }
280
281
282 // Translate the fault byte into usable fault data and print it out
283 void print_fault_data(uint8_t fault_byte)
284 {
285 //
286 Serial.print(F(" FAULT DATA = "));
287 Serial.println(fault_byte, BIN);
288
289 if (fault_byte & SENSOR_HARD_FAILURE)
290 Serial.println(F(" - SENSOR HARD FALURE"));
291 if (fault_byte & ADC_HARD_FAILURE)
292 Serial.println(F(" - ADC_HARD_FAILURE"));
293 if (fault_byte & CJ_HARD_FAILURE)
294 Serial.println(F(" - CJ_HARD_FAILURE"));
295 if (fault_byte & CJ_SOFT_FAILURE)
296 Serial.println(F(" - CJ_SOFT_FAILURE"));
297 if (fault_byte & SENSOR_ABOVE)
298 Serial.println(F(" - SENSOR_ABOVE"));
299 if (fault_byte & SENSOR_BELOW)
300 Serial.println(F(" - SENSOR_BELOW"));
301 if (fault_byte & ADC_RANGE_ERROR)
302 Serial.println(F(" - ADC_RANGE_ERROR"));
303 if (!(fault_byte & VALID))
304 Serial.println(F("INVALID READING !!!!!!"));
305 if (fault_byte == 0b11111111)
306 Serial.println(F("CONFIGURATION ERROR !!!!!!"));
307 }
308
309 // *********************
310 // SPI RAM data transfer
311 // *********************
312 // To write to the RAM, set ram_read_or_write = WRITE_TO_RAM.
313 // To read from the RAM, set ram_read_or_write = READ_FROM_RAM.
314 // input_data is the data to send into the RAM. If you are reading from the part, set input_data = 0.
315
316 uint32_t transfer_four_bytes(uint8_t chip_select, uint8_t ram_read_or_write, uint16_t start_address, uint32_t input_data)
317 {
318 uint32_t output_data;
319 uint8_t tx[7], rx[7];
320
321 tx[6] = ram_read_or_write;
322 tx[5] = highByte(start_address);
323 tx[4] = lowByte(start_address);
324 tx[3] = (uint8_t)(input_data >> 24);
325 tx[2] = (uint8_t)(input_data >> 16);
326 tx[1] = (uint8_t)(input_data >> 8);
327 tx[0] = (uint8_t) input_data;
328
329 spi_transfer_block(chip_select, tx, rx, 7);
330
331 output_data = (uint32_t) rx[3] << 24 |
332 (uint32_t) rx[2] << 16 |
333 (uint32_t) rx[1] << 8 |
334 (uint32_t) rx[0];
335
336 return output_data;
337 }
338
339
340 uint8_t transfer_byte(uint8_t chip_select, uint8_t ram_read_or_write, uint16_t start_address, uint8_t input_data)
341 {
342 uint8_t tx[4], rx[4];
343
344 tx[3] = ram_read_or_write;
345 tx[2] = (uint8_t)(start_address >> 8);
346 tx[1] = (uint8_t)start_address;
347 tx[0] = input_data;
348 spi_transfer_block(chip_select, tx, rx, 4);
349 return rx[0];
350 }
351
352
353 // ******************************
354 // Misc support functions
355 // ******************************
356 uint16_t get_start_address(uint16_t base_address, uint8_t channel_number)
357 {
358 return base_address + 4 * (channel_number-1);
359 }
360
361
362 bool is_number_in_array(uint8_t number, uint8_t *array, uint8_t array_length)
363 // Find out if a number is an element in an array
364 {
365 bool found = false;
366 for (uint8_t i=0; i< array_length; i++)
367 {
368 if (number == array[i])
369 {
370 found = true;
371 }
372 }
373 return found;
374 }
375
376
377
378
379
380
381
382
383
384
WRITE_TO_RAM
#define WRITE_TO_RAM
Definition: configuration_constants_LTC2983.h:326
CONVERSION_RESULT_MEMORY_BASE
#define CONVERSION_RESULT_MEMORY_BASE
Definition: configuration_constants_LTC2983.h:322
EEPROM_START_ADDRESS
#define EEPROM_START_ADDRESS
Definition: configuration_constants_LTC2984.h:325
SENSOR_HARD_FAILURE
#define SENSOR_HARD_FAILURE
Definition: configuration_constants_LTC2983.h:307
transfer_byte
uint8_t transfer_byte(uint8_t chip_select, uint8_t ram_read_or_write, uint16_t start_address, uint8_t input_data)
Definition: support_functions_LTC2984.cpp:340
print_title
void print_title()
Prints the title block when program first starts.
Definition: support_functions_LTC2984.cpp:74
output_high
#define output_high(pin)
Set "pin" high.
Definition: Linduino.h:75
TEMPERATURE
#define TEMPERATURE
Linduino.h
Header File for Linduino Libraries and Demo Code.
EEPROM_STATUS_REGISTER
#define EEPROM_STATUS_REGISTER
Definition: configuration_constants_LTC2984.h:326
get_result
void get_result(uint8_t chip_select, uint8_t channel_number, uint8_t channel_output)
Definition: support_functions_LTC2984.cpp:215
VOUT_CH_BASE
#define VOUT_CH_BASE
Definition: configuration_constants_LTC2983.h:320
wait_for_process_to_finish
void wait_for_process_to_finish(uint8_t chip_select)
Definition: support_functions_LTC2984.cpp:200
support_functions_LTC2984.h
LTC2984: Multi-Sensor High Accuracy Digital Temperature Measurement System.
CONVERSION_CONTROL_BYTE
#define CONVERSION_CONTROL_BYTE
Definition: configuration_constants_LTC2983.h:328
get_start_address
uint16_t get_start_address(uint16_t base_address, uint8_t channel_number)
Definition: support_functions_LTC2984.cpp:356
READ_FROM_RAM
#define READ_FROM_RAM
Definition: configuration_constants_LTC2983.h:327
COMMAND_STATUS_REGISTER
#define COMMAND_STATUS_REGISTER
Definition: configuration_constants_LTC2983.h:318
table_coeffs
LTC2983: Multi-Sensor High Accuracy Digital Temperature Measurement System.
Definition: table_coeffs_LTC2983.h:51
eeprom_transfer
void eeprom_transfer(uint8_t chip_select, uint8_t eeprom_read_or_write)
Definition: support_functions_LTC2984.cpp:149
CJ_SOFT_FAILURE
#define CJ_SOFT_FAILURE
Definition: configuration_constants_LTC2983.h:310
ADC_RANGE_ERROR
#define ADC_RANGE_ERROR
Definition: configuration_constants_LTC2983.h:313
data
union LT_union_int32_4bytes data
Definition: DC2094A.ino:138
table_coeffs_LTC2984.h
output_low
#define output_low(pin)
Set "pin" low.
Definition: Linduino.h:72
QuikEval_EEPROM.h
QuikEval EEPROM Library.
convert_channel
void convert_channel(uint8_t chip_select, uint8_t channel_number)
Definition: support_functions_LTC2984.cpp:191
print_fault_data
void print_fault_data(uint8_t fault_byte)
Definition: support_functions_LTC2984.cpp:283
transfer_four_bytes
uint32_t transfer_four_bytes(uint8_t chip_select, uint8_t ram_read_or_write, uint16_t start_address, uint32_t input_data)
Definition: support_functions_LTC2984.cpp:316
table_coeffs::measurement
uint32_t measurement
Definition: table_coeffs_LTC2983.h:53
SENSOR_BELOW
#define SENSOR_BELOW
Definition: configuration_constants_LTC2983.h:312
print_conversion_result
void print_conversion_result(uint32_t raw_conversion_result, uint8_t channel_output)
Definition: support_functions_LTC2984.cpp:243
write_custom_steinhart_hart
void write_custom_steinhart_hart(uint8_t chip_select, uint32_t steinhart_hart_coeffs[6], uint16_t start_address)
Definition: support_functions_LTC2984.cpp:123
LT_SPI.h
LT_SPI: Routines to communicate with ATmega328P&#39;s hardware SPI port.
ADC_HARD_FAILURE
#define ADC_HARD_FAILURE
Definition: configuration_constants_LTC2983.h:308
LT_I2C.h
LT_I2C: Routines to communicate with ATmega328P&#39;s hardware I2C port.
CJ_HARD_FAILURE
#define CJ_HARD_FAILURE
Definition: configuration_constants_LTC2983.h:309
configuration_constants_LTC2984.h
LTC2984: Multi-Sensor High Accuracy Digital Temperature Measurement System.
UserInterface.h
assign_channel
void assign_channel(uint8_t chip_select, uint8_t channel_number, uint32_t channel_assignment_data)
Definition: support_functions_LTC2984.cpp:89
VOLTAGE
#define VOLTAGE
Definition: configuration_constants_LTC2983.h:330
CH_ADDRESS_BASE
#define CH_ADDRESS_BASE
Definition: configuration_constants_LTC2983.h:319
SENSOR_ABOVE
#define SENSOR_ABOVE
Definition: configuration_constants_LTC2983.h:311
write_custom_table
void write_custom_table(uint8_t chip_select, struct table_coeffs coefficients[64], uint16_t start_address, uint8_t table_length)
Definition: support_functions_LTC2984.cpp:96
read_voltage_or_resistance_results
void read_voltage_or_resistance_results(uint8_t chip_select, uint8_t channel_number)
Definition: support_functions_LTC2984.cpp:269
i
static int i
Definition: DC2430A.ino:184
spi_transfer_block
void spi_transfer_block(uint8_t cs_pin, uint8_t *tx, uint8_t *rx, uint8_t length)
Reads and sends a byte array.
Definition: LT_SPI.cpp:125
table_coeffs::temperature
uint32_t temperature
Definition: table_coeffs_LTC2983.h:54
EEPROM_KEY
#define EEPROM_KEY
Definition: configuration_constants_LTC2984.h:328
is_number_in_array
bool is_number_in_array(uint8_t number, uint8_t *array, uint8_t array_length)
Definition: support_functions_LTC2984.cpp:362
measure_channel
void measure_channel(uint8_t chip_select, uint8_t channel_number, uint8_t channel_output)
Definition: support_functions_LTC2984.cpp:184
VALID
#define VALID
Definition: configuration_constants_LTC2983.h:314