tempsensors.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /*
  2. * tempsensors.c
  3. *
  4. * Created on: Jan 12, 2021
  5. * Author: David Original work by Jose (PTDreamer), 2017
  6. */
  7. #include "tempsensors.h"
  8. #include "math.h"
  9. #define temp_minC 50 // Minimum calibration temperature in degrees of Celsius
  10. #define temp_maxC 480 // Maximum calibration temperature in degrees of Celsius
  11. static tipData *currentTipData;
  12. uint16_t last_TIP_Raw;
  13. uint16_t last_TIP;
  14. int16_t last_NTC;
  15. #ifdef USE_NTC
  16. const int NTC_TABLE; // Defined in board.h
  17. #endif
  18. int16_t readColdJunctionSensorTemp_x10(bool tempUnit) {
  19. #ifdef USE_NTC
  20. int16_t temp;
  21. int16_t p1, p2;
  22. int16_t lastavg=NTC.last_avg;
  23. /* Estimate the interpolating point before and after the ADC value. */
  24. p1 = NTC_Table[(lastavg >> 4)];
  25. p2 = NTC_Table[(lastavg >> 4) + 1];
  26. /* Interpolate between both points. */
  27. temp = p1 - ((p1 - p2) * (lastavg & 0x000F)) / 16;
  28. if(tempUnit==mode_Farenheit){
  29. temp=TempConversion(temp, mode_Farenheit, 1);
  30. }
  31. last_NTC = temp;
  32. return last_NTC;
  33. #else
  34. last_NTC=350; // If no NTC is used, assume 35ºC
  35. return last_NTC;
  36. #endif
  37. }
  38. // Read tip temperature
  39. uint16_t readTipTemperatureCompensated(bool update, bool ReadRaw){
  40. uint16_t temp, temp_Raw;
  41. if(systemSettings.setupMode==setup_On){
  42. return 0;
  43. }
  44. if(update){
  45. temp = adc2Human(TIP.last_avg,1,systemSettings.settings.tempUnit);
  46. temp_Raw = adc2Human(TIP.last_raw,1,systemSettings.settings.tempUnit);
  47. // Limit output values
  48. if(temp>999){
  49. temp=999;
  50. }
  51. else if(temp<0){
  52. temp = 0;
  53. }
  54. if(temp_Raw>999){
  55. temp_Raw=999;
  56. }
  57. else if(temp_Raw<0){
  58. temp_Raw = 0;
  59. }
  60. last_TIP = temp;
  61. last_TIP_Raw = temp_Raw;
  62. }
  63. if(ReadRaw){
  64. return last_TIP_Raw;
  65. }
  66. else{
  67. return last_TIP;
  68. }
  69. }
  70. void setCurrentTip(uint8_t tip) {
  71. currentTipData = &systemSettings.Profile.tip[tip];
  72. setupPID(&currentTipData->PID);
  73. }
  74. tipData* getCurrentTip() {
  75. return currentTipData;
  76. }
  77. // Translate the human readable t into internal value
  78. uint16_t human2adc(int16_t t) {
  79. volatile int16_t temp = t;
  80. volatile int16_t tH;
  81. volatile int16_t ambTemp = readColdJunctionSensorTemp_x10(mode_Celsius) / 10;
  82. // If using Farenheit, convert to Celsius
  83. if(systemSettings.settings.tempUnit==mode_Farenheit){
  84. t = TempConversion(t,mode_Celsius,0);
  85. }
  86. t-=ambTemp;
  87. if (t < temp_minC){ return 0; } // If requested temp below min, return 0
  88. else if (t > temp_maxC){ t = temp_maxC; } // If requested over max, apply limit
  89. // If t>350, map between ADC values Cal_350 - Cal_450
  90. if (t >= 350){
  91. temp = map(t, 350, 450, currentTipData->calADC_At_350, currentTipData->calADC_At_450);
  92. }
  93. // Else, map between ADC values Cal_250 - Cal_350
  94. else{
  95. temp = map(t, 250, 350, currentTipData->calADC_At_250, currentTipData->calADC_At_350);
  96. }
  97. tH = adc2Human(temp,0,mode_Celsius);
  98. if (tH < t) {
  99. while(tH < t){
  100. tH = adc2Human(++temp,0,mode_Celsius);
  101. }
  102. }
  103. else if (tH > t) {
  104. while(tH > t){
  105. tH = adc2Human(--temp,0,mode_Celsius);
  106. }
  107. }
  108. return temp;
  109. }
  110. // Translate temperature from internal units to the human readable value
  111. int16_t adc2Human(uint16_t adc_value,bool correction, bool tempUnit) {
  112. int16_t tempH = 0;
  113. int16_t ambTemp;
  114. ambTemp = readColdJunctionSensorTemp_x10(mode_Celsius) / 10;
  115. if (adc_value >= currentTipData->calADC_At_350) {
  116. tempH = map(adc_value, currentTipData->calADC_At_350, currentTipData->calADC_At_450, 350, 450);
  117. }
  118. else{
  119. tempH = map(adc_value, currentTipData->calADC_At_250, currentTipData->calADC_At_350, 250, 350);
  120. }
  121. if(correction){ tempH+= ambTemp; }
  122. if(tempUnit==mode_Farenheit){
  123. tempH=TempConversion(tempH,mode_Farenheit,0);
  124. }
  125. return tempH;
  126. }
  127. long map(long x, long in_min, long in_max, long out_min, long out_max) {
  128. long ret;
  129. ret = (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
  130. if (ret < 0)
  131. ret = 0;
  132. return ret;
  133. }
  134. // Fixed point calculation
  135. // 2E20*1.8 = 1887437 , 2E20/1.8 = 582542
  136. // So (temp*1887437)>>20 == temp*1.8 (Real: 1,800000191)
  137. // (temp*582542)>>20 == temp/1.8 (Real: 1,800000687)
  138. // Max input = 1100°C / 3700°F, otherwise we will overflow the signed int32
  139. int16_t TempConversion(int16_t temperature, bool conversion, bool x10mode){
  140. if(conversion==mode_Farenheit){ // Input==Celsius, Output==Farenheit
  141. temperature=(((int32_t)temperature*1887437)>>20);// F = (C*1.8)+32
  142. if(x10mode){
  143. temperature += 320;
  144. }
  145. else{
  146. temperature += 32;
  147. }
  148. }
  149. else{// Input==Farenheit, Output==Celsius
  150. if(x10mode){
  151. temperature -= 320;
  152. }
  153. else{
  154. temperature -= 32;
  155. }
  156. temperature=((int32_t)temperature*582542)>>20;// C = (F-32)/1.8
  157. }
  158. return temperature;
  159. }