GY-63_MS5611/libraries/MAX31855_RT/MAX31855.cpp

219 lines
4.9 KiB
C++
Raw Normal View History

2021-01-29 06:31:58 -05:00
//
// FILE: MAX31855.cpp
// AUTHOR: Rob Tillaart
2023-11-12 04:16:51 -05:00
// VERSION: 0.4.4
2021-01-29 06:31:58 -05:00
// PURPOSE: Arduino library for MAX31855 chip for K type thermocouple
// DATE: 2014-01-01
// URL: https://github.com/RobTillaart/MAX31855_RT
#include "MAX31855.h"
2021-12-09 14:42:42 -05:00
MAX31855::MAX31855()
2021-01-29 06:31:58 -05:00
{
}
2021-07-05 02:37:20 -04:00
2021-12-09 14:42:42 -05:00
void MAX31855::begin(const uint8_t select)
2021-01-29 06:31:58 -05:00
{
2021-12-09 14:42:42 -05:00
begin(255, select, 255);
}
void MAX31855::begin(const uint8_t clock, const uint8_t select, const uint8_t miso)
{
_clock = clock;
_miso = miso;
_select = select;
_hwSPI = (_clock == 255);
2021-08-12 05:47:56 -04:00
_lastTimeRead = 0;
_offset = 0;
_SeebeckC = K_TC;
_status = STATUS_NOREAD;
_temperature = MAX31855_NO_TEMPERATURE;
_internal = MAX31855_NO_TEMPERATURE;
_rawData = 0;
2021-12-09 14:42:42 -05:00
setSPIspeed(1000000);
2021-07-05 02:37:20 -04:00
2021-08-12 05:47:56 -04:00
pinMode(_select, OUTPUT);
digitalWrite(_select, HIGH);
2021-01-29 06:31:58 -05:00
if (_hwSPI)
{
2021-08-12 05:47:56 -04:00
#if defined(ESP32)
2022-11-16 09:46:22 -05:00
if (_useHSPI) // HSPI
2021-08-12 05:47:56 -04:00
{
mySPI = new SPIClass(HSPI);
mySPI->end();
2022-11-16 09:46:22 -05:00
mySPI->begin(14, 12, 13, _select); // CLK=14 MISO=12 MOSI=13
2021-08-12 05:47:56 -04:00
}
2022-11-16 09:46:22 -05:00
else // VSPI
2021-08-12 05:47:56 -04:00
{
mySPI = new SPIClass(VSPI);
mySPI->end();
2022-11-16 09:46:22 -05:00
mySPI->begin(18, 19, 23, _select); // CLK=18 MISO=19 MOSI=23
2021-08-12 05:47:56 -04:00
}
2022-11-16 09:46:22 -05:00
#else // generic hardware SPI
2021-08-12 05:47:56 -04:00
mySPI = &SPI;
mySPI->end();
mySPI->begin();
#endif
2021-01-29 06:31:58 -05:00
delay(1);
}
else
{
2021-08-12 05:47:56 -04:00
pinMode(_clock, OUTPUT);
digitalWrite(_clock, LOW);
2021-01-29 06:31:58 -05:00
pinMode(_miso, INPUT);
}
}
2021-08-12 05:47:56 -04:00
void MAX31855::setSPIspeed(uint32_t speed)
{
_SPIspeed = speed;
_spi_settings = SPISettings(_SPIspeed, MSBFIRST, SPI_MODE0);
};
#if defined(ESP32)
void MAX31855::setGPIOpins(uint8_t clock, uint8_t miso, uint8_t mosi, uint8_t select)
{
_clock = clock;
_miso = miso;
_select = select;
pinMode(_select, OUTPUT);
digitalWrite(_select, HIGH);
2021-12-09 14:42:42 -05:00
// disable SPI and enable again
mySPI->end();
2021-08-12 05:47:56 -04:00
mySPI->begin(clock, miso, mosi, select);
}
#endif
2021-01-29 06:31:58 -05:00
uint8_t MAX31855::read()
{
2022-11-16 09:46:22 -05:00
// return value of _read()
// BITS DESCRIPTION
// -------------------------------
// 00 - 02 STATUS
// 03 RESERVED
// 04 - 15 INTERNAL
// 16 FAULT-BIT
// 17 RESERVED
// 18 - 30 TEMPERATURE (RAW)
// 31 SIGN
2021-01-29 06:31:58 -05:00
uint32_t value = _read();
2022-11-16 09:46:22 -05:00
if (value == 0xFFFFFFFF) // needs a pull up on MISO pin to work properly!
2021-01-29 06:31:58 -05:00
{
2022-11-16 09:46:22 -05:00
// bit 3 and bit 17 should always be 0 - P10 datasheet
2021-01-29 06:31:58 -05:00
_status = STATUS_NO_COMMUNICATION;
return _status;
}
2021-08-12 05:47:56 -04:00
_lastTimeRead = millis();
2021-01-29 06:31:58 -05:00
2021-12-09 14:42:42 -05:00
// process status bit 0-2
2021-01-29 06:31:58 -05:00
_status = value & 0x0007;
2022-11-16 09:46:22 -05:00
// if (_status != STATUS_OK) // removed in 0.4.0 as internal can be valid.
2021-12-09 14:42:42 -05:00
// {
// return _status;
// }
2021-01-29 06:31:58 -05:00
value >>= 3;
2022-11-16 09:46:22 -05:00
// reserved bit 3, always 0
2021-01-29 06:31:58 -05:00
value >>= 1;
2022-11-16 09:46:22 -05:00
// process internal bit 4-15
2021-01-29 06:31:58 -05:00
_internal = (value & 0x07FF) * 0.0625;
2022-11-16 09:46:22 -05:00
// negative flag set ?
2021-08-12 05:47:56 -04:00
if (value & 0x0800)
2021-01-29 06:31:58 -05:00
{
_internal = -128 + _internal;
}
value >>= 12;
2022-11-16 09:46:22 -05:00
// Fault bit ignored as we have the 3 status bits
// _fault = value & 0x01;
2021-01-29 06:31:58 -05:00
value >>= 1;
2022-11-16 09:46:22 -05:00
// reserved bit 17, always 0
2021-01-29 06:31:58 -05:00
value >>= 1;
2022-11-16 09:46:22 -05:00
// process temperature bit 18-30 + sign bit = 31
2021-01-29 06:31:58 -05:00
_temperature = (value & 0x1FFF) * 0.25;
2022-11-16 09:46:22 -05:00
// negative flag set ?
2021-01-29 06:31:58 -05:00
if (value & 0x2000)
{
_temperature = -2048 + _temperature;
}
return _status;
}
uint32_t MAX31855::_read(void)
{
_rawData = 0;
2022-11-16 09:46:22 -05:00
// DATA TRANSFER
2021-01-29 06:31:58 -05:00
if (_hwSPI)
{
2021-08-12 05:47:56 -04:00
mySPI->beginTransaction(_spi_settings);
2023-11-12 04:16:51 -05:00
digitalWrite(_select, LOW); // must be after mySPI->beginTransaction() - see #14 STM32
2021-01-29 06:31:58 -05:00
for (uint8_t i = 0; i < 4; i++)
{
_rawData <<= 8;
2021-08-12 05:47:56 -04:00
_rawData += mySPI->transfer(0);
2021-01-29 06:31:58 -05:00
}
2021-08-12 05:47:56 -04:00
digitalWrite(_select, HIGH);
mySPI->endTransaction();
2021-01-29 06:31:58 -05:00
}
2022-11-16 09:46:22 -05:00
else // Software SPI
2021-01-29 06:31:58 -05:00
{
2022-11-16 09:46:22 -05:00
// split _swSPIdelay in equal dLow and dHigh
// dLow should be longer one when _swSPIdelay = odd.
2023-02-26 13:44:17 -05:00
uint16_t dHigh = _swSPIdelay / 2;
2021-12-10 05:12:41 -05:00
uint16_t dLow = _swSPIdelay - dHigh;
2021-08-12 05:47:56 -04:00
digitalWrite(_select, LOW);
2021-01-29 06:31:58 -05:00
for (int8_t i = 31; i >= 0; i--)
{
_rawData <<= 1;
2021-08-12 05:47:56 -04:00
digitalWrite(_clock, LOW);
2022-11-16 09:46:22 -05:00
if (dLow > 0) delayMicroseconds(dLow); // DUE might need 1 us
2021-01-29 06:31:58 -05:00
if ( digitalRead(_miso) ) _rawData++;
2021-08-12 05:47:56 -04:00
digitalWrite(_clock, HIGH);
2022-11-16 09:46:22 -05:00
if (dHigh > 0) delayMicroseconds(dHigh); // DUE
2021-01-29 06:31:58 -05:00
}
2021-08-12 05:47:56 -04:00
digitalWrite(_select, HIGH);
2021-01-29 06:31:58 -05:00
}
2021-07-05 02:37:20 -04:00
2021-01-29 06:31:58 -05:00
return _rawData;
}
2021-07-05 02:37:20 -04:00
2021-01-29 06:31:58 -05:00
float MAX31855::getTemperature()
{
2022-11-16 09:46:22 -05:00
// offset needs to be added after multiplication TCfactor
// not before otherwise offset will be larger / smaller
// default behaviour
2021-08-12 05:47:56 -04:00
if (_SeebeckC == K_TC) return _temperature + _offset;
2021-01-29 06:31:58 -05:00
2022-11-16 09:46:22 -05:00
// EXPERIMENTAL OTHER THERMOCOUPLES
// to be tested
// in practice this works also for K_TC but is way slower..
// 1: reverse calculate the Voltage measured (is this correct?)
float Vout = K_TC * (_temperature - _internal); // PAGE 8 datasheet
2021-01-29 06:31:58 -05:00
2022-11-16 09:46:22 -05:00
// 2: from Voltage to corrected temperature using the Seebeck Coefficient
2021-08-12 05:47:56 -04:00
float _temp = Vout / _SeebeckC + _internal + _offset;
2021-01-29 06:31:58 -05:00
return _temp;
}
2021-07-05 02:37:20 -04:00
2023-02-26 13:44:17 -05:00
// -- END OF FILE --
2021-12-09 14:42:42 -05:00