forked from rvagg/archived-node-max31855pi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
max31855.cc
46 lines (35 loc) · 1.02 KB
/
max31855.cc
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
#include "bcm2835.h"
#include "max31855.h"
MAX31855::MAX31855 (int sckPin, int csPin, int soPin)
: sckPin(sckPin), csPin(csPin), soPin(soPin) {
if (!bcm2835_init())
throw "Error initialising bcm2835";
bcm2835_gpio_fsel(csPin, BCM2835_GPIO_FSEL_OUTP);
bcm2835_gpio_fsel(sckPin, BCM2835_GPIO_FSEL_OUTP);
bcm2835_gpio_fsel(soPin, BCM2835_GPIO_FSEL_INPT);
bcm2835_gpio_write(csPin, HIGH);
bcm2835_delay(1);
}
MAX31855::~MAX31855 () {
bcm2835_close();
}
double MAX31855::ReadCelsius () {
uint32_t temp = 0;
int i;
bcm2835_gpio_write(sckPin, LOW);
bcm2835_delayMicroseconds(100);
bcm2835_gpio_write(csPin, LOW);
bcm2835_delayMicroseconds(100);
for (i = 15; i >= 0; i--) {
bcm2835_gpio_write(sckPin, LOW);
bcm2835_delayMicroseconds(100);
if (bcm2835_gpio_lev(soPin) == HIGH)
temp |= (1 << i);
bcm2835_gpio_write(sckPin, HIGH);
bcm2835_delayMicroseconds(100);
}
bcm2835_gpio_write(csPin, HIGH);
bcm2835_delayMicroseconds(100);
temp &= 0xfffc;
return temp / 16.0;
}