Skip to content

Commit

Permalink
Merge pull request #12 from RobTillaart/develop
Browse files Browse the repository at this point in the history
fix #8 support hardware SPI + example
  • Loading branch information
RobTillaart authored Aug 31, 2020
2 parents b67bc07 + 6aec1ca commit 88f1b40
Show file tree
Hide file tree
Showing 6 changed files with 145 additions and 20 deletions.
61 changes: 46 additions & 15 deletions MAX31855.cpp
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
//
// FILE: MAX31855.cpp
// AUTHOR: Rob Tillaart
// VERSION: 0.2.2
// VERSION: 0.2.3
// PURPOSE: Arduino library for MAX31855 chip for K type thermocouple
// DATE: 2014-01-01
// URL: https://github.com/RobTillaart/MAX31855_RT
//
// HISTORY:
//
// 0.2.3 2020-08-30 fix #8 support hardware SPI + example
// 0.2.2 2020-08-30 fix#9 + fix failing examples + minor refactor
// 0.2.1 2020-08-26 read rawData and STATUS_NO_COMMUNICATION recognition (thanks to FabioBrondo)
// 0.2.0 2020-06-20 #pragma once; major refactor; removed pre 1.0 support; fix offset
Expand All @@ -28,11 +29,25 @@
#include "MAX31855.h"


MAX31855::MAX31855(const uint8_t cs)
{
_cs = cs;
_hwSPI = true;

_offset = 0;
_SC = K_TC;
_status = STATUS_NOREAD;
_temperature = -999;
_internal = -999;
}

MAX31855::MAX31855(const uint8_t sclk, const uint8_t cs, const uint8_t miso)
{
_sclk = sclk;
_cs = cs;
_miso = miso;
_hwSPI = false;

_offset = 0;
_SC = K_TC;
_status = STATUS_NOREAD;
Expand All @@ -44,9 +59,16 @@ void MAX31855::begin()
{
pinMode(_cs, OUTPUT);
digitalWrite(_cs, HIGH);

pinMode(_sclk, OUTPUT);
pinMode(_miso, INPUT);
if (_hwSPI)
{
SPI.begin();
delay(1);
}
else
{
pinMode(_sclk, OUTPUT);
pinMode(_miso, INPUT);
}
}


Expand Down Expand Up @@ -115,21 +137,30 @@ uint8_t MAX31855::read()
uint32_t MAX31855::_read(void)
{
_rawData = 0;

digitalWrite(_cs, LOW);

for (int8_t i = 31; i >= 0; i--)
if (_hwSPI)
{
_rawData <<= 1;
digitalWrite(_sclk, LOW);
// delayMicroseconds(1); // DUE
if ( digitalRead(_miso) ) _rawData++;
digitalWrite(_sclk, HIGH);
// delayMicroseconds(1); // DUE
SPI.beginTransaction(SPISettings(16000000, MSBFIRST, SPI_MODE0));
for (uint8_t i = 0; i < 4; i++)
{
_rawData <<= 8;
_rawData += SPI.transfer(0);
}
SPI.endTransaction();
}
else
{
for (int8_t i = 31; i >= 0; i--)
{
_rawData <<= 1;
digitalWrite(_sclk, LOW);
// delayMicroseconds(1); // DUE
if ( digitalRead(_miso) ) _rawData++;
digitalWrite(_sclk, HIGH);
// delayMicroseconds(1); // DUE
}
}

digitalWrite(_cs, HIGH);

return _rawData;
}

Expand Down
10 changes: 7 additions & 3 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.2.2
// VERSION: 0.2.3
// PURPOSE: Arduino library for MAX31855 chip for K type thermocouple
// DATE: 2014-01-01
// URL: https://github.com/RobTillaart/MAX31855_RT
Expand All @@ -20,8 +20,9 @@
// +---------+

#include "Arduino.h"
#include "SPI.h"

#define MAX31855_VERSION "0.2.2"
#define MAX31855_VERSION "0.2.3"


// STATE constants returnd by read()
Expand Down Expand Up @@ -58,7 +59,9 @@
class MAX31855
{
public:

// HW SPI
MAX31855(uint8_t CS);
// SW SPI
MAX31855(uint8_t SCLK, uint8_t CS, uint8_t MISO);
void begin();

Expand Down Expand Up @@ -97,6 +100,7 @@ class MAX31855
float _SC;
uint32_t _lastRead;
uint32_t _rawData;
bool _hwSPI;

uint8_t _sclk;
uint8_t _miso;
Expand Down
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,28 @@ Library tested with breakout board
```

## Hardware SPI vs software SPI

Default pin connections (ESP32 has more options)

| HW SPI | UNO | ESP32 |
|:---------|:-----:|:-------:|
| CLOCKPIN | 13 | 18 |
| MISO | 12 | 19 |
| MOSI | 11 | 23 |

Performance read() function, timing in us.

| mode | clock | timng UNO |
|:----|----:|----:|
| HWSPI | 16000000 | ~68 |
| HWSPI | 4000000 | ~72 |
| HWSPI | 1000000 | ~100 |
| HWSPI | 500000 | ~128 |
| SWSPI | bitbang | ~500 |



## Interface

To make a temperature reading call **tc.read()**.
Expand Down
68 changes: 68 additions & 0 deletions examples/max31855_hwSPI/max31855_hwSPI.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
//
// FILE: max31855_hw_SPI.ino
// AUTHOR: Rob Tillaart
// VERSION: 0.1.0
// PURPOSE: thermocouple lib demo application
// DATE: 2020-08-30
// URL: https://github.com/RobTillaart/MAX31855_RT
//

#include "MAX31855.h"

//
// | HW SPI | UNO | ESP32 |
// |:---------|:-----:|:-------:|
// | CLOCKPIN | 13 | 18 |
// | MISO | 12 | 19 |
// | MOSI | 11 | 23 |

// read() timing UNO
// HWSPI 16000000 ~68 us
// HWSPI 4000000 ~72 us
// HWSPI 1000000 ~100 us
// HWSPI 500000 ~128 us
// SWSPI bitbang ~500 us

const int csPin = 6;

uint32_t start, stop;

MAX31855 tc(csPin);
// MAX31855 tc(13, 6, 12); // sw SPI

void setup()
{
Serial.begin(115200);
Serial.print("Start max31855_demo0: ");
Serial.println(MAX31855_VERSION);
Serial.println();

tc.begin();
}

void loop()
{
start = micros();
int status = tc.read();
stop = micros();

Serial.println();
Serial.print("time:\t\t");
Serial.println(stop - start);

Serial.print("stat:\t\t");
Serial.println(status);

uint32_t raw = tc.getRawData();
Serial.print("raw:\t\t");
Serial.println(raw, BIN);

float internal = tc.getInternal();
Serial.print("internal:\t");
Serial.println(internal);

float temp = tc.getTemperature();
Serial.print("temperature:\t");
Serial.println(temp);
delay(1000);
}
2 changes: 1 addition & 1 deletion library.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"type": "git",
"url": "https://github.com/RobTillaart/MAX31855_RT"
},
"version":"0.2.2",
"version":"0.2.3",
"frameworks": "arduino",
"platforms": "*"
}
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=MAX31855_RT
version=0.2.2
version=0.2.3
author=Rob Tillaart <[email protected]>
maintainer=Rob Tillaart <[email protected]>
sentence=Arduino library for MAX31855 chip for K type thermocouple.
Expand Down

0 comments on commit 88f1b40

Please sign in to comment.