/*
 * ad_sample.c
 *

 */
#include "IQmathLib.h"         // Include header for IQmath library
#include "DSP281x_Device.h"     // DSP281x Headerfile Include File
#include "math.h"
int16   ad_run_flag,   ad_zero_flag;
float32   ad_ref_u_last,ad_ref_uu_last,ad_ref_v_last,ad_ref_vv_last;
float32   ad_ref_u_now,ad_ref_uu_now,ad_ref_v_now,ad_ref_vv_now;
Uint16   ad_ref_u_final,ad_ref_uu_final,ad_ref_v_final,ad_ref_vv_final;
float32  ad_filter_para;
Uint16   ad_data_u,ad_data_uu,ad_data_v,ad_data_vv,ad_data_resv1,ad_data_resv2,ad_data_resv3,ad_data_vdc;
float32  I_f_u,I_f_v;///AD输出U相V相电流
Uint16   U_VDC;////
float32  P_i_resolution,P_i_resolution_small;///电流分辨率
float32  P_vdc_resolution;///总线电压分辨率
void AD_test(void);
void read_addata(void);
void read_addata2(void);
void AD_adjust_cal(void);///ad 补偿系数计算
void I_adsample_cal(void);///电流满（大）量程计算
void AD_init(void);
float32 P_i_u_adjust,P_i_v_adjust;//////电流校正系数
Uint16  Flag_i_u_adjust,Flag_i_v_adjust;//////ad校正标识
float32 AD_adjust_cal_u,AD_adjust_cal_v;
Uint16  AD_adjcal_startflag,AD_adjcal_endflag;
Uint16 addata0,addata1,addata2,addata3,addata4,addata5,addata6,addata7,addata8,addata9,addata10,addata11,addata12,addata13,addata14,addata15;
Uint16 addata20,addata21,addata22,addata23,addata24,addata25,addata26,addata27;
_iq19 iq_u_now,iq_u_last,iq_uu_now,iq_uu_last,iq_v_now,iq_v_last,iq_vv_now,iq_vv_last;
_iq19 iq_last_p,iq_new_p;
_iq19 iq_I_f_u,iq_I_f_v,iq_P_i_resolution,iq_P_i_resolution_small,iq_P_i_u_adjust,iq_P_i_v_adjust;
_iq19 iq_minus_one;//负1
_iq19 iq_U_VDC,iq_P_vdc_resolution;
_iq19 iq_u_nnn,iq_u_lll;
int32 P_inttoiq;/////整形直接变为浮点的IQ值系数
Uint16 upper_limit,lower_limit;//////大小通道切换边界
float32  ad_I_range =50;

