GY-63_MS5611/libraries/MAX6675/MAX6675.cpp

148 lines
2.9 KiB
C++
Raw Normal View History

2022-01-12 08:49:49 -05:00
//
// FILE: MAX6675.cpp
// AUTHOR: Rob Tillaart
2024-01-19 12:32:44 -05:00
// VERSION: 0.3.0
2022-01-12 08:49:49 -05:00
// PURPOSE: Arduino library for MAX6675 chip for K type thermocouple
// DATE: 2022-01-11
// URL: https://github.com/RobTillaart/MAX6675
#include "MAX6675.h"
2023-11-29 05:22:40 -05:00
// HW SPI
MAX6675::MAX6675(uint8_t select, __SPI_CLASS__ * mySPI)
2022-01-12 08:49:49 -05:00
{
2023-11-29 05:22:40 -05:00
_select = select;
_miso = 255;
_clock = 255;
_mySPI = mySPI;
_hwSPI = true;
2022-01-12 08:49:49 -05:00
}
2023-11-29 05:22:40 -05:00
// SW SPI
MAX6675::MAX6675(uint8_t select, uint8_t miso, uint8_t clock)
2022-01-12 08:49:49 -05:00
{
2023-11-29 05:22:40 -05:00
_select = select;
_miso = miso;
_clock = clock;
_mySPI = NULL;
_hwSPI = false;
2022-01-12 08:49:49 -05:00
}
2023-11-29 05:22:40 -05:00
void MAX6675::begin()
2022-01-12 08:49:49 -05:00
{
_lastTimeRead = 0;
_offset = 0;
_status = STATUS_NOREAD;
_temperature = MAX6675_NO_TEMPERATURE;
_rawData = 0;
2023-11-29 05:22:40 -05:00
2022-01-12 08:49:49 -05:00
setSPIspeed(1000000);
pinMode(_select, OUTPUT);
digitalWrite(_select, HIGH);
if (_hwSPI)
{
2024-01-19 12:32:44 -05:00
// _mySPI->end();
// _mySPI->begin();
// delay(1);
2022-01-12 08:49:49 -05:00
}
else
{
pinMode(_clock, OUTPUT);
digitalWrite(_clock, LOW);
pinMode(_miso, INPUT);
}
}
void MAX6675::setSPIspeed(uint32_t speed)
{
_SPIspeed = speed;
_spi_settings = SPISettings(_SPIspeed, MSBFIRST, SPI_MODE0);
};
uint8_t MAX6675::read()
{
2022-11-16 07:14:42 -05:00
// return value of _read() page 5 datasheet
// BITS DESCRIPTION
// ------------------------------
// 00 three state ?
// 01 device ID ?
// 02 INPUT OPEN
// 03 - 14 TEMPERATURE (RAW)
// 15 SIGN
2022-01-12 08:49:49 -05:00
uint16_t value = _read();
2023-11-12 04:18:34 -05:00
// needs a pull up on MISO pin to work properly!
2022-01-12 08:49:49 -05:00
if (value == 0xFFFF)
{
_status = STATUS_NO_COMMUNICATION;
return _status;
}
_lastTimeRead = millis();
// process status bit 2
_status = value & 0x04;
value >>= 3;
2022-11-16 07:14:42 -05:00
// process temperature bits
2022-01-12 08:49:49 -05:00
_temperature = (value & 0x1FFF) * 0.25;
2022-11-16 07:14:42 -05:00
// dummy negative flag set ?
// if (value & 0x2000)
2022-01-12 08:49:49 -05:00
return _status;
}
2023-11-29 05:22:40 -05:00
///////////////////////////////////////////////////
//
// PRIVATE
//
2022-01-12 08:49:49 -05:00
uint32_t MAX6675::_read(void)
{
_rawData = 0;
2022-11-16 07:14:42 -05:00
// DATA TRANSFER
2022-01-12 08:49:49 -05:00
if (_hwSPI)
{
2023-11-29 05:22:40 -05:00
_mySPI->beginTransaction(_spi_settings);
// must be after mySPI->beginTransaction() - STM32 (#14 MAX31855_RT)
2022-01-12 08:49:49 -05:00
digitalWrite(_select, LOW);
2023-11-29 05:22:40 -05:00
_rawData = _mySPI->transfer(0);
2022-01-12 08:49:49 -05:00
_rawData <<= 8;
2023-11-29 05:22:40 -05:00
_rawData += _mySPI->transfer(0);
2022-01-12 08:49:49 -05:00
digitalWrite(_select, HIGH);
2023-11-29 05:22:40 -05:00
_mySPI->endTransaction();
2022-01-12 08:49:49 -05:00
}
2022-11-16 07:14:42 -05:00
else // Software SPI
2022-01-12 08:49:49 -05:00
{
2022-11-16 07:14:42 -05:00
// split _swSPIdelay in equal dLow and dHigh
// dLow should be longer one when _swSPIdelay = odd.
2023-11-12 04:18:34 -05:00
uint16_t dHigh = _swSPIdelay / 2;
2023-11-29 05:22:40 -05:00
uint16_t dLow = _swSPIdelay - dHigh;
2022-01-12 08:49:49 -05:00
digitalWrite(_select, LOW);
for (int8_t i = 15; i >= 0; i--)
{
_rawData <<= 1;
digitalWrite(_clock, LOW);
2022-11-16 07:14:42 -05:00
if (dLow > 0) delayMicroseconds(dLow); // DUE might need 1 us
2022-01-12 08:49:49 -05:00
if ( digitalRead(_miso) ) _rawData++;
digitalWrite(_clock, HIGH);
2022-11-16 07:14:42 -05:00
if (dHigh > 0) delayMicroseconds(dHigh); // DUE
2022-01-12 08:49:49 -05:00
}
digitalWrite(_select, HIGH);
}
return _rawData;
}
2023-11-12 04:18:34 -05:00
// -- END OF FILE --
2022-01-12 08:49:49 -05:00