

#include "v_pwm24.h"
//#include "DSP281x_Device.h"     // DSP281x Headerfile Include File
//#include "big_dsp_module.h"
//#include "rmp2cntl.h"        	// Include header for the VHZPROF object
//#include "rmp_cntl_my1.h"        	// Include header for the VHZPROF object
#include "pid_reg3.h"        	// Include header for the VHZPROF object

#include "params.h"
// #include "PWMTools.h"
#include "adc_tools.h"
#include "v_pwm24.h"

#include "dq_to_alphabeta_cos.h"

#include "IQmathLib.h"
// #include "log_to_memory.h"
//Xilinx
//#include "x_parameters.h"
// #include "x_basic_types.h"
// #include "xp_project.h"
// #include "xp_cds_tk.h"
#include "svgen_dq.h"
#include "xp_write_xpwm_time.h"

#include "def.h"

#define DEF_FREQ_PWM_XTICS 	T1_PRD
#define DEF_PERIOD_MIN_XTICS 	(DT + 10e-6)*FTBCLK

// Частота ШИМ в xilinx тиках (60000000 / 16 / FREQ_PWM = 3750000 / FREQ_PWM)
// #pragma DATA_SECTION(VAR_FREQ_PWM_XTICS,".fast_vars1");
int VAR_FREQ_PWM_XTICS = DEF_FREQ_PWM_XTICS;

// Минимальное значение ШИМа в xilinx тиках
// #pragma DATA_SECTION(VAR_PERIOD_MAX_XTICS,".fast_vars1");
int VAR_PERIOD_MAX_XTICS = DEF_FREQ_PWM_XTICS - DEF_PERIOD_MIN_XTICS;

// Минимальное значение ШИМа в xilinx тиках (mintime+deadtime) (Fпилы * Tмин.ключа.сек = (60 / 16 / 2) * Tмкс = (60  * Tмкс / 16 / 2))
// #pragma DATA_SECTION(VAR_PERIOD_MIN_XTICS,".fast_vars1");
int VAR_PERIOD_MIN_XTICS = DEF_PERIOD_MIN_XTICS;// 

// Минимальное значение ШИМа в xilinx тиках для тормозных ключей (mintime) (Fпилы * Tмин.ключа.сек = (60 / 16 / 2) * Tмкс = (60  * Tмкс / 16 / 2))
// #pragma DATA_SECTION(VAR_PERIOD_MIN_BR_XTICS,".fast_vars1");
// int VAR_PERIOD_MIN_BR_XTICS = DEF_PERIOD_MIN_BR_XTICS;// 

#define PWM_ONE_INTERRUPT_RUN       1
#define PWM_TWICE_INTERRUPT_RUN     0



#define SQRT3 29058990  //1.7320508075688772935274463415059
#define CONST_IQ_1  16777216 //1
#define CONST_IQ_05  8388608 //0.5
#define CONST_IQ_2	33554432 //2

#define CONST_IQ_PI6  8784530	//30
#define CONST_IQ_PI3  17569060  // 60 
#define CONST_IQ_PI   52707178  // 180
#define CONST_IQ_OUR1 35664138  //
#define CONST_IQ_2PI  105414357 // 360
#define CONST_IQ_120G 35138119  // 120 grad
#define CONST_IQ_3 50331648 // 3

#define IQ_ALFA_SATURATION1 15099494//16441671//15099494
#define IQ_ALFA_SATURATION2 1677721//16441671//15099494


#define PI 3.1415926535897932384626433832795

// #pragma DATA_SECTION(iq_alfa_coef,".fast_vars");
_iq iq_alfa_coef = 16777216;

// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
//#pragma DATA_SECTION(freq_array,".v_24pwm_vars");
//int freq_array[COUNT_VAR_FREQ];
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

//#pragma DATA_SECTION(pidCur_Kp,".v_24pwm_vars");
//_iq pidCur_Kp = 0;

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

// #pragma DATA_SECTION(ar_tph,".fast_vars");
_iq ar_tph[7];

// #pragma DATA_SECTION(winding_displacement,".fast_vars");
_iq winding_displacement = CONST_IQ_PI6;

//#pragma DATA_SECTION(iq_koef_mod_korrect_1,".fast_vars");//v_24pwm_vars
//_iq iq_koef_mod_korrect_1;

//#pragma DATA_SECTION(iq_koef_mod_korrect_2,".v_24pwm_vars");
//_iq iq_koef_mod_korrect_2;

//#pragma DATA_SECTION(ar_sa_all,".v_24pwm_vars");
// #pragma DATA_SECTION(ar_sa_all,".fast_vars");
int ar_sa_all[3][6][4][7] = { { 
							{
									{-1,0,0,0,1,1,1},{0,1,1,1,0,0,0},{0,0,1,1,1,0,0},{0,1,1,1,0,0,0}
							},
							{
									{-1,-1,0,0,0,1,1},{-1,-1,0,0,0,0,0},{-1,0,0,0,1,0,0},{0,0,1,1,0,0,0}
							},
							{
									{-1,-1,-1,0,0,0,1},{-1,-1,-1,0,0,0,0},{-1,-1,-1,0,0,0,0},{-1,-1,-1,0,0,0,0}
							},
							{
									{-1,-1,-1,0,0,0,1},{-1,-1,-1,0,0,0,0},{-1,-1,-1,0,0,0,0},{-1,-1,-1,0,0,0,0}
							},
							{
									{-1,-1,0,0,0,1,1},{-1,-1,0,0,0,0,0},{-1,0,0,0,1,0,0},{0,0,1,1,0,0,0}
							},
							{
									{-1,0,0,0,1,1,1},{0,1,1,1,0,0,0},{0,0,1,1,1,0,0},{0,1,1,1,0,0,0}
							}
						 },
							{
							{
									{-1,-1,0,0,0,1,1},{-1,-1,0,0,0,0,0},{-1,0,0,0,1,0,0},{0,0,1,1,0,0,0}
							},
							{
									{-1,0,0,0,1,1,1},{0,1,1,1,0,0,0},{0,0,1,1,1,0,0},{0,1,1,1,0,0,0}
							},
							{
									{-1,0,0,0,1,1,1},{0,1,1,1,0,0,0},{0,0,1,1,1,0,0},{0,1,1,1,0,0,0}
							},
							{
									{-1,-1,0,0,0,1,1},{-1,-1,0,0,0,0,0},{-1,0,0,0,1,0,0},{0,0,1,1,0,0,0}
							},
							{
									{-1,-1,-1,0,0,0,1},{-1,-1,-1,0,0,0,0},{-1,-1,-1,0,0,0,0},{-1,-1,-1,0,0,0,0}
							},
							{
									{-1,-1,-1,0,0,0,1},{-1,-1,-1,0,0,0,0},{-1,-1,-1,0,0,0,0},{-1,-1,-1,0,0,0,0}
							}
						 },
						{
							{
									{-1,-1,-1,0,0,0,1},{-1,-1,-1,0,0,0,0},{-1,-1,-1,0,0,0,0},{-1,-1,-1,0,0,0,0}
							},
							{
									{-1,-1,-1,0,0,0,1},{-1,-1,-1,0,0,0,0},{-1,-1,-1,0,0,0,0},{-1,-1,-1,0,0,0,0}
							},
							{
									{-1,-1,0,0,0,1,1},{-1,-1,0,0,0,0,0},{-1,0,0,0,1,0,0},{0,0,1,1,0,0,0}
							},
							{
									{-1,0,0,0,1,1,1},{0,1,1,1,0,0,0},{0,0,1,1,1,0,0},{0,1,1,1,0,0,0}
							},
							{
									{-1,0,0,0,1,1,1},{0,1,1,1,0,0,0},{0,0,1,1,1,0,0},{0,1,1,1,0,0,0}
							},
							{
									{-1,-1,0,0,0,1,1},{-1,-1,0,0,0,0,0},{-1,0,0,0,1,0,0},{0,0,1,1,0,0,0}
							}
						 }
					};


// #pragma DATA_SECTION(svgen_pwm24_1,".v_24pwm_vars");
//#pragma DATA_SECTION(svgen_pwm24_1,".fast_vars");
SVGEN_PWM24 svgen_pwm24_1 = SVGEN_PWM24_DEFAULTS;
// #pragma DATA_SECTION(svgen_pwm24_2,".v_24pwm_vars");
//#pragma DATA_SECTION(svgen_pwm24_2,".fast_vars");
SVGEN_PWM24 svgen_pwm24_2 = SVGEN_PWM24_DEFAULTS;

// #pragma DATA_SECTION(svgen_dq_1,".v_24pwm_vars");
SVGENDQ svgen_dq_1 = SVGENDQ_DEFAULTS;
// #pragma DATA_SECTION(svgen_dq_2,".v_24pwm_vars");
SVGENDQ svgen_dq_2 = SVGENDQ_DEFAULTS;

//#pragma DATA_SECTION(delta_t1_struct,".v_24pwm_vars");
//#pragma DATA_SECTION(delta_t1_struct,".fast_vars");
//PIDREG3 delta_t1_struct = PIDREG3_DEFAULTS;
//#pragma DATA_SECTION(delta_t2_struct,".v_24pwm_vars");
//#pragma DATA_SECTION(delta_t2_struct,".fast_vars");
//PIDREG3 delta_t2_struct = PIDREG3_DEFAULTS;

