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

📄 disp.c

📁 ATT7022B与C8051F的通讯licheng
💻 C
📖 第 1 页 / 共 5 页
字号:
	SPIF = 0;

	SPI0DAT = revise_parameter2;
	spi_delay(30);
	while(!SPIF);
	spi_delay(60);
	SPIF = 0;

	SPI0DAT = revise_paremeter3;
	spi_delay(30);
	while(!SPIF);
	spi_delay(60);
	SPIF = 0;

	SPI_CS_HIGH_LEVEL();	 
	spi_delay(200);	
}

/*
***********************************************************************************************************
**函数原型		:  	recombine_i_data()
**参数说明		:  
				:	
					
**返回值		:		
**说	明		:	本函数用于把读取的24位电流数据重新组合成16位数据
					5A时(包括带电能):舍弃24位中的前4位和后4位
					1A时(包括带电能):舍弃24位中的前3位和后5位
************************************************************************************************************/


void recombine_i_data(){
	uchar temp1;
#ifdef MAX_PAR_300_00V_5A
	new_recombine_high = ((original_high_byte&0x0f) << 4);
	temp1 = ((original_middle_byte&0xf0) >> 4);
	new_recombine_high |= temp1;

	new_recombine_low = ((original_middle_byte & 0x0f) << 4);
	temp1 = ((original_low_byte & 0xf0) >> 4);
	new_recombine_low |= temp1;	
#endif

#ifdef MAX_PAR_57_74V_5A
	new_recombine_high = ((original_high_byte&0x0f) << 4);
	temp1 = ((original_middle_byte & 0xf0) >> 4);
	new_recombine_high |= temp1;

	new_recombine_low = ((original_middle_byte & 0x0f) << 4);
	temp1 = ((original_low_byte & 0xf0) >> 4);
	new_recombine_low |= temp1;	
#endif
 	  
#ifdef MAX_PAR_57_74V_1A
	new_recombine_high = ((original_high_byte & 0x1f) << 3);
	temp1 = ((original_middle_byte & 0xe0) >> 5);
	new_recombine_high |= temp1;
	
	new_recombine_low = ((original_middle_byte & 0x1f) << 3);
	temp1 = ((original_low_byte & 0xe0) >> 5);
	new_recombine_low |= temp1;	
#endif

#ifdef MAX_PAR_300_00V_1A
	new_recombine_high = ((original_high_byte & 0x1f) << 3);
	temp1 = ((original_middle_byte & 0xe0) >> 5);
	new_recombine_high |= temp1;
	
	new_recombine_low = ((original_middle_byte & 0x1f) << 3);
	temp1 = ((original_low_byte & 0xe0) >> 5);
	new_recombine_low |= temp1;	
#endif

/*¥¥¥¥¥¥¥¥¥¥¥¥¥¥加电能¥¥¥¥¥¥¥¥¥¥¥*/
#ifdef MAX_PAR_57_74V_5A_ENERGY //舍弃前4后4位
	new_recombine_high = ((original_high_byte & 0x0f) << 4);
	temp1 = ((original_middle_byte & 0xf0) >> 4);
	new_recombine_high |= temp1;

	new_recombine_low = ((original_middle_byte & 0x0f) << 4);
	temp1 = ((original_low_byte & 0xf0) >> 4);
	new_recombine_low |= temp1;		
#endif
//下面的情况有待商量
#ifdef MAX_PAR_300_00V_5A_ENERGY
	new_recombine_high = ((original_high_byte&0x0f) << 4);
	temp1 = ((original_middle_byte&0xf0) >> 4);
	new_recombine_high |= temp1;

	new_recombine_low = ((original_middle_byte & 0x0f) << 4);
	temp1 = ((original_low_byte & 0xf0) >> 4);
	new_recombine_low |= temp1;	
#endif

#ifdef MAX_PAR_57_74V_1A_ENERGY
	new_recombine_high = ((original_high_byte & 0x1f) << 3);
	temp1 = ((original_middle_byte & 0xe0) >> 5);
	new_recombine_high |= temp1;
	
	new_recombine_low = ((original_middle_byte & 0x1f) << 3);
	temp1 = ((original_low_byte & 0xe0) >> 5);
	new_recombine_low |= temp1;	
#endif

#ifdef MAX_PAR_300_00V_1A_ENERGY
	new_recombine_high = ((original_high_byte & 0x1f) << 3);
	temp1 = ((original_middle_byte & 0xe0) >> 5);
	new_recombine_high |= temp1;
	
	new_recombine_low = ((original_middle_byte & 0x1f) << 3);
	temp1 = ((original_low_byte & 0xe0) >> 5);
	new_recombine_low |= temp1;	
#endif

	   
}
/*
***********************************************************************************************************
**函数原型		:  	recombine_pq_data()
**参数说明		:  
				:	
					
**返回值		:		
**说	明		:	本函数用于把读取的24位有功或无功功率数据重新组合成16位数据
					    5A时(不带电能):舍弃24位中的前4位和后4位;
					    1A时(不带电能):舍弃24位中的前3位和后5位;
					    带电能(包括5A和1A):舍弃24位中的前6位和后2位
************************************************************************************************************/

