-
Notifications
You must be signed in to change notification settings - Fork 0
/
max31855.c
62 lines (56 loc) · 1.13 KB
/
max31855.c
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
#include <stdint.h>
#include "max31855.h"
void setupMAX31855(unsigned char (*SPIgetc)(void))
{
GetSPIc = SPIgetc;
MAX31855_CS_TRIS = 0;
MAX31855_CS_LAT = 1;
}
int16_t readFirst16bits(void)
{
int16_t ret = 0;
MAX31855_CS_LAT = 0;
for (int8_t i = 1; i >= 0; i--)
{
unsigned char tmp = GetSPIc();
ret |= (tmp << (i*8));
}
MAX31855_CS_LAT = 1;
return ret;
}
int16_t readSecond32bits(void)
{
int16_t ret = 0;
MAX31855_CS_LAT = 0;
for (int8_t i = 3; i >= 0; i--)
{
unsigned char tmp = GetSPIc();
if (i <= 1)
ret |= (tmp << (i*8));
}
MAX31855_CS_LAT = 1;
return ret;
}
float readCelsius(void)
{
int16_t v = readFirst16bits();
if (v & 0x1)
return 9999.0;
if (v & 0x8000)
return (((~v) & 0xfffc) + 0x4) / -32.0;
else
return (v & 0xfffc) / 32.0;
}
float readCJCelsius(void)
{
int16_t v = readSecond32bits();
if (v & 0x8000)
return (((~v) & 0xfff0) + 0x10) / -512.0;
else
return (v & 0xfff0) / 512.0;
}
char readStatus(void)
{
int16_t v = readSecond32bits();
return v & 0x7;
}