F2837xD_TempSensorConv.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. // Included Files
  44. //
  45. #include "F2837xD_device.h"
  46. #include "F2837xD_Examples.h"
  47. //
  48. // Defines
  49. //
  50. #define FP_SCALE 32768 //Scale factor for Q15 fixed point numbers (2^15)
  51. #define FP_ROUND FP_SCALE/2 //Added to Q15 numbers before converting to
  52. //integer to round the number.
  53. #define KELVIN 273 // Amount to add to Q15 fixed point numbers
  54. // to shift from Celsius to Kelvin
  55. // (Converting guarantees number is
  56. // positive, which makes rounding more
  57. // efficient)
  58. #define KELVIN_OFF FP_SCALE*KELVIN
  59. #define getTempSlope() (*(int (*)(void))0x7036E)() //Slope of temperature sensor
  60. //(deg. C / ADC code).
  61. //Stored in fixed point Q15
  62. //format.
  63. #define getTempOffset() (*(int (*)(void))0x70372)() //ADC code corresponding to
  64. //temperature sensor output
  65. //at 0 deg. C
  66. //
  67. // Globals
  68. //
  69. float32 tempSensor_tempSlope;
  70. float32 tempSensor_tempOffset;
  71. float32 tempSensor_scaleFactor;
  72. //
  73. // InitTempSensor - Initialize the temperature sensor by powering up the
  74. // sensor, loading the calibration values from OTP to RAM,
  75. // and recording the intended VREFHI voltage.
  76. // Note: This function doesn't support VREFLO != 0.0V,
  77. // but this could be implemented if desired.
  78. //
  79. void InitTempSensor(float32 vrefhi_voltage)
  80. {
  81. EALLOW;
  82. //
  83. //power up the the temperature sensor
  84. //
  85. AnalogSubsysRegs.TSNSCTL.bit.ENABLE = 1;
  86. //
  87. //delay to allow the sensor to power up
  88. //
  89. DELAY_US(1000);
  90. EDIS;
  91. //
  92. //need to remember VREFHI voltage so that sensor readings can be scaled
  93. //to match 2.5V values used for calibration data.
  94. //
  95. tempSensor_scaleFactor = vrefhi_voltage;
  96. //
  97. //check the device revision
  98. //
  99. if(DevCfgRegs.REVID >= 3)
  100. {
  101. //
  102. //for production devices (Rev. C), pull the slope and offset from OTP
  103. //
  104. tempSensor_tempSlope = (int32)getTempSlope();
  105. tempSensor_tempOffset = getTempOffset();
  106. }
  107. else
  108. {
  109. //
  110. //for pre-production devices, use these static values for slope
  111. //and offset
  112. //
  113. tempSensor_tempSlope = 5196;
  114. tempSensor_tempOffset = 1788;
  115. }
  116. }
  117. //
  118. // GetTemperatureC - This function uses the reference data stored in OTP to
  119. // convert the raw temperature sensor reading into degrees C
  120. //
  121. int16 GetTemperatureC(int16 sensorSample)
  122. {
  123. sensorSample = (int16)((tempSensor_scaleFactor/2.5)*(sensorSample));
  124. return (((sensorSample - tempSensor_tempOffset)*tempSensor_tempSlope +
  125. FP_ROUND + KELVIN_OFF)/FP_SCALE - KELVIN);
  126. }
  127. //
  128. // GetTemperatureK - This function uses the reference data stored in OTP to
  129. // convert the raw temperature sensor reading into degrees K
  130. //
  131. int16 GetTemperatureK(int16 sensorSample)
  132. {
  133. sensorSample = (int16)((2.5/tempSensor_scaleFactor)*(sensorSample));
  134. return (((sensorSample - tempSensor_tempOffset)*tempSensor_tempSlope +
  135. FP_ROUND + KELVIN_OFF)/FP_SCALE);
  136. }
  137. //
  138. // End of file
  139. //