void recombine_pq_data(){
#ifndef HAVE_ENERGY_COLLECT
	uchar temp1;
	if((original_high_byte == 0xff) && (original_middle_byte == 0xff)  && (original_low_byte > 0xc2)){
		original_high_byte = 0;
		original_middle_byte = 0;
		original_low_byte = 0;
	}
#else
	if((original_high_byte == 0xff) && (original_middle_byte == 0xff)  && (original_low_byte > VAL_TO_ZERO_NEGATIVE_PQ)){
		original_high_byte = 0;
		original_middle_byte = 0;
		original_low_byte = 0;
	}
#endif
	
#ifdef MAX_PAR_300_00V_5A
	new_recombine_high = ((original_high_byte&0x0f) << 4);
	temp1 = ((original_middle_byte&0xf0) >> 4);
	new_recombine_high |= temp1;

	new_recombine_low = ((original_middle_byte & 0x0f) << 4);
	temp1 = ((original_low_byte & 0xf0) >> 4);
	new_recombine_low |= temp1;	
#endif

#ifdef MAX_PAR_57_74V_5A
	new_recombine_high = ((original_high_byte&0x0f) << 4);
	temp1 = ((original_middle_byte&0xf0) >> 4);
	new_recombine_high |= temp1;

	new_recombine_low = ((original_middle_byte & 0x0f) << 4);
	temp1 = ((original_low_byte & 0xf0) >> 4);
	new_recombine_low |= temp1;	
#endif
 	  
#ifdef MAX_PAR_57_74V_1A

	new_recombine_high = ((original_high_byte & 0x1f) << 3);
	temp1 = ((original_middle_byte & 0xe0) >> 5);
	new_recombine_high |= temp1;
	
	new_recombine_low = ((original_middle_byte & 0x1f) << 3);
	temp1 = ((original_low_byte & 0xe0) >> 5);
	new_recombine_low |= temp1;	
#endif

#ifdef MAX_PAR_300_00V_1A

	new_recombine_high = ((original_high_byte & 0x1f) << 3);
	temp1 = ((original_middle_byte & 0xe0) >> 5);
	new_recombine_high |= temp1;
	
	new_recombine_low = ((original_middle_byte & 0x1f) << 3);
	temp1 = ((original_low_byte & 0xe0) >> 5);
	new_recombine_low |= temp1;	
#endif


/*¥¥¥¥¥¥¥¥¥¥¥¥¥¥加电能¥¥¥¥¥¥¥¥¥¥¥*/

#ifdef MAX_PAR_57_74V_5A_ENERGY //不移位
	new_recombine_high = original_high_byte;
	new_recombine_middle = original_middle_byte;
	new_recombine_low = original_low_byte;
#endif

#ifdef MAX_PAR_57_74V_1A_ENERGY //舍弃前6后2位
	new_recombine_high = ((original_high_byte & 0x03) << 6);
	temp1 = ((original_middle_byte & 0xfc) >> 2);
	new_recombine_high |= temp1;

	new_recombine_low = ((original_middle_byte & 0x03) << 6);
	temp1 = ((original_low_byte & 0xfc) >> 2);
	new_recombine_low |= temp1;	
#endif

#ifdef MAX_PAR_300_00V_5A_ENERGY //不移位
	new_recombine_high = original_high_byte;
	new_recombine_middle = original_middle_byte;
	new_recombine_low = original_low_byte;	
#endif

#ifdef MAX_PAR_300_00V_1A_ENERGY //舍弃前6后2位
	new_recombine_high = ((original_high_byte & 0x03) << 6);
	temp1 = ((original_middle_byte & 0xfc) >> 2);
	new_recombine_high |= temp1;

	new_recombine_low = ((original_middle_byte & 0x03) << 6);
	temp1 = ((original_low_byte & 0xfc) >> 2);
	new_recombine_low |= temp1;	
#endif

}
/*
***********************************************************************************************************
**函数原型		:  	init_7022B()
**参数说明		:  
				:	
					
**返回值		:		
**说	明		:	本函数用于调整7022B的数据精度,功率采用2次校正
************************************************************************************************************/
void init_7022B()
{
	clear_7022B_data();//清校表数据
	
	spi_delay(200);
	spi_delay(200);
	spi_delay(200);
	spi_delay(200);
	spi_delay(200);

	SPI0CFG = 0x60;
	SPI0CN = 1;
	SPI0CKR = 0xff;//把SPI波特率降到最低
	SPI_CS_HIGH_LEVEL();  //CS下降沿表示spi传输开始
	spi_delay(200);//change by zhugaoyun08-07-24 spi_delay(150)
	
	/*
	
  CF,加电能,顺序上必须先设置合相电能,高频输出参数,再做功率增益校正,再相位校正
	CF口发一个电能脉冲,相应的电能寄存器数值加1,
	实际用电度数 = 能量寄存器的值 / 脉冲常数
	写高频脉冲输出参数到20H(命令为0A0H)后,新片就按设计脉冲常数发出CF信号,
	
	要求0xd00>Hfreq>0x04	
	*/
		
#ifdef MAX_PAR_57_74V_5A_ENERGY	
	SPI_CS_LOW_LEVEL();
	SPI0DAT = (w_HFConst | 0x80);
	revise_data(REVISE_HFREQ_HIGH,REVISE_HFREQ_MIDDLE, REVISE_HFREQ_LOW);			 
#endif

#ifdef MAX_PAR_57_74V_1A_ENERGY	
	SPI_CS_LOW_LEVEL();
	SPI0DAT = (w_HFConst | 0x80);
	revise_data(REVISE_HFREQ_HIGH,REVISE_HFREQ_MIDDLE, REVISE_HFREQ_LOW);					 
 #endif

 #ifdef MAX_PAR_300_00V_5A_ENERGY	
	SPI_CS_LOW_LEVEL();
	SPI0DAT = (w_HFConst | 0x80);
	revise_data(REVISE_HFREQ_HIGH,REVISE_HFREQ_MIDDLE, REVISE_HFREQ_LOW);					 
 #endif

 #ifdef MAX_PAR_300_00V_1A_ENERGY	
	SPI_CS_LOW_LEVEL();
	SPI0DAT = (w_HFConst | 0x80);
	revise_data(REVISE_HFREQ_HIGH,REVISE_HFREQ_MIDDLE, REVISE_HFREQ_LOW);					 
 #endif
 /************************add by zhugaoyun****************************/
  SPI_CS_LOW_LEVEL();//正反向电能
	SPI0DAT = (w_EAddMode | 0x80);
	revise_data(REVISE_EN_HIGH,REVISE_EN_MIDDLE,REVISE_EN_LOW);
 /************************end****************************/
 
	SPI_CS_LOW_LEVEL();//Pa,twice revise
	SPI0DAT = (w_PgainA0 | 0x80);
	revise_data(REVISE_PA_HIGH,REVISE_PA_MIDDLE,REVISE_PA_LOW);	

	SPI_CS_LOW_LEVEL();
	SPI0DAT = (w_PgainA1 | 0x80);
	revise_data(REVISE_PA_HIGH,REVISE_PA_MIDDLE,REVISE_PA_LOW);	

	SPI_CS_LOW_LEVEL();//Pb
	SPI0DAT = (w_PgainB0 | 0x80);
	revise_data(REVISE_PB_HIGH,REVISE_PB_MIDDLE,REVISE_PB_LOW);
	SPI_CS_LOW_LEVEL();
	SPI0DAT = (w_PgainB1 | 0x80);
	revise_data(REVISE_PB_HIGH,REVISE_PB_MIDDLE,REVISE_PB_LOW);

	SPI_CS_LOW_LEVEL();//Pc
	SPI0DAT = (w_PgainC0 | 0x80);
	revise_data(REVISE_PC_HIGH,REVISE_PC_MIDDLE,REVISE_PC_LOW);	
	SPI_CS_LOW_LEVEL();
	SPI0DAT = (w_PgainC1 | 0x80);
	revise_data(REVISE_PC_HIGH,REVISE_PC_MIDDLE,REVISE_PC_LOW);	
 	
	SPI_CS_LOW_LEVEL();//Ua
	SPI0DAT = (w_UgainA | 0x80);
	revise_data(REVISE_UA_HIGH,REVISE_UA_MIDDLE,REVISE_UA_LOW);	

	SPI_CS_LOW_LEVEL();//Ub
	SPI0DAT = (w_UgainB | 0x80);
	revise_data(REVISE_UB_HIGH,REVISE_UB_MIDDLE,REVISE_UB_LOW);	

	SPI_CS_LOW_LEVEL();//Uc
	SPI0DAT = (w_UgainC | 0x80);
	revise_data(REVISE_UC_HIGH,REVISE_UC_MIDDLE,REVISE_UC_LOW);	

	SPI_CS_LOW_LEVEL();//Ia
	SPI0DAT = (w_IgainA | 0x80);//Ia,写入校表积存器地址,最高位置1
	revise_data(REVISE_IA_HIGH, REVISE_IA_MIDDLE, REVISE_IA_LOW);
	
	SPI_CS_LOW_LEVEL();//Ib
	SPI0DAT = (w_IgainB | 0x80);
	revise_data(REVISE_IB_HIGH,REVISE_IB_MIDDLE,REVISE_IB_LOW);

	SPI_CS_LOW_LEVEL();//Ic
	SPI0DAT = (w_IgainC | 0x80);
	revise_data(REVISE_IC_HIGH,REVISE_IC_MIDDLE,REVISE_IC_LOW);	

 

}