void calc_t_abc(_iq k, _iq teta, int region, _iq *iq_tt0, _iq *iq_tt1, _iq *iq_tt2, _iq *iq_tt3, _iq *iq_tt4, _iq *iq_tt5);
void set_predel_dshim24(SVGEN_PWM24_TIME *T,int16 dmin,int16 dmax);
void set_predel_dshim24_simple0(SVGEN_PWM24_TIME *T,int16 dmin,int16 dmax);
void set_predel_dshim24_simple1(SVGEN_PWM24_TIME *T,int16 dmin,int16 dmax);
int detect_sec(_iq teta);
int detect_region(_iq k, _iq teta);
//




//void init_alpha(void)
//{
//
////  power_ain1.init(&power_ain1);
////  power_ain2.init(&power_ain2);
//
//    svgen_mf1.NewEntry = 0;//_IQ(0.5);
//    svgen_mf2.NewEntry = 0;
//
//    svgen_mf1.SectorPointer = 0;
//    svgen_mf2.SectorPointer = 0;
//
////сдвиг по умолчанию 0 градусов
//    svgen_mf1.Alpha = _IQ(0);
//    svgen_mf2.Alpha = _IQ(0);
//
//
//
//
//
//#if (SETUP_SDVIG_OBMOTKI == SDVIG_OBMOTKI_30_PLUS)
//// 30 град. сдвиг
//    svgen_mf1.Alpha = _IQ(0.5);
//    svgen_mf2.Alpha = _IQ(0);
//
//    svgen_mf1.Full_Alpha = svgen_mf1.Alpha;
//    svgen_mf2.Full_Alpha = svgen_mf2.Alpha;
//#else
//
//
//#if (SETUP_SDVIG_OBMOTKI == SDVIG_OBMOTKI_30_MINUS)
//// -30 град. сдвиг
//    svgen_mf1.Alpha = _IQ(0);
//    svgen_mf2.Alpha = _IQ(0.5);
//    svgen_mf1.Full_Alpha = svgen_mf1.Alpha;
//    svgen_mf2.Full_Alpha = svgen_mf2.Alpha;
//#else
//
//#if (SETUP_SDVIG_OBMOTKI ==  SDVIG_OBMOTKI_ZERO)
//// -30 град. сдвиг
//    svgen_mf1.Alpha = _IQ(0);
//    svgen_mf2.Alpha = _IQ(0);
//    svgen_mf1.Full_Alpha = svgen_mf1.Alpha;
//    svgen_mf2.Full_Alpha = svgen_mf2.Alpha;
//#else
//   #error "!!!ОШИБКА!!! Не определен SETUP_SDVIG_OBMOTKI в params_motor.h!!!"
//
//
//#endif
//
//#endif
//
//
//
//#endif
//
//
//
//}

void InitVariablesSvgen(unsigned int freq)
{
////////// Inserted from 'initPWM_Variables' for modulation project //////////

	//Для первой пилы закрыто, когда выше заданного уровня, для второй ниже
    xpwm_time.Tclosed_0 = 0;
    xpwm_time.Tclosed_1 = VAR_FREQ_PWM_XTICS + 1;
    xpwm_time.Tclosed_high = VAR_FREQ_PWM_XTICS + 1;
    xpwm_time.pwm_tics = VAR_FREQ_PWM_XTICS;
    // выбрали направление пил
    // "направление пил для ШИМа типа=0x0
    //Если SAW_DIRECTbit = 0 то значение пилы>уставки выставит активное включенное состояние=0
    //на выходе ШИМа
    //Если SAW_DIRECTbit = 1 то значение пилы<=уставки выставит активное включенное состояние=0
    //на выходе ШИМа
    // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    //Для 22220 инициализирую 0, т.к. при записи в разных частях пилы появляется дизбаланс напряжения.
    xpwm_time.saw_direct.all = 0;//0x0555;
    xpwm_time.one_or_two_interrupts_run = PWM_TWICE_INTERRUPT_RUN;
    
    initXpwmTimeStructure(&xpwm_time);
    init_alpha_pwm24(VAR_FREQ_PWM_XTICS);
/////////////////////////////////////////////////////////////

    svgen_pwm24_1.pwm_minimal_impuls_zero_minus = 0;//(float)DEF_PERIOD_MIN_MKS*1000.0*FREQ_INTERNAL_GENERATOR_XILINX_TMS/1000000000.0;//   DEF_PERIOD_MIN_XTICS_100;//DEF_PERIOD_MIN_XTICS_80;
    svgen_pwm24_1.pwm_minimal_impuls_zero_plus  = 0;//(float)DEF_PERIOD_MIN_MKS*1000.0*FREQ_INTERNAL_GENERATOR_XILINX_TMS/1000000000.0;// DEF_PERIOD_MIN_XTICS_80;

    svgen_pwm24_2.pwm_minimal_impuls_zero_minus = svgen_pwm24_1.pwm_minimal_impuls_zero_minus;
    svgen_pwm24_2.pwm_minimal_impuls_zero_plus  = svgen_pwm24_1.pwm_minimal_impuls_zero_plus;


    svgen_pwm24_1.Tclosed_high = xpwm_time.Tclosed_1;
    svgen_pwm24_2.Tclosed_high = xpwm_time.Tclosed_1;


    

    svgen_dq_1.Ualpha = 0;
    svgen_dq_1.Ubeta = 0;

    svgen_dq_2.Ualpha = 0;
    svgen_dq_2.Ubeta = 0;



}










// #pragma CODE_SECTION(recalc_time_pwm_minimal_2_xilinx_pwm24_l,".fast_run2");
void recalc_time_pwm_minimal_2_xilinx_pwm24_l(SVGEN_PWM24 *pwm24,
                                            _iq *T0, _iq *T1,
                                            _iq timpuls_corr )
{

//_iq pwm_t, timpuls_corr;

    volatile unsigned long pwm_t;
    volatile unsigned int minimal_plus, minimal_minus;



    minimal_plus = pwm24->pwm_minimal_impuls_zero_plus;
    minimal_minus = pwm24->pwm_minimal_impuls_zero_minus;

 //   if (pwm24->prev_level == V_PWM24_PREV_PWM_CLOSE || pwm24->prev_level == V_PWM24_PREV_PWM_MIDDLE || pwm24->prev_level == V_PWM24_PREV_PWM_WORK_KM0)
 //   {
 //       minimal_plus *= 2;
//        minimal_minus *= 2;
//    }

    pwm_t = timpuls_corr / pwm24->XilinxFreq;

 //   *T_imp =  pwm_t;

//    if (pwm_t>(pwm24->Tclosed_high-4*minimal_minus))
//        pwm_t=(pwm24->Tclosed_high-4*minimal_minus);


    if (timpuls_corr >= 0)
    {
        *T0 = pwm_t + minimal_plus;
        // *T1 = 0;
        *T1 = pwm24->Tclosed_high - minimal_minus;
    }
    else
    {
        *T0 = 0 + minimal_plus;
        // *T1 = -pwm_t - minimal_minus;
        *T1 = pwm24->Tclosed_high + pwm_t - minimal_minus;
    }


    // if (*T0 < minimal_plus)
    //     *T0 = minimal_plus;

    // if (*T0 > (pwm24->Tclosed_high - 2 * minimal_plus))
    //     *T0 = (pwm24->Tclosed_high - 2 * minimal_plus);

    // if (*T1 < (2 * minimal_minus))
    //     *T1 = 2 * minimal_minus;

    // if (*T1 > (pwm24->Tclosed_high - minimal_minus))
    //     *T1 = (pwm24->Tclosed_high - minimal_minus);

}

static DQ_TO_ALPHABETA dq_to_ab = DQ_TO_ALPHABETA_DEFAULTS;

