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

📄 main.c

📁 51单片机测温程序
💻 C
字号:
//======================================================
//  The information contained herein is the exclusive property of
//  Sunnnorth Technology Co. And shall not be distributed, reproduced,
//  or disclosed in whole in part without prior written permission.
//  (C) COPYRIGHT 2003  SUNNORTH TECHNOLOGY CO.
//  ALL RIGHTS RESERVED
//  The entire notice above must be reproduced on all authorized copies.
//========================================================

//========================================================
//  工程名称:	temperature_ctrl
//  功能描述:	水温的采集,转换,控制等
//  涉及的库:	无
//  组成文件:	main.c
//	硬件连接:	见数据采集系统使用说明书
//	维护记录:	2005-11-25 v1.0
//========================================================
#include "key.h"
#include "SPCE061V004.h"

typedef struct PID 
{
        float  SetPoint;			//  设定目标 Desired Value
        float  Proportion;			//  比例常数 Proportional Const
        float  Integral;			//  积分常数 Integral Const
        float  Derivative;			//  微分常数 Derivative Const
        int    LastError;			//  Error[1]
        int    SumError;			//  误差累计值
} PID;
PID  stPID;

int  fOut = 0;						// PID计算结果
float fT,K,B;    

enum system_status { system_temperature_set,system_temperature_control};
enum system_status status;

float PIDCalc( PID *pp, int NextPoint );   		// PID计算函数             
void  ADC_temperature(void);					// AD转换值的数值处理
void  Send_temperature(void); 					// AD转换值的通信
void  Control_temperature(void); 				// AD转换值的结果处理
void  PIDinit(void);							// PID初始化
int   adc_data_cmp();							// ADC结果处理
void  key_value_process(int key);

unsigned int    giADC_DataSave[18] ;			//ADC转换的数据
unsigned int    guiLED_Value[3] ={0,0,0};		//保存显示的LED数字
unsigned int    iAddrSpeech;
unsigned int  	G_ADC_flag = 0;
//=============================================================
// 语法格式:	int main(void);
// 实现功能:	主函数
// 参数:		无
// 返回值:		无
//=============================================================
int main(void){ 
    int iKeyValue;
    unsigned int uiTime = 5;
    
	System_Initial();									//系统初始化
    status = system_temperature_set;
	while(1)
	{
	    System_ServiceLoop();							//键盘扫描
	    iKeyValue = SP_GetCh();							//取键值
		if(iKeyValue!=0)
		    key_value_process(iKeyValue);				//键值处理
	    if(status == system_temperature_control && G_ADC_flag == 1)
	    {												//控制状态下执行以下操作
		    	ADC_temperature();						//测量温度结果处理
	        	Send_temperature();						//测量温度结果通信
	        	Control_temperature();					//执行控制
		}
		*P_Watchdog_Clear = 0x0001;						//清看门狗
   }
}
//=============================================================
// 语法格式:	void PIDinit(void);
// 实现功能:	PID初始化
// 参数:		无
// 返回值:		无
//=============================================================
void PIDinit(void)
{
	stPID.LastError = 0;
	stPID.SumError =  0;
}   

