Skip to content

Commit 2c2316f

Browse files
committed
+ version 0.1.01
+ optimized _read() function for speed based upon numbers from datasheet + some refactoring + added demo2 - delta-read + added demo3 - measure timing
1 parent d59d553 commit 2c2316f

File tree

4 files changed

+120
-26
lines changed

4 files changed

+120
-26
lines changed

libraries/MAX31855/MAX31855.cpp

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//
22
// FILE: MAX31855.cpp
33
// AUTHOR: Rob Tillaart
4-
// VERSION: 0.1.00
4+
// VERSION: 0.1.01
55
// PURPOSE: MAX31855 - Thermocouple
66
// DATE: 2014-01-01
77
// URL:
@@ -11,7 +11,7 @@
1111

1212
#include "MAX31855.h"
1313

14-
MAX31855::MAX31855(uint8_t sclk, uint8_t cs, uint8_t miso)
14+
MAX31855::MAX31855(uint8_t sclk, uint8_t cs, uint8_t miso)
1515
{
1616
_sclk = sclk;
1717
_cs = cs;
@@ -30,57 +30,49 @@ void MAX31855::begin()
3030
uint8_t MAX31855::read()
3131
{
3232
uint32_t value = _read();
33-
33+
3434
// process status bit 0-2
3535
_status = value & 0x0007;
3636
value >>= 3;
3737

3838
// reserved bit 3
3939
value >>= 1;
40-
40+
4141
// process internal bit 4-15
4242
_internal = (value & 0x07FF) * 0.0625;
4343
if (value & 0x0800) _internal *= -1;
4444
value >>= 12;
45-
45+
4646
// Fault bit ignored as we have the 3 status bits
4747
// _fault = value & 0x01;
4848
value >>= 1;
49-
49+
5050
// reserved bit 17
5151
value >>= 1;
52-
52+
5353
// process temperature bit 18-31
5454
_temperature = (value & 0x1FFF) * 0.25;
5555
if (value & 0x2000) _temperature *= -1;
56-
56+
5757
return _status;
5858
}
5959

60-
61-
uint32_t MAX31855::_read(void)
60+
uint32_t MAX31855::_read(void)
6261
{
6362
uint32_t value = 0;
6463

65-
digitalWrite(_sclk, LOW);
66-
delayMicroseconds(1000);
6764
digitalWrite(_cs, LOW);
68-
delayMicroseconds(1000);
6965

70-
for (int8_t i=31; i>=0; i--)
66+
for (int8_t i = 31; i >= 0; i--)
7167
{
72-
digitalWrite(_sclk, LOW);
73-
delayMicroseconds(1000);
74-
7568
value <<= 1;
76-
if (digitalRead(_miso) == HIGH) value += 1;
77-
69+
digitalWrite(_sclk, LOW);
70+
if ( digitalRead(_miso) ) value += 1;
7871
digitalWrite(_sclk, HIGH);
79-
delayMicroseconds(1000);
8072
}
8173

8274
digitalWrite(_cs, HIGH);
83-
75+
8476
return value;
8577
}
8678

libraries/MAX31855/MAX31855.h

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
//
44
// FILE: MAX31855.h
55
// AUTHOR: Rob Tillaart
6-
// VERSION: 0.1.00
6+
// VERSION: 0.1.01
77
// PURPOSE: MAX31855 - Thermocouple
88
// DATE: 2014-01-01
99
// URL:
@@ -17,14 +17,14 @@
1717
#include "Arduino.h"
1818
#endif
1919

20-
#define MAX31855_VERSION "0.1.00"
20+
#define MAX31855_VERSION "0.1.01"
2121

2222
#define STATUS_OK 0x00
2323
#define STATUS_OPEN_CIRCUIT 0x01
2424
#define STATUS_SHORT_TO_GND 0x02
2525
#define STATUS_SHORT_TO_VCC 0x04
2626

27-
class MAX31855
27+
class MAX31855
2828
{
2929
public:
3030
MAX31855(uint8_t SCLK, uint8_t CS, uint8_t MISO);
@@ -40,10 +40,12 @@ class MAX31855
4040
float _internal;
4141
float _temperature;
4242
uint8_t _status;
43-
43+
4444
uint8_t _sclk;
4545
uint8_t _miso;
4646
uint8_t _cs;
4747
};
4848

49-
#endif
49+
#endif
50+
51+
// END OF FILE
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
//
2+
// FILE: max31855_demo2.ino
3+
// AUTHOR: Rob Tillaart
4+
// VERSION: 0.1.01
5+
// PURPOSE: thermocouple lib demo application
6+
// DATE: 2014-01-02
7+
// URL:
8+
//
9+
// Released to the public domain
10+
//
11+
12+
#include "MAX31855.h"
13+
14+
const int doPin = 7;
15+
const int csPin = 6;
16+
const int clPin = 5;
17+
18+
MAX31855 tc(clPin, csPin, doPin);
19+
20+
void setup()
21+
{
22+
Serial.begin(115200);
23+
Serial.print("Start max31855_demo: ");
24+
Serial.println(MAX31855_VERSION);
25+
Serial.println();
26+
27+
tc.begin();
28+
tc.read();
29+
}
30+
31+
void loop()
32+
{
33+
float t1 = tc.getTemperature();
34+
delay(1000);
35+
tc.read();
36+
float t2 = tc.getTemperature();
37+
Serial.print("delta:\t");
38+
Serial.println(t2-t1, 2);
39+
delay(1000);
40+
}
41+
42+
43+
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
//
2+
// FILE: max31855_demo2.ino
3+
// AUTHOR: Rob Tillaart
4+
// VERSION: 0.1.01
5+
// PURPOSE: thermocouple lib demo application
6+
// DATE: 2014-01-02
7+
// URL:
8+
//
9+
// Released to the public domain
10+
//
11+
12+
#include "MAX31855.h"
13+
14+
const int doPin = 7;
15+
const int csPin = 6;
16+
const int clPin = 5;
17+
18+
MAX31855 tc(clPin, csPin, doPin);
19+
20+
void setup()
21+
{
22+
Serial.begin(115200);
23+
Serial.print("Start max31855_demo: ");
24+
Serial.println(MAX31855_VERSION);
25+
Serial.println();
26+
27+
tc.begin();
28+
29+
uint32_t start = micros();
30+
tc.read();
31+
uint32_t stop = micros();
32+
Serial.print("read:\t");
33+
Serial.println(stop - start);
34+
35+
start = micros();
36+
float t1 = tc.getTemperature();
37+
stop = micros();
38+
Serial.print("getTemperature:\t");
39+
Serial.println(stop - start);
40+
41+
start = micros();
42+
tc.getInternal();
43+
stop = micros();
44+
Serial.print("getInternal:\t");
45+
Serial.println(stop - start);
46+
47+
Serial.println(t1,2);
48+
}
49+
50+
void loop()
51+
{
52+
}
53+
54+
55+
56+
57+

0 commit comments

Comments
 (0)