#include "DSP2833x_Project.h"
#include <stdio.h>
#include <string.h>
#include <math.h>

#if (CPU_FRQ_150MHZ)
  #define CPU_CLK   150e6
#endif
#if (CPU_FRQ_100MHZ)
  #define CPU_CLK   100e6
#endif

// ADC start parameters
#define ADC_MODCLK 0x3 // HSPCLK = SYSCLKOUT/2*ADC_MODCLK2 = 150/(2*3)   = 25.0 MHz
#define ADC_CKPS   0x0   // ADC module clock = HSPCLK/1      = 25.5MHz/(1)   = 25.0 MHz
#define ADC_SHCLK  0x1   // S/H width in ADC module periods                  = 2 ADC cycle
#define BUF_SIZE   25     // Sample buffer size


#define PWM_CLK   100e3                //频率设置
#define SP        CPU_CLK/(2* PWM_CLK)//配置EPWM1的周期值，对应EPwm.c中TBPRD
#define SP2       CPU_CLK/(2* PWM_CLK)   //配置采样频率
// Up-down cnt, timebase = SYSCLKOUT
#define lag 0x000E
#define lead 0x200E
#define phase_shift_cycle 750			//PWM周期常数

//-------------------------------中断函数声明--------------------------//
interrupt void Phase_shift(void);		//内移相移相中断函数
interrupt void ISRTimer0(void); 	//声明定时器0的周期中断函数
//-------------------------------中断函数声明--------------------------//

//-------------------------------子函数声明---------------------------//
void PID_initial(void);				//声明PID初始化函数
void StartEPwm(void);				//声明PWM初始化函数
//-------------------------------子函数声明---------------------------//


double kstep=15;						//内移相速度常数

float Security = 0;//安全标志位

//------------------------------PID相关参数定义-----------------------//
float Vin = 400;                         //输入电压
float v_ref_o = 11;						//端口2参考电压
//float v_ref_o_2 = 10;                   //端口3参考电压
float P3_ref = -121;					    //端口3输出功率
float refresh = 0;						//当refresh = 5e5，向上位机发送数据

//ADC采样变量，中断配置 采样保护
float v_adc_o;                      //采样直接输出值
float v_adc_o2;                      //采样直接输出值2
float v_adc_o3;						//
float i_adc_o;                      //采样直接输出值
float i_adc_o2;                      //采样直接输出值2
float i_adc_o3;
float P1 = 0;
float P2 = 0;
float P3 = 0;

//PI参数 误差
float error2 = 0;//error(n-2)
float error1 = 0;//error(n-1)
float ierror = 0;//error(n)
float PID_OUT = 0;
float error2_2 = 0;//error(n-2)
float error1_2 = 0;//error(n-1)
float ierror_2 = 0;//error(n)
float PID_OUT_2 = 0;
float Ki;
float Kp;
float Kd;
float Ki_2;
float Kp_2;
float Kd_2;
float error_sum = 0;//位置式

//PWM比较周期定义
float PWM1_CC_Duty = 0;
float PWM2_CC_Duty = -0.007;
float PWM2_CC_Duty0 = -0.007;
float PWM2_CC_Duty1 = 0;
//------------------------------PID相关参数定义-----------------//

//-----------------------------SCI接收相关参数定义---------------//
Uint16 array_pointer=0;                                     //定义数组指针
Uint16 sum1=0,sum2=0,sum=0;         //定义数据之和(sum1)以及和校验(sum2)
Uint16 serial_flag=0;                                      //定义串口检测标志位
Uint16 buf_size=0;                                         //定义数据长度
Uint16 receive_buf[BUF_SIZE];                                   //定义存储数组
Uint16 istep=600;						//内移相启动常数
Uint16 a1=0;
Uint16 a2=0;
Uint16 a3=0;
Uint16 a4=0;
Uint16 Pid_start = 0;
Uint16 i;
Uint16 array_index = 0;             //采样数组



