1- /* ************************************************************************
2- * RTD shield libary - Version 1
3- * by Christodoulos P. Lekkos <tolis81@gmail.com> , August 10, 2012.
4- *
5- * This file is free software; you can redistribute it and/or modify
6- * it under the terms of either the GNU General Public License version 3
7- * published by the Free Software Foundation.
8- *************************************************************************/
9-
10- #if defined(ARDUINO) && ARDUINO >= 100
11- #include < Arduino.h>
12- #else
13- #include < WProgram.h>
14- #endif
15-
16- #include < RTD_v1.h>
17- #include < ../SPI/SPI.h>
18-
19- #define WIRE_COMP_OFFSET -0.7 // mV
20- #define VREF 2.31 // reference voltage (V)
21- #define IEX 0.9537 // excitation current (mA)
22- #define EOCPIN 12 // EOC pin for conversion monitoring
23-
24- const int _muxPin[] = {5 , 4 , 3 }; // pins used for multiplexing
25- const int _csPin = 10 ; // ADC cs pin
26- int _muxPinState[] = {0 , 0 , 0 }; // array to store current channel configuration
27-
28- RTD::RTD (int nChannel)
29- {
30- _nChannel = nChannel;
31- for (int i = 0 ; i < 3 ; i++) pinMode (_muxPin[i], OUTPUT); // configure the multiplexer pins
32- }
33-
34- void RTD::ADC_INIT () // function to initialize a measurement after the appropriate channel has been choosen
35- {
36- digitalWrite (_csPin, LOW);
37- byte initByte = SPI.transfer (0x00 );
38- digitalWrite (_csPin, HIGH);
39- }
40-
41- void RTD::MUX_SELECT (int _nChannel) // function for channel selection
42- {
43- _muxPinState[0 ] = bitRead (_nChannel,0 );
44- _muxPinState[1 ] = bitRead (_nChannel,1 );
45- _muxPinState[2 ] = bitRead (_nChannel,2 );
46- digitalWrite (_muxPin[0 ], _muxPinState[0 ]);
47- digitalWrite (_muxPin[1 ], _muxPinState[1 ]);
48- digitalWrite (_muxPin[2 ], _muxPinState[2 ]);
49- // Serial.print("Channel Selected: "); // uncomment for debugging
50- // Serial.println(_nChannel); // uncomment for debugging
51- }
52-
53- long RTD::ADC_READ () // function to read the ADC
54- {
55- digitalWrite (_csPin, LOW);
56- if (digitalRead (EOCPIN))
57- {
58- // Serial.println("!EOC"); // uncomment for debugging
59- digitalWrite (_csPin, HIGH);
60- ADC_READ ();
61- }
62- else
63- {
64- byte adcByte = SPI.transfer (0x00 );
65- // Serial.println(adcByte, BIN); // uncomment for debugging
66- adcByte &= 0x0F ;
67- // Serial.println(adcByte, BIN); // uncomment for debugging
68- unsigned long adcValue = adcByte << 8 ;
69- adcByte = SPI.transfer (0x00 );
70- // Serial.println(adcByte, BIN); // uncomment for debugging
71- adcValue |= adcByte;
72- adcValue <<= 8 ;
73- adcByte = SPI.transfer (0x00 );
74- // Serial.println(adcByte, BIN); // uncomment for debugging
75- adcValue |= adcByte;
76- digitalWrite (_csPin, HIGH);
77- return adcValue;
78- }
79- }
80-
81- double RTD::ADC_2_mV (unsigned long adcRaw) // convert raaw ADC data to mV
82- {
83- double mV = (double )adcRaw*0.00000095367431640625 *(double )VREF*1000 + (double )WIRE_COMP_OFFSET;
84- // Serial.println(mV); // uncomment for debugging
85- return mV ;
86- }
87-
88- double RTD::OHM2CELSIUS (double mVadc ) // calculate temperature from mV (degree Celcius)
89- {
90- double ohm = mVadc / (double )IEX;
91- double celsius = (0.000000003 *ohm*ohm*ohm*ohm - 0.000001 *ohm*ohm*ohm + 0.0013 *ohm*ohm + 2.3141 *ohm - 243.85 );
92- // double celsius = 3*pow(10, -9)*pow(ohm, 4)-1*pow(10, -6)*pow(ohm, 3)+0.0013*pow(ohm, 2)+2.3141*ohm - 243.85;
93- // Serial.println(ohm); // uncomment for debugging
94- return celsius;
95- }
96-
97- double RTD::tempC () // main function to read the RTD temperature in degree Celsius
98- {
99- MUX_SELECT (_nChannel);
100- ADC_INIT ();
101- long adcRaw = ADC_READ ();
102- double rtdTempC = OHM2CELSIUS (ADC_2_mV (adcRaw));
103- return rtdTempC;
104- }
105-
106- double RTD::tempK () // convert to Kelvin
107- {
108- double rtdTempK = tempC ();
109- rtdTempK += 273.0 ;
110- return rtdTempK;
111- }
112-
113- double RTD::tempF () // convert to degree Fahrenheit
114- {
115- double rtdTempF = tempC ();
116- rtdTempF = (rtdTempF*9 /5 )+32 ;
117- return rtdTempF;
1+ /* ************************************************************************
2+ * RTD shield libary - Version 1
3+ * by Christodoulos P. Lekkos <tolis81@gmail.com> , August 10, 2012.
4+ *
5+ * This file is free software; you can redistribute it and/or modify
6+ * it under the terms of either the GNU General Public License version 3
7+ * published by the Free Software Foundation.
8+ *************************************************************************/
9+
10+ #if defined(ARDUINO) && ARDUINO >= 100
11+ #include < Arduino.h>
12+ #else
13+ #include < WProgram.h>
14+ #endif
15+
16+ #include < RTD_v1.h>
17+ #include < ../SPI/SPI.h>
18+
19+ #define WIRE_COMP_OFFSET -0.7 // mV
20+ #define VREF 2.31 // reference voltage (V)
21+ #define IEX 0.9537 // excitation current (mA)
22+ #define EOCPIN 12 // ! EOC pin for conversion monitoring
23+
24+ const int _muxPin[] = {5 , 4 , 3 }; // pins used for multiplexing
25+ const int _csPin = 10 ; // ADC cs pin
26+ int _muxPinState[] = {0 , 0 , 0 }; // array to store current channel configuration
27+
28+ RTD::RTD (int nChannel)
29+ {
30+ _nChannel = nChannel;
31+ for (int i = 0 ; i < 3 ; i++) pinMode (_muxPin[i], OUTPUT); // configure the multiplexer pins
32+ }
33+
34+ void RTD::ADC_INIT () // function to initialize a measurement after the appropriate channel has been choosen
35+ {
36+ digitalWrite (_csPin, LOW);
37+ byte initByte = SPI.transfer (0x00 );
38+ digitalWrite (_csPin, HIGH);
39+ }
40+
41+ void RTD::MUX_SELECT (int _nChannel) // function for channel selection
42+ {
43+ _muxPinState[0 ] = bitRead (_nChannel,0 );
44+ _muxPinState[1 ] = bitRead (_nChannel,1 );
45+ _muxPinState[2 ] = bitRead (_nChannel,2 );
46+ digitalWrite (_muxPin[0 ], _muxPinState[0 ]);
47+ digitalWrite (_muxPin[1 ], _muxPinState[1 ]);
48+ digitalWrite (_muxPin[2 ], _muxPinState[2 ]);
49+ // Serial.print("Channel Selected: "); // uncomment for debugging
50+ // Serial.println(_nChannel); // uncomment for debugging
51+ }
52+
53+ long RTD::ADC_READ () // function to read the ADC
54+ {
55+ digitalWrite (_csPin, LOW);
56+ if (digitalRead (EOCPIN))
57+ {
58+ // Serial.println("!EOC"); // uncomment for debugging
59+ digitalWrite (_csPin, HIGH);
60+ ADC_READ ();
61+ }
62+ else
63+ {
64+ byte adcByte = SPI.transfer (0x00 );
65+ // Serial.println(adcByte, BIN); // uncomment for debugging
66+ adcByte &= 0x0F ;
67+ // Serial.println(adcByte, BIN); // uncomment for debugging
68+ long adcValue = adcByte << 8 ;
69+ adcByte = SPI.transfer (0x00 );
70+ // Serial.println(adcByte, BIN); // uncomment for debugging
71+ adcValue |= adcByte;
72+ adcValue <<= 8 ;
73+ adcByte = SPI.transfer (0x00 );
74+ // Serial.println(adcByte, BIN); // uncomment for debugging
75+ adcValue |= adcByte;
76+ digitalWrite (_csPin, HIGH);
77+ return adcValue;
78+ }
79+ }
80+
81+ double RTD::ADC_2_mV (long adcRaw) // convert raw ADC data to mV
82+ {
83+ double mV = (double )adcRaw*0.00000095367431640625 *(double )VREF*1000 + (double )WIRE_COMP_OFFSET;
84+ // Serial.println(mV); // uncomment for debugging
85+ return mV ;
86+ }
87+
88+ double RTD::OHM2CELSIUS (double mVadc ) // calculate temperature from mV (degree Celcius)
89+ {
90+ double ohm = mVadc / (double )IEX;
91+ double celsius = (0.000000003 *ohm*ohm*ohm*ohm - 0.000001 *ohm*ohm*ohm + 0.0013 *ohm*ohm + 2.3141 *ohm - 243.85 );
92+ // double celsius = 3*pow(10, -9)*pow(ohm, 4)-1*pow(10, -6)*pow(ohm, 3)+0.0013*pow(ohm, 2)+2.3141*ohm - 243.85;
93+ // Serial.println(ohm); // uncomment for debugging
94+ return celsius;
95+ }
96+
97+ double RTD::tempC () // main function to read the RTD temperature in degree Celsius
98+ {
99+ MUX_SELECT (_nChannel);
100+ ADC_INIT ();
101+ long adcRaw = ADC_READ ();
102+ double rtdTempC = OHM2CELSIUS (ADC_2_mV (adcRaw));
103+ return rtdTempC;
104+ }
105+
106+ double RTD::tempK () // convert to Kelvin
107+ {
108+ double rtdTempK = tempC ();
109+ rtdTempK += 273.0 ;
110+ return rtdTempK;
111+ }
112+
113+ double RTD::tempF () // convert to degree Fahrenheit
114+ {
115+ double rtdTempF = tempC ();
116+ rtdTempF = (rtdTempF*9 /5 )+32 ;
117+ return rtdTempF;
118118}
0 commit comments