⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 aio.c

📁 LPC213x系列启动代码,支持ADS1.2编译器,已成功运行
💻 C
字号:
#include "config.h"

// 从ADS1100读来的模拟量
WORD wAiSamp[AI_CHANN_NUM][AI_SAMP_NUM];
WORD wAiFact[AI_CHANN_NUM];
WORD wAiZero[AI_CHANN_NUM];
WORD wAiZeroTemp[AI_CHANN_NUM];
WORD wAiAdjust[AI_CHANN_NUM];
WORD wAiAdjustTemp[AI_CHANN_NUM];
WORD wAiSampNum;
WORD wAiChann;
WORD wAiCount;
UDWORD udAiSwitchDelay;

UWORD uwAo[AO_CHANN_NUM];

WORD wxAiSamp[xAI_CHANN_NUM][xAI_SAMP_NUM];
WORD wxAiFact[xAI_CHANN_NUM];
WORD wxAiZero[xAI_CHANN_NUM];
WORD wxAiZeroTemp[xAI_CHANN_NUM];
WORD wxAiAdjust[xAI_CHANN_NUM];
WORD wxAiAdjustTemp[xAI_CHANN_NUM];
WORD wxAiChann;
WORD wxAiCount;

// 0 - 1200
float Krdo[13] = {0.000, 4.096, 8.138, 12.209, 16.397, 20.644, 24.905, 29.129, 33.275, 37.326, 41.276, 45.119, 48.838};

// -20 - 80
float Ntc[101]={
		37.4,	35.49,	33.7,	32.01,	30.42,	28.92,	27.51,	26.18,	24.92,	23.73,
		22.61,	21.56,	20.56,	19.62,	18.72,	17.88,	17.07,	16.31,	15.58,	14.9,
		14.25,	13.62,	13.02,	12.46,	11.92,	11.41,	10.92,	10.46,	10.02,	9.597,
		9.197,	8.816,	8.453,	8.106,	7.776,	7.461,	7.161,	6.874,	6.601,	6.34,
		6.09,	5.852,	5.624,	5.407,	5.199,	5.000,	4.811,	4.63,	4.457,	4.291,
		4.132,	3.98,	3.835,	3.695,	3.562,	3.434,	3.311,	3.194,	3.081,	2.973,
		2.869,	2.77,	2.674,	2.582,	2.494,	2.41,	2.328,	2.25,	2.175,	2.103,
		2.034,	1.967,	1.903,	1.842,	1.783,	1.726,	1.671,	1.618,	1.567,	1.518,
		1.471,	1.425,	1.381,	1.339,	1.298,	1.258,	1.22,	1.184,	1.148,	1.114,
		1.081,	1.049,	1.018,	0.9886,	0.9598,	0.9321,	0.9052,	0.8793,	0.8542,	0.83,
		0.8065,	
		 };
		 
void Ai_Init(void)
{
	WORD i;
	
	// 读出保存在5045中的校准值
	for(i = 0; i < AI_CHANN_NUM; i++) 
	{
		wAiZero[i] = ReadWord(ZERO_ADDR + i * 2);
		wAiAdjust[i] = ReadWord(ADJUST_ADDR + i * 2);
	}
	
	for(i = 0; i < xAI_CHANN_NUM; i++) 
	{
		wxAiZero[i] = 0;		
		wxAiAdjust[i] = ReadWord(xADJUST_ADDR + i * 2);
	}
	
	// 热电偶通道
	wAiSampNum = 0;
	wAiChann = 0;
	
	// 温度补偿通道
	wxAiChann = 0;
	wxAiCount = 0;
	AD0CR = (1 << 24) | (1 << 21) | (13 << 8) | (1 << (6 + wxAiChann));
	
	DelayMS(2000);	// 保证读到正确的温度值
}//void Ai_Init(void)