extern Uint16 secureRamFuncs_loadstart;
extern Uint16 secureRamFuncs_loadsize;
extern Uint16 secureRamFuncs_runstart;              //烧录Flash

void main(void)
{
   InitSysCtrl();
   asm(" RPT #8 || NOP");
   InitGpio();
   InitScibGpio();
   InitPieCtrl();
   DINT;

   //------------------------烧录Flash---------------------------------//
   // Section secureRamFuncs contains user defined code that runs from CSM secured RAM
       memcpy(&secureRamFuncs_runstart, &secureRamFuncs_loadstart, (Uint32)&secureRamFuncs_loadsize);
       InitFlash();                      // Initialize the Flash
   //------------------------烧录Flash---------------------------------//

   EALLOW;
         SysCtrlRegs.HISPCP.all = ADC_MODCLK;  // HSPCLK = SYSCLKOUT/ADC_MODCLK
   EDIS;

   IER = 0x0000;        //清除CPU中断使能位
   IFR = 0x0000;        //清除CPU中断标志位
   InitPieVectTable();
   asm(" RPT #8 || NOP");    //延时
   DINT;

   EALLOW;
//-----------------中断函数地址-------------//
   PieVectTable.TINT0 = &ISRTimer0;
   PieVectTable.EPWM1_INT=&Phase_shift;//内移相中断函数定义
//-----------------中断函数地址-------------//

   InitCpuTimers();   // For this example, only initialize the Cpu Timers
   ConfigCpuTimer(&CpuTimer0, 150, 10);//设置CPU定时器0
   StopCpuTimer0();

//------------------------ePWM GPIO口配置---------------//
   InitEPwm1Gpio();
   InitEPwm2Gpio();
   InitEPwm3Gpio();
   InitEPwm4Gpio();
   InitEPwm5Gpio();
   InitEPwm6Gpio();
//------------------------ePWM GPIO口配置---------------//
   EDIS;
//------------------------EPWM初始化配置
   DELAY_US(6e6);         //延时6s

   StartEPwm();

   EALLOW;

   SysCtrlRegs.PCLKCR1.bit.EPWM2ENCLK=0;				//关闭PWM2，3的时钟
   SysCtrlRegs.PCLKCR1.bit.EPWM3ENCLK=0;
   SysCtrlRegs.PCLKCR1.bit.EPWM5ENCLK=0;				//关闭PWM2，3的时钟
   SysCtrlRegs.PCLKCR1.bit.EPWM6ENCLK=0;

   EDIS;
//...................................内移相中断初始化------------------------
   EPwm1Regs.ETSEL.bit.INTSEL=2;         //TBCTR = TBPRD 触发中断
   EPwm1Regs.ETPS.bit.INTPRD=1;          //一次TBCTR = TBPRD事件触发一次中断
   EPwm1Regs.ETCLR.bit.INT=1;           //清除中断标志位
//-------------------------ADC初始化配置

   InitAdc();

//--------------------------使能PIE中断------------------------------------

   PieCtrlRegs.PIEIER1.bit.INTx7 = 1;   //定时器0中断，包括采样数据和PID
   PieCtrlRegs.PIEIER3.bit.INTx1 = 1; //使能内移相中断

//--------------------------使能CPU第1组中断--------------------------------

   IER |= M_INT1;
   IER |= M_INT3;       //使能CPU第三组中断

//------------------------------------------------------------------------

   EINT;   // 使能全局中断
   ERTM;   // 使能全局实时中断
   EPwm1Regs.ETSEL.bit.INTEN=1;             //使能Epwm1中断

//-------------------------------配置PID参数---------------------------------------------
     PID_initial();
//--------------------------------------------------------------------------------------

     SysCtrlRegs.PCLKCR3.bit.CPUTIMER0ENCLK=1;

//------------------------------进入主循环--------------------------------------------
  for(;;)
  {


  }
}

