forked from adafruit/MAX6675-library
-
Notifications
You must be signed in to change notification settings - Fork 0
/
max6675.cpp
95 lines (76 loc) · 1.71 KB
/
max6675.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
// this library is public domain. enjoy!
// www.ladyada.net/learn/sensors/thermocouple
#ifdef __AVR
#include <avr/pgmspace.h>
#elif defined(ESP8266)
#include <pgmspace.h>
#endif
#include <util/delay.h>
#include <stdlib.h>
#include "max6675.h"
#include <SPI.h>
MAX6675::MAX6675(int8_t SCLK, int8_t CS, int8_t MISO) : hwSPI(false) {
sclk = SCLK;
cs = CS;
miso = MISO;
//define pin modes
pinMode(cs, OUTPUT);
pinMode(sclk, OUTPUT);
pinMode(miso, INPUT);
digitalWrite(cs, HIGH);
}
MAX6675::MAX6675(int8_t CS) : hwSPI(true) {
cs = CS;
//define pin modes
pinMode(cs, OUTPUT);
digitalWrite(cs, HIGH);
}
double MAX6675::readCelsius(void) {
uint16_t v;
digitalWrite(cs, LOW);
// CSB Fall to Output Enable
delayMicroseconds(1);
if (hwSPI) {
#ifdef MAX6675_LIBRARY_HW_SLOWDOWN
uint8_t oldSPCR = SPCR;
SPCR |= 3; // As slow as possible (clock/128 or clock/64 depending on SPI2X)
#endif // MAX6675_LIBRARY_HW_SLOWDOWN
v = SPI.transfer16(0);
#ifdef MAX6675_LIBRARY_HW_SLOWDOWN
SPCR = oldSPCR;
#endif // MAX6675_LIBRARY_HW_SLOWDOWN
} else {
v = spiread();
v <<= 8;
v |= spiread();
}
digitalWrite(cs, HIGH);
// CSB Rise to Output Disable
delayMicroseconds(1);
if (v & 0x4) {
// uh oh, no thermocouple attached!
return NAN;
//return -100;
}
v >>= 3;
return v*0.25;
}
double MAX6675::readFahrenheit(void) {
return readCelsius() * 9.0/5.0 + 32;
}
byte MAX6675::spiread(void) {
int i;
byte d = 0;
for (i=7; i>=0; i--)
{
digitalWrite(sclk, LOW);
_delay_ms(1);
if (digitalRead(miso)) {
//set the bit to 0 no matter what
d |= (1 << i);
}
digitalWrite(sclk, HIGH);
_delay_ms(1);
}
return d;
}