NTC(Negative Temperature Coefficient)是指随温度上升电阻呈指数关系减小、具有负温度系数的热敏电阻现象和材料。该材料是利用锰、铜、硅、钴、铁、镍、锌等两种或两种以上的金属氧化物进行充分混合、成型、烧结等工艺而成的半导体陶瓷,可制成具有负温度系数(NTC)的热敏电阻。其电阻率和材料常数随材料成分比例、烧结气氛、烧结温度和结构状态不同而变化。现在还出现了以碳化硅、硒化锡、氮化钽等为代表的非氧化物系NTC热敏电阻材料。

STM32 HAL NTC(3950 10k)查表法-LMLPHP

在实际电路中,则是采用分压的方式进行,该点的电压采集,其NTC设计原理图如图所示:

STM32 HAL NTC(3950 10k)查表法-LMLPHP

而图中的下面则是接NTC的接口。

下面则是ntc程序

ntc_3950.c

#include "ntc_3950.h"

#define SHORT_CIRCUIT_THRESHOLD 15
#define OPEN_CIRCUIT_THRESHOLD 4080
uint16_t DATA_BUFF1[ADC_LOOP1_MARK] = {0};
uint16_t DATA_BUFF2[ADC_LOOP2_MARK] = {0};

/*定义一个结构体类型数组:(温度-阻值)表 温度按降序填*/
const NTC_10KTYPE  ntc_10k_table[] = {
125,325, 	
124,333, 	
123,342,  	
122,352, 	
121,361, 	
120,371, 		
119,381, 
118,392,
117,403,
116,414,	
115,426, 	
114,438, 	
113,450,
112,463,
111,476,
110,490,
109,504,
108,519,
107,534,
106,549,
105,566,
104,582,
103,600,
102,618,
101,636,
100,656,
99,675,
98,696,
97,718,
96,740,
95,763,
94,786,
93,811,
92,837,
91,863,
90,891,
89,919,
88,949,
87,980,
86,1011,
85,1045,
84,1079,
83,1114,
82,1151,
81,1190,
80,1230,
79,1271,
78,1314,
77,1359,
76,1406,
75,1454,
74,1504,
73,1557,
72,1611,
71,1668,
70,1726,
69,1788,
68,1851,		
67,1918, 		
66,1987,	
65,2059, 	
64,2134, 	
63,2212, 	
62,2293, 	
61,2378, 	
60,2466, 	
59,2558, 	
58,2654, 	
57,2755, 	
56,2859, 	
55,2968, 	
54,3082,  	
53,3201, 	
52,3324, 	
51,3454, 	
50,3572, 	
49,3730, 	
48,3877, 	
47,4031, 	
46,4192, 	
45,4360, 	
44,4536, 		
43,4720, 		
42,4913, 	
41,5114, 	
40,5324, 	
39,5545, 	
38,5775,
37,6017,
36,6270,	
35,6534, 	
34,6812, 	
33,7103,	
32,7407,
31,7727, 	
30,8062, 	
29,8413, 	
28,8781, 	
27,9168, 	
26,9574, 	
25,10000,	
24,10447, 	
23,10916, 	
22,11409, 	
21,11928,  	
20,12472, 	
19,13045, 	
18,13647, 	
17,14280, 	
16,14945, 	
15,15646,	
14,16383, 	
13,17160, 	
12,17977, 	
11,18838, 	
10,19788, 	
9,20700, 	
8,21707,  	
7,22769, 	
6,23889,	
5,25071, 	
4,26317, 	
3,27633, 	
2,29022, 	
1,30490, 	
0,32040, 	
-1,33677, 	
-2,35409, 		
-3,37239, 	
-4,39175, 	
-5,41223, 	 
-6,43391, 	
-7,45686,	
-8,48117, 	
-9,50692, 		
-10,53420,
-11,56313,
-12,59380,
-13,62634,
-14,66087,
-15,69753,
-16,73646,
-17,77782,
-18,82178,
-19,86852, 		
-20,91824, 	
-21,97115,
-22,102747, 
-23,108745,
-24,115136, 
-25,121950, 	
-26,129215,
-27,136968,
-28,145244,
-29,154083,
-30,163528,
-31,173626, 	
-32,184428, 	
-33,195990, 	
-34,208371,
-35,221637, 	
-36,235861, 	
-37,251120, 	
-38,267501,	
-39,285097,	
-40,304011,
-41,324356,
-42,346255,
-43,369843,
-44,395272, 	
-45,422704, 	
-46,452323, 	
-47,484327,
-48,518939, 	
-49,556404, 	
-50,596993, 	
-51,641008,	
-52,688785,	
-53,740696,
-54,797157,
-55,858631,
};


