pt1000.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. #include "math.h"
  2. // PT1000温度阻值对照表-50 单位:欧姆
  3. #define PT1000_table_len(array) (sizeof(array) / sizeof(array[0]))
  4. #define TEMP_ERR_NEGATIVE (float)-50
  5. #define TEMP_ERR_POSITIVE (float)1000
  6. const float PT1000_TempList[] = {
  7. 803.063, 807.033, 811.003, 814.970, 818.937, 822.902, 826.865, 830.828, 834.789, 838.748, 842.707, 846.663, 850.619, 854.573, 858.526,
  8. 862.478, 866.428, 870.377, 874.325, 878.271, 882.217, 886.160, 890.103, 894.044, 897.985, 901.923, 905.861, 909.797, 913.732, 917.666,
  9. 921.599, 925.530, 929.461, 933.390, 937.317, 941.244, 945.169, 949.093, 953.016, 956.938, 960.859, 964.778, 968.696, 972.613, 976.529,
  10. 980.444, 984.358, 988.270, 992.181, 996.091, 1000.000, 1003.908, 1007.814, 1011.720, 1015.624, 1019.527, 1023.429, 1027.330, 1031.229, 1035.128,
  11. 1039.025, 1042.921, 1046.816, 1050.710, 1054.603, 1058.495, 1062.385, 1066.274, 1070.162, 1074.049, 1077.935, 1081.820, 1085.703, 1089.585, 1093.467,
  12. 1097.347, 1101.225, 1105.103, 1108.980, 1112.855, 1116.729, 1120.602, 1124.474, 1128.345, 1132.215, 1136.083, 1139.950, 1143.817, 1147.681, 1151.545,
  13. 1155.408, 1159.270, 1163.130, 1166.989, 1170.847, 1174.704, 1178.560, 1182.414, 1186.268, 1190.120, 1193.971, 1197.821, 1201.670, 1205.518, 1209.364,
  14. 1213.210, 1217.054, 1220.897, 1224.739, 1228.579, 1232.419, 1236.257, 1240.095, 1243.931, 1247.766, 1251.600, 1255.432, 1259.264, 1263.094, 1266.923,
  15. 1270.751, 1274.578, 1278.404, 1282.228, 1286.052, 1289.874, 1293.695, 1297.515, 1301.334, 1305.152, 1308.968, 1312.783, 1316.597, 1320.411, 1324.222,
  16. 1328.033, 1331.843, 1335.651, 1339.458, 1343.264, 1347.069, 1350.873, 1354.676, 1358.477, 1362.277, 1366.077, 1369.875, 1373.671, 1377.467, 1381.262,
  17. 1385.055, 1388.847, 1392.638, 1396.428, 1400.217, 1404.005, 1407.791, 1411.576, 1415.360, 1419.143, 1422.925, 1426.706, 1430.485, 1434.264, 1438.041,
  18. 1441.817, 1445.592, 1449.366, 1453.138, 1456.910, 1460.680, 1464.449, 1468.217, 1471.984, 1475.750, 1479.514, 1483.277, 1487.040, 1490.801, 1494.561,
  19. 1498.319, 1502.077, 1505.833, 1509.589, 1513.343, 1517.096, 1520.847, 1524.598, 1528.347, 1532.096, 1535.843, 1539.589, 1543.334, 1547.078, 1550.820,
  20. 1554.562, 1558.302, 1562.041, 1565.779, 1569.516, 1573.251, 1576.986, 1580.719, 1584.451, 1588.182, 1591.912, 1595.641, 1599.368, 1603.095, 1606.820,
  21. 1610.544, 1614.267, 1617.989, 1621.709, 1625.429, 1629.147, 1632.864, 1636.580, 1640.295, 1644.009, 1647.721, 1651.433, 1655.143, 1658.852, 1662.560,
  22. 1666.267, 1669.972, 1673.677, 1677.380, 1681.082, 1684.783, 1688.483, 1692.181, 1695.879, 1699.575, 1703.271, 1706.965, 1710.658, 1714.349, 1718.040,
  23. 1721.729, 1725.418, 1729.105, 1732.791, 1736.475, 1740.159, 1743.842, 1747.523, 1751.203, 1754.882, 1758.560};
  24. unsigned int table_len = PT1000_table_len(PT1000_TempList);
  25. const float PB7_TempList[] = {37.56, 35.89, 34.30, 32.80, 31.36, 30, 28.71, 27.48, 26.31, 25.19, 24.13, 23.12, 22.16, 21.24, 20.37, 19.53, 18.74, 17.98,
  26. 17.26, 16.57, 15.91, 15.28, 14.68, 14.11, 13.56, 13.03, 12.53, 12.06, 11.60, 11.16, 10.74, 10.34, 9.955, 9.587, 9.235, 8.898,
  27. 8.574, 8.264, 7.967, 7.683, 7.410, 7.148, 6.896, 6.655, 6.424, 6.201, 5.988, 5.783, 5.586, 5.397, 5.215, 5.041, 4.873, 4.711,
  28. 4.556, 4.406, 4.262, 4.124, 3.991, 3.863, 3.739, 3.620, 3.506, 3.396, 3.289, 3.187, 3.088, 2.993, 2.901, 2.813, 2.727, 2.645,
  29. 2.565, 2.489, 2.415, 2.343, 2.274, 2.208, 2.143, 2.081, 2.021, 1.963, 1.907, 1.853, 1.800, 1.749, 1.700, 1.653, 1.607, 1.563,
  30. 1.520, 1.478, 1.438, 1.399, 1.361, 1.325, 1.289, 1.255, 1.222, 1.190, 1.159, 1.128, 1.099, 1.071, 1.043, 1.017, 0.9908
  31. };
  32. unsigned int PB7_table_len = PT1000_table_len(PB7_TempList);
  33. const float K_Thermocouple_Reference_Tables[] = {
  34. 0.000, 0.039, 0.079, 0.119, 0.158, 0.198, 0.238, 0.277, 0.317, 0.357, 0.397, 0.437, 0.477, 0.517, 0.557, 0.597, 0.637, 0.677, 0.718, 0.758,
  35. 0.798, 0.838, 0.879, 0.919, 0.960, 1.000, 1.041, 1.081, 1.122, 1.163, 1.203, 1.244, 1.285, 1.326, 1.366, 1.407, 1.448, 1.489, 1.530, 1.571,
  36. 1.612, 1.653, 1.694, 1.735, 1.776, 1.817, 1.858, 1.899, 1.941, 1.982, 2.023, 2.064, 2.106, 2.147, 2.188, 2.230, 2.271, 2.312, 2.354, 2.395,
  37. 2.436, 2.478, 2.519, 2.561, 2.602, 2.644, 2.685, 2.727, 2.768, 2.810, 2.851, 2.893, 2.934, 2.976, 3.017, 3.059, 3.100, 3.142, 3.184, 3.225,
  38. 3.267, 3.308, 3.350, 3.391, 3.433, 3.474, 3.516, 3.557, 3.599, 3.640, 3.682, 3.723, 3.765, 3.806, 3.848, 3.889, 3.931, 3.972, 4.013, 4.055,
  39. 4.096, 4.138, 4.179, 4.220, 4.262, 4.303, 4.344, 4.385, 4.427, 4.468, 4.509, 4.550, 4.591, 4.633, 4.674, 4.715, 4.756, 4.797, 4.838, 4.879,
  40. 4.920, 4.961, 5.002, 5.043, 5.084, 5.124, 5.165, 5.206, 5.247, 5.288, 5.328, 5.369, 5.410, 5.450, 5.491, 5.532, 5.572, 5.613, 5.653, 5.694,
  41. 5.735, 5.775, 5.815, 5.856, 5.896, 5.937, 5.977, 6.017, 6.058, 6.098, 6.138, 6.179, 6.219, 6.259, 6.299, 6.339, 6.380, 6.420, 6.460, 6.500,
  42. 6.540, 6.580, 6.620, 6.660, 6.701, 6.741, 6.781, 6.821, 6.861, 6.901, 6.941, 6.981, 7.021, 7.060, 7.100, 7.140, 7.180, 7.220, 7.260, 7.300,
  43. 7.340, 7.380, 7.420, 7.460, 7.500, 7.540, 7.579, 7.619, 7.659, 7.699, 7.739, 7.779, 7.819, 7.859, 7.899, 7.939, 7.979, 8.019, 8.059, 8.099};
  44. unsigned int K_Thermocouple_Reference_Tables_len = PT1000_table_len(K_Thermocouple_Reference_Tables);
  45. float getTemp(float R) {
  46. float temp;
  47. if (R < PT1000_TempList[0]) {
  48. return TEMP_ERR_NEGATIVE;
  49. } else if (R >= PT1000_TempList[table_len - 1]) {
  50. return (PT1000_table_len(PT1000_TempList) - 50);
  51. }
  52. for (int i = 0; i < (table_len - 1); i++) {
  53. if (R >= PT1000_TempList[i] && R < PT1000_TempList[i + 1]) {
  54. temp = (R - PT1000_TempList[i]) / (PT1000_TempList[i + 1] - PT1000_TempList[i]);
  55. temp = temp + i - 50;
  56. return temp;
  57. }
  58. }
  59. return TEMP_ERR_POSITIVE;
  60. }
  61. float getPB7_Temp(float R) {
  62. float temp;
  63. R = R / 1000;
  64. if (R > PB7_TempList[0]) {
  65. return TEMP_ERR_NEGATIVE;
  66. } else if (R < PB7_TempList[PB7_table_len - 1]) {
  67. return (101);
  68. }
  69. for (int i = 0; i < (PB7_table_len - 1); i++) {
  70. if (R <= PB7_TempList[i] && R > PB7_TempList[i + 1]) {
  71. temp = (R - PB7_TempList[i]) / (PB7_TempList[i + 1] - PB7_TempList[i]);
  72. temp = temp + i - 5;
  73. return temp;
  74. }
  75. }
  76. return TEMP_ERR_POSITIVE;
  77. }
  78. float Get_Temp_K_Thermocouple(float Vt) {
  79. float temp;
  80. if (Vt < K_Thermocouple_Reference_Tables[0]) {
  81. return TEMP_ERR_NEGATIVE;
  82. } else if (Vt >= K_Thermocouple_Reference_Tables[K_Thermocouple_Reference_Tables_len - 1]) {
  83. return (PT1000_table_len(K_Thermocouple_Reference_Tables));
  84. }
  85. for (int i = 0; i < (K_Thermocouple_Reference_Tables_len - 1); i++) {
  86. if (Vt >= K_Thermocouple_Reference_Tables[i] && Vt < K_Thermocouple_Reference_Tables[i + 1]) {
  87. temp = (Vt - K_Thermocouple_Reference_Tables[i]) / (K_Thermocouple_Reference_Tables[i + 1] - K_Thermocouple_Reference_Tables[i]);
  88. temp = temp + i;
  89. return temp;
  90. }
  91. }
  92. return TEMP_ERR_POSITIVE;
  93. }
  94. const float Rp = 10000.0; // 10K
  95. const float T2 = (273.15 + 25.0);
  96. ; // T2
  97. const float Bx = 3380.0; // B
  98. const float Ka = 273.15;
  99. float Get_Temp_NCP18XH103(float Rt) {
  100. float temp;
  101. // Rt = Get_TempResistor();
  102. // like this R=5000, T2=273.15+25,B=3470, RT=5000*EXP(3470*(1/T1-1/(273.15+25)),
  103. temp = Rt / Rp;
  104. temp = log(temp); // ln(Rt/Rp)
  105. temp /= Bx; // ln(Rt/Rp)/B
  106. temp += (1 / T2);
  107. temp = 1 / (temp);
  108. temp -= Ka;
  109. if(temp < 0){temp = TEMP_ERR_NEGATIVE;}
  110. if(temp > 150){temp = TEMP_ERR_POSITIVE;}
  111. return temp;
  112. }
  113. static const unsigned char crc8_table[256] = {
  114. 0x00, 0x07, 0x0e, 0x09, 0x1c, 0x1b, 0x12, 0x15, 0x38, 0x3f, 0x36, 0x31, 0x24, 0x23, 0x2a, 0x2d, 0x70, 0x77, 0x7e, 0x79, 0x6c, 0x6b, 0x62, 0x65, 0x48, 0x4f,
  115. 0x46, 0x41,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,
  122. 0x3e, 0x39, 0x30, 0x37, 0x22, 0x25, 0x2c, 0x2b, 0x06, 0x01, 0x08, 0x0f, 0x1a, 0x1d, 0x14, 0x13, 0xae, 0xa9, 0xa0, 0xa7, 0xb2, 0xb5, 0xbc, 0xbb, 0x96, 0x91,
  123. 0x98, 0x9f, 0x8a, 0x8d, 0x84, 0x83, 0xde, 0xd9, 0xd0, 0xd7, 0xc2, 0xc5, 0xcc, 0xcb, 0xe6, 0xe1, 0xe8, 0xef, 0xfa, 0xfd, 0xf4, 0xf3};
  124. unsigned char crc8_check(unsigned char* data, unsigned char len) {
  125. unsigned int i = 0;
  126. unsigned char fcs = 0x0;
  127. // 计算数据校验
  128. for (i = 0; i < (len); i++) {
  129. fcs = crc8_table[(unsigned char)fcs ^ *data];
  130. data++;
  131. }
  132. // 计算的 CRC 校验与数据最后一个字节对比
  133. return fcs;
  134. }