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

📄 or100_sw.c

📁 用STC单片机实现光发射机的自动增益控制
💻 C
📖 第 1 页 / 共 2 页
字号:
//#define DEBUG

//#include <STC12C2052.H>
#include <myheadfile.h>

extern unsigned char code ConstSelf[];			//常量区,定义见《数据结构》
extern unsigned char value_G[];					//变量区,定义见《数据结构》
extern unsigned char code CurrentSet[];			//控制区,定义见《数据结构》

extern unsigned char sample_data_G[];
extern unsigned char idata netlayer_buff_G[];

extern void unES_en485trans(void);
extern void enES_un485trans(void);
extern void wr_eeprom(unsigned char, unsigned char *);

unsigned char idata eep_buff[SET_AREA_LEN];


/*************************************************************************/
/*任务定义:对浮点数做字节倒序                                        */
/*输入:倒序前的浮点数                                                        */
/*输出:倒序后的浮点数                                                      */
/*************************************************************************/
float ByteReversal(float float_in)
{
union { float flt; unsigned char bytes[4]; } float2byte ;
unsigned char temp;

	float2byte.flt = float_in;
	temp = float2byte.bytes[0];
	float2byte.bytes[0] = float2byte.bytes[3];
	float2byte.bytes[3] = temp;
	temp = float2byte.bytes[1];
	float2byte.bytes[1] = float2byte.bytes[2];
	float2byte.bytes[2] = temp;
	return(float2byte.flt);
}



/*************************************************************************/
/*任务定义:报警检查和状态设置,保护                                        */
/*输入:采样后的平均值,外部变量CurrentSet                              */
/*输出:报警个数                                                             */
/* NOTE:2个采样通道,输入光功率1 = AD[0];输入光功率2 = AD[1]  */
//只比较低低门限,高、高高门限及死区、使能都不用(仅在掉电后存贮供NE2使用)
/*************************************************************************/
/*
unsigned char Check8Alarm(float op1,float op2)
{
unsigned char alarm_number;
static unsigned char count;
union { float flt; unsigned char bytes[4]; } float2byte;

	alarm_number = 0;
	count = (count+1)%4096;
	
	float2byte.bytes[0] = CurrentSet[18];		//低报警门限,注意控制量存贮也是字节倒序了的
	float2byte.bytes[1] = CurrentSet[17];
	float2byte.bytes[2] = CurrentSet[16];
	float2byte.bytes[3] = CurrentSet[15];
	if(op1<float2byte.flt)
	{
		alarm_number++;
		float2byte.bytes[0] = CurrentSet[14];		//低低报警门限
		float2byte.bytes[1] = CurrentSet[13];
		float2byte.bytes[2] = CurrentSet[12];
		float2byte.bytes[3] = CurrentSet[11];
		if(op1<float2byte.flt) LED1 = (count==0) ? !LED1 : LED1;	//闪烁
		else LED1 = 0;			//长亮
	}
	else LED1 = 1;
	
	float2byte.bytes[0] = CurrentSet[61];
	float2byte.bytes[1] = CurrentSet[60];
	float2byte.bytes[2] = CurrentSet[59];
	float2byte.bytes[3] = CurrentSet[58];
	if(op2<float2byte.flt)
	{
		alarm_number++;
		float2byte.bytes[0] = CurrentSet[57];
		float2byte.bytes[1] = CurrentSet[56];
		float2byte.bytes[2] = CurrentSet[55];
		float2byte.bytes[3] = CurrentSet[54];
		if(op2<float2byte.flt) LED2 = (count==0) ? !LED2 : LED2;
		else LED2 = 0;
	}
	else LED2 = 1;

	return(alarm_number);
}
*/

/*************************************************************************/
/*任务定义:保护                                                  */
/*输入:保护使能设置,报警数量                                                              */
/*输出:当前保护状态                                                              */
/* 算法:如果保护使能设置是允许状态(默认状态),并且报警数量不为0,则检查要不要进行保护,否则清除保护*/
/* NOTE:  */
/*************************************************************************/
/* 算法:如果保护使能设置是允许状态(默认状态),并且报警数量不为0,则检查要不要进行保护,否则清除保护*/
/*
unsigned char Safeguard(unsigned char safeguard_en)
{
	if(safeguard_en) return(0);

}
*/