//------------------------------------------PID定义----------------------------------------//
void PID_initial()
{
    Kp = 0;//150震荡
    Ki = 30;//10数量级震荡
    Kd = 0;
    Kp_2 = 0;//150震荡
    Ki_2 = 0.3;//10数量级震荡
    Kd_2 = 0;
}
//------------------------------------------PID定义----------------------------------------//

//------------------------------------------PWM定义----------------------------------------//
void StartEPwm()
{
	 //后面转移到EPWM.c
	    EALLOW;
	    SysCtrlRegs.PCLKCR0.bit.TBCLKSYNC = 0;
	    //时钟配置，先置0，配置好EPWM后置为1
	    //先配置副边驱动PWM2，3，5，6   后配置PWM1，4
	    //--------------------------------------PWM2----------------------------------------------//
	        EPwm2Regs.TBSTS.all=0;
	        EPwm2Regs.TBPHS.half.TBPHS=0;
	        EPwm2Regs.TBCTR=0;

	        EPwm2Regs.CMPCTL.all=0x50;        // Immediate mode for CMPA and CMPB
	        EPwm2Regs.CMPA.half.CMPA =SP*0.5;
	        EPwm2Regs.CMPB=0;

	        EPwm2Regs.AQCTLA.all=0x6;        // EPWMxA = 1 when CTR=CMPA and counter inc
	                                          // EPWMxA = 0 when CTR=CMPA and counter dec
	        EPwm2Regs.AQCTLB.all=0;
	        EPwm2Regs.AQSFRC.all=0;
	        EPwm2Regs.AQCSFRC.all=0;

	        EPwm2Regs.TZSEL.all=0x0300;		//启动错误联防功能
	        EPwm2Regs.TZCTL.all=0;
	        EPwm2Regs.TZEINT.all=0;
	        EPwm2Regs.TZFLG.all=0;
	        EPwm2Regs.TZCLR.all=0;
	        EPwm2Regs.TZFRC.all=0;
	        EPwm2Regs.TZCTL.bit.TZA = 0x2;   //OSHT状态下，EPWM1输出拉低
	        EPwm2Regs.TZCTL.bit.TZB = 0x2;

	        EPwm2Regs.DBRED=20;				//死区设置
	        EPwm2Regs.DBFED=20;

	        EPwm2Regs.ETSEL.all=0;            // Interrupt when TBCTR = 0x0000
	        EPwm2Regs.ETFLG.all=0;
	        EPwm2Regs.ETCLR.all=0;
	        EPwm2Regs.ETFRC.all=0;

	        EPwm2Regs.PCCTL.all=0;
	        EPwm2Regs.TBCTL.all=0x0020+lag;  // 设置移相方向，lag滞后移相
	        EPwm2Regs.TBPRD=SP;
	    //--------------------------------------PWM3----------------------------------------------
	        EPwm3Regs.TBSTS.all=0;
	        EPwm3Regs.TBPHS.half.TBPHS=0;   //移相
	        EPwm3Regs.TBCTR=0;

	        EPwm3Regs.CMPCTL.all=0x50;        // Immediate mode for CMPA and CMPB
	        EPwm3Regs.CMPA.half.CMPA =SP*0.5;
	        EPwm3Regs.CMPB=0;

	        EPwm3Regs.AQCTLA.all=0x6;        // EPWMxA = 1 when CTR=CMPA and counter inc
	                                          // EPWMxA = 0 when CTR=CMPA and counter dec
	        EPwm3Regs.AQCTLB.all=0;
	        EPwm3Regs.AQSFRC.all=0;
	        EPwm3Regs.AQCSFRC.all=0;

	        EPwm3Regs.TZSEL.all=0x0300;			//启动错误联防功能
	        EPwm3Regs.TZCTL.all=0;
	        EPwm3Regs.TZEINT.all=0;
	        EPwm3Regs.TZFLG.all=0;
	        EPwm3Regs.TZCLR.all=0;
	        EPwm3Regs.TZCTL.bit.TZA = 0x2;   //OSHT状态下，EPWM1输出拉低
	        EPwm3Regs.TZCTL.bit.TZB = 0x2;
	        					//------基础参数配置完成后再让PWMB反相输出-------//
	        EPwm2Regs.DBCTL.all=0xb;          // EPWMxB is inverted
	        EPwm3Regs.DBCTL.all=0xb;          // EPWMxB is inverted

	        EPwm2Regs.TZFRC.bit.OST = 1;        //强制拉低EPWM2B
	        EPwm3Regs.TZFRC.bit.OST = 1;       //强制拉低EPWM3B

	        EPwm3Regs.DBRED=20;
	        EPwm3Regs.DBFED=20;

	        EPwm3Regs.ETSEL.all=0;            // Interrupt when TBCTR = 0x0000
	        EPwm3Regs.ETFLG.all=0;
	        EPwm3Regs.ETCLR.all=0;
	        EPwm3Regs.ETFRC.all=0;

	        EPwm3Regs.PCCTL.all=0;

	        EPwm3Regs.TBCTL.all=0x0020+lag;  // 设置移相方向
	        EPwm3Regs.TBPRD=SP;
//--------------------------------------PWM5----------------------------------------------
	        EPwm5Regs.TBSTS.all=0;
	        EPwm5Regs.TBPHS.half.TBPHS=0;     // 相位寄存器赋值 0
	        EPwm5Regs.TBCTR=0;                // TBCTR时基计数器

	        EPwm5Regs.CMPCTL.all=0x50;        // Immediate mode!!! for CMPA and CMPB
	        EPwm5Regs.CMPA.half.CMPA =SP*0.5;   // 配置占空比
	        EPwm5Regs.CMPB=0;

	        EPwm5Regs.AQCTLA.all=0x6;        // EPWMxA = 1 when CTR=CMPA and counter inc
	                                                  // EPWMxA = 0 when CTR=CMPA and counter dec
	        EPwm5Regs.AQCTLB.all=0;
	        EPwm5Regs.AQSFRC.all=0;
	        EPwm5Regs.AQCSFRC.all=0;

	        EPwm5Regs.TZSEL.all=0x0300;
	        EPwm5Regs.TZCTL.all=0;
	        EPwm5Regs.TZEINT.all=0;
	        EPwm5Regs.TZFLG.all=0;
	        EPwm5Regs.TZCLR.all=0;
	        EPwm5Regs.TZCTL.bit.TZA = 0x2;   //OSHT状态下，EPWM1输出拉低
	        EPwm5Regs.TZCTL.bit.TZB = 0x2;
	                                                  // DB模块 死区配置
	        EPwm5Regs.DBRED=10;              // 延时 ?个时钟周期
	        EPwm5Regs.DBFED=10;

	        EPwm5Regs.ETSEL.all=0;            // Interrupt when TBCTR = 0x0000
	        EPwm5Regs.ETFLG.all=0;
	        EPwm5Regs.ETCLR.all=0;
	        EPwm5Regs.ETFRC.all=0;

	        EPwm5Regs.PCCTL.all=0;
	        EPwm5Regs.TBCTL.all=0x0020+lead;    // 时基控制寄存器 Enable Timer，超前移相
	        EPwm5Regs.TBPRD=SP;               //时基周期寄存器

	   //--------------------------------------PWM6----------------------------------------------

	        EPwm6Regs.TBSTS.all=0;
	        EPwm6Regs.TBPHS.half.TBPHS=0;
	        EPwm6Regs.TBCTR=0;

	        EPwm6Regs.CMPCTL.all=0x50;        // Immediate mode for CMPA and CMPB
	        EPwm6Regs.CMPA.half.CMPA =SP*0.5;
	        EPwm6Regs.CMPB=0;

	        EPwm6Regs.AQCTLA.all=0x6;        // EPWMxA = 1 when CTR=CMPA and counter inc
	                                         // EPWMxA = 0 when CTR=CMPA and counter dec
	        EPwm6Regs.AQCTLB.all=0;
	        EPwm6Regs.AQSFRC.all=0;
	        EPwm6Regs.AQCSFRC.all=0;

	        EPwm6Regs.TZSEL.all=0x0300;
	        EPwm6Regs.TZCTL.all=0;
	        EPwm6Regs.TZEINT.all=0;
	        EPwm6Regs.TZFLG.all=0;
	        EPwm6Regs.TZCLR.all=0;
	        EPwm6Regs.TZCTL.bit.TZA = 0x2;   //OSHT状态下，EPWM1输出拉低
	        EPwm6Regs.TZCTL.bit.TZB = 0x2;

	        EPwm5Regs.DBCTL.all=0xb;          // EPWMxB is inverted
	        EPwm6Regs.DBCTL.all=0xb;          // EPWMxB is inverted

	        EPwm5Regs.TZFRC.bit.OST = 1;        //强制拉低EPWM1B
	        EPwm6Regs.TZFRC.bit.OST = 1;       //强制拉低EPWM4B

	        EPwm6Regs.DBRED=12;               //死区时间
	        EPwm6Regs.DBFED=12;

	        EPwm6Regs.ETSEL.all=0;            // Interrupt when TBCTR = 0x0000
	        EPwm6Regs.ETFLG.all=0;
	        EPwm6Regs.ETCLR.all=0;
	        EPwm6Regs.ETFRC.all=0;

	        EPwm6Regs.PCCTL.all=0;
	        EPwm6Regs.TBCTL.all=0x0020+lag;         // Enable Timer
	        EPwm6Regs.TBPRD=SP;

//--------------------------------------PWM1----------------------------------------------

	    EPwm1Regs.TBSTS.all=0;
	    EPwm1Regs.TBPHS.half.TBPHS=0;     // 相位寄存器赋值 0
	    EPwm1Regs.TBCTR=0;                // TBCTR时基计数器

	    EPwm1Regs.CMPCTL.all=0x50;        // Immediate mode!!! for CMPA and CMPB
	    EPwm1Regs.CMPA.half.CMPA =SP*0.5;   // 配置占空比
	    EPwm1Regs.CMPB=0;

	    EPwm1Regs.AQCTLA.all=0x6;        // EPWMxA = 1 when CTR=CMPA and counter inc
	                                      // EPWMxA = 0 when CTR=CMPA and counter dec
	    EPwm1Regs.AQCTLB.all=0;
	    EPwm1Regs.AQSFRC.all=0;
	    EPwm1Regs.AQCSFRC.all=0;

	    EPwm1Regs.TZSEL.all=0x0300;
	    EPwm1Regs.TZCTL.all=0;
	    EPwm1Regs.TZEINT.all=0;
	    EPwm1Regs.TZFLG.all=0;
	    EPwm1Regs.TZCLR.all=0;
	    EPwm1Regs.TZCTL.bit.TZA = 0x2;   //OSHT状态下，EPWM1输出拉低
	    EPwm1Regs.TZCTL.bit.TZB = 0x2;
	                                      // DB模块 死区配置
	    EPwm1Regs.DBRED=10;              // 延时 ?个时钟周期
	    EPwm1Regs.DBFED=10;

	    //ADC&ePWM，触发ADC的SOC信号
	    EPwm1Regs.ETSEL.all=0x0A00;       //TB一周期启动一次SOCA
	                                      //enable ePWMxSOCA
	    EPwm1Regs.ETPS.all=0x0100;        //TB周期匹配一次，给一次触发
	    EPwm1Regs.ETCLR.bit.SOCA=1;       //清除SOCA flag

	    EPwm1Regs.PCCTL.all=0;
	    EPwm1Regs.TBCTL.all=0x0020+lead;    // 时基控制寄存器 Enable Timer
	    EPwm1Regs.TBPRD=SP;               //时基周期寄存器

	    //--------------------------------------PWM4----------------------------------------------
	         EPwm4Regs.TBSTS.all=0;
	         EPwm4Regs.TBPHS.half.TBPHS=istep;				//内移相常数
	         EPwm4Regs.TBCTR=700;

	         EPwm4Regs.CMPCTL.all=0x50;        // Immediate mode for CMPA and CMPB
	         EPwm4Regs.CMPA.half.CMPA =SP*0.5;
	         EPwm4Regs.CMPB=0;

	         EPwm4Regs.AQCTLA.all=0x6;        // EPWMxA = 1 when CTR=CMPA and counter inc
	                                           // EPWMxA = 0 when CTR=CMPA and counter dec
	         EPwm4Regs.AQCTLB.all=0;
	         EPwm4Regs.AQSFRC.all=0;
	         EPwm4Regs.AQCSFRC.all=0;

	         EPwm4Regs.TZSEL.all=0x0300;
	         EPwm4Regs.TZCTL.all=0;
	         EPwm4Regs.TZEINT.all=0;
	         EPwm4Regs.TZFLG.all=0;
	         EPwm4Regs.TZCLR.all=0;
	         EPwm4Regs.TZCTL.bit.TZA = 0x2;   //OSHT状态下，EPWM1输出拉低
	         EPwm4Regs.TZCTL.bit.TZB = 0x2;

	         EPwm1Regs.DBCTL.all=0xb;          // EPWMxB is inverted
	         EPwm4Regs.DBCTL.all=0xb;          // EPWMxB is inverted

	         EPwm1Regs.TZFRC.bit.OST = 1;        //强制拉低EPWM1B
	         EPwm4Regs.TZFRC.bit.OST = 1;       //强制拉低EPWM4B

	         EPwm4Regs.DBRED=12;               //死区时间
	         EPwm4Regs.DBFED=12;

	         EPwm4Regs.ETSEL.all=0;            // Interrupt when TBCTR = 0x0000
	         EPwm4Regs.ETFLG.all=0;
	         EPwm4Regs.ETCLR.all=0;
	         EPwm4Regs.ETFRC.all=0;

	         EPwm4Regs.PCCTL.all=0;
	         EPwm4Regs.TBCTL.all=0x0020+lag;         // Enable Timer
	         EPwm4Regs.TBPRD=SP;

	    SysCtrlRegs.PCLKCR0.bit.TBCLKSYNC = 1;  //启动PWM时钟
	    			//--------------------让PWM1，4，5，6恢复正常---------------------//
	    EPwm1Regs.TZCLR.bit.OST=1;
	    EPwm4Regs.TZCLR.bit.OST=1;
//	    EPwm5Regs.TZCLR.bit.OST=1;
//	    EPwm6Regs.TZCLR.bit.OST=1;
	     EDIS;
}
//------------------------------------------PWM定义----------------------------------------//

//------------------------------------------定时器0中断函数，包括PID，采样数据计算，电压电流数据发送----------------------------------------//
interrupt void ISRTimer0(void)
{

           //--------------------移相角限幅-------------------//
            if(PWM2_CC_Duty > 0.4)
            {
           	 PWM2_CC_Duty = 0.4;
            }
            else if(PWM2_CC_Duty < -0.4)
            {
           	 PWM2_CC_Duty = -0.4;
            }

            if(PWM2_CC_Duty <= 0.4 && PWM2_CC_Duty>= 0 && a1 == 0)
            {
                  PWM2_CC_Duty= PWM2_CC_Duty+ 0.00001;
                  a2 = 1;
            }

            if(PWM2_CC_Duty> 0.4)
            {
                PWM2_CC_Duty= PWM2_CC_Duty- 0.00001;
                a1 = 1;
            }
            if(PWM2_CC_Duty<= 0.4 && PWM2_CC_Duty>=0 && a1 == 1 && a2 ==1)
            {
                PWM2_CC_Duty= PWM2_CC_Duty-0.00001;
                a3 = 0;
                a4 = 0;
            }

            if(PWM2_CC_Duty< 0 && PWM2_CC_Duty>= -0.4 && a4 ==0)
            {
                    PWM2_CC_Duty= PWM2_CC_Duty-0.00001;
                    a3 = 1;
            }
            if(PWM2_CC_Duty< -0.4)
            {
                PWM2_CC_Duty= PWM2_CC_Duty+ 0.0001;
                a4 = 1;
            }
            if(PWM2_CC_Duty>= -0.4 && PWM2_CC_Duty< 0 && a3 ==1 && a4 ==1)
            {
                PWM2_CC_Duty= PWM2_CC_Duty+0.00001;
                a1 = 0;
                a2 = 0;
            }
            PWM2_CC_Duty1 = PWM2_CC_Duty;
            if(PWM2_CC_Duty1 >= 0)
            {
    	        EPwm5Regs.TBCTL.all=0x0020+lag;
                EPwm5Regs.TBPHS.half.TBPHS=PWM2_CC_Duty1*phase_shift_cycle;
            }
            else if(PWM2_CC_Duty1 < 0)
            {
            	PWM2_CC_Duty1 = -PWM2_CC_Duty1;
    	        EPwm5Regs.TBCTL.all=0x0020+lead;
                EPwm5Regs.TBPHS.half.TBPHS=PWM2_CC_Duty1*phase_shift_cycle;
            }

    PieCtrlRegs.PIEACK.all = PIEACK_GROUP1;
    CpuTimer0Regs.TCR.bit.TIF=1;
    CpuTimer0Regs.TCR.bit.TRB=1;
    EINT;
}

//------------------------------------------内移相中断函数----------------------------------------//
interrupt void Phase_shift(void)
{
    kstep++;

    if(kstep==175)
    {
        istep--;
        EPwm4Regs.TBPHS.half.TBPHS=istep;
//        EPwm6Regs.TBPHS.half.TBPHS=istep;
        kstep=15;
    }
    if(istep==0)							//内移相结束，PWM2，3启动
    {
    	EPwm1Regs.ETSEL.bit.INTEN=0;		//关PWM1（内移相）中断
    	EALLOW;
        EPwm2Regs.TBCTR=0;
        EPwm3Regs.TBCTR=0;
        EPwm5Regs.TBCTR=0;
        EPwm6Regs.TBCTR=0;
        CpuTimer0Regs.TCR.bit.TSS = 0;					//启动PID中断，即启动定时器0
        DELAY_US(1.6);									//为了错开启动PWM2，3时因PWM1产生同步信号而产生的影响
        SysCtrlRegs.PCLKCR1.bit.EPWM2ENCLK=1;			//PWM2，3时钟开启
        SysCtrlRegs.PCLKCR1.bit.EPWM3ENCLK=1;
        SysCtrlRegs.PCLKCR1.bit.EPWM5ENCLK=1;			//PWM2，3时钟开启
        SysCtrlRegs.PCLKCR1.bit.EPWM6ENCLK=1;
        EPwm2Regs.TZCLR.bit.OST=1;						//清除PWM2，3错误联防标志位
        EPwm3Regs.TZCLR.bit.OST=1;
        EPwm5Regs.TZCLR.bit.OST=1;						//清除PWM2，3错误联防标志位
        EPwm6Regs.TZCLR.bit.OST=1;
//        EPwm5Regs.TBPHS.half.TBPHS=0.007*750;     // 相位寄存器赋值 0
//        EPwm5Regs.TBCTL.bit.PHSEN = 1;         // Enable Timer
        EDIS;
    }

    EPwm1Regs.ETCLR.bit.INT=1;							//清除PWM1中断标志位
    PieCtrlRegs.PIEACK.all=PIEACK_GROUP3;				//PIE3组可以中断
}
//------------------------------------------内移相中断函数----------------------------------------//
