Hardware Reference
In-Depth Information
transformed later. On line 27, a logical AND is made, comparing the value to
0x8000 ; which is the bitmask , used to access a specii c byte in the data (refer to
the discussion of “Reading and Writing Bits” in Chapter 6 for more information
on bitmasks). If the value is true, then the i rst bit is equal to one, meaning that
the temperature reading is negative, and the neg variable is updated.
Bit number 16 corresponds to a fault condition; if it is true, then the MAX31855
is reporting an error and a bitwise comparison is made on the second 16-bit
value where bits 0, 1, and 2 correspond to specii c faults.
On line 45, a bitmask is created. The i rst 16 bits of data correspond to the
temperature, but you will not need all that information. By creating a bitmask,
you can i lter out bits that do not interest you. In this case, the i rst bit, the sign,
isn't required; it has already been placed in a variable. The last two are also of
no interest and are discarded. The data is still not usable in its current state; the
last 2 bits have been discarded and are equal to zero, but now the data has to be
“shifted”; pushing the bits right until they are aligned as required.
On line 51, a new variable is created, a double . On the Due, this type of vari-
able can contain l oating point values with 64 bits of precision. Because the
MAX31855 returns values in increments of 0.25 degrees, using a double or a
float ensures that the decimal values are kept. First, the shifted 16-bit value is
copied into this variable, and then it is multiplied by 0.25; it now contains the
correct temperature in degrees Celsius.
Finally, the temperature might be negative. This is checked on line 55; if the
neg variable is true, then the value returned was negative, and so the tempera-
ture is multiplied by -1.
On line 59, this temperature is written to the serial port, and the Arduino is
told to wait for 2 seconds. The MAX31855 continues to monitor the temperature
and continues to convert that temperature. When SPI.transfer() is next called
through loop() , the MAX31855 communicates the temperature to the Arduino
without the need for waiting.
Exercises
This sketch displays the temperature in degrees Celsius but not in Fahrenheit.
Try to add a function to convert between Celsius and Fahrenheit. The conver-
sion is a simple mathematical formula; multiply the temperature in Celsius by
1.8, and then add 32.
The MAX31855 is designed so that the i rst 16 bits correspond to the tempera-
ture with an additional fault bit. The last 16 bits are not normally required for
normal operations; how would you modify the sketch to read the next 16 bits
only if a fault is detected.
 
Search WWH ::




Custom Search