// #pragma CODE_SECTION(test_calc_dq_pwm24,".v_24pwm_run");
void test_calc_dq_pwm24(_iq Ud, _iq Uq, _iq Ud2, _iq Uq2, _iq tetta,_iq Uzad_max)
{
//	DQ_TO_ALPHABETA dq_to_ab = DQ_TO_ALPHABETA_DEFAULTS;
	_iq maxUq = 0;
	_iq Uzad_max_square = _IQmpy(Uzad_max, Uzad_max);

	if (tetta > CONST_IQ_2PI)
	{
		tetta -= CONST_IQ_2PI;
	}
	
	if (tetta < 0)
	{
		tetta += CONST_IQ_2PI;
	}
	//Ограничил коэффициент модуляции
    maxUq = _IQsqrt(Uzad_max_square - _IQmpy(Ud, Ud));
    if (Uq > maxUq) { Uq = maxUq;}
    if (Uq < -maxUq) { Uq = -maxUq;}


	//Reculct dq to alpha-beta
	dq_to_ab.Tetta = tetta;
    dq_to_ab.Ud = Ud;
    dq_to_ab.Uq = Uq;
    dq_to_ab.calc2(&dq_to_ab);
	//Calc swgen times for 1-st winding
    svgen_dq_1.Ualpha = dq_to_ab.Ualpha;
    svgen_dq_1.Ubeta = dq_to_ab.Ubeta;
	svgen_dq_1.calc(&svgen_dq_1);

	//Ограничил коэффициент модуляции
    maxUq = _IQsqrt(Uzad_max_square - _IQmpy(Ud, Ud));
    if (Uq2 > maxUq) { Uq2 = maxUq;}
    if (Uq2 < -maxUq) { Uq2 = -maxUq;}

	//Reculc dq to alpha-beta for 2-nd winding with winding displasement
	dq_to_ab.Tetta = tetta - winding_displacement;
    dq_to_ab.Ud = Ud2;
    dq_to_ab.Uq = Uq2;
    dq_to_ab.calc2(&dq_to_ab);
	//Calc swgen times for 1-st winding
	svgen_dq_2.Ualpha = dq_to_ab.Ualpha;
	svgen_dq_2.Ubeta = dq_to_ab.Ubeta;
	svgen_dq_2.calc(&svgen_dq_2);

	//1 winding
	recalc_time_pwm_minimal_2_xilinx_pwm24_l (&svgen_pwm24_1, &svgen_pwm24_1.Ta_0.Ti, &svgen_pwm24_1.Ta_1.Ti,  svgen_dq_1.Ta);
    recalc_time_pwm_minimal_2_xilinx_pwm24_l (&svgen_pwm24_1, &svgen_pwm24_1.Tb_0.Ti, &svgen_pwm24_1.Tb_1.Ti, svgen_dq_1.Tb);
    recalc_time_pwm_minimal_2_xilinx_pwm24_l (&svgen_pwm24_1, &svgen_pwm24_1.Tc_0.Ti, &svgen_pwm24_1.Tc_1.Ti, svgen_dq_1.Tc);

    // 2 winding
    recalc_time_pwm_minimal_2_xilinx_pwm24_l (&svgen_pwm24_2, &svgen_pwm24_2.Ta_0.Ti, &svgen_pwm24_2.Ta_1.Ti, svgen_dq_2.Ta);
    recalc_time_pwm_minimal_2_xilinx_pwm24_l (&svgen_pwm24_2, &svgen_pwm24_2.Tb_0.Ti, &svgen_pwm24_2.Tb_1.Ti, svgen_dq_2.Tb);
    recalc_time_pwm_minimal_2_xilinx_pwm24_l (&svgen_pwm24_2, &svgen_pwm24_2.Tc_0.Ti, &svgen_pwm24_2.Tc_1.Ti, svgen_dq_2.Tc);

//    set_predel_dshim24_simple0(&svgen_pwm24_1.Ta_0,VAR_PERIOD_MIN_XTICS,VAR_FREQ_PWM_XTICS);
//    set_predel_dshim24_simple1(&svgen_pwm24_1.Ta_1,VAR_PERIOD_MIN_XTICS,VAR_FREQ_PWM_XTICS);
//
//    set_predel_dshim24_simple0(&svgen_pwm24_1.Tb_0,VAR_PERIOD_MIN_XTICS,VAR_FREQ_PWM_XTICS);
//    set_predel_dshim24_simple1(&svgen_pwm24_1.Tb_1,VAR_PERIOD_MIN_XTICS,VAR_FREQ_PWM_XTICS);
//
//    set_predel_dshim24_simple0(&svgen_pwm24_1.Tc_0,VAR_PERIOD_MIN_XTICS,VAR_FREQ_PWM_XTICS);
//    set_predel_dshim24_simple1(&svgen_pwm24_1.Tc_1,VAR_PERIOD_MIN_XTICS,VAR_FREQ_PWM_XTICS);
//
//    set_predel_dshim24_simple0(&svgen_pwm24_2.Ta_0,VAR_PERIOD_MIN_XTICS,VAR_FREQ_PWM_XTICS);
//    set_predel_dshim24_simple1(&svgen_pwm24_2.Ta_1,VAR_PERIOD_MIN_XTICS,VAR_FREQ_PWM_XTICS);
//
//    set_predel_dshim24_simple0(&svgen_pwm24_2.Tb_0,VAR_PERIOD_MIN_XTICS,VAR_FREQ_PWM_XTICS);
//    set_predel_dshim24_simple1(&svgen_pwm24_2.Tb_1,VAR_PERIOD_MIN_XTICS,VAR_FREQ_PWM_XTICS);
//
//    set_predel_dshim24_simple0(&svgen_pwm24_2.Tc_0,VAR_PERIOD_MIN_XTICS,VAR_FREQ_PWM_XTICS);
//    set_predel_dshim24_simple1(&svgen_pwm24_2.Tc_1,VAR_PERIOD_MIN_XTICS,VAR_FREQ_PWM_XTICS);
/*
    set_predel_dshim24(&svgen_pwm24_1.Ta_0,VAR_PERIOD_MIN_XTICS,VAR_FREQ_PWM_XTICS);
	set_predel_dshim24(&svgen_pwm24_1.Ta_1,VAR_PERIOD_MIN_XTICS,VAR_FREQ_PWM_XTICS);

	set_predel_dshim24(&svgen_pwm24_1.Tb_0,VAR_PERIOD_MIN_XTICS,VAR_FREQ_PWM_XTICS);
	set_predel_dshim24(&svgen_pwm24_1.Tb_1,VAR_PERIOD_MIN_XTICS,VAR_FREQ_PWM_XTICS);

	set_predel_dshim24(&svgen_pwm24_1.Tc_0,VAR_PERIOD_MIN_XTICS,VAR_FREQ_PWM_XTICS);
	set_predel_dshim24(&svgen_pwm24_1.Tc_1,VAR_PERIOD_MIN_XTICS,VAR_FREQ_PWM_XTICS);

	set_predel_dshim24(&svgen_pwm24_2.Ta_0,VAR_PERIOD_MIN_XTICS,VAR_FREQ_PWM_XTICS);
	set_predel_dshim24(&svgen_pwm24_2.Ta_1,VAR_PERIOD_MIN_XTICS,VAR_FREQ_PWM_XTICS);

	set_predel_dshim24(&svgen_pwm24_2.Tb_0,VAR_PERIOD_MIN_XTICS,VAR_FREQ_PWM_XTICS);
	set_predel_dshim24(&svgen_pwm24_2.Tb_1,VAR_PERIOD_MIN_XTICS,VAR_FREQ_PWM_XTICS);

	set_predel_dshim24(&svgen_pwm24_2.Tc_0,VAR_PERIOD_MIN_XTICS,VAR_FREQ_PWM_XTICS);
	set_predel_dshim24(&svgen_pwm24_2.Tc_1,VAR_PERIOD_MIN_XTICS,VAR_FREQ_PWM_XTICS);*/
}


// #pragma CODE_SECTION(test_calc_simple_dq_pwm24,".v_24pwm_run");
void test_calc_simple_dq_pwm24(_iq uz1, _iq uz2, _iq fz1, _iq fz2,_iq Uzad_max)
{
    static _iq hz_to_angle = _IQ(2.0 * PI * NORMA_FROTOR / FREQ_PWM / 2);
//	static _iq tetta = 0;
    DQ_TO_ALPHABETA dq_to_ab = DQ_TO_ALPHABETA_DEFAULTS;

    _iq Ud = 0;
    _iq Uq = _IQsat(uz1,Uzad_max,0);

	analog.tetta += _IQmpy(fz1, hz_to_angle);

	if (analog.tetta >= CONST_IQ_2PI)
	{
		analog.tetta -= CONST_IQ_2PI;
	}
	
	if (analog.tetta < 0)
	{
		analog.tetta += CONST_IQ_2PI;
	}

    dq_to_ab.Tetta = analog.tetta;
    dq_to_ab.Ud = Ud;
    dq_to_ab.Uq = Uq;
    dq_to_ab.calc2(&dq_to_ab);

    svgen_dq_1.Ualpha = dq_to_ab.Ualpha;
    svgen_dq_1.Ubeta = dq_to_ab.Ubeta;

//    svgen_dq_1.Ualpha = 0;
//    svgen_dq_1.Ubeta = 0;

    svgen_dq_1.calc(&svgen_dq_1);

    dq_to_ab.Tetta = analog.tetta - winding_displacement;
    dq_to_ab.Ud = Ud;
    dq_to_ab.Uq = Uq;
    dq_to_ab.calc2(&dq_to_ab);

    svgen_dq_2.Ualpha = dq_to_ab.Ualpha;
    svgen_dq_2.Ubeta = dq_to_ab.Ubeta;

    svgen_dq_2.calc(&svgen_dq_2);

    recalc_time_pwm_minimal_2_xilinx_pwm24_l (&svgen_pwm24_1, &svgen_pwm24_1.Ta_0.Ti, &svgen_pwm24_1.Ta_1.Ti,  svgen_dq_1.Ta);
    recalc_time_pwm_minimal_2_xilinx_pwm24_l (&svgen_pwm24_1, &svgen_pwm24_1.Tb_0.Ti, &svgen_pwm24_1.Tb_1.Ti, svgen_dq_1.Tb);
    recalc_time_pwm_minimal_2_xilinx_pwm24_l (&svgen_pwm24_1, &svgen_pwm24_1.Tc_0.Ti, &svgen_pwm24_1.Tc_1.Ti, svgen_dq_1.Tc);

    // 2
    recalc_time_pwm_minimal_2_xilinx_pwm24_l (&svgen_pwm24_2, &svgen_pwm24_2.Ta_0.Ti, &svgen_pwm24_2.Ta_1.Ti, svgen_dq_2.Ta);
    recalc_time_pwm_minimal_2_xilinx_pwm24_l (&svgen_pwm24_2, &svgen_pwm24_2.Tb_0.Ti, &svgen_pwm24_2.Tb_1.Ti, svgen_dq_2.Tb);
    recalc_time_pwm_minimal_2_xilinx_pwm24_l (&svgen_pwm24_2, &svgen_pwm24_2.Tc_0.Ti, &svgen_pwm24_2.Tc_1.Ti, svgen_dq_2.Tc);

//	logpar.log1 = (int16)(_IQtoIQ15(uz1));
//	logpar.log2 = (int16)(_IQtoIQ15(fz1));
//	logpar.log3 = (int16)(_IQtoIQ15(Ud));
//	logpar.log4 = (int16)(_IQtoIQ15(Uq));
//	logpar.log5 = (int16)(_IQtoIQ15(svgen_dq_1.Ualpha));
//	logpar.log6 = (int16)(_IQtoIQ15(svgen_dq_1.Ubeta));
//	logpar.log7 = (int16)(_IQtoIQ15(svgen_dq_1.Ta));
//	logpar.log8 = (int16)(_IQtoIQ15(svgen_dq_1.Tb));
//	logpar.log9 = (int16)(_IQtoIQ15(svgen_dq_1.Tc));
//	logpar.log10 = (int16)(_IQtoIQ12(analog.tetta));
//	logpar.log11 = (int16)(svgen_pwm24_1.Ta_0.Ti);
//	logpar.log12 = (int16)((svgen_pwm24_1.Ta_1.Ti));
//	logpar.log13 = (int16)(svgen_pwm24_1.Tb_0.Ti);
//	logpar.log14 = (int16)((svgen_pwm24_1.Tb_1.Ti));
//	logpar.log15 = (int16)(svgen_pwm24_1.Tc_0.Ti);
//	logpar.log16 = (int16)((svgen_pwm24_1.Tc_1.Ti));


//    svgen_pwm24_1.calc(&svgen_pwm24_1);
//    svgen_pwm24_2.calc(&svgen_pwm24_2);

    // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

    // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

//	set_predel_dshim24_simple0(&svgen_pwm24_1.Ta_0,VAR_PERIOD_MIN_XTICS,VAR_FREQ_PWM_XTICS);
//	set_predel_dshim24_simple1(&svgen_pwm24_1.Ta_1,VAR_PERIOD_MIN_XTICS,VAR_FREQ_PWM_XTICS);
//
//	set_predel_dshim24_simple0(&svgen_pwm24_1.Tb_0,VAR_PERIOD_MIN_XTICS,VAR_FREQ_PWM_XTICS);
//	set_predel_dshim24_simple1(&svgen_pwm24_1.Tb_1,VAR_PERIOD_MIN_XTICS,VAR_FREQ_PWM_XTICS);
//
//	set_predel_dshim24_simple0(&svgen_pwm24_1.Tc_0,VAR_PERIOD_MIN_XTICS,VAR_FREQ_PWM_XTICS);
//	set_predel_dshim24_simple1(&svgen_pwm24_1.Tc_1,VAR_PERIOD_MIN_XTICS,VAR_FREQ_PWM_XTICS);
//
//	set_predel_dshim24_simple0(&svgen_pwm24_2.Ta_0,VAR_PERIOD_MIN_XTICS,VAR_FREQ_PWM_XTICS);
//	set_predel_dshim24_simple1(&svgen_pwm24_2.Ta_1,VAR_PERIOD_MIN_XTICS,VAR_FREQ_PWM_XTICS);
//
//	set_predel_dshim24_simple0(&svgen_pwm24_2.Tb_0,VAR_PERIOD_MIN_XTICS,VAR_FREQ_PWM_XTICS);
//	set_predel_dshim24_simple1(&svgen_pwm24_2.Tb_1,VAR_PERIOD_MIN_XTICS,VAR_FREQ_PWM_XTICS);
//
//	set_predel_dshim24_simple0(&svgen_pwm24_2.Tc_0,VAR_PERIOD_MIN_XTICS,VAR_FREQ_PWM_XTICS);
//	set_predel_dshim24_simple1(&svgen_pwm24_2.Tc_1,VAR_PERIOD_MIN_XTICS,VAR_FREQ_PWM_XTICS);

    set_predel_dshim24(&svgen_pwm24_1.Ta_0,VAR_PERIOD_MIN_XTICS,VAR_FREQ_PWM_XTICS);
	set_predel_dshim24(&svgen_pwm24_1.Ta_1,VAR_PERIOD_MIN_XTICS,VAR_FREQ_PWM_XTICS);

	set_predel_dshim24(&svgen_pwm24_1.Tb_0,VAR_PERIOD_MIN_XTICS,VAR_FREQ_PWM_XTICS);
	set_predel_dshim24(&svgen_pwm24_1.Tb_1,VAR_PERIOD_MIN_XTICS,VAR_FREQ_PWM_XTICS);

	set_predel_dshim24(&svgen_pwm24_1.Tc_0,VAR_PERIOD_MIN_XTICS,VAR_FREQ_PWM_XTICS);
	set_predel_dshim24(&svgen_pwm24_1.Tc_1,VAR_PERIOD_MIN_XTICS,VAR_FREQ_PWM_XTICS);

	set_predel_dshim24(&svgen_pwm24_2.Ta_0,VAR_PERIOD_MIN_XTICS,VAR_FREQ_PWM_XTICS);
	set_predel_dshim24(&svgen_pwm24_2.Ta_1,VAR_PERIOD_MIN_XTICS,VAR_FREQ_PWM_XTICS);

	set_predel_dshim24(&svgen_pwm24_2.Tb_0,VAR_PERIOD_MIN_XTICS,VAR_FREQ_PWM_XTICS);
	set_predel_dshim24(&svgen_pwm24_2.Tb_1,VAR_PERIOD_MIN_XTICS,VAR_FREQ_PWM_XTICS);

	set_predel_dshim24(&svgen_pwm24_2.Tc_0,VAR_PERIOD_MIN_XTICS,VAR_FREQ_PWM_XTICS);
	set_predel_dshim24(&svgen_pwm24_2.Tc_1,VAR_PERIOD_MIN_XTICS,VAR_FREQ_PWM_XTICS);

}

// #pragma CODE_SECTION(test_calc_pwm24,".v_24pwm_run");
void test_calc_pwm24(_iq uz1, _iq uz2, _iq fz1)
{
	//static int i =0;
	svgen_pwm24_1.Freq = fz1;
	svgen_pwm24_2.Freq = fz1;
	
	svgen_pwm24_1.Gain = uz1;//_IQmpy(uz1,iq_koef_mod_korrect_1);
	svgen_pwm24_2.Gain = uz2;//_IQmpy(uz2,iq_koef_mod_korrect_2);
	
	svgen_pwm24_1.delta_U = filter.iqU_1_fast - filter.iqU_2_fast;
	svgen_pwm24_2.delta_U = filter.iqU_3_fast - filter.iqU_4_fast;
	
	// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
	svgen_pwm24_1.delta_U = 0;
	svgen_pwm24_2.delta_U = 0;
	// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

	svgen_pwm24_1.Ia = analog.iqIa1_1;
	svgen_pwm24_1.Ib = analog.iqIb1_1;
	svgen_pwm24_1.Ic = analog.iqIc1_1;

	svgen_pwm24_2.Ia = analog.iqIa2_1;
	svgen_pwm24_2.Ib = analog.iqIb2_1;
	svgen_pwm24_2.Ic = analog.iqIc2_1;

	svgen_pwm24_1.calc(&svgen_pwm24_1);
	svgen_pwm24_2.calc(&svgen_pwm24_2);

	// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
	
	// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

	set_predel_dshim24(&svgen_pwm24_1.Ta_0,VAR_PERIOD_MIN_XTICS,VAR_FREQ_PWM_XTICS);
	set_predel_dshim24(&svgen_pwm24_1.Ta_1,VAR_PERIOD_MIN_XTICS,VAR_FREQ_PWM_XTICS);

	set_predel_dshim24(&svgen_pwm24_1.Tb_0,VAR_PERIOD_MIN_XTICS,VAR_FREQ_PWM_XTICS);
	set_predel_dshim24(&svgen_pwm24_1.Tb_1,VAR_PERIOD_MIN_XTICS,VAR_FREQ_PWM_XTICS);

	set_predel_dshim24(&svgen_pwm24_1.Tc_0,VAR_PERIOD_MIN_XTICS,VAR_FREQ_PWM_XTICS);
	set_predel_dshim24(&svgen_pwm24_1.Tc_1,VAR_PERIOD_MIN_XTICS,VAR_FREQ_PWM_XTICS);

	set_predel_dshim24(&svgen_pwm24_2.Ta_0,VAR_PERIOD_MIN_XTICS,VAR_FREQ_PWM_XTICS);
	set_predel_dshim24(&svgen_pwm24_2.Ta_1,VAR_PERIOD_MIN_XTICS,VAR_FREQ_PWM_XTICS);

	set_predel_dshim24(&svgen_pwm24_2.Tb_0,VAR_PERIOD_MIN_XTICS,VAR_FREQ_PWM_XTICS);
	set_predel_dshim24(&svgen_pwm24_2.Tb_1,VAR_PERIOD_MIN_XTICS,VAR_FREQ_PWM_XTICS);

	set_predel_dshim24(&svgen_pwm24_2.Tc_0,VAR_PERIOD_MIN_XTICS,VAR_FREQ_PWM_XTICS);
	set_predel_dshim24(&svgen_pwm24_2.Tc_1,VAR_PERIOD_MIN_XTICS,VAR_FREQ_PWM_XTICS);

//	if(((svgen_pwm24_2.Ta_0.Ti > (VAR_FREQ_PWM_XTICS - VAR_PERIOD_MIN_XTICS)) && (svgen_pwm24_2.Ta_0.Ti <= (VAR_FREQ_PWM_XTICS))) ||
//			((svgen_pwm24_2.Ta_1.Ti > (VAR_FREQ_PWM_XTICS - VAR_PERIOD_MIN_XTICS)) && (svgen_pwm24_2.Ta_1.Ti <= (VAR_FREQ_PWM_XTICS))) ||
//			((svgen_pwm24_2.Tb_0.Ti > (VAR_FREQ_PWM_XTICS - VAR_PERIOD_MIN_XTICS)) && (svgen_pwm24_2.Tb_0.Ti <= (VAR_FREQ_PWM_XTICS))) ||
//			((svgen_pwm24_2.Tb_1.Ti > (VAR_FREQ_PWM_XTICS - VAR_PERIOD_MIN_XTICS)) && (svgen_pwm24_2.Tb_1.Ti <= (VAR_FREQ_PWM_XTICS))) ||
//			((svgen_pwm24_2.Tc_0.Ti > (VAR_FREQ_PWM_XTICS - VAR_PERIOD_MIN_XTICS)) && (svgen_pwm24_2.Tc_0.Ti <= (VAR_FREQ_PWM_XTICS))) ||
//			((svgen_pwm24_2.Tc_1.Ti > (VAR_FREQ_PWM_XTICS - VAR_PERIOD_MIN_XTICS)) && (svgen_pwm24_2.Tc_1.Ti <= (VAR_FREQ_PWM_XTICS))))
//	{
//		asm(" NOP ");
//	}
//
//	if( ((svgen_pwm24_2.Ta_0.Ti > 0) && (svgen_pwm24_2.Ta_0.Ti < (VAR_PERIOD_MIN_XTICS))) ||
//		((svgen_pwm24_2.Ta_1.Ti > 0) && (svgen_pwm24_2.Ta_1.Ti < (VAR_PERIOD_MIN_XTICS))) ||
//		((svgen_pwm24_2.Tb_0.Ti > 0) && (svgen_pwm24_2.Tb_0.Ti < (VAR_PERIOD_MIN_XTICS))) ||
//		((svgen_pwm24_2.Tb_1.Ti > 0) && (svgen_pwm24_2.Tb_1.Ti < (VAR_PERIOD_MIN_XTICS))) ||
//		((svgen_pwm24_2.Tc_0.Ti > 0) && (svgen_pwm24_2.Tc_0.Ti < (VAR_PERIOD_MIN_XTICS))) ||
//		((svgen_pwm24_2.Tc_1.Ti > 0) && (svgen_pwm24_2.Tc_1.Ti < (VAR_PERIOD_MIN_XTICS))))
//	{
//		asm(" NOP ");
//	}
}


// #pragma CODE_SECTION(svgen_pwm24_calc,".v_24pwm_run");
void svgen_pwm24_calc(SVGEN_PWM24 *vt)
{
	_iq StepAngle;
	
	StepAngle = _IQmpy(vt->Freq,vt->FreqMax);
	
	vt->Alpha = vt->Alpha + StepAngle;
	
	
	if (vt->Alpha > CONST_IQ_2PI)
	{
		vt->Alpha -= CONST_IQ_2PI;
	}
	
	if (vt->Alpha < 0)
	{
		vt->Alpha += CONST_IQ_2PI;
	}


	calc_time_one_tk(vt->Gain, vt->Alpha, vt->delta_U,  vt->Ia,  vt->Ib, vt->Ic,
					 vt->number_svgen, &vt->Ta_0, &vt->Ta_1,&vt->Tb_0, &vt->Tb_1,&vt->Tc_0, &vt->Tc_1);



	vt->Ta_0.Ti = vt->Ta_0.Ti/vt->XilinxFreq;
	vt->Ta_1.Ti = vt->Ta_1.Ti/vt->XilinxFreq;
	
	vt->Tb_0.Ti = vt->Tb_0.Ti/vt->XilinxFreq;
	vt->Tb_1.Ti = vt->Tb_1.Ti/vt->XilinxFreq;
	
	vt->Tc_0.Ti = vt->Tc_0.Ti/vt->XilinxFreq;
	vt->Tc_1.Ti = vt->Tc_1.Ti/vt->XilinxFreq;
	
}


// #pragma CODE_SECTION(calc_time_one_tk,".v_24pwm_run");
void calc_time_one_tk(_iq gain, _iq teta, _iq delta_U,
					  _iq Ia, _iq Ib, _iq Ic,
					  unsigned int number, 
					  SVGEN_PWM24_TIME *tk0, 
					  SVGEN_PWM24_TIME *tk1,
					  SVGEN_PWM24_TIME *tk2, 
					  SVGEN_PWM24_TIME *tk3,
					  SVGEN_PWM24_TIME *tk4, 
					  SVGEN_PWM24_TIME *tk5)
{
	_iq iq_t0 = 0, iq_t1 = 0, iq_t2 = 0, iq_t3 = 0, iq_t4 = 0, iq_t5 = 0;
	_iq t_pos = 0, t_neg = 0;
	_iq delta_ttt;
	_iq teta60;
	
	int sector, region;
	int cur_sign, i, ki;
	int updown_tk0;
	int updown_tk1;

	volatile _iq t_tk0, t_tk1;
	
	updown_tk0 = 1;
	updown_tk1 = 0;
	
	sector = detect_sec(teta);	//
	teta60 = teta - _IQmpy(_IQ(sector - 1),CONST_IQ_PI3);	//
	
	if ((sector == 2) || (sector == 4) || (sector == 6))
	{
		teta60 = CONST_IQ_PI3 - teta60;
	}
	
	region = detect_region(gain, teta60); //
	
	calc_t_abc(gain, teta60, region, &iq_t0, &iq_t1, &iq_t2, &iq_t3, &iq_t4, &iq_t5);
	

	delta_ttt = 0; //calc_delta_t(delta_U,number,region);
	//delta_ttt = 0;

	//if (number == 1)
	//{
		//logpar.log1 = (int16)(_IQtoIQ15(delta_U));
		//logpar.log2 = (int16)(_IQtoIQ15(delta_ttt));
	//}
	//else
	//{
		//logpar.log3 = (int16)(_IQtoIQ15(delta_U));
		//logpar.log4 = (int16)(_IQtoIQ15(delta_ttt));
	//}
	
	calc_arr_tph(sector, region, iq_t0, iq_t1, iq_t2, iq_t3, iq_t4, iq_t5, delta_ttt,number, Ia, Ib, Ic);

	for (ki = 0; ki < 3; ki++)
	{
		t_pos = 0;
		t_neg = 0;
		
		for (i = 0; i < 7; i++)
		{
			cur_sign = ar_sa_all[ki][sector - 1][region - 1][i];
			
	 		if (cur_sign > 0)
	 		{
	 			t_pos += ar_tph[i];
			}

	 		if (cur_sign < 0)
	 		{
	 			t_neg += ar_tph[i];
			}
	 	}

		t_pos = t_pos << 1;
   
   		t_neg = t_neg << 1;

		if (t_neg == 0)
		{
			t_tk0 = 0;
		}
		else
		{
			t_tk0 = t_neg;
		}
	  
		if (t_pos == 0)
	 	{
	 		t_tk1 = CONST_IQ_1;
		}
		else
		{
	   		t_tk1 = CONST_IQ_1 - t_pos;
	   	}

		switch (ki)
		{
			case 0:
				tk0->up_or_down = updown_tk0;
				tk0->Ti = t_tk0;
				
				tk1->up_or_down = updown_tk1;
				tk1->Ti = t_tk1;

				break;
				
			case 1:
				tk2->up_or_down = updown_tk0;
				tk2->Ti = t_tk0;
				
				tk3->up_or_down = updown_tk1;
				tk3->Ti = t_tk1;

				break;
				
			case 2:
				tk4->up_or_down = updown_tk0;
				tk4->Ti = t_tk0;
				
				tk5->up_or_down = updown_tk1;
				tk5->Ti = t_tk1;

				break;
				
			default: break;
		}
	}
}




// #pragma CODE_SECTION(detect_region,".v_24pwm_run");
int detect_region(_iq k, _iq teta)
{
  volatile _iq x,y;
  volatile int reg=0;
   
  x = _IQmpy(k,_IQcos(teta));
  y = _IQmpy(k,_IQsin(teta));

  if (y>=CONST_IQ_05) reg=4;
	else if (y < (CONST_IQ_1 - _IQmpy(x,SQRT3))) reg = 1;
		else if (y < (_IQmpy(x,SQRT3) - CONST_IQ_1)) reg = 2;
			else reg = 3;

  return reg;
}




// #pragma CODE_SECTION(detect_sec,".v_24pwm_run");
int detect_sec(_iq teta)
{
	volatile _iq sector;
	volatile int sectorint;
	
	sector = _IQdiv(teta,CONST_IQ_PI3);
	sectorint = (sector >> 24) + 1;
	
	if (sectorint > 6) sectorint-=6;
	
	return sectorint;
}


#define nSIN_t(k,t)  _IQmpy(k,_IQsin(t))

#define nSIN_p3pt(k,t)  _IQmpy(k,_IQsin(CONST_IQ_PI3+t))

#define nSIN_p3mt(k,t)  _IQmpy(k,_IQsin(CONST_IQ_PI3-t))

#define nSIN_tmp3(k,t)  _IQmpy(k,_IQsin(t-CONST_IQ_PI3))

//k - (Uzad)
//teta -
//region - 
/*
 * iq_tt0 - time of vectors op, oo, on
 * iq_tt1 - time of vectors ap, an
 * iq_tt2 - time of vectors bp, bn
 * iq_tt3 - time of vector c
 * iq_tt4 - time of vector a
 * iq_tt5 - time of vector b
 */
// #pragma CODE_SECTION(calc_t_abc,".v_24pwm_run");
void calc_t_abc(_iq k, _iq teta, int region, _iq *iq_tt0, _iq *iq_tt1, _iq *iq_tt2, _iq *iq_tt3, _iq *iq_tt4, _iq *iq_tt5)
{
	switch(region)
	{
		case 1 :
			*iq_tt0 = CONST_IQ_05 - nSIN_p3pt(k,teta);
			*iq_tt1 = nSIN_p3mt(k,teta);
			*iq_tt2 = nSIN_t(k,teta);
			*iq_tt3 = 0;
			*iq_tt4 = 0;
			*iq_tt5 = 0;
			break;

		case 2 :
			*iq_tt0 = 0;
			*iq_tt1 = CONST_IQ_1 - nSIN_p3pt(k,teta);
			*iq_tt2 = 0;
			*iq_tt3 = nSIN_t(k,teta);
			*iq_tt4 = nSIN_p3mt(k,teta) - CONST_IQ_05;
			*iq_tt5 = 0;
			break;

		case 3 :
			*iq_tt0 = 0;
			*iq_tt1 = CONST_IQ_05 - nSIN_t(k,teta);
			*iq_tt2 = CONST_IQ_05 - nSIN_p3mt(k,teta);
			*iq_tt3 = nSIN_p3pt(k,teta) - CONST_IQ_05;
			*iq_tt4 = 0;
			*iq_tt5 = 0;
			break;

		case 4 :
			*iq_tt0 = 0;
			*iq_tt1 = 0;
			*iq_tt2 = CONST_IQ_1 - nSIN_p3pt(k,teta);
			*iq_tt3 = nSIN_p3mt(k,teta);
			*iq_tt4 = 0;
			*iq_tt5 = nSIN_t(k,teta) - CONST_IQ_05;
			break;

		default :
			*iq_tt0 = 0;
			*iq_tt1 = 0;
			*iq_tt2 = 0;
			*iq_tt3 = 0;
			*iq_tt4 = 0;
			*iq_tt5 = 0;
			break;
	}

    return;
} 

//sector 
//region 
//iq_ttt0 - iq_ttt5 - times from calc_t_abs
//delta_t - 
//number_sv - 
//iqIa, iqIb, iqIc 
// #pragma CODE_SECTION(calc_arr_tph, ".v_24pwm_run");
void calc_arr_tph(int sector,int region, _iq iq_ttt0, _iq iq_ttt1, _iq iq_ttt2, _iq iq_ttt3, _iq iq_ttt4,
				  _iq iq_ttt5, _iq delta_t, unsigned int number_sv,
				  _iq iqIa, _iq iqIb, _iq iqIc)
{
	_iq iq_var1 = 0;
	_iq iqIx, iqIy, iqIz;
	_iq iq_alfa_1_p = CONST_IQ_05, iq_alfa_1_n = CONST_IQ_05, iq_alfa_2_n = CONST_IQ_05, iq_alfa_2_p = CONST_IQ_05;
	_iq iq_alfa = 0;
//	_iq iqIa, iqIb, iqIc;
	_iq iq_mpy1 = 0;
	_iq iq_mpy3 = 0;
	_iq summ = 0;
	

  	switch (sector)
  	{
		case 1:
				iqIx = iqIc;
				iqIy = iqIa;
				iqIz = iqIb;
		
				break;
		case 2:

				iqIx = iqIb;
				iqIy = iqIa;
				iqIz = iqIc;

				break;
		case 3:

				iqIx = iqIb;
				iqIy = iqIc;
				iqIz = iqIa;

				break;
		case 4:

				iqIx = iqIa;
				iqIy = iqIc;
				iqIz = iqIb;

				break;
		case 5:

				iqIx = iqIa;
				iqIy = iqIb;
				iqIz = iqIc;

				break;
		case 6:

				iqIx = iqIc;
				iqIy = iqIb;
				iqIz = iqIa;

				break;
		default:

				iqIx = 0;
				iqIy = 0;
				iqIz = 0;

				break;
	}

	if (region == 1)
	{
//		if (delta_t != 0)	//разбаланс напряжений	//отключено тестировать
//		{
//			iq_alfa = _IQsat((CONST_IQ_05 - _IQmpy(delta_t,IQ_KP_DELTA_T)),IQ_ALFA_SATURATION1,IQ_ALFA_SATURATION2);

			//if (delta_t < 0)
			//{
				//iq_alfa = IQ_ALFA_SATURATION1;
			//}
			//else
			//{
				//iq_alfa = IQ_ALFA_SATURATION2;
			//}
//		}
//		else
		{
			iq_alfa = CONST_IQ_05;
		}
	}
	else
	{
		iq_mpy1 = _IQmpy(_IQabs(iqIx),iq_ttt1)+_IQmpy(_IQabs(iqIy),iq_ttt2);
		iq_mpy3 = _IQmpy(iqIz,iq_ttt3);

		summ = _IQdiv((iq_mpy3),(iq_mpy1));

		//iq_alfa = _IQsat((_IQmpy(CONST_IQ_05,(CONST_IQ_1 + summ)) - _IQmpy(delta_t,IQ_KP_DELTA_T)),IQ_ALFA_SATURATION1,IQ_ALFA_SATURATION2);
		iq_alfa = CONST_IQ_05;	//test
	}

			
	if (iqIx >= 0)
	{
		iq_alfa_1_p = iq_alfa;
		iq_alfa_1_n = CONST_IQ_1 - iq_alfa;
	}
	else
	{
		iq_alfa_1_p = CONST_IQ_1 - iq_alfa;
		iq_alfa_1_n = iq_alfa;
	}

	if (iqIy >= 0)
	{
		iq_alfa_2_p = CONST_IQ_1 - iq_alfa;
		iq_alfa_2_n = iq_alfa;
	}
	else
	{
		iq_alfa_2_p = iq_alfa;
		iq_alfa_2_n = CONST_IQ_1 - iq_alfa;
	}
			
		
	//if (number_sv == 2)
	//{
		//logpar.log1 = (int16)(sector);
		//logpar.log2 = (int16)(region);
		//logpar.log3 = (int16)(_IQtoIQ15(iq_alfa));
		//logpar.log4 = (int16)(_IQtoIQ15(iq_alfa_1_p));
		//logpar.log5 = (int16)(_IQtoIQ15(iq_alfa_2_p));
		//logpar.log6 = (int16)(_IQtoIQ13(summ));
		//logpar.log3 = (int16)(_IQtoIQ14(iq_ttt0));
		//logpar.log4 = (int16)(_IQtoIQ14(iq_ttt1));
		//logpar.log5 = (int16)(_IQtoIQ14(iq_ttt2));
		//logpar.log6 = (int16)(_IQtoIQ14(iq_ttt3));
		//logpar.log7 = (int16)(_IQtoIQ14(iq_ttt4));
		//logpar.log8 = (int16)(_IQtoIQ14(iq_ttt5));
		//logpar.log10 = (int16)(_IQtoIQ15(delta_t1_struct.Up));
		//logpar.log11 = (int16)(_IQtoIQ15(delta_t1_struct.Ui));
		//logpar.log12 = (int16)(_IQtoIQ15(delta_t1_struct.Ud));
		//logpar.log13 = (int16)(_IQtoIQ15(iqIx));
		//logpar.log14 = (int16)(_IQtoIQ15(iqIy));
		//logpar.log15 = (int16)(_IQtoIQ15(iqIz));

		//logpar.log12 = (int16)(_IQtoIQ15(_IQmpy(iq_alfa_2_p,iq_ttt2)));
		//logpar.log13 = (int16)(_IQtoIQ15(_IQmpy(iq_alfa_2_n,iq_ttt2)));
		//logpar.log14 = (int16)(_IQtoIQ15(delta_t));
	//}
	//else
			//logpar.log15 = (int16)(_IQtoIQ15(delta_t));
	
//	if (region == 1)
//	{
//		if (f.Rele3 == 1)
//		{
//			iq_alfa_1_p = CONST_IQ_05;
//			iq_alfa_2_p = CONST_IQ_05;
//			iq_alfa_1_n = CONST_IQ_05;
//			iq_alfa_2_n = CONST_IQ_05;
//		}
//	}
//	else
//	{
//		if (f.Down50 == 1)
//		{
//			iq_alfa_1_p = CONST_IQ_05;
//			iq_alfa_2_p = CONST_IQ_05;
//			iq_alfa_1_n = CONST_IQ_05;
//			iq_alfa_2_n = CONST_IQ_05;
//		}
//	}

	switch (region)
	{
		case 1: 
				iq_var1 = _IQdiv(iq_ttt0,CONST_IQ_3);

				ar_tph[0] = iq_var1;
				ar_tph[1] = _IQmpy(iq_alfa_1_n,iq_ttt1);
				ar_tph[2] = _IQmpy(iq_alfa_2_n,iq_ttt2);
				ar_tph[3] = iq_var1;
				ar_tph[4] = _IQmpy(iq_alfa_1_p,iq_ttt1);
				ar_tph[5] = _IQmpy(iq_alfa_2_p,iq_ttt2);
				ar_tph[6] = iq_var1;
				break;

		case 2:
				ar_tph[0] = _IQmpy(iq_alfa_1_n,iq_ttt1);
				ar_tph[1] = iq_ttt4;
				ar_tph[2] = iq_ttt3;
				ar_tph[3] = _IQmpy(iq_alfa_1_p,iq_ttt1);
				ar_tph[4] = 0;
				ar_tph[5] = 0;
				ar_tph[6] = 0;											
				break;

		case 3: 
				ar_tph[0] = _IQmpy(iq_alfa_1_n,iq_ttt1);
				ar_tph[1] = _IQmpy(iq_alfa_2_n,iq_ttt2);
				ar_tph[2] = iq_ttt3;
				ar_tph[3] = _IQmpy(iq_alfa_1_p,iq_ttt1);
				ar_tph[4] = _IQmpy(iq_alfa_2_p,iq_ttt2);
				ar_tph[5] = 0;
				ar_tph[6] = 0;											
				break;

		case 4: 
				ar_tph[0] = _IQmpy(iq_alfa_2_n,iq_ttt2);
				ar_tph[1] = iq_ttt3;
				ar_tph[2] = iq_ttt5;
				ar_tph[3] = _IQmpy(iq_alfa_2_p,iq_ttt2);
				ar_tph[4] = 0;
				ar_tph[5] = 0;
				ar_tph[6] = 0;
				break;

		default :
				break;
	}


}

/*
 // Function is commented because of in project 222220 should not be large voltage diviation
// #pragma CODE_SECTION(calc_delta_t,".v_24pwm_run");
_iq calc_delta_t(_iq delta_1, unsigned int number,int region)
{
	if(_IQabs(delta_1) > MAX_LEVEL_DELTA_T)
	{
		// Ошибка разбаланса          ConvErrors.m2.bit.Razbalans |= 1;
		return 0;
	}
	
	if (number == 1)
	{
		delta_t1_struct.Fdb = delta_1;
		delta_t1_struct.calc(&delta_t1_struct);
		
		if (_IQabs(delta_t1_struct.Out) <= INSENSITIVE_LEVEL_DELTA_T) return 0;
		else return delta_t1_struct.Out;
	}
	else
	{
		delta_t2_struct.Fdb = delta_1;
		delta_t2_struct.calc(&delta_t2_struct);
		
		if (_IQabs(delta_t2_struct.Out) <= INSENSITIVE_LEVEL_DELTA_T) return 0;
		else return delta_t2_struct.Out;
	}
}

*/
//#pragma CODE_SECTION(set_predel_dshim24,".fast_run2");

// #pragma CODE_SECTION(set_predel_dshim24_simple0,".v_24pwm_run");
void set_predel_dshim24_simple0(SVGEN_PWM24_TIME *T,int16 dmin,int16 dmax)
{
    dmax++; //Надо задать значение больше, чем макс значение пилы, чтобы небыло пересечений
    if (T->Ti < dmin)
    {
        if (T->Ti < dmin/2)
         T->Ti = 0;
        else
         T->Ti = dmin;

    } else if (T->Ti >= (dmax - dmin)) {
        T->Ti = (dmax - dmin);
    }
}
// #pragma CODE_SECTION(set_predel_dshim24_simple1,".v_24pwm_run");
void set_predel_dshim24_simple1(SVGEN_PWM24_TIME *T,int16 dmin,int16 dmax)
{
    dmax++; //Надо задать значение больше, чем макс значение пилы, чтобы небыло пересечений
    if (T->Ti >= (dmax - dmin))
    {
        if (T->Ti >= (dmax - dmin/2))
            T->Ti = dmax;
        else
            T->Ti = dmax-dmin;
//        T->Ti = dmax;
    } else if (T->Ti <= dmin) {
        T->Ti = dmin;
    }
}


// #pragma CODE_SECTION(set_predel_dshim24,".v_24pwm_run");
void set_predel_dshim24(SVGEN_PWM24_TIME *T,int16 dmin,int16 dmax)
{
	//static unsigned int counter_pass = 0;
	//static unsigned int summ = 0;
	//int16 dshim24 = 0;
	dmax++;	//Надо задать значение больше, чем макс значение пилы, чтобы небыло пересечений
	if (T->Ti < dmin)
	{
		T->impuls_lenght_max = 0;
		T->counter_pass_max = 0;
		
		T->impuls_lenght_min = T->impuls_lenght_min + T->Ti;
		T->counter_pass_min++;
		
		if (T->counter_pass_min <= 3)
		{
			if (T->impuls_lenght_min <= dmin)
			{
				T->Ti = 0;
			}
			else
			{
//				T->Ti = dmin; //T->impuls_lenght_min;
//				T->impuls_lenght_min -= dmin;// = 0;
				T->Ti = T->impuls_lenght_min;
				T->impuls_lenght_min = 0;
				T->counter_pass_min = 0;
//				if (T->impuls_lenght_min < 0) {
//				    T->counter_pass_min = 0;
//				    T->impuls_lenght_min = 0;
//				} else {
//				    T->counter_pass_min -= 1;
//				}
			}
	   	}
		else
		{
			T->counter_pass_min = 1;
			T->impuls_lenght_min = T->Ti;
			T->Ti = 0;
		}
	}
	else
	{
		T->impuls_lenght_min = 0;
		T->counter_pass_min = 0;

//		if (T->Ti > (dmax - dmin))
//	   	{
//			dshim = dmax;
//		}
//		else
//		{
//			dshim = T->Ti;
//		}

		if (T->Ti >= (dmax - dmin))
		{
			T->impuls_lenght_max = T->impuls_lenght_max + (dmax - T->Ti - 1);
			T->counter_pass_max++;

			if (T->counter_pass_max <= 3)
			{
				if (T->impuls_lenght_max <= dmin)
				{
					T->Ti = dmax;
				}
				else
				{
//					T->Ti = dmax - dmin; //T->impuls_lenght_max;
//					T->impuls_lenght_max -= dmin;// = 0;
					T->Ti = dmax - T->impuls_lenght_max;
					T->impuls_lenght_max = 0;
					T->counter_pass_max = 0;
//					if (T->impuls_lenght_max < 0) {
//					    T->impuls_lenght_max = 0;
//					    T->counter_pass_max = 0;
//					} else {
//					    T->counter_pass_max -= 1;
//					}
				}
		   	}
			else
	   		{
				T->counter_pass_max = 1;
				T->impuls_lenght_max = dmax - T->Ti;
				T->Ti = dmax;
			}
		}
		else
		{	
			T->counter_pass_max = 0;
			T->impuls_lenght_max = 0;
		}
	}
	
	//return dshim24;
}



void init_alpha_pwm24(int xFreq)
{
	xFreq = xFreq + 1;

	svgen_pwm24_1.number_svgen = 1;
	svgen_pwm24_2.number_svgen = 2;

	//pidCur_Kp = _IQ(PID_KP_DELTA_KOMP_I);
	//pidCur_Ki = _IQ(PID_KI_DELTA_KOMP_I);

//	svgen_pwm24_1.prev_level = V_PWM24_PREV_PWM_CLOSE;
	svgen_pwm24_1.saw_direct.all = xpwm_time.saw_direct.all & 0x3f;
	svgen_pwm24_1.Tclosed_saw_direct_0 = xpwm_time.Tclosed_saw_direct_0;//  xpwm_time.Tclosed_high;//var_freq_pwm_xtics + 1;
	svgen_pwm24_1.Tclosed_saw_direct_1 = xpwm_time.Tclosed_saw_direct_1;
	svgen_pwm24_1.Tclosed_high = xpwm_time.Tclosed_high;

//	svgen_pwm24_2.prev_level = V_PWM24_PREV_PWM_CLOSE;
	svgen_pwm24_2.saw_direct.all = (xpwm_time.saw_direct.all >> 6) & 0x3f;
	svgen_pwm24_2.Tclosed_saw_direct_0 = xpwm_time.Tclosed_saw_direct_0;//  xpwm_time.Tclosed_high;//var_freq_pwm_xtics + 1;
	svgen_pwm24_2.Tclosed_saw_direct_1 = xpwm_time.Tclosed_saw_direct_1;
	svgen_pwm24_2.Tclosed_high = xpwm_time.Tclosed_high;
	
	svgen_pwm24_1.FreqMax = _IQ(2.0*PI*F_STATOR_MAX/FREQ_PWM/2);//Допполнительное деление на 2, т.к. 2 раза заходит в прерывание за период шим
 	svgen_pwm24_2.FreqMax = _IQ(2.0*PI*F_STATOR_MAX/FREQ_PWM/2);
 	
 	svgen_pwm24_1.XilinxFreq = CONST_IQ_1/xFreq;
	svgen_pwm24_2.XilinxFreq = CONST_IQ_1/xFreq;
	
	// 30 
    svgen_pwm24_1.Alpha = 0;	//winding_displacement;
	svgen_pwm24_2.Alpha = -winding_displacement;

	svgen_pwm24_1.delta_t = 0;
	svgen_pwm24_2.delta_t = 0;
}

/*
void init_freq_array(void)
{
	unsigned int i = 0;
	//unsigned int j = 0;
	int var1 = 0;

	var1 = 32767 / (FREQ_PWM_MAX - FREQ_PWM_MIN);

	for (i = 0; i < COUNT_VAR_FREQ; i++)
	{
		//j = rand() / 1023;
		//freq_array[i] = array_optim_freq[j];
		//do
			freq_array[i] = FREQ_PWM_MIN + (rand() / var1);
		//while ((freq_array[i] < 945) && (freq_array[i] > 930));
	}

	//freq_array[0] = 991;
	//freq_array[1] = 1430;
}
*/