//=============================================================
// 语法格式:	PIDCalc( PID *pp, int NextPoint);
// 实现功能:	PID运算处理
// 参数:		PID *pp:PID指针;
//				int NextPoint:当前温度结果
// 返回值:		PID运算结果
//=============================================================
float PIDCalc( PID *pp, int NextPoint )
{
	int dError,Error;       
    Error = pp->SetPoint*10 -  NextPoint;				// 偏差
    pp->SumError += Error;                      		// 积分
    dError = Error - pp->LastError;     				// 当前微分
    pp->LastError = Error;        
    return (pp->Proportion * Error              		// 比例项
//		+   pp->Integral * pp->SumError         		// 积分项
		-   pp->Derivative * dError	* dError    		// 微分项
    );
}
//=============================================================
// 语法格式:	void ADC_temperature(void);
// 实现功能:	将ADC_Value转换为温度值
// 参数:		无
// 返回值:		无
//=============================================================
void ADC_temperature(void)
{
    int adc_data;
    static int siTlast = 0;
    adc_data = adc_data_cmp();                     
    adc_data /= 16;        								//计算温度平均值
	K = 0.0891;  										//确定温度系数
    B = 2.4;
    fT = (adc_data * K) - B;				            //换算成温度值	   
  														//将温度值转换成十进制
    guiLED_Value[0] = (int)fT/10;		   				//十位
	guiLED_Value[1] = (int)fT%10;						//个位
    guiLED_Value[2] = (int)(fT*10)%10;					//小数位

	G_ADC_flag = 0;
} 
//=============================================================
// 语法格式:	Send_temperature(void);
// 实现功能:	将温度值传送至上位机
// 参数:		无
// 返回值:		无
//=============================================================
void Send_temperature(void)								//Uart Send
{															
	*P_UART_Data = 0xaa;								//桢头
	while((*P_UART_Command2&0x0040)==0);				//Wait
	*P_UART_Data = (int)fT;								//整数部分
	while((*P_UART_Command2&0x0040)==0);				//Wait
	*P_UART_Data = guiLED_Value[2];						//小数部分
	while((*P_UART_Command2&0x0040)==0);				//Wait
	*P_UART_Data = 0x55;								//桢尾
}
//=============================================================
// 语法格式:	Control_temperature();
// 实现功能:	设置PID调解参数,并跟据PID运算结果控制加热器
// 参数:		无
// 返回值:		无
//=============================================================
void Control_temperature()
{	
    stPID.Proportion = 1.3;              				//设置PID比例值
	stPID.Integral   = 0;              					//设置PID积分值
    stPID.Derivative = 2;              					//设置PID微分值
    fOut = PIDCalc ( &stPID,(int)(fT*10) );				//PID计算

	if(fOut<=0)
	    *P_IOB_Buffer &= 0xfdff; 			          	//温度高于设定值,关闭电炉
	else
	    *P_IOB_Buffer |= 0x0200; 			          	//温度低于设定值,打开电炉
}
//=============================================================
// 语法格式:	adc_data_cmp();
// 实现功能:	ADC参数处理,去掉最大值和最小值
// 参数:		无
// 返回值:		去除最大值和最小值后的16次采样总和
//=============================================================
int adc_data_cmp()
{
    int max;
    int min;
    int Sum=0;    
    int i;
    max = giADC_DataSave[0];
    for(i=0;i<18;i++)
    {
    	if(giADC_DataSave[i]>max)     
    	   max = giADC_DataSave[i];          //取出最大值
    }
    min = giADC_DataSave[0];
    for(i=0;i<18;i++){
    	if(giADC_DataSave[i]<min)
    		min =  giADC_DataSave[i];        //取出最小值
    }
    for(i=0;i<18;i++)
       Sum += giADC_DataSave[i];             //累计值
    Sum = Sum - max-min;                     //排除最大最小值
    return(Sum);
}
//=============================================================
// 语法格式:	void key_value_process(int key);
// 实现功能:	键值处理程序
// 参数:		int key:键值
// 返回值:		无
//=============================================================
void key_value_process(int key)
{
	int iAddr;
	switch(key)
	{
    	case 0: break;
    	case 1:
    		if(status == system_temperature_set)     //温度设置状态下
    		{
    		  	guiLED_Value[0]++;                   //温度值增加
    			if(guiLED_Value[0]==10)
	            guiLED_Value[0] = 0;                
	        }	    
	        break;
		case 2: 
	    	if(status == system_temperature_set)
	        {
	          	guiLED_Value[1]++;
    			if(guiLED_Value[1]==10)
	           	guiLED_Value[1] = 0;
	        }	    	
	        break;
		case 4: 
	    	if(status == system_temperature_set)
	        {
	          	guiLED_Value[2]++;
    			if(guiLED_Value[2]==10)
	           	guiLED_Value[2] = 0;
	        }	    	
	        break;
		case 8:
	    	if(status == system_temperature_set)
	        {    
	      	   	status = system_temperature_control;
	          	stPID.SetPoint = guiLED_Value[0]*10 
	          				+ guiLED_Value[1] 
	          				+ 0.1*guiLED_Value[2];
	          	SP_INT_IRQ5();                          //允许ADC
			}
	        else
	        {
	        	turn_off_IRQ5();                   		//禁止ADC
	         	status =  system_temperature_set;  		//系统状态恢复为设置状态
	        }		               	    	               	    
	        break;
		default: break;						
	}
}	

⌨️ 快捷键说明

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