//void AD_init(void)
//{
//	ad_run_flag = 0;	 ad_zero_flag = 0;
//		 ad_data_u = 0; ad_data_uu = 0; ad_data_v = 0; ad_data_vv = 0; ad_data_resv1 = 0; ad_data_resv2 = 0; ad_data_resv3 = 0; ad_data_vdc = 0;
//		 ad_ref_u_last = 0;  ad_ref_uu_last = 0;  ad_ref_v_last = 0;  ad_ref_vv_last = 0;
//		 ad_ref_u_now = 0;   ad_ref_uu_now = 0;   ad_ref_v_now = 0;   ad_ref_vv_now = 0;
//		 ad_ref_u_final = 2048; ad_ref_uu_final = 2048; ad_ref_v_final = 2048; ad_ref_vv_final = 2048;
//		 ad_filter_para = 0.01;
//
//		 addata0 = 0;  addata1 = 0;  addata2 = 0;  addata3 = 0;  addata4 = 0;
//		 addata5 = 0;  addata6 = 0;  addata7 = 0;  addata8 = 0;  addata9 = 0;
//		 addata10 = 0;  addata11 = 0;  addata12 = 0;  addata13 = 0;  addata14 = 0;  addata15 = 0;
//		 iq_u_now = _IQ19(0);   iq_u_last = _IQ19(0);
//		 iq_uu_now = _IQ19(0);   iq_uu_last = _IQ19(0);
//		 iq_v_now = _IQ19(0);   iq_v_last = _IQ19(0);
//		 iq_vv_now = _IQ19(0);   iq_vv_last = _IQ19(0);
//		 iq_last_p = _IQ19(0.99);  iq_new_p = _IQ19(0.01);
//		 P_i_u_adjust = 4.312;  P_i_v_adjust = 4.3648;
//		 Flag_i_u_adjust = 0;     Flag_i_v_adjust = 0;
//	//	 P_i_resolution = I_range/2048.0/4.0;///电流小量程
//		 P_i_resolution = ad_I_range/2048.0;///电流大量程
//		 P_i_resolution_small = ad_I_range/2048.0/P_i_u_adjust;///电流小量程
//
//		 I_f_u = 0;               I_f_v = 0;
//		 iq_P_i_u_adjust = _IQ19(P_i_u_adjust);
//		 iq_P_i_v_adjust = _IQ19(P_i_v_adjust);
//		 iq_P_i_resolution = _IQ19(P_i_resolution);
//		 iq_P_i_resolution_small = _IQ19(P_i_resolution_small);
//		 iq_minus_one = _IQ19(-1);
//
//		 P_vdc_resolution = 0.5449;//0.6526;////1000.0/4095.0;山西系数0.34434;//
//	//	 P_vdc_resolution = 0.2387;//1406.0/4095.0;/////北京系数
//		 iq_P_vdc_resolution  = _IQ19(P_vdc_resolution);
//		 P_inttoiq = pow(2,19);
//		 upper_limit = 3000;	 lower_limit = 1200;
//		 AD_adjcal_startflag = 0;  AD_adjcal_endflag = 0;
//}
void AD_test()
{
	while(AdcRegs.ADCST.bit.INT_SEQ1 == 0)///等待ad转换完成
		;
	read_addata();
	if(ad_run_flag == 0)
	{
		InitAdc_2nd();///init 2nd ad channel
		AdcRegs.ADCST.bit.INT_SEQ1_CLR = 1;       // Clear INT SEQ1 bit
		while(AdcRegs.ADCST.bit.INT_SEQ1 == 1)
			;
		AdcRegs.ADCTRL2.bit.RST_SEQ1 = 1;////reset seq1
		AdcRegs.ADCTRL2.bit.SOC_SEQ1 = 1;///软件中断启动AD转换
		//二次AD转换期间处理一次AD的数据
		ad_data_u = (addata0 + addata1 + addata2 + addata3)>>2;
		ad_data_v = (addata4 + addata5 + addata6 + addata7)>>2;
		ad_data_vv = (addata8 + addata9 + addata10 + addata11)>>2;
		ad_data_uu = (addata12 + addata13 + addata14 + addata15)>>2;

		iq_u_now =_IQ19mpy(iq_new_p,ad_data_u*P_inttoiq) + _IQ19mpy(iq_last_p,iq_u_last);
		iq_u_last = iq_u_now;
		ad_ref_u_now = _IQ19toF(iq_u_now);
		iq_uu_now =_IQ19mpy(iq_new_p,ad_data_uu*P_inttoiq) + _IQ19mpy(iq_last_p,iq_uu_last);
		iq_uu_last = iq_uu_now;
		ad_ref_uu_now = _IQ19toF(iq_uu_now);
		iq_v_now =_IQ19mpy(iq_new_p,ad_data_v*P_inttoiq) + _IQ19mpy(iq_last_p,iq_v_last);
		iq_v_last = iq_v_now;
		ad_ref_v_now = _IQ19toF(iq_v_now);
		iq_vv_now =_IQ19mpy(iq_new_p,ad_data_vv*P_inttoiq) + _IQ19mpy(iq_last_p,iq_vv_last);
		iq_vv_last = iq_vv_now;
		ad_ref_vv_now = _IQ19toF(iq_vv_now);

		ad_zero_flag = 1;
	}
	else
	{
		//////二次启动AD转换
		InitAdc_2nd();///init 2nd ad channel
		AdcRegs.ADCST.bit.INT_SEQ1_CLR = 1;       // Clear INT SEQ1 bit
		while(AdcRegs.ADCST.bit.INT_SEQ1 == 1)
			;
		AdcRegs.ADCTRL2.bit.RST_SEQ1 = 1;////reset seq1
		AdcRegs.ADCTRL2.bit.SOC_SEQ1 = 1;///软件中断启动AD转换
		//二次AD转换期间处理一次AD的数据
		ad_data_u = (addata0 + addata1 + addata2 + addata3)>>2;
		ad_data_v = (addata4 + addata5 + addata6 + addata7)>>2;
		ad_data_vv = (addata8 + addata9 + addata10 + addata11)>>2;
		ad_data_uu = (addata12 + addata13 + addata14 + addata15)>>2;
		if(ad_zero_flag)
		{
			ad_ref_u_final = ad_ref_u_now;
			ad_ref_v_final = ad_ref_v_now;
			ad_ref_uu_final = ad_ref_uu_now;
			ad_ref_vv_final = ad_ref_vv_now;
			ad_zero_flag = 0;
		}
	}
	I_adsample_cal();///大小通道切换
	while(AdcRegs.ADCST.bit.INT_SEQ1 == 0)///等待二次ad转换完成
		;
	read_addata2();//读取二次AD数据
	ad_data_vdc = (addata20 + addata21 + addata22 + addata23)>>2;
	ad_data_resv1 = (addata24 + addata25 + addata26 + addata27)>>2;
	iq_U_VDC = _IQ19mpy(iq_P_vdc_resolution,ad_data_vdc<<3);///_IQ16toIQ19
	U_VDC = iq_U_VDC>>3;///_IQ19toIQ16
	InitAdc_1st();///AD第一次转换初始化
}
void I_adsample_cal()
{
	if(ad_data_uu<=lower_limit || ad_data_uu>=upper_limit)///u超出小量程百分之七十  大量程
	{
		if(ad_data_u<=ad_ref_u_final)
			iq_I_f_u = _IQ19mpy((ad_ref_u_final- ad_data_u)*P_inttoiq,iq_P_i_resolution);
		else
			iq_I_f_u = _IQ19mpy(_IQ19mpy(((ad_data_u - ad_ref_u_final)*P_inttoiq),iq_P_i_resolution),iq_minus_one);
	}
	else/////小量程
	{
		if(ad_data_uu<=ad_ref_uu_final)
			iq_I_f_u = _IQ19mpy(((ad_ref_uu_final- ad_data_uu)*P_inttoiq),iq_P_i_resolution_small);
		else
			iq_I_f_u = _IQ19mpy(_IQ19mpy(((ad_data_uu - ad_ref_uu_final)*P_inttoiq),iq_P_i_resolution_small),iq_minus_one);
	}
	if(ad_data_vv<=lower_limit || ad_data_vv>=upper_limit)//v超出小量程百分之七十  大量程
	{
		if(ad_data_v<=ad_ref_v_final)
			iq_I_f_v = _IQ19mpy(((ad_ref_v_final- ad_data_v)*P_inttoiq),iq_P_i_resolution);
		else
			iq_I_f_v = _IQ19mpy(_IQ19mpy(((ad_data_v - ad_ref_v_final)*P_inttoiq),iq_P_i_resolution),iq_minus_one);
	}
	else////v小量程
	{
		if(ad_data_vv<=ad_ref_vv_final)
			iq_I_f_v = _IQ19mpy(((ad_ref_vv_final- ad_data_vv)*P_inttoiq),iq_P_i_resolution_small);
		else
			iq_I_f_v = _IQ19mpy(_IQ19mpy(((ad_data_vv - ad_ref_vv_final)*P_inttoiq),iq_P_i_resolution_small),iq_minus_one);
	}
	I_f_u = _IQ19toF(iq_I_f_u);
	I_f_v = _IQ19toF(iq_I_f_v);
}
void AD_adjust_cal()
{
	AD_adjust_cal_u = _IQ19toF(_IQ19div((ad_ref_uu_final - ad_data_uu)*P_inttoiq,(ad_ref_u_final - ad_data_u)*P_inttoiq));
	AD_adjust_cal_v = _IQ19toF(_IQ19div((ad_ref_vv_final - ad_data_vv)*P_inttoiq,(ad_ref_v_final - ad_data_v)*P_inttoiq));
}
void read_addata(void)
{
//	addata0 =   AdcRegs.ADCRESULT0 &0xfff0;
//	addata1 =   AdcRegs.ADCRESULT1 &0xfff0;
//	addata2 =   AdcRegs.ADCRESULT2 &0xfff0;
//	addata3 =   AdcRegs.ADCRESULT3 &0xfff0;
//	addata4 =   AdcRegs.ADCRESULT4 &0xfff0;
//	addata5 =   AdcRegs.ADCRESULT5 &0xfff0;
//	addata6 =   AdcRegs.ADCRESULT6 &0xfff0;
//	addata7 =   AdcRegs.ADCRESULT7 &0xfff0;
//	addata8 =   AdcRegs.ADCRESULT8 &0xfff0;
//	addata9 =   AdcRegs.ADCRESULT9 &0xfff0;
//	addata10 = AdcRegs.ADCRESULT10 &0xfff0;
//	addata11 = AdcRegs.ADCRESULT11 &0xfff0;
//	addata12 = AdcRegs.ADCRESULT12 &0xfff0;
//	addata13 = AdcRegs.ADCRESULT13 &0xfff0;
//	addata14 = AdcRegs.ADCRESULT14 &0xfff0;
//	addata15 = AdcRegs.ADCRESULT15 &0xfff0;
	addata0 =   AdcRegs.ADCRESULT0 >>4;
	addata1 =   AdcRegs.ADCRESULT1 >>4;
	addata2 =   AdcRegs.ADCRESULT2 >>4;
	addata3 =   AdcRegs.ADCRESULT3 >>4;
	addata4 =   AdcRegs.ADCRESULT4 >>4;
	addata5 =   AdcRegs.ADCRESULT5 >>4;
	addata6 =   AdcRegs.ADCRESULT6 >>4;
	addata7 =   AdcRegs.ADCRESULT7 >>4;
	addata8 =   AdcRegs.ADCRESULT8 >>4;
	addata9 =   AdcRegs.ADCRESULT9 >>4;
	addata10 = AdcRegs.ADCRESULT10 >>4;
	addata11 = AdcRegs.ADCRESULT11 >>4;
	addata12 = AdcRegs.ADCRESULT12 >>4;
	addata13 = AdcRegs.ADCRESULT13 >>4;
	addata14 = AdcRegs.ADCRESULT14 >>4;
	addata15 = AdcRegs.ADCRESULT15 >>4;
}
void read_addata2(void)
{
	addata20 =   AdcRegs.ADCRESULT0 >>4;
	addata21 =   AdcRegs.ADCRESULT1 >>4;
	addata22 =   AdcRegs.ADCRESULT2 >>4;
	addata23 =   AdcRegs.ADCRESULT3 >>4;
	addata24 =   AdcRegs.ADCRESULT4 >>4;
	addata25 =   AdcRegs.ADCRESULT5 >>4;
	addata26 =   AdcRegs.ADCRESULT6 >>4;
	addata27 =   AdcRegs.ADCRESULT7 >>4;
//	addata8 =   AdcRegs.ADCRESULT8 >>4;
//	addata9 =   AdcRegs.ADCRESULT9 >>4;
//	addata10 = AdcRegs.ADCRESULT10 >>4;
//	addata11 = AdcRegs.ADCRESULT11 >>4;
}
