1 //###########################################################################
2 //
3 // FILE: F2837xD_TempSensorConv.c
4 //
5 // TITLE: F2837xD Temperature Sensor Conversion Functions
6 //
7 //###########################################################################
8 // $TI Release: F2837xD Support Library v3.05.00.00 $
9 // $Release Date: Tue Jun 26 03:15:23 CDT 2018 $
10 // $Copyright:
11 // Copyright (C) 2013-2018 Texas Instruments Incorporated - http://www.ti.com/
12 //
13 // Redistribution and use in source and binary forms, with or without
14 // modification, are permitted provided that the following conditions
15 // are met:
16 //
17 // Redistributions of source code must retain the above copyright
18 // notice, this list of conditions and the following disclaimer.
19 //
20 // Redistributions in binary form must reproduce the above copyright
21 // notice, this list of conditions and the following disclaimer in the
22 // documentation and/or other materials provided with the
23 // distribution.
24 //
25 // Neither the name of Texas Instruments Incorporated nor the names of
26 // its contributors may be used to endorse or promote products derived
27 // from this software without specific prior written permission.
28 //
29 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
30 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
31 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
32 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
33 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
34 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
35 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
36 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
37 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
38 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
39 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
40 // $
41 //###########################################################################
42
43 //
44 // Included Files
45 //
46 #include "F2837xD_device.h"
47 #include "F2837xD_Examples.h"
48
49 //
50 // Defines
51 //
52 #define FP_SCALE 32768 //Scale factor for Q15 fixed point numbers (2^15)
53 #define FP_ROUND FP_SCALE/2 //Added to Q15 numbers before converting to
54 //integer to round the number.
55 #define KELVIN 273 // Amount to add to Q15 fixed point numbers
56 // to shift from Celsius to Kelvin
57 // (Converting guarantees number is
58 // positive, which makes rounding more
59 // efficient)
60 #define KELVIN_OFF FP_SCALE*KELVIN
61 #define getTempSlope() (*(int (*)(void))0x7036E)() //Slope of temperature sensor
62 //(deg. C / ADC code).
63 //Stored in fixed point Q15
64 //format.
65 #define getTempOffset() (*(int (*)(void))0x70372)() //ADC code corresponding to
66 //temperature sensor output
67 //at 0 deg. C
68
69 //
70 // Globals
71 //
72 float32 tempSensor_tempSlope;
73 float32 tempSensor_tempOffset;
74 float32 tempSensor_scaleFactor;
75
76 //
77 // InitTempSensor - Initialize the temperature sensor by powering up the
78 // sensor, loading the calibration values from OTP to RAM,
79 // and recording the intended VREFHI voltage.
80 // Note: This function doesn't support VREFLO != 0.0V,
81 // but this could be implemented if desired.
82 //
InitTempSensor(float32 vrefhi_voltage)83 void InitTempSensor(float32 vrefhi_voltage)
84 {
85 EALLOW;
86
87 //
88 //power up the the temperature sensor
89 //
90 AnalogSubsysRegs.TSNSCTL.bit.ENABLE = 1;
91
92 //
93 //delay to allow the sensor to power up
94 //
95 DELAY_US(1000);
96
97 EDIS;
98
99 //
100 //need to remember VREFHI voltage so that sensor readings can be scaled
101 //to match 2.5V values used for calibration data.
102 //
103 tempSensor_scaleFactor = vrefhi_voltage;
104
105 //
106 //check the device revision
107 //
108 if(DevCfgRegs.REVID >= 3)
109 {
110 //
111 //for production devices (Rev. C), pull the slope and offset from OTP
112 //
113 tempSensor_tempSlope = (int32)getTempSlope();
114 tempSensor_tempOffset = getTempOffset();
115 }
116 else
117 {
118 //
119 //for pre-production devices, use these static values for slope
120 //and offset
121 //
122 tempSensor_tempSlope = 5196;
123 tempSensor_tempOffset = 1788;
124 }
125 }
126
127 //
128 // GetTemperatureC - This function uses the reference data stored in OTP to
129 // convert the raw temperature sensor reading into degrees C
130 //
GetTemperatureC(int16 sensorSample)131 int16 GetTemperatureC(int16 sensorSample)
132 {
133 sensorSample = (int16)((tempSensor_scaleFactor/2.5)*(sensorSample));
134
135 return (((sensorSample - tempSensor_tempOffset)*tempSensor_tempSlope +
136 FP_ROUND + KELVIN_OFF)/FP_SCALE - KELVIN);
137 }
138
139 //
140 // GetTemperatureK - This function uses the reference data stored in OTP to
141 // convert the raw temperature sensor reading into degrees K
142 //
GetTemperatureK(int16 sensorSample)143 int16 GetTemperatureK(int16 sensorSample)
144 {
145 sensorSample = (int16)((2.5/tempSensor_scaleFactor)*(sensorSample));
146
147 return (((sensorSample - tempSensor_tempOffset)*tempSensor_tempSlope +
148 FP_ROUND + KELVIN_OFF)/FP_SCALE);
149 }
150
151 //
152 // End of file
153 //
154