Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100755 152 lines (109 sloc) 3.519 kb
dfb64d13 »
2011-08-11 Updated description and readme. Added includes to prevent errors
1 /*
03b73e6d »
2012-06-20 Fix indentation before I bother to read the code
2 MAX31855.cpp - Library for reading temperature from a MAX31855.
3
4 This work is licensed under a Creative Commons Attribution-ShareAlike 3.0
5 Unported License. http://creativecommons.org/licenses/by-sa/3.0/
dfb64d13 »
2011-08-11 Updated description and readme. Added includes to prevent errors
6 */
7
8 #include "MAX31855.h"
9
beb9bb05 »
2011-12-26 More debugging and features
10 MAX31855::MAX31855(int SCK_pin, int CS_pin, int SO_pin) {
03b73e6d »
2012-06-20 Fix indentation before I bother to read the code
11 _sck_pin = SCK_pin;
12 _cs_pin = CS_pin;
13 _so_pin = SO_pin;
14
15 //define pin modes
16 pinMode(_cs_pin, OUTPUT);
17 pinMode(_sck_pin, OUTPUT);
18 pinMode(_so_pin, INPUT);
19 digitalWrite(_cs_pin, HIGH);
dfb64d13 »
2011-08-11 Updated description and readme. Added includes to prevent errors
20 }
21
22
ca00e148 »
2011-12-23 interface fixes and updates for Arduino 1.0
23 double MAX31855::readCelsius(void) {
03b73e6d »
2012-06-20 Fix indentation before I bother to read the code
24 int v;
25 v = spiread16();
26 //if fault bit set // return 2000deg
eabfa0aa »
2012-06-20 Fix position of braces and add missing braces around bug-magnet brace…
27
28 if (v & 0x1) {
03b73e6d »
2012-06-20 Fix indentation before I bother to read the code
29 return 2000;
eabfa0aa »
2012-06-20 Fix position of braces and add missing braces around bug-magnet brace…
30 }
31
03b73e6d »
2012-06-20 Fix indentation before I bother to read the code
32 v&=0xfffc; // mask lower two bits
33 return v / 16.0;
dfb64d13 »
2011-08-11 Updated description and readme. Added includes to prevent errors
34 }
35
ca00e148 »
2011-12-23 interface fixes and updates for Arduino 1.0
36
37 double MAX31855::readFahrenheit(void) {
03b73e6d »
2012-06-20 Fix indentation before I bother to read the code
38 return readCelsius() * 9.0/5.0 + 32;
dfb64d13 »
2011-08-11 Updated description and readme. Added includes to prevent errors
39 }
40
ca00e148 »
2011-12-23 interface fixes and updates for Arduino 1.0
41
42 double MAX31855::readCJC(void) {
03b73e6d »
2012-06-20 Fix indentation before I bother to read the code
43 int tc, cjc;
44 spiread32(&tc, &cjc);
45 return (cjc & 0xfff0) / 256.0;
dfb64d13 »
2011-08-11 Updated description and readme. Added includes to prevent errors
46 }
47
48
2317f663 »
2012-01-31 fixed negative output values
49
ca00e148 »
2011-12-23 interface fixes and updates for Arduino 1.0
50 int MAX31855::readFaultCode(void) {
03b73e6d »
2012-06-20 Fix indentation before I bother to read the code
51 return (spiread32() & 0x7) ; // return low 3 bits
dfb64d13 »
2011-08-11 Updated description and readme. Added includes to prevent errors
52 }
ca00e148 »
2011-12-23 interface fixes and updates for Arduino 1.0
53
54
03b73e6d »
2012-06-20 Fix indentation before I bother to read the code
55 long MAX31855::spiread32(void) {
56 int i;
57 long d = 0; // we only need last 16 bits, first 16 will be discarded
58 digitalWrite(_cs_pin, LOW);
eabfa0aa »
2012-06-20 Fix position of braces and add missing braces around bug-magnet brace…
59
60 for (i=31; i>=0; i--) {
03b73e6d »
2012-06-20 Fix indentation before I bother to read the code
61 digitalWrite(_sck_pin, LOW);
62 if (digitalRead(_so_pin))
63 d |= (1 << i);
64 digitalWrite(_sck_pin, HIGH);
65 }
eabfa0aa »
2012-06-20 Fix position of braces and add missing braces around bug-magnet brace…
66
03b73e6d »
2012-06-20 Fix indentation before I bother to read the code
67 digitalWrite(_cs_pin, HIGH);
68 return d;
dfb64d13 »
2011-08-11 Updated description and readme. Added includes to prevent errors
69 }
ca00e148 »
2011-12-23 interface fixes and updates for Arduino 1.0
70
03b73e6d »
2012-06-20 Fix indentation before I bother to read the code
71 void MAX31855::spiread32(int *tc, int *cjc) {
72 int i;
73 *tc = *cjc = 0;
74 digitalWrite(_cs_pin, LOW);
eabfa0aa »
2012-06-20 Fix position of braces and add missing braces around bug-magnet brace…
75
76 for (i=15; i>=0; i--) {
03b73e6d »
2012-06-20 Fix indentation before I bother to read the code
77 digitalWrite(_sck_pin, LOW);
eabfa0aa »
2012-06-20 Fix position of braces and add missing braces around bug-magnet brace…
78
79 if (digitalRead(_so_pin)) {
03b73e6d »
2012-06-20 Fix indentation before I bother to read the code
80 *tc |= (1 << i);
eabfa0aa »
2012-06-20 Fix position of braces and add missing braces around bug-magnet brace…
81 }
82
03b73e6d »
2012-06-20 Fix indentation before I bother to read the code
83 digitalWrite(_sck_pin, HIGH);
84 }
eabfa0aa »
2012-06-20 Fix position of braces and add missing braces around bug-magnet brace…
85
86 for (i=15; i>=0; i--) {
03b73e6d »
2012-06-20 Fix indentation before I bother to read the code
87 digitalWrite(_sck_pin, LOW);
eabfa0aa »
2012-06-20 Fix position of braces and add missing braces around bug-magnet brace…
88
89 if (digitalRead(_so_pin)) {
03b73e6d »
2012-06-20 Fix indentation before I bother to read the code
90 *cjc |= (1 << i);
eabfa0aa »
2012-06-20 Fix position of braces and add missing braces around bug-magnet brace…
91 }
92
03b73e6d »
2012-06-20 Fix indentation before I bother to read the code
93 digitalWrite(_sck_pin, HIGH);
94 }
eabfa0aa »
2012-06-20 Fix position of braces and add missing braces around bug-magnet brace…
95
03b73e6d »
2012-06-20 Fix indentation before I bother to read the code
96 digitalWrite(_cs_pin, HIGH);
2317f663 »
2012-01-31 fixed negative output values
97 }
ca00e148 »
2011-12-23 interface fixes and updates for Arduino 1.0
98
03b73e6d »
2012-06-20 Fix indentation before I bother to read the code
99 int MAX31855::spiread16(void) {
100 int i;
101 int d = 0;
102 digitalWrite(_cs_pin, LOW);
eabfa0aa »
2012-06-20 Fix position of braces and add missing braces around bug-magnet brace…
103
104 for (i=15; i>=0; i--) {
03b73e6d »
2012-06-20 Fix indentation before I bother to read the code
105 digitalWrite(_sck_pin, LOW);
eabfa0aa »
2012-06-20 Fix position of braces and add missing braces around bug-magnet brace…
106
107 if (digitalRead(_so_pin)) {
03b73e6d »
2012-06-20 Fix indentation before I bother to read the code
108 d |= (1 << i);
eabfa0aa »
2012-06-20 Fix position of braces and add missing braces around bug-magnet brace…
109 }
110
03b73e6d »
2012-06-20 Fix indentation before I bother to read the code
111 digitalWrite(_sck_pin, HIGH);
112 }
eabfa0aa »
2012-06-20 Fix position of braces and add missing braces around bug-magnet brace…
113
03b73e6d »
2012-06-20 Fix indentation before I bother to read the code
114 digitalWrite(_cs_pin, HIGH);
115
116 return d;
dfb64d13 »
2011-08-11 Updated description and readme. Added includes to prevent errors
117 }
118
ca00e148 »
2011-12-23 interface fixes and updates for Arduino 1.0
119
beb9bb05 »
2011-12-26 More debugging and features
120 bool MAX31855::readMAX31855(double *tempTC, double *tempCJC, bool *faultOpen, bool *faultShortGND, bool *faultShortVCC, bool temp_unit){
03b73e6d »
2012-06-20 Fix indentation before I bother to read the code
121 int tc = 0;
122 int cjc = 0;
123 bool fault = false;
124
125 spiread32(&tc, &cjc);
126
eabfa0aa »
2012-06-20 Fix position of braces and add missing braces around bug-magnet brace…
127 if (tc & 0x1) {
03b73e6d »
2012-06-20 Fix indentation before I bother to read the code
128 fault=true;
129 *tempTC =9999;
eabfa0aa »
2012-06-20 Fix position of braces and add missing braces around bug-magnet brace…
130 } else {
03b73e6d »
2012-06-20 Fix indentation before I bother to read the code
131 tc&=0xfffc; // mask lower two bits
132 *tempTC = tc / 16.0;
eabfa0aa »
2012-06-20 Fix position of braces and add missing braces around bug-magnet brace…
133
03b73e6d »
2012-06-20 Fix indentation before I bother to read the code
134 if(temp_unit == 1) {
135 *tempTC = *tempTC * 9.0/5.0 + 32;
136 }
137 }
138
139 *faultOpen = (cjc & 0x1) ? true : false ;
140 *faultShortGND = ((cjc>>1) & 0x1) ? true : false;
141 *faultShortVCC = ((cjc>>2) & 0x1) ? true : false;
142 cjc = cjc & 0xfff0;// mask lower 4 bits
143 *tempCJC = cjc / 256.0;
eabfa0aa »
2012-06-20 Fix position of braces and add missing braces around bug-magnet brace…
144
03b73e6d »
2012-06-20 Fix indentation before I bother to read the code
145 if(temp_unit == 1) {
146 *tempCJC = *tempCJC * 9.0/5.0 + 32;
147 }
148
149 return fault;
dfb64d13 »
2011-08-11 Updated description and readme. Added includes to prevent errors
150 }
03b73e6d »
2012-06-20 Fix indentation before I bother to read the code
151
Something went wrong with that request. Please try again.