NTC 测温度的表值计算方法

/*
***************************************************************************************************
*                                       INCLUDE HEADER FILES
***************************************************************************************************
*/
//#pragma  src
#include "cpu.h"

#include <math.h>
#include "adc0.h"

/*
***************************************************************************************************
*                                       ntc对应采样电压值数组
*--------------------------------------------------------------------------------------------------
* 注释第一行:对应温度
*--------------------------------------------------------------------------------------------------
* 测量温度范围定为-10℃-150℃(可能会有零点几的数据出入)
***************************************************************************************************
*/
static int16u code ntc_adc[] =
{      
        4018,   3990,   3961,   3932,   3901,   3869,   3836,   3802,
        3767,   3731,   3694,   3656,   3617,   3577,   3536,   3494,   
        3451,   3407,   3363,   3318,   3271,   3225,   3177,   3129,   
        3080,   3031,   2981,   2931,   2881,   2830,   2779,   2728,   
        2676,   2625,   2573,   2522,   2470,   2419,   2368,   2317,   
        2267,   2216,   2167,   2117,   2068,   2020,   1972,   1925,   
        1878,   1832,   1787,   1742,   1698,   1655,   1612,   1571,   
        1530,   1490,   1450,   1412,   1374,   1337,   1301,   1265,   
        1231,   1197,   1164,   1132,   1101,   1070,   1040,   1011,   
         983,    956,    929,    903,    877,    853,    829,    805,   
         783,    761,    739,    718,    698,    678,    659,    641,   
         623,    605,    588,    572,    556,    541,    526,    511,   
         497,    483,    470,    457,    444,    432,    420,    409,   
         398,    387,    377,    367,    357,    347,    338,    329,   
         320,    312,    304,    296,    288,    280,    273,    266,   
         259,    253,    246,    240,    234,    228,    222,    217,   
         211,    206,    201,    196,    191,    186,    182,    177,   
         173,    169,    165,    161,    157,    153,    150,    146,   
         143,    139,    136,    133,    130,    127,    124,    121,   
         119,    116,    113,    111,    108,    106,    103,    101,   
          99,     97,     95,     93,     91,     89,     87,     85,   
          83,     81,          80,
};                                                                                        
#define NTC_ADC_ARRAY_NUM (sizeof(ntc_adc)/sizeof(int16u))
#define INDEX_MAX       (NTC_ADC_ARRAY_NUM-3)        /* 数组下标的最大可用取值,对应 80℃采样值下标 */
#define INDEX_MIN       3                                       /* 数组下标的最小可用取值,对应-10℃采样值下标 */
#define TEMPER_REF      -15                                     /* 温度基准点,即下标为0时对应的温度                    */
#define TEMPER_LAST     155                                     /* 温度最后值,即数组最后一个对应的温度        */

/*
***************************************************************************************************
* 函数名称: search_data()
* 输   入: search_value  查找基准值
* 输   出: 
* 作   者: Leigh Meng
* 日   期: 2010.08.11
* 功能描述: 二分查找adc采样值
*--------------------------------------------------------------------------------------------------
*注     意:
*    1.    返回值为查找表格中第一个小于查找值(search_value)的下标;
*    2.    查找值(search_value)大于查找表格中最大值时,返回1
*          查找值(search_value)小于查找表格中最小值时,返回查找表格数量,即最大下标+1 
***************************************************************************************************
*/
static int16u search_data(int16u search_value)
{
    int16u  max = NTC_ADC_ARRAY_NUM - 1;
    int16u  min = 1;
    int16u  mid;

    while(min <= max)
    {
        mid = (max + min) / 2;

        if(search_value > ntc_adc[mid])
        {
            max = mid - 1;
        }
        else 
        {
            min = mid + 1;
        }
    }

    return min;
}