uint16_t NTC_ADC(void) //ADC采集程序
{
	HAL_ADC_Start(&hadc);//开始ADC采集
	HAL_ADC_PollForConversion(&hadc,100);//等待采集结束
	if(HAL_IS_BIT_SET(HAL_ADC_GetState(&hadc), HAL_ADC_STATE_REG_EOC))//读取ADC完成标志位
	{
		return HAL_ADC_GetValue(&hadc);//读出ADC数值
	}
	return 0;
}

//[-级ADC滤波]读1次ADC值放入以往数组,去掉最大最小值后取平均值
uint16_t adc_getavg1(uint16_t adc)
{
	uint16_t maxA,minA;
	uint32_t avg=0;
	uint16_t jA;
	
	maxA = 0;
	minA = 0xffff;
	
	for(jA=0;jA<(ADC_LOOP1_MARK-1);jA++)	//将数据左移一位,空出【0】存新数据
    {
		DATA_BUFF1[(ADC_LOOP1_MARK-1)-jA] = DATA_BUFF1[(ADC_LOOP1_MARK-2)-jA];
    }
	DATA_BUFF1[0] = adc;	//存新数据
	
	for(jA=0;jA<ADC_LOOP1_MARK;jA++)	//将最大值与最小值存入maxA、minA
    {
		if(maxA<=DATA_BUFF1[jA]) maxA = DATA_BUFF1[jA];
		if(maxA>=DATA_BUFF1[jA]) maxA = DATA_BUFF1[jA];
    }
	for(jA=0;jA<ADC_LOOP1_MARK;jA++)	//除去最大与最小值的相加
    {
		if((DATA_BUFF1[jA] != maxA) && (DATA_BUFF1[jA] != minA))
		{
			avg+=DATA_BUFF1[jA];
		}
		if(DATA_BUFF1[jA] == maxA) maxA=0xffff;
		if(DATA_BUFF1[jA] == minA) maxA=0xffff;
    }
	minA = avg/(ADC_LOOP1_MARK-1);	//求取平均数
	return minA;
}

//[第二级adc滤波]读10次一级滤波后的数据,只有10次都相同才返回最終ADC数据值
uint16_t adc_getavg2(uint16_t adc)
{
	uint8_t jA;
	uint8_t cou=0;
	for(jA=0;jA<(ADC_LOOP2_MARK-1);jA++)
    {
		DATA_BUFF2[(ADC_LOOP2_MARK-1)-jA] = DATA_BUFF2[(ADC_LOOP2_MARK-2)-jA];
    }
	DATA_BUFF2[0] = adc;	//存新数据
	
	for(jA=0;jA<(ADC_LOOP2_MARK-2);jA++)
    {
		if(DATA_BUFF2[jA] == adc)
		{
			cou++;
			if(cou >= ADC_LOOP2_MARK-2)
			{
				DATA_BUFF2[ADC_LOOP2_MARK-1] = adc;
			}
		}else{
			cou = 0;
		}
    }
	return DATA_BUFF2[ADC_LOOP2_MARK-1];
}

#include "debug.h"