void ReadAi(void)
{
	WORD i;
	long temp;
	float tempMin,tempMax;
	float tempf;	
	WORD flag;
	WORD wmin,wmax;
		
	if(udTickCount > udAiSwitchDelay)
	{
		// 温度补偿通道
		flag = TRUE;
		while(flag)
		{
			temp = AD0DR;
			if(temp & ADC_DONE)
			{
				wxAiCount++;		
				if(wxAiCount >= xAI_KILL_NUM)
				{
					wxAiSamp[wxAiChann][wxAiCount - xAI_KILL_NUM] = (temp & 0x0ffc0) >> 6;
					
					if(wxAiCount == (xAI_SAMP_NUM + xAI_KILL_NUM - 1))
					{
						// 求平准值
						tempf = 0; wmin = 32767; wmax = -32768;
						for(i = 0; i < xAI_SAMP_NUM; i++) 
						{
							tempf += wxAiSamp[wxAiChann][i];
							
							if(wxAiSamp[wxAiChann][i] <= wmin) wmin = wxAiSamp[wxAiChann][i];
							if(wxAiSamp[wxAiChann][i] >= wmax) wmax = wxAiSamp[wxAiChann][i];
						}
						tempf = (tempf - wmin - wmax) / (xAI_SAMP_NUM - 2);
						
						wxAiAdjustTemp[wxAiChann] = tempf;
						tempf = tempf * wxAiAdjust[wxAiChann] / 1024;
							
						for(i = 0; i < 100; i++)
						{
							tempMin = (15.0 * 1024.0) / ((5.1 + Ntc[i] + 1.0) * 3.3);
							tempMax = (15.0 * 1024.0) / ((5.1 + Ntc[i + 1] + 1.0) * 3.3);
							if(tempf >= tempMin && tempf < tempMax)
							{
								tempf = (tempf - tempMin) * 10 / (tempMax - tempMin);
								tempf = (i - 20) * 10 + tempf;
								break;
							}
						}
							
						if(i == 100) wxAiFact[wxAiChann] = 0;
						else wxAiFact[wxAiChann] = tempf;
						
						wxAiCount = 0;
						flag = FALSE;
					}
				}
				
				AD0CR = (1 << 24) | (1 << 21) | (13 << 8) | (1 << (6 + wxAiChann));
			}
		}
		
		// 热电偶通道
		// 保存当前采样值
		wAiSamp[wAiChann][wAiCount++] = ADS1100_ReadData();
		if(wAiCount >= AI_SAMP_NUM) wAiCount = 0;
		
		// 已保存的采样值个数
		wAiSampNum++;
		if(wAiSampNum > AI_SAMP_NUM) wAiSampNum = AI_SAMP_NUM;
		
		// 求平均值
		temp = 0; wmin = 32767; wmax = -32768;
		for(i = 0; i < wAiSampNum; i++) 
		{
			temp += wAiSamp[wAiChann][i];
			
			if(wAiSamp[wAiChann][i] <= wmin) wmin = wAiSamp[wAiChann][i];	// 找最小值
			if(wAiSamp[wAiChann][i] >= wmax) wmax = wAiSamp[wAiChann][i];	// 找最大值
		}
		if(wAiSampNum == AI_SAMP_NUM)
		{
			temp = (temp - wmin - wmax) / (wAiSampNum - 2);
		}
		else
		{
			temp = temp / wAiSampNum;
		}
		wAiZeroTemp[wAiChann] = (WORD)temp;		
		temp = temp - wAiZero[wAiChann];
		
		// 求实际物理值470K/10K
		for(i = 0; i < 12; i++)
		{
			tempMin = (Krdo[i] * 47 * 32768.0) / 5000.0;
			tempMax = (Krdo[i + 1] * 47 * 32768.0) / 5000.0;
			if(temp >= tempMin && temp < tempMax)
			{
				temp = (temp - tempMin) * 1000 / (tempMax - tempMin);
				temp = i * 1000 + temp;
				break;
			}
		}
		wxAiFact[wxAiChann] = 250;
		if(i == 12) wAiFact[wAiChann] = 0 + wxAiFact[wxAiChann];
		else wAiFact[wAiChann] = (WORD)temp  + wxAiFact[wxAiChann];
		wAiAdjustTemp[wAiChann] = wAiFact[wAiChann] - wxAiFact[wxAiChann];
		
		// 读采样值的周期控制
		udAiSwitchDelay = udTickCount + AI_SWITCH_DELAY;		
	}
}//void ReadAi(void)

void WriteAo(void)
{
	DACR = (1 << 16) | (uwAo[0] << 6);
}//void WriteAo(void)

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -