// #include "project.h"
#include "adc_tools.h"
// #include "xp_project.h"
#include "IQmathLib.h"
#include "math.h"
#include "filter_v1.h"
#include "params.h"
// #include "params_protect.h"
#include "vector.h"
// #include "xp_adc.h"
// #include "TuneUpPlane.h" //Ìîðãàíèå ñâåòîäèîäîì
// #include "log_to_memory.h"
// #include "errors.h"

// #define COUNT_ARR_ADC_BUF 2
#define Shift_Filter 1  //2

#define BTR_ENABLED

#define R_ADC_DEFAULT     { 1180 ,1180 , 256,  256, 256, 256, 256, 1180, 1180, 256, 256, 256, 256, 256, 256, 256, 256, 256 }
#define  K_LEM_ADC_DEFAULT { 60000,60000,5000, 5000,5000,5000,5000,60000,60000,5000,5000,5000,5000,5000,5000,5000,5000,5000 }
//#define LOG_ACP_TO_BUF



#if (USE_INTERNAL_ADC==1)

#if(C_adc_number==1)
unsigned int const R_ADC[COUNT_ARR_ADC_BUF][16] = { R_ADC_DEFAULT_0,R_ADC_DEFAULT_INTERNAL };
unsigned int const K_LEM_ADC[COUNT_ARR_ADC_BUF][16] = { K_LEM_ADC_DEFAULT_0, K_LEM_ADC_DEFAULT_INTERNAL };
float const K_NORMA_ADC[COUNT_ARR_ADC_BUF][16] = { NORMA_ADC_DEFAULT_0, NORMA_ADC_DEFAULT_INTERNAL };
#endif
#if(C_adc_number==2)
unsigned int const R_ADC[COUNT_ARR_ADC_BUF][16] = { R_ADC_DEFAULT_0, R_ADC_DEFAULT_1,R_ADC_DEFAULT_INTERNAL };
unsigned int const K_LEM_ADC[COUNT_ARR_ADC_BUF][16] = { K_LEM_ADC_DEFAULT_0, K_LEM_ADC_DEFAULT_1, K_LEM_ADC_DEFAULT_INTERNAL };
float const K_NORMA_ADC[COUNT_ARR_ADC_BUF][16] = { NORMA_ADC_DEFAULT_0, NORMA_ADC_DEFAULT_1, NORMA_ADC_DEFAULT_INTERNAL };
#endif
#if(C_adc_number==3)
unsigned int const R_ADC[COUNT_ARR_ADC_BUF][16] = { R_ADC_DEFAULT_0, R_ADC_DEFAULT_1, R_ADC_DEFAULT_2,R_ADC_DEFAULT_INTERNAL };
unsigned int const K_LEM_ADC[COUNT_ARR_ADC_BUF][16] = { K_LEM_ADC_DEFAULT_0, K_LEM_ADC_DEFAULT_1, K_LEM_ADC_DEFAULT_2, K_LEM_ADC_DEFAULT_INTERNAL };
float const K_NORMA_ADC[COUNT_ARR_ADC_BUF][16] = { NORMA_ADC_DEFAULT_0, NORMA_ADC_DEFAULT_1, NORMA_ADC_DEFAULT_2, NORMA_ADC_DEFAULT_INTERNAL };
#endif

#else

#if(C_adc_number==1)
#pragma DATA_SECTION(R_ADC,".slow_vars")
unsigned int R_ADC[COUNT_ARR_ADC_BUF][16] = { R_ADC_DEFAULT_0 };
#pragma DATA_SECTION(K_LEM_ADC,".slow_vars")
unsigned int K_LEM_ADC[COUNT_ARR_ADC_BUF][16] = { K_LEM_ADC_DEFAULT_0 };
#pragma DATA_SECTION(K_NORMA_ADC,".slow_vars")
float K_NORMA_ADC[COUNT_ARR_ADC_BUF][16] = { NORMA_ADC_DEFAULT_0 };
#endif
#if(C_adc_number==2)
#pragma DATA_SECTION(R_ADC,".slow_vars")
unsigned int const R_ADC[COUNT_ARR_ADC_BUF][16] = { R_ADC_DEFAULT_0, R_ADC_DEFAULT_1 };
#pragma DATA_SECTION(K_LEM_ADC,".slow_vars")
unsigned int const K_LEM_ADC[COUNT_ARR_ADC_BUF][16] = { K_LEM_ADC_DEFAULT_0, K_LEM_ADC_DEFAULT_1 };
#pragma DATA_SECTION(K_NORMA_ADC,".slow_vars")
float K_NORMA_ADC[COUNT_ARR_ADC_BUF][16] = { NORMA_ADC_DEFAULT_0, NORMA_ADC_DEFAULT_1 };
#endif
#if(C_adc_number==3)
#pragma DATA_SECTION(R_ADC,".slow_vars")
unsigned int R_ADC[COUNT_ARR_ADC_BUF][16] = { R_ADC_DEFAULT_0, R_ADC_DEFAULT_1, R_ADC_DEFAULT_2 };
#pragma DATA_SECTION(K_LEM_ADC,".slow_vars")
unsigned int K_LEM_ADC[COUNT_ARR_ADC_BUF][16] = { K_LEM_ADC_DEFAULT_0, K_LEM_ADC_DEFAULT_1, K_LEM_ADC_DEFAULT_2 };
#pragma DATA_SECTION(K_NORMA_ADC,".slow_vars")
float K_NORMA_ADC[COUNT_ARR_ADC_BUF][16] = { NORMA_ADC_DEFAULT_0, NORMA_ADC_DEFAULT_1, NORMA_ADC_DEFAULT_2 };
#endif

#endif

#pragma DATA_SECTION(ADC_f,".fast_vars");
int ADC_f[COUNT_ARR_ADC_BUF][16];

#pragma DATA_SECTION(ADC_fast,".fast_vars");
int ADC_fast[COUNT_ARR_ADC_BUF][16][COUNT_ARR_ADC_BUF_FAST_POINT];


#pragma DATA_SECTION(ADC_sf,".fast_vars");
int ADC_sf[COUNT_ARR_ADC_BUF][16];

#pragma DATA_SECTION(analog,".fast_vars");
ANALOG_VALUE	analog = ANALOG_VALUE_DEFAULT;

#pragma DATA_SECTION(filter,".fast_vars");
ANALOG_VALUE	filter = ANALOG_VALUE_DEFAULT;

#pragma DATA_SECTION(analog_zero,".fast_vars");
ANALOG_VALUE    analog_zero = ANALOG_VALUE_DEFAULT;



unsigned int const level_err_ADC_PLUS[16] = level_err_ADC_PLUS_default;
unsigned int const level_err_ADC_MINUS[16] = level_err_ADC_MINUS_default;


#pragma DATA_SECTION(err_adc_protect,".fast_vars");
#pragma DATA_SECTION(mask_err_adc_protect,".fast_vars");
ERR_ADC_PROTECT err_adc_protect[COUNT_ARR_ADC_BUF], mask_err_adc_protect[COUNT_ARR_ADC_BUF];



_iq koef_Im_filter = 0;
_iq koef_Power_filter = 0;
_iq koef_Power_filter2 = 0;

#pragma DATA_SECTION(k_norm_ADC,".slow_vars")
_iq19 k_norm_ADC[COUNT_ARR_ADC_BUF][16];

#pragma DATA_SECTION(iq19_zero_ADC,".fast_vars");
_iq19 iq19_zero_ADC[COUNT_ARR_ADC_BUF][16];


#pragma DATA_SECTION(zero_ADC,".slow_vars")
int zero_ADC[COUNT_ARR_ADC_BUF][16];


#pragma DATA_SECTION(iq19_k_norm_ADC,".fast_vars");
_iq19 iq19_k_norm_ADC[COUNT_ARR_ADC_BUF][16];

#pragma DATA_SECTION(iq_norm_ADC,".fast_vars");
_iq iq_norm_ADC[COUNT_ARR_ADC_BUF][16];

#pragma DATA_SECTION(iq_norm_ADC_sf,".fast_vars");
_iq iq_norm_ADC_sf[COUNT_ARR_ADC_BUF][16];


#pragma DATA_SECTION(koef_Uzpt_long_filter,".fast_vars");
_iq koef_Uzpt_long_filter = 0;

#pragma DATA_SECTION(koef_Uzpt_fast_filter,".fast_vars");
_iq koef_Uzpt_fast_filter = 0;

#pragma DATA_SECTION(koef_Uin_filter,".fast_vars");
_iq koef_Uin_filter = 0;


ANALOG_RAW_DATA rawData = {0};




void calc_norm_ADC(void);
void analog_values_calc(void);
_iq im_calc( _iq ia, _iq ib, _iq ic);

#define Shift_Filter 1  //2
void read_adc()
{
    static unsigned int adr_adc = 0;
    if(project.adc[0].status == component_Ready)
    {
        project.adc->read_pbus(&project.adc[0]);
        ADC_f[0][0] = project.adc[0].read.pbus.adc_value[0];
        ADC_f[0][1] = project.adc[0].read.pbus.adc_value[1];
        ADC_f[0][2] = project.adc[0].read.pbus.adc_value[2];
        ADC_f[0][3] = project.adc[0].read.pbus.adc_value[3];
        ADC_f[0][4] = project.adc[0].read.pbus.adc_value[4];
        ADC_f[0][5] = project.adc[0].read.pbus.adc_value[5];
        ADC_f[0][6] = project.adc[0].read.pbus.adc_value[6];
        ADC_f[0][7] = project.adc[0].read.pbus.adc_value[7];
        ADC_f[0][8] = project.adc[0].read.pbus.adc_value[8];
        ADC_f[0][9] = project.adc[0].read.pbus.adc_value[9];
        ADC_f[0][10] = project.adc[0].read.pbus.adc_value[10];
        ADC_f[0][11] = project.adc[0].read.pbus.adc_value[11];
        ADC_f[0][12] = project.adc[0].read.pbus.adc_value[12];
        ADC_f[0][13] = project.adc[0].read.pbus.adc_value[13];
        ADC_f[0][14] = project.adc[0].read.pbus.adc_value[14];
        ADC_f[0][15] = project.adc[0].read.pbus.adc_value[15];


        ADC_sf[0][0] += (ADC_f[0][0] - ADC_sf[0][0]) >> Shift_Filter;
        ADC_sf[0][1] += (ADC_f[0][1] - ADC_sf[0][1]) >> Shift_Filter;
        ADC_sf[0][2] += (ADC_f[0][2] - ADC_sf[0][2]) >> Shift_Filter;
        ADC_sf[0][3] += (ADC_f[0][3] - ADC_sf[0][3]) >> Shift_Filter;
        ADC_sf[0][4] += (ADC_f[0][4] - ADC_sf[0][4]) >> Shift_Filter;
        ADC_sf[0][5] += (ADC_f[0][5] - ADC_sf[0][5]) >> Shift_Filter;
        ADC_sf[0][6] += (ADC_f[0][6] - ADC_sf[0][6]) >> Shift_Filter;

        ADC_sf[0][7] += (ADC_f[0][7] - ADC_sf[0][7]) >> Shift_Filter;
        ADC_sf[0][8] += (ADC_f[0][8] - ADC_sf[0][8]) >> Shift_Filter;
        ADC_sf[0][9] += (ADC_f[0][9] - ADC_sf[0][9]) >> Shift_Filter;
        ADC_sf[0][10] += (ADC_f[0][10] - ADC_sf[0][10]) >> Shift_Filter;
        ADC_sf[0][11] += (ADC_f[0][11] - ADC_sf[0][11]) >> Shift_Filter;
        ADC_sf[0][12] += (ADC_f[0][12] - ADC_sf[0][12]) >> Shift_Filter;
        ADC_sf[0][13] += (ADC_f[0][13] - ADC_sf[0][13]) >> Shift_Filter;
        ADC_sf[0][14] += (ADC_f[0][14] - ADC_sf[0][14]) >> Shift_Filter;
        ADC_sf[0][15] += (ADC_f[0][15] - ADC_sf[0][15]) >> Shift_Filter;
    }
    if(project.adc[1].status == component_Ready)
    {
        project.adc->read_pbus(&project.adc[1]);
        //          adr_adc = project.adc[1].adr_pbus.adr_table[0];
        ADC_f[1][0] = project.adc[1].read.pbus.adc_value[0];
        ADC_f[1][1] = project.adc[1].read.pbus.adc_value[1];
        ADC_f[1][2] = project.adc[1].read.pbus.adc_value[2];
        ADC_f[1][3] = project.adc[1].read.pbus.adc_value[3];

        ADC_sf[1][0] += (ADC_f[1][0] - ADC_sf[1][0]) >> Shift_Filter;
        ADC_sf[1][1] += (ADC_f[1][1] - ADC_sf[1][1]) >> Shift_Filter;
        ADC_sf[1][2] += (ADC_f[1][2] - ADC_sf[1][2]) >> Shift_Filter;
        ADC_sf[1][3] += (ADC_f[1][3] - ADC_sf[1][3]) >> Shift_Filter;
        /* Not used
        ADC_sf[1][4] += (ADC_f[1][4] - ADC_sf[1][4]) >> Shift_Filter;
        ADC_sf[1][5] += (ADC_f[1][5] - ADC_sf[1][5]) >> Shift_Filter;
        ADC_sf[1][6] += (ADC_f[1][6] - ADC_sf[1][6]) >> Shift_Filter;
        ADC_sf[1][7] += (ADC_f[1][7] - ADC_sf[1][7]) >> Shift_Filter;
        ADC_sf[1][8] += (ADC_f[1][8] - ADC_sf[1][8]) >> Shift_Filter;
        ADC_sf[1][9] += (ADC_f[1][9] - ADC_sf[1][9]) >> Shift_Filter;
        ADC_sf[1][10] += (ADC_f[1][10] - ADC_sf[1][10]) >> Shift_Filter;
        ADC_sf[1][11] += (ADC_f[1][11] - ADC_sf[1][11]) >> Shift_Filter;
        ADC_sf[1][12] += (ADC_f[1][12] - ADC_sf[1][12]) >> Shift_Filter;
        ADC_sf[1][13] += (ADC_f[1][13] - ADC_sf[1][13]) >> Shift_Filter;
        ADC_sf[1][14] += (ADC_f[1][14] - ADC_sf[1][14]) >> Shift_Filter;
        ADC_sf[1][15] += (ADC_f[1][15] - ADC_sf[1][15]) >> Shift_Filter;
        */
    }
}

void acp_Handler(void)
{
	 //read_adc();
	calc_norm_ADC_0(0);
	//analog_values_calc();
	// Read_Fast_Errors();
}

//#define COUNT_DETECT_ZERO 500



void init_Adc_Variables(void)
{
    unsigned int k, i, j;
    int* panalog, * pfilter;
    volatile float k_f;


    for (i = 0; i < COUNT_ARR_ADC_BUF; i++)
    {
        for (k = 0; k < 16; k++)
        {
            ADC_f[i][k] = 0;
            ADC_sf[i][k] = 0;

            for (j = 0; j < COUNT_ARR_ADC_BUF_FAST_POINT; j++)
                ADC_fast[i][k][j] = 0;

            k_f = K_LEM_ADC[i][k] * 2.5 / R_ADC[i][k] / 4096.0;
            k_norm_ADC[i][k] = _IQ19(k_f);

            k_f = K_LEM_ADC[i][k] * 250.0 / R_ADC[i][k] / K_NORMA_ADC[i][k] / 4096.0;
            iq19_k_norm_ADC[i][k] = _IQ19(k_f);

            zero_ADC[i][k] = DEFAULT_ZERO_ADC;//1835;

            iq19_zero_ADC[i][k] = _IQ19(zero_ADC[i][k]);   //_IQ19(2030);//_IQ19(1770);

        }
    }


    panalog = (int*)&analog;
    pfilter = (int*)&filter;
    for (k = 0; k < sizeof(ANALOG_VALUE) / sizeof(int); k++)
    {

        *(panalog + k) = 0;
        *(pfilter + k) = 0;
    }


    for (i = 0; i < COUNT_ARR_ADC_BUF; i++)
    {
        if (project.adc[i].status >= component_Ready)
            detect_zero_analog(i);
    }

    //	  zero_ADC[1][2] = 2010;//1976; // uab
    //	  zero_ADC[1][3] = 2010;//1989; // ubc
    //	  zero_ADC[1][4] = 2010;//1994; // uca


    zero_ADC[0][0] = zero_ADC[0][2];//2042;//1992;//1835; //uzpt
    zero_ADC[0][1] = zero_ADC[0][2];//2042;//1992;//1835; //uzpt



#if (COUNT_ARR_ADC_BUF>1)
    zero_ADC[1][1] = zero_ADC[1][15];
    zero_ADC[1][2] = zero_ADC[1][15];
    zero_ADC[1][3] = zero_ADC[1][15];
    zero_ADC[1][4] = zero_ADC[1][15];
    zero_ADC[1][5] = zero_ADC[1][15];
    zero_ADC[1][6] = zero_ADC[1][15];
    zero_ADC[1][7] = zero_ADC[1][15];
    zero_ADC[1][8] = zero_ADC[1][15];
    zero_ADC[1][9] = zero_ADC[1][15];
    zero_ADC[1][10] = zero_ADC[1][15];
    zero_ADC[1][11] = zero_ADC[1][15];
    zero_ADC[1][12] = zero_ADC[1][15];
    zero_ADC[1][13] = zero_ADC[1][15];
    zero_ADC[1][14] = zero_ADC[1][15];
#endif


    for (k = 0; k < 16; k++)
    {
        for (i = 0; i < COUNT_ARR_ADC_BUF; i++)
        {
            if ((zero_ADC[i][k] > 2200) || (zero_ADC[i][k] < 1900))
                zero_ADC[i][k] = DEFAULT_ZERO_ADC;
        }
    }



    for (k = 0; k < 16; k++)
    {
        for (i = 0; i < COUNT_ARR_ADC_BUF; i++)
        {
            iq19_zero_ADC[i][k] = _IQ19(zero_ADC[i][k]);//_IQ19(1770);
        }
    }


    //	koef_allADC_filter = _IQ19(0.00002/0.00003185);//koef_ADC_filter[0];
    //	koef_zero_ADC_filter=_IQ19(0.00002/0.0003185);
    koef_Uzpt_long_filter = _IQ(0.001 / 0.016666);
    koef_Uzpt_fast_filter = _IQ(0.001 / 0.002); //_IQ(0.001/0.00131);
    koef_Uin_filter = _IQ(0.001 / 0.00931);

    //	koef_Im_filter = _IQ(0.001/0.006);
    koef_Im_filter = _IQ(0.001 / 0.065);
    koef_Power_filter = _IQ(0.001 / 0.065);
    koef_Power_filter2 = _IQ(0.001 / 0.2);

    //	koef_Iabc_filter = _IQ(0.001/0.006);


    filter.iqU_1_fast = 0;
    filter.iqU_1_long = 0;

    filter.iqU_2_fast = 0;
    filter.iqU_2_long = 0;

    filter.iqUin_m1 = 0;
    filter.iqUin_m2 = 0;


    //    filter.iqU_3_fast = 0;
    //    filter.iqU_4_fast = 0;

    //	filter.iqU_1_long = 0;
    //	filter.iqU_2_long = 0;
    //	filter.iqU_3_long = 0;
    //	filter.iqU_4_long = 0;

    //	filter.iqIin_1 = 0;
    //	filter.iqIin_2 = 0;


    filter.iqIm_1 = 0;
    filter.iqIm_2 = 0;
    //	filter.iqUin_m1 = 0;
    //	filter.iqUin_m2 = 0;


    for (i = 0; i < COUNT_ARR_ADC_BUF; i++)
    {
        mask_err_adc_protect[i].plus.all = 0;
        mask_err_adc_protect[i].minus.all = 0x0;

        err_adc_protect[i].plus.all = 0;
        err_adc_protect[i].minus.all = 0x0;
    }

#if (USE_INTERNAL_ADC==1)
    Init_Internal_Adc();
#endif


}

inline _iq norma_adc(int plane, int chan, int n_norm)
{
	// return _IQ19toIQ(_IQ19mpy((iq19_zero_ADC[n_norm] - ((long)ADC_sf[plane][chan]<<19) ),iq19_k_norm_ADC[n_norm]));
	// return _IQ19toIQ(_IQ19mpy((((long)ADC_sf[plane][chan]<<19) - iq19_zero_ADC[n_norm]),iq19_k_norm_ADC[n_norm]));
	return _IQ19toIQ(_IQ19mpy((((long)ADC_sf[plane][chan]<<19)),iq19_k_norm_ADC[n_norm]));
}
////////////////////////////////////////////////////////////////////

#pragma CODE_SECTION(norma_adc_nc,".fast_run");
#define SDVIG_K_FILTER_S 2 //1//(27.08.2009) //3
void norma_adc_nc(int nc)
{
	int k;
	//  int bb;

	for (k = 0; k < 16; k++)
	{

		ADC_f[nc][k] = project.adc[nc].read.pbus.adc_value[k];
		ADC_sf[nc][k] += (((int)(ADC_f[nc][k] - ADC_sf[nc][k])) >> SDVIG_K_FILTER_S);
		iq_norm_ADC[nc][k] = _IQ19toIQ(_IQ19mpy((-iq19_zero_ADC[nc][k] + ((long)ADC_f[nc][k] << 19)), iq19_k_norm_ADC[nc][k]));
	}
}

