Skip to content

Commit

Permalink
fix #21 move constructor code to begin() - breaking change (#22)
Browse files Browse the repository at this point in the history
* fix #21 breaking change
* move constructor code to begin()
* fix examples
* removed "premature" return from read() so internal and temperature are always converted from raw data.
  • Loading branch information
RobTillaart authored Dec 9, 2021
1 parent 61c538d commit 195afcd
Show file tree
Hide file tree
Showing 23 changed files with 183 additions and 88 deletions.
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2014-2021 Rob Tillaart
Copyright (c) 2014-2022 Rob Tillaart

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
47 changes: 26 additions & 21 deletions MAX31855.cpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
//
// FILE: MAX31855.cpp
// AUTHOR: Rob Tillaart
// VERSION: 0.3.0
// VERSION: 0.4.0
// PURPOSE: Arduino library for MAX31855 chip for K type thermocouple
// DATE: 2014-01-01
// URL: https://github.com/RobTillaart/MAX31855_RT
//
// HISTORY:
// 0.4.0 2021-12-09 fix #21 breaking change for HW SPI
// move constructor code to begin()
// read() removed "premature" return on status.
// 0.3.0 2021-08-11 VSPI / HSPI support for ESP32
// add setGIOpins - ESP32 specific
// add get/setSPIspeed() - all
Expand All @@ -33,18 +36,23 @@
#include "MAX31855.h"


MAX31855::MAX31855(const uint8_t select)
MAX31855::MAX31855()
{
MAX31855(255, select, 255);
}


MAX31855::MAX31855(const uint8_t clock, const uint8_t select, const uint8_t miso)
void MAX31855::begin(const uint8_t select)
{
_clock = clock;
_select = select;
_miso = miso;
_hwSPI = (clock == 255);
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);

_lastTimeRead = 0;
_offset = 0;
Expand All @@ -53,16 +61,11 @@ MAX31855::MAX31855(const uint8_t clock, const uint8_t select, const uint8_t miso
_temperature = MAX31855_NO_TEMPERATURE;
_internal = MAX31855_NO_TEMPERATURE;
_rawData = 0;
}

setSPIspeed(1000000);

void MAX31855::begin()
{
pinMode(_select, OUTPUT);
digitalWrite(_select, HIGH);

_spi_settings = SPISettings(_SPIspeed, MSBFIRST, SPI_MODE0);

if (_hwSPI)
{
#if defined(ESP32)
Expand Down Expand Up @@ -110,7 +113,8 @@ void MAX31855::setGPIOpins(uint8_t clock, uint8_t miso, uint8_t mosi, uint8_t se
pinMode(_select, OUTPUT);
digitalWrite(_select, HIGH);

mySPI->end(); // disable SPI
// disable SPI and enable again
mySPI->end();
mySPI->begin(clock, miso, mosi, select);
}
#endif
Expand All @@ -130,7 +134,7 @@ uint8_t MAX31855::read()
// 31 SIGN
uint32_t value = _read();

if (value == 0xFFFFFFFF) // needs a pull up on miso pin to work properly!
if (value == 0xFFFFFFFF) // needs a pull up on MISO pin to work properly!
{
// bit 3 and bit 17 should always be 0 - P10 datasheet
_status = STATUS_NO_COMMUNICATION;
Expand All @@ -139,12 +143,12 @@ uint8_t MAX31855::read()

_lastTimeRead = millis();

// process status bit 0-2
// process status bit 0-2
_status = value & 0x0007;
if (_status != STATUS_OK)
{
return _status;
}
// if (_status != STATUS_OK) // removed in 0.4.0 as internal can be valid.
// {
// return _status;
// }

value >>= 3;

Expand Down Expand Up @@ -233,3 +237,4 @@ float MAX31855::getTemperature()


// -- END OF FILE --

17 changes: 9 additions & 8 deletions MAX31855.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//
// FILE: MAX31855.h
// AUTHOR: Rob Tillaart
// VERSION: 0.3.0
// VERSION: 0.4.0
// PURPOSE: Arduino library for MAX31855 chip for K type thermocouple
// DATE: 2014-01-01
// URL: https://github.com/RobTillaart/MAX31855_RT
Expand All @@ -25,7 +25,7 @@
#include "SPI.h"


#define MAX31855_VERSION (F("0.3.0"))
#define MAX31855_VERSION (F("0.4.0"))

#define MAX31855_NO_TEMPERATURE -999

Expand Down Expand Up @@ -63,12 +63,13 @@
class MAX31855
{
public:
// HW SPI
MAX31855(uint8_t select);
// SW SPI
MAX31855(uint8_t clock, uint8_t select, uint8_t miso);

void begin();
MAX31855();

// HW SPI
void begin(uint8_t select);
// SW SPI
void begin(uint8_t clock, uint8_t select, uint8_t miso);

// returns state - bit field: 0 = STATUS_OK
uint8_t read();
Expand Down Expand Up @@ -128,7 +129,7 @@ class MAX31855
uint8_t _miso;
uint8_t _select;

uint32_t _SPIspeed = 1000000;
uint32_t _SPIspeed;
SPIClass * mySPI;
SPISettings _spi_settings;
#if defined(ESP32)
Expand Down
27 changes: 17 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@
[![License: MIT](https://img.shields.io/badge/license-MIT-green.svg)](https://github.com/RobTillaart/MAX31855_RT/blob/master/LICENSE)
[![GitHub release](https://img.shields.io/github/release/RobTillaart/MAX31855_RT.svg?maxAge=3600)](https://github.com/RobTillaart/MAX31855_RT/releases)


# MAX31855_RT

Arduino library for MAX31855 chip for K type thermocouple.

The library has experimental support for other types of thermocouples E, J, N, R, S, T
The library has experimental support for other types of thermocouples E, J, N, R, S, T.


## Description
Expand Down Expand Up @@ -42,11 +43,11 @@ Library tested with breakout board
Default pin connections. ESP32 can overrule with **setGPIOpins()**.

| HW SPI | UNO | ESP32 VSPI | ESP32 HSPI | Notes
|:---------|:-----:|:-------:|:-------:|:----------|
| CLOCKPIN | 13 | 18 | 14 |
| MISO | 12 | 19 | 12 |
| MOSI | 11 | 23 | 13 | *not used...*
| SELECT | eg. 4 | 5 | 15 | *can be others too.*
|:---------|:-----:|:-----------:|:-----------:|:----------|
| CLOCKPIN | 13 | 18 | 14 |
| MISO | 12 | 19 | 12 |
| MOSI | 11 | 23 | 13 | *not used...*
| SELECT | eg. 4 | 5 | 15 | *can be others too.*


Performance read() function, timing in us. (ESP32 @240MHz)
Expand All @@ -67,16 +68,17 @@ Performance read() function, timing in us. (ESP32 @240MHz)

### Constructor

- **MAX31855(const uint8_t select)** create object and set select pin => hardware SPI
- **MAX31855(const uint8_t sclk, const uint8_t select, const uint8_t miso)** create object, set clock, select and miso pin => software SPI
- **MAX31855()** create object.
- **void begin(const uint8_t select)** set select pin => hardware SPI
- **void begin(const uint8_t sclk, const uint8_t select, const uint8_t miso)** set clock, select and miso pin => software SPI


### Hardware SPI

To be used only if one needs a specific speed.

- **void setSPIspeed(uint32_t speed)** set SPI transfer rate
- **uint32_t getSPIspeed()** returns SPI transfer rate
- **void setSPIspeed(uint32_t speed)** set SPI transfer rate.
- **uint32_t getSPIspeed()** returns SPI transfer rate.


### ESP32 specific
Expand Down Expand Up @@ -228,6 +230,11 @@ or

See examples

#### breaking change 0.4.0

In issue #21 it became clear that the code in the constructor is not always executed correctly.
Therefore this code + parameters is moved to the **Begin()** function.


## Experimental part (to be tested)

Expand Down
11 changes: 8 additions & 3 deletions examples/Demo_getRawData/Demo_getRawData.ino
Original file line number Diff line number Diff line change
@@ -1,27 +1,30 @@
//
// FILE: Demo_getRawData.ino
// AUTHOR: FabioBrondo
// VERSION: 0.1.1
// VERSION: 0.4.0
// PURPOSE: thermocouple lib demo application
// DATE: 2020-08-24
// URL: https://github.com/RobTillaart/MAX31855_RT
//


#include <SPI.h>
#include <MAX31855.h>


#define MAXDO 7 // Defining the MISO pin
#define MAXCS 6 // Defining the CS pin
#define MAXCLK 5 // Defining the SCK pin


MAX31855 thermocouple(MAXCLK, MAXCS, MAXDO);
MAX31855 thermocouple;


void setup ()
{
Serial.begin(115200);
delay(250);
thermocouple.begin();
thermocouple.begin(MAXCLK, MAXCS, MAXDO);
}


Expand Down Expand Up @@ -52,4 +55,6 @@ void loop ()
delay(100);
}


// -- END OF FILE --

1 change: 1 addition & 0 deletions examples/max31855_ESP32_HSPI/.arduino-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ compile:
# - leonardo
# - due
# - zero
- esp32
12 changes: 8 additions & 4 deletions examples/max31855_ESP32_HSPI/max31855_ESP32_HSPI.ino
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
// one might to need to disconnect pin 12 during upload of the code
// when HSPI is used.


#include "MAX31855.h"


Expand All @@ -35,8 +36,7 @@ const int csPin = 15;

uint32_t start, stop;


MAX31855 tc(csPin);
MAX31855 tc;


void setup()
Expand All @@ -49,8 +49,11 @@ void setup()
Serial.println();

tc.selectHSPI(); // needs to be called before begin()
tc.begin();
tc.setSPIspeed(16000000);

tc.begin(csPin);
// tc.begin(14, csPin, 12); // SW SPI for testing

tc.setSPIspeed(1000000);
}


Expand Down Expand Up @@ -90,3 +93,4 @@ void loop()


// -- END OF FILE --

1 change: 1 addition & 0 deletions examples/max31855_ESP32_VSPI/.arduino-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ compile:
# - leonardo
# - due
# - zero
- esp32
10 changes: 7 additions & 3 deletions examples/max31855_ESP32_VSPI/max31855_ESP32_VSPI.ino
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@
// | MOSI | 11 | 23 | 13 | * not used...
// | SELECT | eg. 4 | 5 | 15 | * can be others too.

const int csPin = 15;
const int csPin = 25;

uint32_t start, stop;


MAX31855 tc(csPin);
MAX31855 tc;


void setup()
Expand All @@ -47,7 +47,10 @@ void setup()
Serial.println();

tc.selectVSPI(); // needs to be called before begin()
tc.begin();

tc.begin(csPin);
// tc.begin(14, csPin, 12); // SW SPI for testing

tc.setSPIspeed(16000000);
}

Expand Down Expand Up @@ -88,3 +91,4 @@ void loop()


// -- END OF FILE --

16 changes: 11 additions & 5 deletions examples/max31855_array/max31855_array.ino
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,18 @@
const int dataPin = 7;
const int clockPin = 6;

MAX31855 sensors[] =

MAX31855 A, B, C;

MAX31855 sensors[] =
{
MAX31855(clockPin, 5, dataPin),
MAX31855(clockPin, 4, dataPin),
MAX31855(clockPin, 3, dataPin)
A, B, C
};


const uint8_t sensorCount = sizeof(sensors) / sizeof(MAX31855);


void setup()
{
Serial.begin(115200);
Expand All @@ -35,10 +38,11 @@ void setup()

for (int i = 0; i < sensorCount; i++)
{
sensors[i].begin();
sensors[i].begin(clockPin, 3 + i, dataPin); // three different select pins.
}
}


void loop()
{
Serial.print("\tTime:\t");
Expand Down Expand Up @@ -66,4 +70,6 @@ void loop()
delay(1000);
}


// -- END OF FILE --

Loading

0 comments on commit 195afcd

Please sign in to comment.