float ntc_temp(uint16_t adc , uint8_t *ntc_state)
{
    float gather_now_voltage=0;
    float ntc_now_resistance=0;
    int index=0;
	if(adc <= SHORT_CIRCUIT_THRESHOLD)			//小于15为短路
	{
		*ntc_state = 1;
	}
	else if(adc >= OPEN_CIRCUIT_THRESHOLD)		//大于4080等于开路
	{
		*ntc_state = 2;
	}else{
		*ntc_state = 0;
	}
	/*采集得到Va点电压 Va=gather_now_voltage*/
	gather_now_voltage=adc * ADC_REFERENCE_VOLTAGE/4096;
	if(gather_now_voltage==0)
	{
		gather_now_voltage=0.01;
	}
	/*求出热敏电阻的阻值 我这里Vcc电压和adc基准电压相等 r2=10k=10000*/
	ntc_now_resistance=gather_now_voltage/((ADC_REFERENCE_VOLTAGE-gather_now_voltage)/10000);/*r=u/i*/
	/*根据热敏电阻阻值查表得出温度*/
	for(index=0; index<NTC_LIST_MAX_SIZE; index++)
	{
		if(ntc_10k_table[index].resistance >= ntc_now_resistance)/*查到该温度表索引为 index*/
		{
			float min_resistance = (float)ntc_10k_table[(index>0) ? index-1 : 0].resistance;/*得出下区间电阻值*/
			float max_resistance = (float)ntc_10k_table[index].resistance;/*得出上区间电阻值*/
			float d1 = (float)(max_resistance - min_resistance) / 10;/*将该区间的阻值分为10等分*/
			float d2 = (float)(((ntc_now_resistance - min_resistance) / d1));/*得出n份*0.1°C*/
			d2 = (10 - d2);
			/*以上四步是求小数点温度*/
			return (float)(ntc_10k_table[index].temperature*10) + d2;
		}
	}
	return (0.0);
}


ntc_3950.h

#ifndef __NTC_3950_H
#define __NTC_3950_H

#include "stm32f0xx_hal.h"
#include "main.h"

#define	ADC_LOOP1_MARK 20
#define ADC_LOOP2_MARK 5

/*定义一个结构体:温度,电阻阻值*/
typedef struct{
   int    temperature;//单位:°C
   float  resistance;//单位:Ω
}NTC_10KTYPE;

#define NTC_LIST_MAX_SIZE             (sizeof(ntc_10k_table) / sizeof(ntc_10k_table[0]))/*表的长度*/
#define ADC_REFERENCE_VOLTAGE         (float)3300 /*adc 电压基准*/


extern ADC_HandleTypeDef hadc;
extern uint16_t DATA_BUFF1[ADC_LOOP1_MARK];
extern uint16_t DATA_BUFF2[ADC_LOOP2_MARK];

uint16_t NTC_ADC(void);
uint16_t adc_getavg1(uint16_t adc);
uint16_t adc_getavg2(uint16_t adc);
float ntc_temp(uint16_t adc , uint8_t *ntc_state);

#endif

在main.c的使用如下:

    static float temp_data=0.0;
	temp_data = ntc_temp(adc_getavg2(adc_getavg1(NTC_ADC())),&ntc_state);
	Info.temp = temp_data;	//进行整数转化
	switch (ntc_state)
    {
		case 0:		//NTC正常工作
			if(Info.temp >= 0)		//单位确认
			{
				Info.temp_unit = 0;	//负号关闭
			}else if(Info.temp < 0){
				Info.temp_unit = 1;	//负号开启
				Info.temp = abs(Info.temp);		//转换为正数
			}
    		break;
    	case 1:		//NTC短路

    		break;
    	case 2:		//NTC开路

    		break;
    	default:
    		break;
    }

最终采集温度比较理想,这章主要就贴了个代码,主要是有点难受不想写了,后面在补充吧

02-03 09:35