////////////////////////////////////////////////////////////////////
#pragma CODE_SECTION(calc_norm_ADC_0,".fast_run");
void calc_norm_ADC_0(int run_norma)
{
	_iq a1, a2, a3;

#if (USE_ADC_0)

	if (run_norma)
		norma_adc_nc(0);

#if (_FLOOR6)
	analog.iqU_1 = iq_norm_ADC[0][0] - analog_zero.iqU_1 + analog.iqU_1_imit;
	analog.iqU_2 = iq_norm_ADC[0][1] - analog_zero.iqU_2 + analog.iqU_1_imit;
#else
	analog.iqU_1 = iq_norm_ADC[0][0] - analog_zero.iqU_1;
	analog.iqU_2 = iq_norm_ADC[0][1] - analog_zero.iqU_2;
#endif
	analog.iqIu_1 = iq_norm_ADC[0][2];
	analog.iqIv_1 = iq_norm_ADC[0][3];
	analog.iqIw_1 = iq_norm_ADC[0][4];

	analog.iqIu_2 = iq_norm_ADC[0][5];
	analog.iqIv_2 = iq_norm_ADC[0][6];
	analog.iqIw_2 = iq_norm_ADC[0][7];

	analog.iqIin_1 = -iq_norm_ADC[0][9]; // датчик перевернут
	analog.iqIin_2 = -iq_norm_ADC[0][9]; // датчик перевернут

	analog.iqUin_A1B1 = iq_norm_ADC[0][10];

	// два варианта подключения датчиков 23550.1 более правильный - по схеме
	// 23550.1

	analog.iqUin_B1C1 = iq_norm_ADC[0][11]; // 23550.1
	analog.iqUin_A2B2 = iq_norm_ADC[0][12]; // 23550.1

// 23550.3 bs1 bs2

//    analog.iqUin_B1C1 = iq_norm_ADC[0][12]; // 23550.3
//    analog.iqUin_A2B2 = iq_norm_ADC[0][11]; // 23550.3
//
	analog.iqUin_B2C2 = iq_norm_ADC[0][13];

	analog.iqIbreak_1 = iq_norm_ADC[0][14];
	analog.iqIbreak_2 = iq_norm_ADC[0][15];

#else
	analog.iqU_1 = analog.iqIu_1 = analog.iqIu_2 = analog.iqIv_1 = analog.iqIv_2 =
		analog.iqIw_1 = analog.iqIw_2 = analog.iqIin_1 = analog.iqIin_2 = analog.iqUin_A1B1 =
		analog.iqUin_B1C1 = analog.iqUin_A2B2 = analog.iqUin_B2C2 = analog.iqIbreak_1 = analog.iqIbreak_2
		= 0;
#endif

	analog.iqUin_C1A1 = -(analog.iqUin_A1B1 + analog.iqUin_B1C1);
	analog.iqUin_C2A2 = -(analog.iqUin_A2B2 + analog.iqUin_B2C2);



	filter.iqU_1_long = exp_regul_iq(koef_Uzpt_long_filter, filter.iqU_1_long, analog.iqU_1);
	filter.iqU_2_long = exp_regul_iq(koef_Uzpt_long_filter, filter.iqU_2_long, analog.iqU_2);


	//    analog.iqU_1_fast = filter_U1_3point(analog.iqU_1_fast);
	filter.iqU_1_fast = exp_regul_iq(koef_Uzpt_fast_filter, filter.iqU_1_fast, analog.iqU_1);
	filter.iqU_2_fast = exp_regul_iq(koef_Uzpt_fast_filter, filter.iqU_2_fast, analog.iqU_2);


	//    filter.iqUzpt_2_2_fast = exp_regul_iq(koef_Uzpt_fast_filter, filter.iqUzpt_2_2_fast, analog.iqUzpt_2_2);



	//15


	analog.iqIm_1 = im_calc(analog.iqIu_1, analog.iqIv_1, analog.iqIw_1);
	analog.iqIm_2 = im_calc(analog.iqIu_2, analog.iqIv_2, analog.iqIw_2);

	analog.iqIu = analog.iqIu_1 + analog.iqIu_2;
	analog.iqIv = analog.iqIv_1 + analog.iqIv_2;
	analog.iqIw = analog.iqIw_1 + analog.iqIw_2;

	analog.iqIm = im_calc(analog.iqIu, analog.iqIv, analog.iqIw);


	analog.iqIin_sum = analog.iqIin_1 + analog.iqIin_2;

	//   analog.iqIm_3 = im_calc(analog.iqIa1_1_fir_n+analog.iqIa2_1_fir_n, analog.iqIb1_1_fir_n+analog.iqIb2_1_fir_n, analog.iqIc1_1_fir_n+analog.iqIc2_1_fir_n);

	analog.iqUin_m1 = im_calc(analog.iqUin_A1B1, analog.iqUin_B1C1, analog.iqUin_C1A1);
	analog.iqUin_m2 = im_calc(analog.iqUin_A2B2, analog.iqUin_B2C2, analog.iqUin_C2A2);

	//   analog.iqUin_m2 = im_calc(analog.UinA2, analog.UinB2, analog.UinC2);

	filter.iqUin_m1 = exp_regul_iq(koef_Uin_filter, filter.iqUin_m1, analog.iqUin_m1);
	filter.iqUin_m2 = exp_regul_iq(koef_Uin_filter, filter.iqUin_m2, analog.iqUin_m2);



	//   i_led1_on_off(0);
	//   i_led1_on_off(1);

	//1

	filter.iqIm_1 = exp_regul_iq(koef_Im_filter, filter.iqIm_1, analog.iqIm_1);
	filter.iqIm_2 = exp_regul_iq(koef_Im_filter, filter.iqIm_2, analog.iqIm_2);
	filter.iqIm = exp_regul_iq(koef_Im_filter, filter.iqIm, analog.iqIm);

	filter.iqIin_sum = exp_regul_iq(koef_Im_filter, filter.iqIin_sum, analog.iqIin_sum);

	//3
	//   filter_batter2_Iin.InpVarCurr = (analog.iqIin_1)-ZERO_I_IN;
	 //  filter_batter2_Iin.calc(&filter_batter2_Iin);

	//   filter.iqIin = _IQmpy(filter_batter2_Iin.Out,_IQ_09);


	filter.iqIin_1 = exp_regul_iq(koef_Im_filter, filter.iqIin_1, analog.iqIin_1);
	filter.iqIin_2 = exp_regul_iq(koef_Im_filter, filter.iqIin_2, analog.iqIin_2);

	a1 = analog.iqU_1 + analog.iqU_2;
	a2 = analog.iqIin_1;
	a3 = _IQmpy(a1, a2);
	analog.PowerScalar = a3;
	//   filter.Power = analog.iqU_1+analog.iqU_2;
	filter.PowerScalar = exp_regul_iq(koef_Power_filter, filter.PowerScalar, analog.PowerScalar);
	filter.PowerScalarFilter2 = exp_regul_iq(koef_Power_filter2, filter.PowerScalarFilter2, filter.PowerScalar);

}
////////////////////////////////////////////////////////////////////
void analog_values_calc(void)
{

//#if (USE_ADC_0)
//
//#if (_FLOOR6)
//	analog.iqU_1 = iq_norm_ADC[0][0] - analog_zero.iqU_1 + analog.iqU_1_imit;
//	analog.iqU_2 = iq_norm_ADC[0][1] - analog_zero.iqU_2 + analog.iqU_1_imit;
//#else
//	analog.iqU_1 = iq_norm_ADC[0][0] - analog_zero.iqU_1;
//	analog.iqU_2 = iq_norm_ADC[0][1] - analog_zero.iqU_2;
//#endif
//	analog.iqIu_1 = iq_norm_ADC[0][2];
//	analog.iqIv_1 = iq_norm_ADC[0][3];
//	analog.iqIw_1 = iq_norm_ADC[0][4];
//
//	analog.iqIu_2 = iq_norm_ADC[0][5];
//	analog.iqIv_2 = iq_norm_ADC[0][6];
//	analog.iqIw_2 = iq_norm_ADC[0][7];
//
//	analog.iqIin_1 = -iq_norm_ADC[0][9]; // датчик перевернут
//	analog.iqIin_2 = -iq_norm_ADC[0][9]; // датчик перевернут
//
//	analog.iqUin_A1B1 = iq_norm_ADC[0][10];
//
//	// два варианта подключения датчиков 23550.1 более правильный - по схеме
//	// 23550.1
//
//	analog.iqUin_B1C1 = iq_norm_ADC[0][11]; // 23550.1
//	analog.iqUin_A2B2 = iq_norm_ADC[0][12]; // 23550.1
//
//// 23550.3 bs1 bs2
//
////    analog.iqUin_B1C1 = iq_norm_ADC[0][12]; // 23550.3
////    analog.iqUin_A2B2 = iq_norm_ADC[0][11]; // 23550.3
////
//	analog.iqUin_B2C2 = iq_norm_ADC[0][13];
//
//	analog.iqIbreak_1 = iq_norm_ADC[0][14];
//	analog.iqIbreak_2 = iq_norm_ADC[0][15];
//
//	#else
//	analog.iqU_1 = analog.iqIu_1 = analog.iqIu_2 = analog.iqIv_1 = analog.iqIv_2 =
//	analog.iqIw_1 = analog.iqIw_2 = analog.iqIin_1 = analog.iqIin_2 = analog.iqUin_A1B1 =
//	analog.iqUin_B1C1 = analog.iqUin_A2B2 = analog.iqUin_B2C2 = analog.iqIbreak_1 = analog.iqIbreak_2
//	= 0;
//	#endif
//
//	analog.iqUin_C1A1 = -(analog.iqUin_A1B1 + analog.iqUin_B1C1);
//	analog.iqUin_C2A2 = -(analog.iqUin_A2B2 + analog.iqUin_B2C2);
//
//
//
//	filter.iqU_1_long = exp_regul_iq(koef_Uzpt_long_filter, filter.iqU_1_long, analog.iqU_1);
//	filter.iqU_2_long = exp_regul_iq(koef_Uzpt_long_filter, filter.iqU_2_long, analog.iqU_2);
//
//
//	//    analog.iqU_1_fast = filter_U1_3point(analog.iqU_1_fast);
//	filter.iqU_1_fast = exp_regul_iq(koef_Uzpt_fast_filter, filter.iqU_1_fast, analog.iqU_1);
//	filter.iqU_2_fast = exp_regul_iq(koef_Uzpt_fast_filter, filter.iqU_2_fast, analog.iqU_2);
//
//
//	//    filter.iqUzpt_2_2_fast = exp_regul_iq(koef_Uzpt_fast_filter, filter.iqUzpt_2_2_fast, analog.iqUzpt_2_2);
//
//
//
//	//15
//
//
//	analog.iqIm_1 = im_calc(analog.iqIu_1, analog.iqIv_1, analog.iqIw_1);
//	analog.iqIm_2 = im_calc(analog.iqIu_2, analog.iqIv_2, analog.iqIw_2);
//
//	analog.iqIu = analog.iqIu_1 + analog.iqIu_2;
//	analog.iqIv = analog.iqIv_1 + analog.iqIv_2;
//	analog.iqIw = analog.iqIw_1 + analog.iqIw_2;
//
//	analog.iqIm = im_calc(analog.iqIu, analog.iqIv, analog.iqIw);
//
//
//	analog.iqIin_sum = analog.iqIin_1 + analog.iqIin_2;
//
//	//   analog.iqIm_3 = im_calc(analog.iqIa1_1_fir_n+analog.iqIa2_1_fir_n, analog.iqIb1_1_fir_n+analog.iqIb2_1_fir_n, analog.iqIc1_1_fir_n+analog.iqIc2_1_fir_n);
//
//	analog.iqUin_m1 = im_calc(analog.iqUin_A1B1, analog.iqUin_B1C1, analog.iqUin_C1A1);
//	analog.iqUin_m2 = im_calc(analog.iqUin_A2B2, analog.iqUin_B2C2, analog.iqUin_C2A2);
//
//	//   analog.iqUin_m2 = im_calc(analog.UinA2, analog.UinB2, analog.UinC2);
//
//	filter.iqUin_m1 = exp_regul_iq(koef_Uin_filter, filter.iqUin_m1, analog.iqUin_m1);
//	filter.iqUin_m2 = exp_regul_iq(koef_Uin_filter, filter.iqUin_m2, analog.iqUin_m2);
//
//
//
//	//   i_led1_on_off(0);
//	//   i_led1_on_off(1);
//
//	//1
//
//	filter.iqIm_1 = exp_regul_iq(koef_Im_filter, filter.iqIm_1, analog.iqIm_1);
//	filter.iqIm_2 = exp_regul_iq(koef_Im_filter, filter.iqIm_2, analog.iqIm_2);
//	filter.iqIm = exp_regul_iq(koef_Im_filter, filter.iqIm, analog.iqIm);
//
//	filter.iqIin_sum = exp_regul_iq(koef_Im_filter, filter.iqIin_sum, analog.iqIin_sum);
//
//	//3
//	//   filter_batter2_Iin.InpVarCurr = (analog.iqIin_1)-ZERO_I_IN;
//	 //  filter_batter2_Iin.calc(&filter_batter2_Iin);
//
//	//   filter.iqIin = _IQmpy(filter_batter2_Iin.Out,_IQ_09);
//
//
//	filter.iqIin_1 = exp_regul_iq(koef_Im_filter, filter.iqIin_1, analog.iqIin_1);
//	filter.iqIin_2 = exp_regul_iq(koef_Im_filter, filter.iqIin_2, analog.iqIin_2);
//
//	a1 = analog.iqU_1 + analog.iqU_2;
//	a2 = analog.iqIin_1;
//	a3 = _IQmpy(a1, a2);
//	analog.PowerScalar = a3;
//	//   filter.Power = analog.iqU_1+analog.iqU_2;
//	filter.PowerScalar = exp_regul_iq(koef_Power_filter, filter.PowerScalar, analog.PowerScalar);
//	filter.PowerScalarFilter2 = exp_regul_iq(koef_Power_filter2, filter.PowerScalarFilter2, filter.PowerScalar);


}

