ALGO_Temperature_ATY.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. /**
  2. * @file ALGO_Temperature_ATY.c
  3. *
  4. * @param Project ALGO_Algorithm_ATY_LIB
  5. *
  6. * @author ATY
  7. *
  8. * @copyright
  9. * - Copyright 2017 - 2023 MZ-ATY
  10. * - This code follows:
  11. * - MZ-ATY Various Contents Joint Statement -
  12. * <a href="https://mengze.top/MZ-ATY_VCJS">
  13. * https://mengze.top/MZ-ATY_VCJS</a>
  14. * - CC 4.0 BY-NC-SA -
  15. * <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/">
  16. * https://creativecommons.org/licenses/by-nc-sa/4.0/</a>
  17. * - Your use will be deemed to have accepted the terms of this statement.
  18. *
  19. * @brief Familiar functions of NTC or others temperature calc
  20. *
  21. * @version
  22. * - 1_01_230107 > ATY
  23. * -# Preliminary version, first Release
  24. ********************************************************************************
  25. */
  26. #ifndef __ALGO_Temperature_ATY_C
  27. #define __ALGO_Temperature_ATY_C
  28. #include "ALGO_Temperature_ATY.h"
  29. /******************************* For user *************************************/
  30. /******************************************************************************/
  31. /**
  32. * @brief Calculate temperature from ntc resistance(Steinhart-Hart)
  33. * @param Rntc Current NTC resistance value
  34. * @param A A value of NTC
  35. * @param B B value of NTC
  36. * @param C C value of NTC
  37. * @return Current temperature in Celsius
  38. */
  39. float ALGO_ResToKelvinTempABC(float Rntc, float A, float B, float C)
  40. {
  41. float Tn = 0.0;
  42. float Cn = 0.0;
  43. Tn = (A + (B * ALGO_MATH_LogLn(Rntc)) + (C * ALGO_MATH_LogLn(Rntc) * ALGO_MATH_LogLn(Rntc) * ALGO_MATH_LogLn(Rntc)));
  44. Tn = 1 / Tn;
  45. Cn = ALGO_TEMP_TtoC(Tn);
  46. return Cn;
  47. }
  48. /**
  49. * @brief Calculate temperature from ntc resistance(Steinhart-Hart change)
  50. * @param Rntc Current NTC resistance value
  51. * @param R25 NTC standard resistance value at 25C
  52. * @param B B value of NTC
  53. * @return Current temperature in Celsius
  54. * @note T25: Kelvin temperature at 25C = 298.15 = ALGO_TEMP_CtoT(25)
  55. * R25: NTC standard resistance value at 25C like 10K,5K,100K...
  56. * B: B value of NTC like 3435,3950...
  57. * Rntc: Current NTC resistance value
  58. * Tn: Actual Kelvin temperature(Cn = Tn-273.15)
  59. * B = (lnR25 - lnRntc)/(1/T25 - 1/Tn)
  60. */
  61. float ALGO_ResToKelvinTemp(float Rntc, float R25, float B)
  62. {
  63. float Tn = 0.0;
  64. float Cn = 0.0;
  65. float temp_f[2];
  66. temp_f[0] = (ALGO_MATH_LogLn(R25) - ALGO_MATH_LogLn(Rntc)) / B;
  67. temp_f[1] = (1 / ALGO_TEMP_CtoT(25)) - temp_f[0];
  68. Tn = 1 / temp_f[1];
  69. Cn = ALGO_TEMP_TtoC(Tn);
  70. return Cn;
  71. }
  72. /**
  73. * @brief Calculate temperature from ntc resistance
  74. * @param vADC ADC voltage in mV
  75. * @param vRef NTC ref voltage in mV
  76. * @param rRefK ref resistance in kOhm
  77. * @param R25 NTC standard resistance value at 25C
  78. * @param B B value of NTC
  79. * @param rRefPos ref res psition, 1 for pull up, 0 for pull down(for ntc)
  80. * @return Current temperature in Celsius
  81. * @note T25: Kelvin temperature at 25C = 298.15 = ALGO_TEMP_CtoT(25)
  82. * R25: NTC standard resistance value at 25C like 10K,5K,100K...
  83. * B: B value of NTC like 3435,3950...
  84. * Rntc: Current NTC resistance value
  85. * Tn: Actual Kelvin temperature(Cn = Tn-273.15)
  86. * B = (lnR25 - lnRntc)/(1/T25 - 1/Tn)
  87. */
  88. float ALGO_VolToKelvinTemp(float vADC, float vRef, float rRefK, float R25, float B, uint8_t rRefPos)
  89. {
  90. if(rRefPos == 1){
  91. return ALGO_ResToKelvinTemp(ALGO_VoltageToResDown(vADC, vRef, rRefK), R25, B);
  92. }
  93. else{
  94. return ALGO_ResToKelvinTemp(ALGO_VoltageToResUp(vADC, vRef, rRefK), R25, B);
  95. }
  96. }
  97. float ALGO_Temp_RTD_Res_Fast(float rtdRes)
  98. {
  99. return (float)((rtdRes - 100.0f) / 0.385f);
  100. }
  101. // #include "math.h"
  102. #include "ALGO_AlgorithmBase_ATY.h"
  103. float ALGO_Temp_RTD_Res_Above(float rtdRes)
  104. {
  105. return (float)((-(3.9083e-3)
  106. + ALGO_Sqrt_NewtonNumber(((3.9083e-3) * (3.9083e-3))
  107. // + sqrt(((3.9083e-3) * (3.9083e-3))
  108. - 4 * (-5.775e-7) * (1 - (rtdRes / 100.0f))))
  109. / (2 * (-5.775e-7)));
  110. }
  111. float ALGO_Temp_RTD_Res_Below(float rtdRes)
  112. {
  113. return (float)(-242.02f
  114. + 2.2228f * rtdRes
  115. + (2.5859e-3) * rtdRes * rtdRes
  116. - (4.826e-6) * rtdRes * rtdRes * rtdRes
  117. - (2.8183e-8) * rtdRes * rtdRes * rtdRes * rtdRes
  118. + (1.5243e-10) * rtdRes * rtdRes * rtdRes * rtdRes * rtdRes);
  119. }
  120. // PT1000
  121. // R(t)=R0(1+At+Bt2)
  122. // A=0.0038623139728
  123. // B=-0.00000065314932626
  124. #include "math.h"
  125. #define A 3.9083e-3
  126. #define B -5.775e-7
  127. #define C -4.183e-12
  128. //#define A2 3.9083e-2
  129. //#define B2 -5.775e-6
  130. //#define C2 -4.183e-11
  131. double ALGO_Temp_RTD_T_PT1000(double T)
  132. {
  133. if(T >= -200 && T < 0)
  134. {
  135. return 100 * (1 + A * T + B * T * T + C * (T - 100) * T * T * T);
  136. }
  137. else if(T >= 0 && T <= 850)
  138. {
  139. return 100 * (1 + A * T + B * T * T);
  140. }
  141. return 0;
  142. }
  143. /**
  144. * @brief ???????
  145. * @param[in] resist ??
  146. * @param[out] temp ???
  147. * @retval ????
  148. * @note ????????,????????????,?1?3??????????????:
  149. * 1. ???????????????:
  150. * t1 = (Rt / R0 - 1) / A
  151. *
  152. * ??t1???:
  153. * 0??????:Rt1 = R0 * (1 + A * t1 + B * t1 * t1);
  154. * 0??????:Rt1 = R0 * [1 + A * t1 + B * t1 * t1 + C * (t1 - 100) * t1 * t1 * t1];
  155. *
  156. * ?? |Rt1 - Rt| < 0.001,t1 ???????,???????????:
  157. *
  158. * 2. ???????????:
  159. * ?? Rt = R0
  160. * t1' = 1 / [R0 * (A + 2 * B * t1)]
  161. * t1'' =-2 * B * R0 * t1' * t1' * t1'
  162. * ?? Rt < R0
  163. * t1' = 1 / [R0 * (A + 2 * B * t1 - 300 * C * t1 * t1 + 4 * C * t1 * t1 * t1)]
  164. * t1''=- R0 * (2 * B - 600 * C * t1 + 12 * C * t1 * t1) * t1' * t1' * t1'
  165. *
  166. * 3. ?? Rt,t1,Rt1 ?????? t2:
  167. * t2 = t1 + t1' * (Rt - Rt1) + 0.5 * t1'' * (Rt - Rt1) * (Rt - Rt1),???? t2 ????? Rt2?
  168. *
  169. * 4. ?? |Rt2 - Rt| < 0.001,t2 ???????,??????????? t2 ??????,???????????
  170. */
  171. double ALGO_Temp_RTD_Res_PT100(double resist)
  172. {
  173. double fT, fT0;
  174. short i;
  175. /* 1. use a linear formula to get a rough temperature first */
  176. fT0 = (resist / 100 - 1) / A;
  177. /* -200C ~ 0C */
  178. if(resist >= 18.52 && resist < 100)
  179. {
  180. for(i = 0; i < 50; i++)
  181. {
  182. fT = fT0 + (resist - 100 * (1 + A * fT0 + B * fT0 * fT0 - 100 * C * fT0 * fT0 * fT0 + C * fT0 * fT0 * fT0 * fT0)) /
  183. (100 * (A + 2 * B * fT0 - 300 * C * fT0 * fT0 + 4 * C * fT0 * fT0 * fT0));
  184. if(fabs(fT - fT0) < 0.001) /* If | Rt1-Rt | < 0.001, t1 is the desired temperature */
  185. {
  186. return fT;
  187. }
  188. else
  189. {
  190. fT0 = fT;
  191. }
  192. }
  193. }
  194. /* 0C ~ 850C */
  195. else if(resist >= 100 && resist <= 390.481)
  196. {
  197. for(i = 0; i < 50; i++)
  198. {
  199. fT = fT0 + (resist - 100 * (1 + A * fT0 + B * fT0 * fT0)) / (100 * (A + 2 * B * fT0));
  200. if(fabs(fT - fT0) < 0.001) /* If | Rt1-Rt | < 0.001, t1 is the desired temperature */
  201. {
  202. return fT;
  203. }
  204. else
  205. {
  206. fT0 = fT;
  207. }
  208. }
  209. }
  210. return 0;
  211. }
  212. double ALGO_Temp_RTD_Res_PT1000(double resist)
  213. {
  214. return ALGO_Temp_RTD_Res_PT100(resist / 10.0f);
  215. }
  216. /**
  217. * @brief
  218. *
  219. * @param type
  220. * @param Temp in Degrees Celsius
  221. * @return float in mV
  222. * @note https://srdata.nist.gov/
  223. */
  224. float ALGO_Temp_TC_TempToVol(uint8_t type, float Temp)
  225. {
  226. if(type == 'T'){
  227. if(Temp == 0)
  228. return 0;
  229. else if(Temp > 0){
  230. return (0
  231. + ((0.387481063640e-1) * Temp)
  232. + ((0.332922278800e-4) * Temp * Temp)
  233. + ((0.206182434040e-6) * Temp * Temp * Temp)
  234. + ((-0.218822568460e-8) * Temp * Temp * Temp * Temp)
  235. + ((0.109968809280e-10) * Temp * Temp * Temp * Temp * Temp)
  236. + ((-0.308157587720e-13) * Temp * Temp * Temp * Temp * Temp * Temp)
  237. + ((0.454791352900e-16) * Temp * Temp * Temp * Temp * Temp * Temp * Temp)
  238. + ((-0.275129016730e-19) * Temp * Temp * Temp * Temp * Temp * Temp * Temp * Temp));
  239. }
  240. else if(Temp < 0){
  241. return (0
  242. + ((0.387481063640e-01) * Temp)
  243. + ((0.441944343470e-04) * Temp * Temp)
  244. + ((0.118443231050e-06) * Temp * Temp * Temp)
  245. + ((0.200329735540e-07) * Temp * Temp * Temp * Temp)
  246. + ((0.901380195590e-09) * Temp * Temp * Temp * Temp * Temp)
  247. + ((0.226511565930e-10) * Temp * Temp * Temp * Temp * Temp * Temp)
  248. + ((0.360711542050e-12) * Temp * Temp * Temp * Temp * Temp * Temp * Temp)
  249. + ((0.384939398830e-14) * Temp * Temp * Temp * Temp * Temp * Temp * Temp * Temp)
  250. + ((0.282135219250e-16) * Temp * Temp * Temp * Temp * Temp * Temp * Temp * Temp * Temp)
  251. + ((0.142515947790e-18) * Temp * Temp * Temp * Temp * Temp * Temp * Temp * Temp * Temp * Temp)
  252. + ((0.487686622860e-21) * Temp * Temp * Temp * Temp * Temp * Temp * Temp * Temp * Temp * Temp * Temp)
  253. + ((0.107955392700e-23) * Temp * Temp * Temp * Temp * Temp * Temp * Temp * Temp * Temp * Temp * Temp * Temp)
  254. + ((0.139450270620e-26) * Temp * Temp * Temp * Temp * Temp * Temp * Temp * Temp * Temp * Temp * Temp * Temp * Temp)
  255. + ((0.797951539270e-30) * Temp * Temp * Temp * Temp * Temp * Temp * Temp * Temp * Temp * Temp * Temp * Temp * Temp * Temp));
  256. }
  257. }
  258. return 0;
  259. }
  260. /**
  261. * @brief
  262. *
  263. * @param type
  264. * @param voltage in mV
  265. * @return float in Degrees Celsius
  266. * @note https://srdata.nist.gov/
  267. */
  268. float ALGO_Temp_TC_VolToTemp(uint8_t type, float voltage)
  269. {
  270. if(type == 'T'){
  271. if(voltage == 0)
  272. return 0;
  273. else if(voltage > 0){
  274. return (0
  275. + ((2.592800e1) * voltage)
  276. + ((-7.602961e-1) * voltage * voltage)
  277. + ((4.637791e-2) * voltage * voltage * voltage)
  278. + ((-2.165394e-3) * voltage * voltage * voltage * voltage)
  279. + ((6.048144e-5) * voltage * voltage * voltage * voltage * voltage)
  280. + ((-7.293422e-7) * voltage * voltage * voltage * voltage * voltage * voltage));
  281. }
  282. else if(voltage < 0){
  283. return (0
  284. + ((2.5949192e1) * voltage)
  285. + ((-2.1316967e-1) * voltage * voltage)
  286. + ((7.9018692e-1) * voltage * voltage * voltage)
  287. + ((4.2527777e-1) * voltage * voltage * voltage * voltage)
  288. + ((1.3304473e-1) * voltage * voltage * voltage * voltage * voltage)
  289. + ((2.0241446e-2) * voltage * voltage * voltage * voltage * voltage * voltage)
  290. + ((1.2668171e-3) * voltage * voltage * voltage * voltage * voltage * voltage * voltage));
  291. }
  292. }
  293. return 0;
  294. }
  295. // resultTemp = ALGO_ResToKelvinTemp(ALGO_VoltageToResDown(resultTemp, vref_t, 10), 1, 3200);
  296. #endif /* __ALGO_Temperature_ATY_C */
  297. /******************************** End Of File *********************************/