/*
***********************************************************************************************************
**函数原型		:  	get_average_data()
**参数说明		:  
				:	
					
**返回值		:		
**说	明		:	本函数用于数字滤波,对采集的数据进行加权平均
************************************************************************************************************/
/*
void get_average_data(){//filter wave,U,I
	 uchar i1 = 0,j1 = 0;
	 uint power_data;
	 int tmp_data = 0;
	 unsigned long power_data1,power_data2;
	 for(i1 = 0;i1 < 5;i1++){ 
		for(j1 = 5;j1 > i1;j1--){
			if(average[j1] <= average[j1-1]){
				tmp_data = average[j1];
				average[j1] = average[j1-1];
				average[j1-1] = tmp_data;
			}
		}
	}
	power_data1 = average[0]+average[4];
	power_data2 = average[1]+average[3];
	power_data = power_data1 * 0.1 + power_data2 * 0.2 + average[2] * 0.4;
	average[2] = power_data;
}	  */


/*
***********************************************************************************************************
**函数原型		:  	get_five_middle_data()
**参数说明		:  
				:	
					
**返回值		:		
**说	明		:	本函数用于数字滤波,对采集的数据进行排序去中值
************************************************************************************************************/

int get_five_middle_data(int t_data[5]){
	 uchar i2 = 0,j2 = 0;
	 int tmp_data = 0;
	 for(i2 = 0;i2 < 5;i2++){ 
		for(j2 = 5;j2 > i2;j2--){
			if(t_data[j2] <= t_data[j2 - 1]){
				tmp_data = t_data[j2];
				t_data[j2] = t_data[j2 - 1];
				t_data[j2 - 1] = tmp_data;

⌨️ 快捷键说明

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