#define CONST_IQ_11PI6 96629827  //11Pi/6
#define CONST_IQ_PI6  8784529 // Pi/6


/********************************************************************/
/* Определение нулy показаний АЦП */
/********************************************************************/
#pragma DATA_SECTION(acp_zero,".slow_vars")
_iq19 acp_zero[16];
#pragma DATA_SECTION(acp_summ,".slow_vars")
long acp_summ[16];
void detect_zero_analog(int nc)
{
    long i, k;


    _iq koef_zero_ADC_filter = _IQ19(0.00002 / 0.0003185);


    for (k = 0; k < 16; k++)
    {
        acp_zero[k] = 0;
        acp_summ[k] = 0;
    }



    for (i = 0; i < COUNT_DETECT_ZERO; i++)
    {
        //      norma_all_adc();
        norma_adc_nc(nc);
        //     norma_adc_nc(1);


        for (k = 0; k < 16; k++)
        {
            acp_zero[k] = exp_regul_iq(koef_zero_ADC_filter, acp_zero[k], ((long)ADC_f[nc][k] << 19));
            acp_summ[k] = acp_summ[k] + ADC_f[nc][k];
        }



        pause_1000(1000);
    }

    // 16 и 7  канал пропускаем т.к. это канал напрyжениy                         
    for (k = 0; k < 16; k++)
    {

        //        if ((k==15))
        //		{
          //         if (ADC_sf[k]<MIN_DETECT_UD_ZERO) 
            //	      iq19_zero_ADC[k] = acp_zero[k];
            //	}
            //	else
        {
            iq19_zero_ADC[nc][k] = acp_zero[k];
        }

        //        zero_ADC[k] =_IQ19toF(acp_zero[k]);
        zero_ADC[nc][k] = acp_summ[k] / COUNT_DETECT_ZERO;//_IQ19toF(acp_zero[k]);
    }






}



unsigned int detect_protect_ACP_plus()
{


}


unsigned int detect_protect_ACP_minus()
{


}