/* 下行数据包解析有错时做出的应答*/
/* 这是MAC层的应答 */
void AckIllegal(unsigned char err_type)
{
unsigned char trans_buff, check_sum;

	unES_en485trans();  			//禁止串口中断并允许发送
//==============================================
	SBUF = 0xA5;				//同步头

	trans_buff = SELF_MAC_ADDR ;		//取本机地址
	check_sum = trans_buff;
	while(!TI);
	TI = 0;
	SBUF = trans_buff;

	trans_buff = 0x1;				//MAC层长度固定为0x1
	check_sum += trans_buff;
	while(!TI);
	TI = 0;
	SBUF = trans_buff;

	trans_buff = err_type;			//错误类型
	check_sum += trans_buff;
	while(!TI);
	TI = 0;
	SBUF = trans_buff;

	trans_buff = -check_sum;		//校验和
	while(!TI);
	TI = 0;
	SBUF = trans_buff;
	while(!TI);
	TI = 0;

//==============================================

	enES_un485trans(); 				//禁止发送并允许串口中断
}


/* 执行写命令后做出的应答*/
/* 输入:应答的结果  */
void AckWrCom(unsigned char result)	
{
unsigned char trans_buff, check_sum;

	unES_en485trans();  			//禁止串口中断并允许发送
//==============================================
	SBUF = 0xA5;				//同步头

	trans_buff = SELF_MAC_ADDR ;		//取本机地址
	check_sum = trans_buff;
	while(!TI);
	TI = 0;
	SBUF = trans_buff;

	trans_buff = 0x4;					//净负荷(网络层)长度固定为0x4
	check_sum += trans_buff;
	while(!TI);
	TI = 0;
	SBUF = trans_buff;
//==================================
	trans_buff = netlayer_buff_G[0]+0x80;	//command on netlayer,加0x80区别上下行数据包
	check_sum += trans_buff;
	while(!TI);
	TI = 0;
	SBUF = trans_buff;
	
	trans_buff = netlayer_buff_G[1];	//offset on netlayer
	check_sum += trans_buff;
	while(!TI);
	TI = 0;
	SBUF = trans_buff;
	
	trans_buff = netlayer_buff_G[2];	//lenth on netlayer
	check_sum += trans_buff;
	while(!TI);
	TI = 0;
	SBUF = trans_buff;

	trans_buff = result;				//
	check_sum += trans_buff;
	while(!TI);
	TI = 0;
	SBUF = trans_buff;
//-------------------------------------	
	trans_buff = -check_sum;		//校验和
	while(!TI);
	TI = 0;
	SBUF = trans_buff;
	while(!TI);
	TI = 0;

//==============================================

	enES_un485trans(); 				//禁止发送并允许串口中断
}	
	


/* 读数据并上传, 包括对常量、变量的读都在这里实现,但不包括读控制量*/
//注意:常量和变量中已经作了字节倒序
/* MAC包结构为:同步(1byte)+MAC地址(1byte)+长度(1byte)+净负荷(n byte)+校验(1byte) */
/* 其中网络包结构为:命令(1byte)+起始地址(1byte)+字节数(1byte)+网络层数据(n byte) */
//输入:无,但实际上用了全局变量value_G和netlayer_buff_G
//输出:无
//本程序中同时完成了串口上传数据
unsigned char  RdConst8Value(void)
{
unsigned char trans_buff, check_sum;
unsigned char *addr;
unsigned char temp;

	temp = netlayer_buff_G[1]+netlayer_buff_G[2];
	if(temp>68 && netlayer_buff_G[0] == 0x2		//按照数据结构,变量边界为68
	 || temp>CONST_AREA_LEN && netlayer_buff_G[0] == 0x1 	//常量边界为77
	 || netlayer_buff_G[2]==0x0 )				//长度为0
		return(FAULS);					//如果写范围超出变量区的范围则返回错误

	unES_en485trans();  			//禁止串口中断并允许发送,同时也不会由于接收中断而造成netlayer_buff_G被破坏 ???
//==============================================
	SBUF = 0xA5;				//同步头

	trans_buff = SELF_MAC_ADDR ;		//取本机地址
	check_sum = trans_buff;
	while(!TI);
	TI = 0;
	SBUF = trans_buff;

⌨️ 快捷键说明

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