/*
***************************************************************************************************
* 函数名称: temper_calc()
* 输   入: index       数组下标
*           adc0_value  ad滤波后的采样值
* 输   出: ntc温度测量值
* 作   者: Leigh Meng
* 日   期: 2010.10.26
* 功能描述: 温度计算(分段线性插值算法)
***************************************************************************************************
*/
static int16s temper_calc(int16u index, int16u adc0_value)
{
    if (index > INDEX_MAX || index < INDEX_MIN)
    {
        return TEMPER_ERROR;           //  返回错误采样温度
    }
//    else if (index < INDEX_MIN)
//    {
//        return -150;        //  返回-15.0℃
//    }
    else
    {
        /*************************************************
         === 直线近似计算 ===
         y1 = kx1+b       x <-> adc_value
         y2 = kx2+b       y <-> temper
         yn = kxn+b
               y2 - y1     yn - y1
         k  = --------- = ---------
               x2 - x1     xn - x1
               (y2 - y1)*(xn - x1)
         yn = --------------------- + y1
                     (x2 - x1)
         *************************************************/

        int16s temper;
                                        
        temper  = 10*(ntc_adc[index-1] - adc0_value);   //  放大10倍,temper放大10倍
        temper /= (ntc_adc[index-1] - ntc_adc[index]);  //  (xn - x1)
        temper += 10*(index + TEMPER_REF - 1);
        
        return temper;            
    }       
}

/*
***************************************************************************************************
* 函数名称: adc0_temper_calc()
* 输   入: adc0_sample_index  温度采样序列值
*           adc0_sample_num    ad采样次数
* 输   出: 
* 作   者: Leigh Meng
* 日   期: 2010.03.22
* 功能描述: 
***************************************************************************************************
*/
void adc0_temper_calc(int8u adc0_sample_index, int8u adc0_sample_num)
{
    static int8u cnt[ST_MAX];                // 采样滤波计数

        if (adc0_sample_index < ST_MAX) // 温度采样索引值在预定义范围内
    {                       
        int16u adc0_filter;         // 滤波处理的ad采样值                                  
        int16s temper;                  // 温度值
        int16u index;               // 查表下标值

                // 算术平均滤波(采样值求和后平均)处理
        adc0_filter = arithmetic_average_filter(&adc0_sample_value[0], adc0_sample_num);  
        index       = search_data(adc0_filter);             // 取温度数组下标
        temper      = temper_calc(index, adc0_filter);      // 温度计算                

                if (abs(battery_info.battery_temper[adc0_sample_index] - temper) > 5)        // 两次温度采样相差0.5℃以上
                {
                        cnt[adc0_sample_index] += 1;
                        
                        if (cnt[adc0_sample_index] > 1)
                        {
                                cnt[adc0_sample_index] = 0;
                                battery_info.battery_temper[adc0_sample_index] = temper; // 赋值NTC温度 
                        }        
                }
                else
                {
                        cnt[adc0_sample_index] = 0;
                        battery_info.battery_temper[adc0_sample_index] = temper; // 赋值NTC温度        
                }
                 
    }
}

 

  • 0
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在 Arduino 中,NTC(负温度系数)和 PTC(正温度系数)温度传感器的温度计算方法如下: 1. NTC 温度传感器: NTC 温度传感器的电阻值随温度变化而变化,因此可以通过测量电阻值来计算温度。 首先,需要根据 NTC 温度传感器的 datasheet 获取其电阻-温度曲线表(也称为 B 值曲线表)。该表通常给出了在不同温度NTC 电阻值的对应关系。 然后,使用 Arduino 的模拟输入引脚测量 NTC 的电阻值,并根据 B 值曲线表计算温度。 具体计算公式如下: Rt = R0 * exp(B * (1/T - 1/T0)) 其中,Rt 是 NTC 的电阻值;R0 是 NTC 在参考温度 T0 下的电阻值;B 是 NTC 的 B 值;T 是当前温度的绝对温度值(单位为 K),可以通过测量 NTC 的电阻值和参考电阻值计算得到。 2. PTC 温度传感器: PTC 温度传感器的电阻值随温度变化而变化,但与 NTC 温度传感器不同的是,PTC 的电阻值随温度升高而增加,因此需要使用不同的计算方法。 首先,需要根据 PTC 温度传感器的 datasheet 获取其电阻-温度曲线表。 然后,使用 Arduino 的模拟输入引脚测量 PTC 的电阻值,并根据电阻-温度曲线表计算温度。 具体计算公式如下: Rt = R0 * (1 + a * T) 其中,Rt 是 PTC 的电阻值;R0 是 PTC 在参考温度 T0 下的电阻值;a 是 PTC 的温度系数;T 是当前温度(单位为 °C),可以通过测量 PTC 的电阻值和参考电阻值计算得到。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值