2014-01-02 08:26:26 -05:00
|
|
|
//
|
|
|
|
// FILE: MAX31855.cpp
|
|
|
|
// AUTHOR: Rob Tillaart
|
2014-01-24 15:13:07 -05:00
|
|
|
// VERSION: 0.1.02
|
2014-01-02 08:26:26 -05:00
|
|
|
// PURPOSE: MAX31855 - Thermocouple
|
|
|
|
// DATE: 2014-01-01
|
|
|
|
// URL:
|
|
|
|
//
|
2014-01-24 15:13:07 -05:00
|
|
|
// HISTORY:
|
|
|
|
// 0.1.02 added offset
|
|
|
|
// 0.1.01 refactored speed/performance
|
|
|
|
// 0.1.00 initial version.
|
|
|
|
//
|
2014-01-02 08:26:26 -05:00
|
|
|
// Released to the public domain
|
|
|
|
//
|
|
|
|
|
|
|
|
#include "MAX31855.h"
|
|
|
|
|
2014-01-02 16:24:55 -05:00
|
|
|
MAX31855::MAX31855(uint8_t sclk, uint8_t cs, uint8_t miso)
|
2014-01-02 08:26:26 -05:00
|
|
|
{
|
|
|
|
_sclk = sclk;
|
|
|
|
_cs = cs;
|
|
|
|
_miso = miso;
|
2014-01-24 15:13:07 -05:00
|
|
|
_offset = 0;
|
2014-01-02 08:26:26 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void MAX31855::begin()
|
|
|
|
{
|
|
|
|
pinMode(_cs, OUTPUT);
|
|
|
|
digitalWrite(_cs, HIGH);
|
|
|
|
|
|
|
|
pinMode(_sclk, OUTPUT);
|
|
|
|
pinMode(_miso, INPUT);
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t MAX31855::read()
|
|
|
|
{
|
|
|
|
uint32_t value = _read();
|
2014-01-02 16:24:55 -05:00
|
|
|
|
2014-01-02 08:26:26 -05:00
|
|
|
// process status bit 0-2
|
|
|
|
_status = value & 0x0007;
|
|
|
|
value >>= 3;
|
|
|
|
|
|
|
|
// reserved bit 3
|
|
|
|
value >>= 1;
|
2014-01-02 16:24:55 -05:00
|
|
|
|
2014-01-02 08:26:26 -05:00
|
|
|
// process internal bit 4-15
|
|
|
|
_internal = (value & 0x07FF) * 0.0625;
|
|
|
|
if (value & 0x0800) _internal *= -1;
|
|
|
|
value >>= 12;
|
2014-01-02 16:24:55 -05:00
|
|
|
|
2014-01-02 08:26:26 -05:00
|
|
|
// Fault bit ignored as we have the 3 status bits
|
|
|
|
// _fault = value & 0x01;
|
|
|
|
value >>= 1;
|
2014-01-02 16:24:55 -05:00
|
|
|
|
2014-01-02 08:26:26 -05:00
|
|
|
// reserved bit 17
|
|
|
|
value >>= 1;
|
2014-01-02 16:24:55 -05:00
|
|
|
|
2014-01-02 08:26:26 -05:00
|
|
|
// process temperature bit 18-31
|
|
|
|
_temperature = (value & 0x1FFF) * 0.25;
|
|
|
|
if (value & 0x2000) _temperature *= -1;
|
2014-01-24 15:13:07 -05:00
|
|
|
if (_offset != 0) _temperature += _offset;
|
2014-01-02 16:24:55 -05:00
|
|
|
|
2014-01-02 08:26:26 -05:00
|
|
|
return _status;
|
|
|
|
}
|
|
|
|
|
2014-01-02 16:24:55 -05:00
|
|
|
uint32_t MAX31855::_read(void)
|
2014-01-02 08:26:26 -05:00
|
|
|
{
|
|
|
|
uint32_t value = 0;
|
|
|
|
|
|
|
|
digitalWrite(_cs, LOW);
|
|
|
|
|
2014-01-02 16:24:55 -05:00
|
|
|
for (int8_t i = 31; i >= 0; i--)
|
2014-01-02 08:26:26 -05:00
|
|
|
{
|
|
|
|
value <<= 1;
|
2014-01-02 16:24:55 -05:00
|
|
|
digitalWrite(_sclk, LOW);
|
2014-01-24 15:13:07 -05:00
|
|
|
// delayMicroseconds(1); // DUE
|
2014-01-02 16:24:55 -05:00
|
|
|
if ( digitalRead(_miso) ) value += 1;
|
2014-01-02 08:26:26 -05:00
|
|
|
digitalWrite(_sclk, HIGH);
|
2014-01-24 15:13:07 -05:00
|
|
|
// delayMicroseconds(1); // DUE
|
2014-01-02 08:26:26 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
digitalWrite(_cs, HIGH);
|
2014-01-02 16:24:55 -05:00
|
|
|
|
2014-01-02 08:26:26 -05:00
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
// END OF FILE
|