[STM32] 普适NTC温度换算源码分享

[复制链接]
67|0
shipeng1989 发表于 2025-11-6 10:58 | 显示全部楼层 |阅读模式
本帖最后由 shipeng1989 于 2025-11-6 11:17 编辑

在嵌入式软件开发中经常会遇到NTC温度换算的功能需求,对于测温要求不高的应用可以采用查表法实现,但是查表法需要繁琐的数据录入工作,其实关于NTC温度换算有一种普适方法可以解决所有的NTC温度换算问题,不同的NTC只需要修改一下相关参数即可适用此套算法。关于这个厉害的算法我也是前几年偶然在网上找到的,当时还收藏了链接只是现在已经找不到了,刚好最近又碰到了NTC温度换算的问题才想起来这个算法很重要,于是决定把它做一个详细记录并分享出来。

  1. //K0C: 0 Celsius = 273.15 Kelvin
  2. #define K0C 273.15F
  3. //T25: 25 Celsius = 298.15 Kelvin
  4. #define T25 298.15F
  5. //R25: NTC resistance value(KOhm) at 25 celsius
  6. #define R25 10.0F
  7. //LNR25=ln(R25)=ln10=2.30258509
  8. #define LNR25 2.302585F
  9. //BNTC: the B value of NTC
  10. #define BNTC  3455.0F
  11. //RPU: PULL-UP resistance(KOhm) of NTC
  12. #define RPU 33.0F
  13. //MAX_ADC: MAX VALUE OF ADC
  14. #define MAX_ADC 4096

  15. //自然对数计算函数
  16. static double myln(double a)
  17. {
  18.         //我们取了前511+1项来估算,N值越大计算结果越精确
  19.    #define N 511
  20.    int k,nk;
  21.    double x,xx,y;
  22.    x = (a-1)/(a+1);
  23.    xx = x*x;
  24.    nk = 2*N+1;
  25.    y = 1.0/nk;
  26.    for(k=N;k>0;k--)
  27.    {
  28.      nk = nk - 2;
  29.      y = 1.0/nk+xx*y;
  30.      
  31.    }
  32.    return 2.0*x*y;
  33. }

  34. //B=(ln R25 - ln Rntc)/(1/T25 - 1/Tn);(Tn is the kelvin temp)
  35. static float Get_Kelvin_Temperature(float Rntc)
  36. {
  37.         float N1,N2,N3;
  38.         if (Rntc==0)return 899.0f;
  39.         N1 = (float)((LNR25-myln((double)Rntc))/BNTC);//LNR25<-->myln(R25)
  40.         N2 = 1/T25 - N1;
  41.         N3 = 1/N2;

  42.         return N3;
  43. }

  44. float ADV2CelsiusTemp(uint16_t adv)
  45. {
  46.         float ctemp,Rntc = RPU*adv/(MAX_ADC-adv);
  47.         ctemp = Get_Kelvin_Temperature(Rntc)-K0C;
  48.         return ctemp;
  49. }


您需要登录后才可以回帖 登录 | 注册

本版积分规则

31

主题

141

帖子

1

粉丝
快速回复 在线客服 返回列表 返回顶部