Library for MAX31855. Based on MAX6675 library. I have only implemented the temperature reading. The chip supports also the reading of internal reference temperature and faults for shorted thermocouple and shorts to vcc/gnd.
MAX31855.cpp@0:a07644a18427, 2012-03-10 (annotated)
- Committer:
- ratsept
- Date:
- Sat Mar 10 19:30:40 2012 +0000
- Revision:
- 0:a07644a18427
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ratsept | 0:a07644a18427 | 1 | #include <mbed.h> |
ratsept | 0:a07644a18427 | 2 | #include "MAX31855.h" |
ratsept | 0:a07644a18427 | 3 | |
ratsept | 0:a07644a18427 | 4 | MAX31855::MAX31855(SPI& _spi, PinName _ncs) : spi(_spi), ncs(_ncs) |
ratsept | 0:a07644a18427 | 5 | { |
ratsept | 0:a07644a18427 | 6 | deselect(); |
ratsept | 0:a07644a18427 | 7 | } |
ratsept | 0:a07644a18427 | 8 | |
ratsept | 0:a07644a18427 | 9 | float MAX31855::read_temp() |
ratsept | 0:a07644a18427 | 10 | { |
ratsept | 0:a07644a18427 | 11 | short value = 0; |
ratsept | 0:a07644a18427 | 12 | float temp = 0; |
ratsept | 0:a07644a18427 | 13 | |
ratsept | 0:a07644a18427 | 14 | int16_t response = 0; |
ratsept | 0:a07644a18427 | 15 | uint8_t resp_byte = 0; |
ratsept | 0:a07644a18427 | 16 | |
ratsept | 0:a07644a18427 | 17 | select(); |
ratsept | 0:a07644a18427 | 18 | |
ratsept | 0:a07644a18427 | 19 | resp_byte = spi.write(0); |
ratsept | 0:a07644a18427 | 20 | response |= resp_byte << 8; |
ratsept | 0:a07644a18427 | 21 | resp_byte = spi.write(0); |
ratsept | 0:a07644a18427 | 22 | response |= resp_byte << 0; |
ratsept | 0:a07644a18427 | 23 | |
ratsept | 0:a07644a18427 | 24 | deselect(); |
ratsept | 0:a07644a18427 | 25 | |
ratsept | 0:a07644a18427 | 26 | if (response & 0x0001) |
ratsept | 0:a07644a18427 | 27 | { |
ratsept | 0:a07644a18427 | 28 | error("Error"); |
ratsept | 0:a07644a18427 | 29 | return 2000.0; |
ratsept | 0:a07644a18427 | 30 | } |
ratsept | 0:a07644a18427 | 31 | |
ratsept | 0:a07644a18427 | 32 | response &= 0xfffc; // mask lower two bits |
ratsept | 0:a07644a18427 | 33 | temp = response / 16.0; |
ratsept | 0:a07644a18427 | 34 | |
ratsept | 0:a07644a18427 | 35 | return temp; |
ratsept | 0:a07644a18427 | 36 | } |
ratsept | 0:a07644a18427 | 37 | |
ratsept | 0:a07644a18427 | 38 | void MAX31855::select() |
ratsept | 0:a07644a18427 | 39 | { |
ratsept | 0:a07644a18427 | 40 | ncs = 0; |
ratsept | 0:a07644a18427 | 41 | } |
ratsept | 0:a07644a18427 | 42 | |
ratsept | 0:a07644a18427 | 43 | void MAX31855::deselect() |
ratsept | 0:a07644a18427 | 44 | { |
ratsept | 0:a07644a18427 | 45 | ncs = 1; |
ratsept | 0:a07644a18427 | 46 | } |