//#pragma CODE_SECTION(calc_freq_pwm,".v_24pwm_run");
//#pragma CODE_SECTION(calc_freq_pwm,".fast_run");
/*void calc_freq_pwm()
{
	static int prev_freq_pwm = 0;
	static float pwm_period = 0;
	static float var0 = 0;
	//static int line = 0;
	//static int i = 0;
	static unsigned int proc_ticks = 1;
	int var1 = 0;
	//static int i = 0;

	if ((f.flag_change_pwm_freq == 1) && (f.flag_random_freq == 1))
	{	
		if (proc_ticks >= 1)
		{
			proc_ticks = 0;
			
			
			if (line == 0)
			{
				VAR_FREQ_PWM_HZ = VAR_FREQ_PWM_HZ + 1;
				if (VAR_FREQ_PWM_HZ > FREQ_PWM_MAX)
				{
					VAR_FREQ_PWM_HZ = FREQ_PWM_MAX;
					line = 1;
				}
			}
			else
			{	
				VAR_FREQ_PWM_HZ = VAR_FREQ_PWM_HZ - 1;
				if (VAR_FREQ_PWM_HZ < FREQ_PWM)
				{
					VAR_FREQ_PWM_HZ = FREQ_PWM;
					line = 0;
				}
			}
			



			// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
			//VAR_FREQ_PWM_HZ = freq_array[i];
			//i_led2_on_off(1);

			var1 = 32767 / (freq_pwm_max_hz - freq_pwm_min_hz);
			VAR_FREQ_PWM_HZ = freq_pwm_min_hz + (rand() / var1);

			//i_led2_on_off(0);
			// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

			if (VAR_FREQ_PWM_HZ > freq_pwm_max_hz)
			{
				VAR_FREQ_PWM_HZ = freq_pwm_max_hz;
			}
			else
			{
				if (VAR_FREQ_PWM_HZ < freq_pwm_min_hz)
				{
					VAR_FREQ_PWM_HZ = freq_pwm_min_hz;
				}
			}
			//i++;

			//if (i >= COUNT_VAR_FREQ)
			//{
				//i = 0;
			//}
			
		}
		
		// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
		//if (VAR_FREQ_PWM_HZ == FREQ_PWM_MIN)
		//{
			//VAR_FREQ_PWM_HZ = FREQ_PWM_MAX;
		//}
		//else
		//{
			//VAR_FREQ_PWM_HZ = FREQ_PWM_MIN;
		//}

		//if (f.Rele1 == 1)
		//{
			//if (i == 0)
			//{
				//VAR_FREQ_PWM_HZ = 1192;;
				//i = 1;
			//}
			//else
			//{
				//VAR_FREQ_PWM_HZ = 792;
			//}
		//}
		//else
		//{
			//i = 0;
			//VAR_FREQ_PWM_HZ = 1192;
		//}
		// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
	}
	//else
	//{
		//VAR_FREQ_PWM_HZ = FREQ_PWM;
	//}

	
	if (prev_freq_pwm != VAR_FREQ_PWM_HZ)
	{
		prev_freq_pwm = VAR_FREQ_PWM_HZ;
		FREQ_MAX = _IQ(2.0*PI*F_STATOR_MAX/VAR_FREQ_PWM_HZ);
	
		var0 = (float)VAR_FREQ_PWM_HZ;
		//pwm_period = ((float64)HSPCLK) / ((float64)VAR_FREQ_PWM_HZ);
	
		pwm_period = HSPCLK / var0;
		
		pwm_period = pwm_period / 2.0;
	
		FREQ_PWM_XTICS = ((int) pwm_period) >> 3;
		
		XILINX_FREQ = 16777216/(FREQ_PWM_XTICS + 1);

		FLAG_CHANGE_FREQ_PWM = 1;
	}

	proc_ticks++;
}
*/

void change_freq_pwm(_iq xFreq) {
	svgen_pwm24_1.FreqMax = _IQ(2.0*PI*F_STATOR_MAX/3750000 / xFreq / 2 /2);//Допполнительное деление на 2, т.к. 2 раза заходит в прерывание за период шим
	svgen_pwm24_2.FreqMax = _IQ(2.0*PI*F_STATOR_MAX/ 3750000 / xFreq / 2 /2);

	xFreq += 1;
	svgen_pwm24_1.XilinxFreq = CONST_IQ_1/xFreq;
	svgen_pwm24_2.XilinxFreq = CONST_IQ_1/xFreq;
}

// #pragma CODE_SECTION(test_calc_pwm24_dq,".v_24pwm_run");
void test_calc_pwm24_dq(_iq U_zad1, _iq U_zad2,_iq teta)
{
	svgen_pwm24_1.Freq = 0;
	svgen_pwm24_2.Freq = 0;
	
	svgen_pwm24_1.Gain = U_zad1;
	svgen_pwm24_2.Gain = U_zad2;

	svgen_pwm24_1.Alpha = teta;
	svgen_pwm24_2.Alpha = teta - winding_displacement;
	
	svgen_pwm24_1.delta_U = filter.iqU_1_fast - filter.iqU_2_fast;
	svgen_pwm24_2.delta_U = filter.iqU_3_fast - filter.iqU_4_fast;
	
	// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
	svgen_pwm24_1.delta_U = 0;
	svgen_pwm24_2.delta_U = 0;
	// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

	svgen_pwm24_1.Ia = analog.iqIa1_1;
	svgen_pwm24_1.Ib = analog.iqIb1_1;
	svgen_pwm24_1.Ic = analog.iqIc1_1;

	svgen_pwm24_2.Ia = analog.iqIa2_1;
	svgen_pwm24_2.Ib = analog.iqIb2_1;
	svgen_pwm24_2.Ic = analog.iqIc2_1;

	svgen_pwm24_1.calc_dq(&svgen_pwm24_1);
	svgen_pwm24_2.calc_dq(&svgen_pwm24_2);

	
	// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

	set_predel_dshim24(&svgen_pwm24_1.Ta_0,VAR_PERIOD_MIN_XTICS,VAR_FREQ_PWM_XTICS);
	set_predel_dshim24(&svgen_pwm24_1.Ta_1,VAR_PERIOD_MIN_XTICS,VAR_FREQ_PWM_XTICS);
	
	set_predel_dshim24(&svgen_pwm24_1.Tb_0,VAR_PERIOD_MIN_XTICS,VAR_FREQ_PWM_XTICS);
	set_predel_dshim24(&svgen_pwm24_1.Tb_1,VAR_PERIOD_MIN_XTICS,VAR_FREQ_PWM_XTICS);
	
	set_predel_dshim24(&svgen_pwm24_1.Tc_0,VAR_PERIOD_MIN_XTICS,VAR_FREQ_PWM_XTICS);
	set_predel_dshim24(&svgen_pwm24_1.Tc_1,VAR_PERIOD_MIN_XTICS,VAR_FREQ_PWM_XTICS);

	set_predel_dshim24(&svgen_pwm24_2.Ta_0,VAR_PERIOD_MIN_XTICS,VAR_FREQ_PWM_XTICS);
	set_predel_dshim24(&svgen_pwm24_2.Ta_1,VAR_PERIOD_MIN_XTICS,VAR_FREQ_PWM_XTICS);

	set_predel_dshim24(&svgen_pwm24_2.Tb_0,VAR_PERIOD_MIN_XTICS,VAR_FREQ_PWM_XTICS);
	set_predel_dshim24(&svgen_pwm24_2.Tb_1,VAR_PERIOD_MIN_XTICS,VAR_FREQ_PWM_XTICS);

	set_predel_dshim24(&svgen_pwm24_2.Tc_0,VAR_PERIOD_MIN_XTICS,VAR_FREQ_PWM_XTICS);
	set_predel_dshim24(&svgen_pwm24_2.Tc_1,VAR_PERIOD_MIN_XTICS,VAR_FREQ_PWM_XTICS);


}


// #pragma CODE_SECTION(svgen_pwm24_calc,".v_24pwm_run");
void svgen_pwm24_calc_dq(SVGEN_PWM24 *vt)
{
	
	if (vt->Alpha > CONST_IQ_2PI)
	{
		vt->Alpha -= CONST_IQ_2PI;
	}
	
	if (vt->Alpha < 0)
	{
		vt->Alpha += CONST_IQ_2PI;
	}


	calc_time_one_tk(vt->Gain, vt->Alpha, vt->delta_U,  vt->Ia,  vt->Ib, vt->Ic,
					 vt->number_svgen, &vt->Ta_0, &vt->Ta_1,&vt->Tb_0, &vt->Tb_1,&vt->Tc_0, &vt->Tc_1);
	
	
	vt->Ta_0.Ti = vt->Ta_0.Ti/vt->XilinxFreq;
	vt->Ta_1.Ti = vt->Ta_1.Ti/vt->XilinxFreq;
	
	vt->Tb_0.Ti = vt->Tb_0.Ti/vt->XilinxFreq;
	vt->Tb_1.Ti = vt->Tb_1.Ti/vt->XilinxFreq;
	
	vt->Tc_0.Ti = vt->Tc_0.Ti/vt->XilinxFreq;
	vt->Tc_1.Ti = vt->Tc_1.Ti/vt->XilinxFreq;

}

void svgen_set_time_keys_closed(SVGEN_PWM24 *vt)
{
	vt->Ta_0.Ti = VAR_FREQ_PWM_XTICS + 1;
	vt->Ta_1.Ti = 0;

	vt->Tb_0.Ti = VAR_FREQ_PWM_XTICS + 1;
	vt->Tb_1.Ti = 0;

	vt->Tc_0.Ti = VAR_FREQ_PWM_XTICS + 1;
	vt->Tc_1.Ti = 0;
}

void svgen_set_time_middle_keys_open(SVGEN_PWM24 *vt)
{
	vt->Ta_0.Ti = 0;
	vt->Ta_1.Ti = VAR_FREQ_PWM_XTICS + 1;

	vt->Tb_0.Ti = 0;
	vt->Tb_1.Ti = VAR_FREQ_PWM_XTICS + 1;

	vt->Tc_0.Ti = 0;
	vt->Tc_1.Ti = VAR_FREQ_PWM_XTICS + 1;
}
