www.pudn.com > svpwm.rar > svpwm.c, change:2013-09-17,size:59157b


 #include "DSP2833x_Device.h"     // DSP2833x Headerfile Include File 
#include "DSP2833x_Examples.h"   // DSP2833x Examples Include File 
#include "string.h" 
 
//#include "math.h"  
// Determine when the shift to right justify the data takes place 
// Only one of these should be defined as 1. 
// The other two should be defined as 0. 
 
// ADC start parameters 
#if (CPU_FRQ_150MHZ)     // Default - 150 MHz SYSCLKOUT 
  #define ADC_MODCLK 0x3 // HSPCLK = SYSCLKOUT/2*ADC_MODCLK2 = 150/(2*3)   = 25.0 MHz 
#endif 
#if (CPU_FRQ_100MHZ) 
  #define ADC_MODCLK 0x2 // HSPCLK = SYSCLKOUT/2*ADC_MODCLK2 = 100/(2*2)   = 25.0 MHz 
#endif 
    // 10^6/50/198=101.01Us 
 #define DCLimtRIn (GpioDataRegs.GPCCLEAR.bit.GPIO78=1) 
 #define DCLimtRout (GpioDataRegs.GPCSET.bit.GPIO78=1) 
 
volatile int32 dtempaavg=0,dtempbavg=0,dtempcavg=0,dtemp3avg=0; 
int32 dtempaCum=0,dtempbCum=0,dtempcCum=0,dtemp3Cum=0; 
 
volatile int32 TPWMPRD; 
 
volatile int16 PWMdtemp=3906;  
volatile int16 Kmd,Kmda,Kmdb,Kmdc; 
volatile int16 deltUa=0,deltUb=0,deltUc=0; 
volatile int16 VirtR=0; 
int16 Uatemp[4]; 
 
int16 Ksend=0,Ksendnum=0, Ksendready=0; //SCI various 
volatile Uint16 flag_ecan_send=0;//eCAN various 
Uint16 flag_eCAN_busy=0; 
 
volatile int16 UREF,UREFa=0,UREFb=0,UREFc=0,IREF=0,IQREF=0,UREFtemp=0,UcREF=0; //rms value 
volatile int16 IQREFa=0, IQREFb=0, IQREFc=0; 
 
int32 IREFa,Ierra,IREFb,Ierrb,IREFc,Ierrc; 
 
volatile int16 utemp=0,utemp0=0; 
 
int16 Zk=0;//5 
int32 Pref=0,Pref1=0; 
 
int16 p_flag=0; 
  
  
volatile int32 RefAngle=0; 
int16 DisWorkNum=0; 
//SCI-DATA 
 
//int16 TxBuff[1250]; 
 
//int16 RxBuff[10]; 
//int16 rx_counter; 
//Uint16 tx_counter; 
//int16 *tx_pointer; 
Uint16 flag=0; 
 
//_iq26 y,x,c; 
//_iq in1,out1; 
//_iq30 in2,out2; 
 
int16 Dza,Dzb,Dzbtemp,Dzc,Dza0=0,Dzb0=0,Dzc0=0,Dzatemp=0,Dzbtemp=0,Dzctemp=0; 
int16 nk; 
 
Uint16 Km1Ttemp,Km2Ttemp; 
volatile int16 KMIPns=50; 
volatile int16 KMSSns=50; 
int16 delt_U=244; 
// Uint16 NUMavermon=0,NUMavermoff=0; 
 
void InitVariable(); 
void PQ_Cal(); 
void KMIP_on(); 
void KMSS_on(); 
void KMIP_off(); 
void KMSS_off(); 
void KM_Timer(); 
void Adc_20msMain(void); 
void Adc_Offset(void); 
void Adc_Soft(void); 
 
 
  
void sync3_configer(void); 
void PQaver_C(void); 
void PQaver_C1(void); 
 
void U_Protect(void); 
void I_Protect(void); 
 
void DispDataCan(void); 
void Udc_judge(void); 
void KMIPOn_C(void); 
void Ugrid_judge(void); 
void Pulse_judge(void); 
void Urmsa_C(void); 
void Urmsb_C(void);  
void Urmsc_C(void);  
 
extern void Copy_to_TxBuff2(int16 *data1,int16 *data2,int16 *data3,int16 *buff); 
extern void DispDataSCi(void); 
extern void TouchScreenComm(void); 
 
//void cstop(void); 
 
 
int16 TABLE[768]={0,34,67,101,134,168,201,234,268,301,335,368,401,435,468,501,535,568,601,634,667,700,733,766,799,  
832,865,897,930,963,995,1028,1060,1092,1125,1157,1189,1221,1253,1285,1317,1348,1380,1411,1443,1474,1505,1536,1567,  
1598,1629,1660,1690,1721,1751,1781,1812,1842,1871,1901,1931,1960,1990,2019,2048,2077,2106,2134,2163,2191,2220,2248,  
2276,2303,2331,2359,2386,2413,2440,2467,2493,2520,2546,2572,2598,2624,2650,2675,2701,2726,2751,2775,2800,2824,2849,  
2873, 2896 ,2920 ,2943 ,2967 ,2990 ,3012 ,3035 ,3057 ,3080  
,3102 ,3123 ,3145 ,3166 ,3187 ,3208 ,3229 ,3250 ,3270 ,3290 ,3310 ,3329 ,3349 ,3368 ,3387 ,3406 ,3424 ,3442 ,3461 ,3478 ,3496 ,3513 ,3530 ,3547 ,3564 ,3580  
,3596 ,3612 ,3628 ,3643 ,3659 ,3674 ,3688 ,3703 ,3717 ,3731 ,3745 ,3758 ,3771 ,3784 ,3797 ,3809 ,3822 ,3833 ,3845 ,3857 ,3868 ,3879  
,3889 ,3900 ,3910 ,3920 ,3929 ,3939 ,3948 ,3956 ,3965 ,3973 ,3981 ,3989 ,3996 ,4004 ,4011 ,4017 ,4024 ,4030 ,4036 ,4041 ,4047  
,4052 ,4056 ,4061 ,4065 ,4069 ,4073 ,4076 ,4079 ,4082 ,4085 ,4087 ,4089 ,4091 ,4093 ,4094 ,4095 ,4095 ,4096 ,4096 ,4096 ,4095 ,4095 ,4094 ,4093 ,4091 ,4089 ,4087 ,4085 ,4082 ,4079 ,4076 ,4073 ,4069 ,4065 ,4061 ,4056 ,4052 ,4047 
,4041 ,4036 ,4030 ,4024 ,4017 ,4011 ,4004 ,3996 ,3989 ,3981 ,3973 ,3965 ,3956 ,3948 ,3939 ,3929 ,3920 ,3910 ,3900 ,3889 ,3879 ,3868  
,3857 ,3845 ,3833 ,3822 ,3809 ,3797 ,3784 ,3771 ,3758 ,3745 ,3731 ,3717 ,3703 ,3688 ,3674 ,3659 ,3643 ,3628 ,3612 ,3596 ,3580 ,3564  
,3547 ,3530 ,3513 ,3496 ,3478 ,3461 ,3442 ,3424 ,3406 ,3387 ,3368 ,3349 ,3329 ,3310 ,3290 ,3270 ,3250 ,3229 ,3208 ,3187 ,3166 ,3145 ,3123 ,3102 ,3080 ,3057 ,3035 ,3012 ,2990 ,2967 ,2943 ,2920 ,2896  
,2873 ,2849 ,2824 ,2800 ,2775 ,2751 ,2726 ,2701 ,2675 ,2650 ,2624 ,2598 ,2572 ,2546 ,2520 ,2493 ,2467 ,2440 ,2413 ,2386 ,2359 ,2331  
,2303 ,2276 ,2248 ,2220 ,2191 ,2163 ,2134 ,2106 ,2077 ,2048 ,2019 ,1990 ,1960 ,1931 ,1901 ,1871 ,1842 ,1812 ,1781 ,1751 ,1721 ,1690 
,1660 ,1629 ,1598 ,1567 ,1536 ,1505 ,1474 ,1443 ,1411 ,1380 ,1348 ,1317 ,1285 ,1253 ,1221 ,1189 ,1157 ,1125 ,1092 ,1060 ,1028 ,995  
,963,930 ,897 ,865 ,832 ,799 ,766 ,733 ,700 ,667 ,634 ,601 ,568 ,535 ,501 ,468 ,435 ,401 ,368 ,335 ,301 ,268 ,234 ,201 ,168 ,134  
,101 ,67 ,34 ,0 ,-34 ,-67 ,-101 ,-134 ,-168 ,-201 ,-234 ,-268 ,-301 ,-335 ,-368 ,-401 ,-435 ,-468 ,-501 ,-535 ,-568 ,-601 ,-634  
,-667 ,-700 ,-733 ,-766 ,-799 ,-832 ,-865 ,-897 ,-930 ,-963 ,-995 ,-1028 ,-1060 ,-1092 ,-1125 ,-1157,-1189 ,-1221 ,-1253 ,-1285  
,-1317 ,-1348 ,-1380 ,-1411 ,-1443 ,-1474 ,-1505 ,-1536,-1567 ,-1598 ,-1629 ,-1660 ,-1690 ,-1721 ,-1751 ,-1781 ,-1812 ,-1842 ,-1871 
,-1901 ,-1931 ,-1960 ,-1990 ,-2019 ,-2048 ,-2077 ,-2106 ,-2134 ,-2163 ,-2191 ,-2220 ,-2248 ,-2276 ,-2303 ,-2331 ,-2359 ,-2386 ,-2413  
,-2440 ,-2467 ,-2493 ,-2520 ,-2546 ,-2572 ,-2598 ,-2624 ,-2650 ,-2675 ,-2701 ,-2726 ,-2751 ,-2775 ,-2800 ,-2824 ,-2849 ,-2873 ,-2896  
,-2920 ,-2943 ,-2967 ,-2990 ,-3012 ,-3035 ,-3057 ,-3080 ,-3102 ,-3123 ,-3145 ,-3166 ,-3187 ,-3208 ,-3229 ,-3250 ,-3270 ,-3290  
,-3310 ,-3329 ,-3349 ,-3368 ,-3387 ,-3406 ,-3424 ,-3442 ,-3461 ,-3478 ,-3496 ,-3513 ,-3530 ,-3547 ,-3564 ,-3580 ,-3596 ,-3612 ,-3628  
,-3643 ,-3659 ,-3674,-3688 ,-3703 ,-3717 ,-3731 ,-3745 ,-3758 ,-3771 ,-3784 ,-3797 ,-3809 ,-3822 ,-3833 ,-3845 ,-3857 ,-3868 ,-3879 ,-3889  
,-3900 ,-3910 ,-3920 ,-3929 ,-3939 ,-3948 ,-3956 ,-3965 ,-3973 ,-3981 ,-3989 ,-3996 ,-4004 ,-4011 ,-4017 ,-4024 ,-4030 ,-4036 ,-4041  
,-4047 ,-4052 ,-4056 ,-4061 ,-4065,-4069 ,-4073 ,-4076 ,-4079 ,-4082 ,-4085 ,-4087 ,-4089 ,-4091 ,-4093 ,-4094 ,-4095 ,-4095 ,-4096 ,-4096  
,-4096 ,-4095 ,-4095 ,-4094 ,-4093 ,-4091 ,-4089 ,-4087 ,-4085 ,-4082,-4079 ,-4076 ,-4073 ,-4069 ,-4065 ,-4061 ,-4056 ,-4052 ,-4047 ,-4041 ,-4036 ,-4030  
,-4024 ,-4017 ,-4011 ,-4004 ,-3996 ,-3989 ,-3981 ,-3973 ,-3965 ,-3956 ,-3948 ,-3939 ,-3929 ,-3920 ,-3910,-3900 ,-3889 ,-3879 ,-3868 ,-3857 ,-3845 ,-3833 ,-3822,-3809 ,-3797 ,-3784  
,-3771 ,-3758 ,-3745 ,-3731 ,-3717 ,-3703 ,-3688 ,-3674 ,-3659,-3643 ,-3628 ,-3612 ,-3596 ,-3580 ,-3564 ,-3547,-3530 ,-3513 ,-3496  
,-3478 ,-3461 ,-3442 ,-3424 ,-3406 ,-3387 ,-3368 ,-3349 ,-3329 ,-3310 ,-3290 ,-3270,-3250 ,-3229 ,-3208 ,-3187 ,-3166 ,-3145 ,-3123 ,-3102 ,-3080 ,-3057 ,-3035 ,-3012  
,-2990 ,-2967 ,-2943 ,-2920 ,-2896 ,-2873 ,-2849 ,-2824,-2800 ,-2775 ,-2751 ,-2726 ,-2701 ,-2675 ,-2650 ,-2624 ,-2598 ,-2572 ,-2546  
,-2520 ,-2493 ,-2467 ,-2440 ,-2413 ,-2386 ,-2359 ,-2331 ,-2303 ,-2276 ,-2248 ,-2220 ,-2191 ,-2163 ,-2134 ,-2106 ,-2077 ,-2048 ,-2019  
,-1990 ,-1960 ,-1931 ,-1901 ,-1871 ,-1842 ,-1812 ,-1781 ,-1751 ,-1721 ,-1690 ,-1660 ,-1629 ,-1598 ,-1567 ,-1536 ,-1505 ,-1474 ,-1443  
,-1411 ,-1380 ,-1348 ,-1317 ,-1285 ,-1253 ,-1221 ,-1189 ,-1157 ,-1125 ,-1092 ,-1060 ,-1028 ,-995 ,-963 ,-930 ,-897 ,-865 ,-832 ,-799  
,-766 ,-733 ,-700 ,-667 ,-634 ,-601 ,-568 ,-535 ,-501 ,-468 ,-435 ,-401 ,-368 ,-335 ,-301 ,-268 ,-234 ,-201 ,-168 ,-134 ,-101 ,-67 ,-34}; 
 
int16 ID=1; //0 1  
int16 uatemp0=0,ubtemp0=0,uctemp0=0,Num_utemp0=0; 
int16 Sum_DisPWM=0; 
main() 
{ 
//   struct ECAN_REGS ECanaShadow; 
   InitSysCtrl(); 
 
   
   EALLOW; 
   SysCtrlRegs.HISPCP.all = ADC_MODCLK;	// HSPCLK = SYSCLKOUT/ADC_MODCLK 
   EDIS; 
 
  //flash use  				  
   MemCopy(&RamfuncsLoadStart, &RamfuncsLoadEnd, &RamfuncsRunStart); 
   InitFlash(); 
 
    //   InitGpio(); 
   EALLOW; 
    
   GpioCtrlRegs.GPCMUX1.bit.GPIO79=0; // IAV-CTRL-A 
   GpioCtrlRegs.GPCDIR.bit.GPIO79=1;  //output pin 
 
   GpioCtrlRegs.GPCMUX1.bit.GPIO70 = 0;   //GPIO pin LINE_EN2 
   GpioCtrlRegs.GPCDIR.bit.GPIO70=1;   //output pin 
 
   GpioCtrlRegs.GPCMUX1.bit.GPIO71 = 0;   //GPIO pin LINE_EN1   
   GpioCtrlRegs.GPCDIR.bit.GPIO71=1;   //output pin 
 
   GpioCtrlRegs.GPBMUX2.bit.GPIO54=0; 
   GpioCtrlRegs.GPBDIR.bit.GPIO54=1;  //output pin Table output 
 
   GpioCtrlRegs.GPCMUX1.bit.GPIO75=0;  //  KM_IP_ON 
   GpioCtrlRegs.GPCDIR.bit.GPIO75=1; 
   GpioCtrlRegs.GPCPUD.bit.GPIO75=1; 
 
   GpioCtrlRegs.GPCMUX1.bit.GPIO76=0;  //  KM_SS_ON 整流用 
   GpioCtrlRegs.GPCDIR.bit.GPIO76=1; 
 
 /*   
   GpioCtrlRegs.GPCMUX1.bit.GPIO74=0;  //  KM_BAT_ON 
   GpioCtrlRegs.GPCDIR.bit.GPIO74=1; 
*/ 
    GpioCtrlRegs.GPCMUX1.bit.GPIO78=0;  //  SS   直流缓启动 
    GpioCtrlRegs.GPCDIR.bit.GPIO78=1; 
    EDIS; 
     
   GpioDataRegs.GPCSET.bit.GPIO77=1; //output 1:lock the pwm 
   GpioDataRegs.GPCSET.bit.GPIO79=1;  //output 1: electric swith is off; IAV-CTRL-A 
//   Iaver_M_off();   //GPIO79 
//   Iaver_M_on(); 
   GpioDataRegs.GPCSET.bit.GPIO71=1; //output 1: Uout_a GPIO24:CAP1 
   GpioDataRegs.GPCCLEAR.bit.GPIO70=1; //output 0: SYNC GPIO25:CAP2 
   GpioDataRegs.GPBSET.bit.GPIO54=1;  // output 1: Table  
   GpioDataRegs.GPCCLEAR.bit.GPIO75=1;  //KM_IP_ON   
   GpioDataRegs.GPCCLEAR.bit.GPIO76=1;  //KM_SS_ON   整流用 
   // GpioDataRegs.GPCCLEAR.bit.GPIO74=1;  //KM_BAT_ON 
   //GpioDataRegs.GPCCLEAR.bit.GPIO78=1;  //SS 直流缓启动 
   DCLimtRIn; 
 
   DINT; 
   
   InitVariable();     
    
  // *PIDREG3_handle=PIDREG3_DEFAULTS; 
    
   InitPieCtrl(); 
  
// Disable CPU interrupts and clear all CPU interrupt flags: 
   IER = 0x0000; 
   IFR = 0x0000; 
 
   InitPieVectTable(); 
 
   EALLOW; 
   PieVectTable.SCIRXINTA=&SCIRXINTA_ISR; 
   PieVectTable.EPWM1_TZINT=&EPWM1_TZINT_ISR; 
   PieVectTable.ADCINT=&ADCINT_ISR; 
   PieVectTable.ECAP1_INT=&ECAP1_INT_ISR; 
   PieVectTable.ECAP2_INT=&ECAP2_INT_ISR;   
   PieVectTable.ECAP5_INT=&ECAP5_INT_ISR;      
   EDIS; 
 
  for (nk=0;nk<3000;nk++) //15s 
   { 
    DELAY_US(5000); //5mS 
   } 
 
   InitSciaGpio(); 
   InitScia(); 
   InitScibGpio(); 
   InitScib(); 
   InitAdc();         // For this example, init the ADC 
 //  InitTzGpio(); 
   InitEPwm(); 
   InitEPwmGpio(); 
 
   
   InitECapGpio(); 
   InitECap();  
 
   //init eCAN_A 
   InitECanaGpio(); 
   InitECana(); 
 
   EALLOW; 
   GpioCtrlRegs.GPAPUD.bit.GPIO20 = 0;   // Enable   
   GpioCtrlRegs.GPAMUX2.bit.GPIO20 = 0;   
   GpioCtrlRegs.GPADIR.bit.GPIO20 = 1;  
   GpioDataRegs.GPASET.bit.GPIO20=1; 
 
   GpioCtrlRegs.GPBPUD.bit.GPIO57 = 0;   // Enable   
   GpioCtrlRegs.GPBMUX2.bit.GPIO57 = 0;//ECAN test pin J1             
   GpioCtrlRegs.GPBDIR.bit.GPIO57 = 1; //output 
   GpioDataRegs.GPBCLEAR.bit.GPIO57=1;//output 0 
 
   GpioCtrlRegs.GPBPUD.bit.GPIO58 = 0;   //pull up Enable 
   GpioCtrlRegs.GPBMUX2.bit.GPIO58 = 0;	//LED display            
   GpioCtrlRegs.GPBDIR.bit.GPIO58 = 1; //output 
   GpioDataRegs.GPBSET.bit.GPIO58=1;  //output 1 
   EDIS; 
  
//   InitVariable(); 
//   GpioDataRegs.GPBSET.bit.GPIO60=1; 
 //  GpioDataRegs.GPCCLEAR.bit.GPIO77=0; //output 0    
    
 //   GpioDataRegs.GPCCLEAR.bit.GPIO77=1; //output 1 
     
  //delay 10s  
 
  for (nk=0;nk<1000;nk++) 
   { 
    DELAY_US(5000); 
   } 
    
   Dza=0;  
   Dzb=128; 
   Dzc=64;	  
   Kmda=0;  
   Kmdb=0; 
   Kmdc=0; 
     
   utemp0=0; 
        
   Adc_Soft();  
   sync3_configer(); 
 
  for (nk=0;nk<1000;nk++) 
   { 
    DELAY_US(5000); 
   } 
    
 //  GpioDataRegs.GPCSET.bit.GPIO78=1;   //直流缓启动结束 
   DCLimtRout; 
   DELAY_US(10000); 
   DELAY_US(10000); 
 
 //   GpioDataRegs.GPCCLEAR.bit.GPIO77=1; //output 1 
     
  //ADCRMS.Udc_bus=400; 
    
 //   DCONST=(float)UREF*TPWMPRD/Udc/4096/128; 
 
    IER|=M_INT4; // Enable CPU INT4 which is connected to ECap6-1 INT; 
    IER|=M_INT1; //Enable CPU INT1 which is connected to ADCINT; 
  //  IER|=M_INT3; // Enable CPU INT3 which is connected to EPWM1 INT; 
  // 	IER|=M_INT2;  //Enable CPU INT2 which is connected to EPWM1_TZINT---EPWM6_TZINT 
    IER|=M_INT9;   //rx tx   can 
 
   PieCtrlRegs.PIEIER4.bit.INTx1=1; //enable eCAP1 INTn in the PIE: Group 3 interrupt 1-6 
   PieCtrlRegs.PIEIER4.bit.INTx2=1; //eanble eCAP2 
   PieCtrlRegs.PIEIER4.bit.INTx5=1; //eanble eCAP5 
 
   PieCtrlRegs.PIEIER1.bit.INTx6=1; //enable ADCINT 
  //  PieCtrlRegs.PIEIER2.bit.INTx1=0;//enable ePWM1_TZINT 
   PieCtrlRegs.PIEIER9.bit.INTx1=1;	//SCI-A rx interrupt 
   PieCtrlRegs.PIEIER9.bit.INTx2=1;	//SCI-A tx interrupt 
 
   PieCtrlRegs.PIEIER9.bit.INTx3=1; //SCI-B interrupt 
   PieCtrlRegs.PIEIER9.bit.INTx4=1; //SCI-B interrupt 
 
//   PieCtrlRegs.PIEIER9.bit.INTx6=1;//ECAN1INTA 
   EINT; 
   ERTM; 
 
//   EnPWM();  
  // Grid frequency and amplitude are right 
   while(1) 
   { 
	#if DSP28_SCIA_COMPUTER 
        DispDataSCi(); 
	#else 
		TouchScreenComm(); 
	#endif 
    
 
     Adc_20msMain(); 
        	   
	  
   asm(" RPT #7 || NOP"); 
  //*************************************************************** 
     
   } 
 } 
 
void InitVariable() 
 {   
 
   if(ID==1) 
     { 
   
      ADKm[Uinv_a1]=4722;    // 1号机 
      ADKm[Uinv_b1]=4722;    // 
      ADKm[Uinv_c1]=4743;    // 
      ADKm[Uout_a1]=4722; 
      ADKm[Uout_b1]=4751; 
      ADKm[Uout_c1]=4794; 
      ADKm[Udc_bus1]=2039;  //2115 
      ADKm[Ifilter_a1]=4152; 
      ADKm[Ifilter_b1]=4150; 
      ADKm[Ifilter_c1]=4150; 
      ADKm[Iload_a1]=4152;  //3.4/A 
      ADKm[Iload_b1]=4156;  //3.4/A 
      ADKm[Iload_c1]=4150;  //3.4/A 
      ADKm[Iav_a1]=2906;  //4152*0.7=2906 
      ADKm[Iav_b1]=2930;    //4186*0.7=2930 
      ADKm[Iav_c1]=2919;    //4170*0.7=2919 
	 /////////////////////////////////////////////////// 
      K.PaverI=0;    //1 
      K.PaverP=0; //10 
      K.QaverI=0;    //1 
      K.QaverP=0;    //1 
      K.Kaver=0; //16 
     } 
   else 
    { 
      ///////////////////////////////////////// 
      ADKm[Uinv_a1]=4686;    // 0号机 
      ADKm[Uinv_b1]=4692;    // 
      ADKm[Uinv_c1]=4684;    // 
      ADKm[Uout_a1]=4687; 
      ADKm[Uout_b1]=4687; 
      ADKm[Uout_c1]=4687; 
      ADKm[Udc_bus1]=2039;  //2115 
      ADKm[Ifilter_a1]=4160;  //3987 
      ADKm[Ifilter_b1]=4164; 
      ADKm[Ifilter_c1]=4167; 
      ADKm[Iload_a1]=4160;  //3.4/A 
      ADKm[Iload_b1]=4164;  //3.4/A 
      ADKm[Iload_c1]=4167;  //3.4/A 
      ADKm[Iav_a1]=2912;   // 4160*0.7=2912 
      ADKm[Iav_b1]=2915;   //4164*0.7=2915 
      ADKm[Iav_c1]=2917;    //4167*0.7=2917 
	  //////////////////////////////////////// 
      K.PaverI=0;  //1 
      K.PaverP=0; //1 
      K.QaverI=0;  //1 
      K.QaverP=0;  //1 
      K.Kaver=0; //16 
    } 
 
     
    K.P_slock=1; 
    K.I_slock=1; 
	K.UcOpen=218; 
    PIDSLOCK.outtempiRem=0; 
    PIDSLOCK.outMax=1; 
    PIDSLOCK.outMin=-1; 
	PIDSLOCK.outi=0; 
	PIDSLOCK.out=0; 
 
	CUNTER.SpaceLock=0; 
 
   memset(&Ug,0,sizeof(Ug)); 
   memset(&CUM,0,sizeof(CUM)); 
 
   memset(&ADCRMS,0,sizeof(ADCRMS));  
   memset(&ADCCURRENT,0,sizeof(ADCCURRENT)); 
 
   memset(&ADRMS,0,sizeof(ADRMS));  
   memset(&ADCURRENT,0,sizeof(ADCURRENT)); 
   memset(ADOFFSET,0,sizeof(ADOFFSET)); 
   memset(ADRMStemp,0,sizeof(ADRMStemp)); 
   memset(ADAVRtemp,0,sizeof(ADAVRtemp)); 
 
   memset(&avgtempa,0,sizeof(avgtempa)); 
   memset(&avgtempb,0,sizeof(avgtempb)); 
   memset(&avgtempc,0,sizeof(avgtempc)); 
   memset(&lockfdb,0,sizeof(lockfdb)); 
   memset(&avgtempIa,0,sizeof(avgtempIa)); 
   memset(&avgtempIb,0,sizeof(avgtempIb)); 
   memset(&avgtempIc,0,sizeof(avgtempIc)); 
   memset(&avgtempIla,0,sizeof(avgtempIla)); 
   memset(&avgtempIlb,0,sizeof(avgtempIlb)); 
   memset(&avgtempIlc,0,sizeof(avgtempIlc)); 
   memset(&PQ,0,sizeof(PQ)); 
   memset(&P_NODE,0,sizeof(P_NODE)); 
   memset(&CAP,0,sizeof(CAP)); 
   memset(&FLAG,0,sizeof(FLAG)); 
 
   memset(&FLAG,0,sizeof(FLAG)); 
   memset(&CUNTER,0,sizeof(CUNTER)); 
 
   Ke.IfiltOvinst=(int16)((int32)K_Idg2phy*550/64);   //64 
   Ke.Iload1P20=(int16)((int32)K_Idg2phy*150*15/640);  //1.2bei 
   Ke.Iload1P05=(int16)((int32)K_Idg2phy*150*105/6400); 
   Ke.UdcOvinst=(int16)((int32)K_Udg2phy*550/64); 
   Ke.Thermconst=30000; 
   Ke.T2S=100; 
    
   Ke.Ugrid1P20=1081;  //220*1.2*4.096=1081; 
   Ke.UgridP10=90;  //22*4.096=90 
   Ke.UgridP80=720; //220*0.8*4.096=720 
 
 
   FLAG1.Csfw4.all=0x00; 
   FLAG1.Csfw4.bit.Usoft=1; 
   FLAG1.Sdsiga1.all=0xFF; 
   FLAG1.Sdsiga2.all=0xFF; 
   FLAG1.Sesiga3.all=0xFF; 
   FLAG1.Sedevice5.all=0x00; 
 
   K.Pam=2688; 
 //  K.ip=20;   
   K.P_i=8; //80 
   K.P_igrid=80;  //80 
   K.P_i1=8; 
   K.P_igrid1=9;  // 
   K.feedforward=1221; 
   K.PaverI=0;  //1 
   K.PaverP=0; //1 
   K.QaverI=0;  //1 
   K.QaverP=0;  //1 
   K.Kaver=0; //16 
 
 
	K.P_u=20; //40 
	PIDUA1.out=0; 
	PIDUA1.outtempi=0; 
	PIDUA1.outtempdelta=0; 
	PIDUA1.outMax=1000;  //482=7.5852*15*1.414*3=  
	PIDUA1.outMin=-1000;   
	PIDUA1.outtempiRem=0; 
	PIDUA1.errmax=500; //254=1.414*86*4.096 
    PIDUA1.errmin=-500; 
	 
 
//	PIDUB1.Kp=60; //48 
	PIDUB1.out=0; 
	PIDUB1.outtempi=0; 
	PIDUB1.outtempdelta=0; 
	PIDUB1.outMax=1000;  //482=7.5852*15*1.414*3=  
	PIDUB1.outMin=-1000;   
	PIDUB1.outtempiRem=0; 
	PIDUB1.errmax=500; //254=1.414*44*4.096 
    PIDUB1.errmin=-500; 
 
//	PIDUC1.Kp=42; 
//	PIDUC1.Kp=60;  //48 
	PIDUC1.out=0; 
	PIDUC1.outtempi=0; 
	PIDUC1.outtempdelta=0; 
	PIDUC1.outMax=1000;  //482=7.5852*15*1.414*3=  
	PIDUC1.outMin=-1000;   
	PIDUC1.outtempiRem=0; 
	PIDUC1.errmax=500; //254=1.414*44*4.096 
	PIDUC1.errmin=-500; 
 
//	K.P_urms=160;    // 4   2 
//	K.I_urms=32;   //1     2 
	K.P_urms=160;    // 160 
	K.I_urms=32;   //32 
 
	PIDUArms.out=0; 
	PIDUArms.outi=0; 
	PIDUArms.errmax=200;   // 
	PIDUArms.errmin=-200;  //24v---100 
	PIDUArms.outk=0; 
	PIDUArms.outMax=100;     //50 
	PIDUArms.outMin=-100;     //-50 
    PIDUArms.outtempiRem=0; 
    PIDUArms.Fdb=0; 
    PIDUArms.Ref=0; 
	PIDUArms.err=0; 
	 
	PIDUBrms.out=0; 
	PIDUBrms.outi=0; 
	PIDUBrms.errmax=200;   // 
	PIDUBrms.errmin=-200;  //24v---100 
	PIDUBrms.outk=0; 
	PIDUBrms.outMax=100;     //50 
	PIDUBrms.outMin=-100;     //-50 
    PIDUBrms.outtempiRem=0; 
    PIDUBrms.Fdb=0; 
    PIDUBrms.Ref=0; 
	PIDUBrms.err=0; 
 
	PIDUCrms.out=0; 
	PIDUCrms.outi=0; 
	PIDUCrms.errmax=200;   // 
	PIDUCrms.errmin=-200;  //24v---100 
	PIDUCrms.outk=0; 
	PIDUCrms.outMax=100;     //50 
	PIDUCrms.outMin=-100;     //-50 
    PIDUCrms.outtempiRem=0; 
    PIDUCrms.Fdb=0; 
    PIDUCrms.Ref=0; 
	PIDUCrms.err=0; 
 
//    K.P_phlock=1024;    // 50 
    K.P_phlock=1024; 
	K.I_phlock=0; 
 
	PIDLOCK.out=0; 
	PIDLOCK.outi=0; 
	PIDLOCK.errmax=1600;  //3du 30000  1000  30  (1200*833)=999600  600 
	PIDLOCK.errmin=-1600;  //24v 
	PIDLOCK.outk=0; 
	PIDLOCK.outMax=78;     //150-1Hz  150*833=  0.5Hz---0.5Hz 
	PIDLOCK.outMin=-78;     // 
    PIDLOCK.outtempiRem=0; 
    PIDLOCK.Fdb=0; 
    PIDLOCK.Ref=0; 
	 
 
 
	PIDPa.out=0; 
	PIDPa.outi=0; 
	PIDPa.errmax=300;  //500 
	PIDPa.errmin=-300;  //24v 
	PIDPa.outk=0; 
	PIDPa.outMax=40;     // +20v   4DU 
	PIDPa.outMin=-40;     //-20V 
    PIDPa.outtempiRem=0; 
    PIDPa.Fdb=0; 
    PIDPa.Ref=0; 
     
 
	PIDPb.out=0; 
	PIDPb.outi=0; 
	PIDPb.errmax=300; 
	PIDPb.errmin=-300;  //24v 
	PIDPb.outk=0; 
	PIDPb.outMax=20;     // // 50Hz--3M  1度 3M/360=8333 8333*30=249990 
	PIDPb.outMin=-20;     // 
    PIDPb.outtempiRem=0; 
    PIDPb.Fdb=0; 
    PIDPb.Ref=0; 
     
	PIDPc.out=0; 
	PIDPc.outi=0; 
	PIDPc.errmax=300; 
	PIDPc.errmin=-300;  //24v 
	PIDPc.outk=0; 
	PIDPc.outMax=20;     //   
	PIDPc.outMin=-20;     // 
    PIDPc.outtempiRem=0; 
    PIDPc.Fdb=0; 
    PIDPc.Ref=0; 
 
 
	PIDQa.out=0; 
	PIDQa.outi=0; 
	PIDQa.errmax=300; 
	PIDQa.errmin=-300;  //24v 
	PIDQa.outk=0; 
	PIDQa.outMax=5;   //  2V    2*4.096*1.414*64=524   25*524=13100 
	PIDQa.outMin=-5;     // 
    PIDQa.outtempiRem=0; 
    PIDQa.Fdb=0; 
    PIDQa.Ref=0; 
   
 
	PIDQb.out=0; 
	PIDQb.outi=0; 
	PIDQb.errmax=300; 
	PIDQb.errmin=-300;  //24v 
	PIDQb.outk=0; 
	PIDQb.outMax=5;     //  
	PIDQb.outMin=-5;     // 
    PIDQb.outtempiRem=0; 
    PIDQb.Fdb=0; 
    PIDQb.Ref=0; 
     
	PIDQc.out=0; 
	PIDQc.outi=0; 
	PIDQc.errmax=300; 
	PIDQc.errmin=-300;  //24v 
	PIDQc.outk=0; 
	PIDQc.outMax=5;     //  
	PIDQc.outMin=-5;     // 
    PIDQc.outtempiRem=0; 
    PIDQc.Fdb=0; 
    PIDQc.Ref=0; 
 
    K.Urms2peak=371; 
 //  Kuac=371;        // 4.096*1.414*64=371  :6 bit  coefficient from rms to peak 
    UREF=220;   //Uref is the rms value;  //220 
    UREFa=UREF; 
	UREFb=UREF; 
	UREFc=UREF; 
	K.feedforward=1221; 
 
    TPWMPRD=7812; 
 } 
 
void PQ_Cal() 
{ 
     PQ.A.Ureal=CUM.Ua0.REAL/TABLE_NUM; 
	 PQ.A.Uimag=CUM.Ua0.IMAG/TABLE_NUM; 
	 PQ.A.Ireal=CUM.Ia0.REAL/TABLE_NUM; 
	 PQ.A.Iimag=CUM.Ia0.IMAG/TABLE_NUM;   
 
     PQ.A.P=(PQ.A.Ureal*PQ.A.Ireal+PQ.A.Uimag*PQ.A.Iimag)*256/288;  //3969 
  //   PQ.A.Q=(PQ.A.Uimag*PQ.A.Ireal-PQ.A.Ureal*PQ.A.Iimag)*256/3969; 
     PQ.A.Q=-(PQ.A.Uimag*PQ.A.Ireal-PQ.A.Ureal*PQ.A.Iimag)*256/288; 
 
	   if(PQ.A.Q>300) PQ.A.sign=1; 
	   else if(PQ.A.Q<-300) PQ.A.sign=-1; 
	   else PQ.A.sign=0; 
//	   PQ.A.Pout=PQ.A.Ptemp0/TABLE_NUM/14;   //4.096*3.4=14  14*192=  
       PQ.A.Pout=PQ.A.Ptemp0/K.Pam;    
       PQ.A.Qout=PQ.A.sign*sqrt(ADCRMS.Uinv_a*ADCRMS.Uinv_a/194*ADCRMS.Iload_a*ADCRMS.Iload_a-PQ.A.Pout*PQ.A.Pout); 
 ////////////////////////////////////////////////////////////// 
     PQ.B.Ureal=CUM.Ub0.REAL/TABLE_NUM; 
	 PQ.B.Uimag=CUM.Ub0.IMAG/TABLE_NUM; 
	 PQ.B.Ireal=CUM.Ib0.REAL/TABLE_NUM; 
	 PQ.B.Iimag=CUM.Ib0.IMAG/TABLE_NUM;    
     PQ.B.P=(PQ.B.Ureal*PQ.B.Ireal+PQ.B.Uimag*PQ.B.Iimag)*256/288;  //3969 
   //  PQ.B.Q=(PQ.B.Uimag*PQ.B.Ireal-PQ.B.Ureal*PQ.B.Iimag)*256/3969; 
      
     
     PQ.B.Q=-(PQ.B.Uimag*PQ.B.Ireal-PQ.B.Ureal*PQ.B.Iimag)*256/288; 
 
	  if(PQ.B.Q>300) PQ.B.sign=1; 
	  else if(PQ.B.Q<-300) PQ.B.sign=-1; 
	  else PQ.B.sign=0; 
	   PQ.B.Pout=PQ.B.Ptemp0/TABLE_NUM/14; 
       PQ.B.Qout=PQ.B.sign*sqrt(ADCRMS.Uinv_b*ADCRMS.Uinv_b/194*ADCRMS.Iload_b*ADCRMS.Iload_b-PQ.B.Pout*PQ.B.Pout); 
 /* 
     PQ.B.Paver=(PQ.B.Ureal*PQ.B.Iaverreal+PQ.B.Uimag*PQ.B.Iaverimag)*256/3969; 
     PQ.B.Qaver=-(PQ.B.Uimag*PQ.B.Iaverreal-PQ.B.Ureal*PQ.B.Iaverimag)*256/3969; 
*/ 
     PQ.C.Ureal=CUM.Uc0.REAL/TABLE_NUM; 
	 PQ.C.Uimag=CUM.Uc0.IMAG/TABLE_NUM; 
 	 PQ.C.Ireal=CUM.Ic0.REAL/TABLE_NUM; 
	 PQ.C.Iimag=CUM.Ic0.IMAG/TABLE_NUM; 
//	 PQ.C.Iaverreal=CumIaverc0.REAL/TABLE_NUM; 
//	 PQ.C.Iaverimag=CumIaverc0.IMAG/TABLE_NUM;     
     PQ.C.P=(PQ.C.Ureal*PQ.C.Ireal+PQ.C.Uimag*PQ.C.Iimag)*256/288; 
     PQ.C.Q=-(PQ.C.Uimag*PQ.C.Ireal-PQ.C.Ureal*PQ.C.Iimag)*256/288; 
 
	  if(PQ.C.Q>300) PQ.C.sign=1; 
	  else if(PQ.C.Q<-300) PQ.C.sign=-1; 
	  else PQ.C.sign=0; 
	  PQ.C.Pout=PQ.C.Ptemp0/TABLE_NUM/14; 
      PQ.C.Qout=PQ.C.sign*sqrt(ADCRMS.Uinv_c*ADCRMS.Uinv_c/194*ADCRMS.Iload_c*ADCRMS.Iload_c-PQ.C.Pout*PQ.C.Pout); 
/* 
     PQ.C.Paver=(PQ.C.Ureal*PQ.C.Iaverreal+PQ.C.Uimag*PQ.C.Iaverimag)*256/3969; 
     PQ.C.Qaver=-(PQ.C.Uimag*PQ.C.Iaverreal-PQ.C.Ureal*PQ.C.Iaverimag)*256/3969; 
*/ 
} 
 
//=========================================================================== 
// No more. 
//========================================================================== 
void Adc_20msMain(void) 
{    
   
  if(p_flag==1)  
    {    
     
     PQ_Cal(); 
 
	 if((PQ.A.Pout>5000)||(PQ.B.Pout>5000)||(PQ.C.Pout>5000)) 
	   FLAG1.Sdsiga2.bit.P=1; 
	 else 
	   FLAG1.Sdsiga2.bit.P=0; 
 
    if((FLAG1.Csfw4.bit.KMIPon==1)&&(FLAG1.Csfw4.bit.GridOrAlone==1)) 
	  { 
		   //   PQaver_C(); 
            if((Pref1-Pref)>200) 
			  Pref+=200; 
			else if((Pref1-Pref)<-200) 
			  Pref-=200; 
			else 
			  Pref=Pref1; 
             
            K.PaverI=1; 
			K.PaverP=1; 
			K.QaverI=1; 
			K.QaverP=1; 
 
		    PQaver_C1(); 
		 }	      
	  else 
		   { 
		     K.PaverI=0; 
			 K.PaverP=0; 
             K.QaverI=0; 
			 K.QaverP=0; 
 
 
		     PIDPa.outtempiRem=0; 
             PIDPa.out=0; 
             PIDPa.outi=0; 
			 PIDPa.outtempkRem=0; 
 
			 PIDQa.out=0; 
             PIDQa.outtempiRem=0; 
			 PIDQa.outtempkRem=0; 
			 PIDQa.outi=0;			 	    
		   } 
 
/* 
   if(FLAG.B.bit.GrdConAldy7==1) 
     { 
      if(FLAG.B.bit.PQReceive8==1)             //  if(P_NODE.GridConect==1) 
	   { 
	     if(P_NODE.GridConect==1)     //if(FLAG.B.bit.PQReceive8==1) 
           {  
              Uouta_av=(ADCRMS.Uinv_a+P_NODE.Uinva)/2; 
			  Uoutb_av=(ADCRMS.Uinv_b+P_NODE.Uinvb)/2; 
			  Uoutc_av=(ADCRMS.Uinv_c+P_NODE.Uinvc)/2; 
 
			  Urmstempa=Urmstempa0+(PIDUArms.out-Urmstempa0+P_NODE.PIDUouta)/2; 
		      Urmstempb=Urmstempb0+(PIDUBrms.out-Urmstempb0+P_NODE.PIDUoutb)/2; 
		      Urmstempc=Urmstempc0+(PIDUCrms.out-Urmstempc0+P_NODE.PIDUoutc)/2; 
 
              PQ.A.Paver=(PQ.A.Pout+P_NODE.Pa)/2; 
			  PQ.A.Qaver=(PQ.A.Qout+P_NODE.Qa)/2; 
			  PQ.B.Paver=(PQ.B.Pout+P_NODE.Pb)/2; 
			  PQ.B.Qaver=(PQ.B.Qout+P_NODE.Qb)/2; 
			  PQ.C.Paver=(PQ.C.Pout+P_NODE.Pc)/2; 
			  PQ.C.Qaver=(PQ.C.Qout+P_NODE.Qc)/2;           
           } 
		else 
		   { 
		     Uouta_av=ADCRMS.Uinv_a; 
	    	 Uoutb_av=ADCRMS.Uinv_b; 
		     Uoutc_av=ADCRMS.Uinv_c; 
 
		     Urmstempa=PIDUArms.out; 
		     Urmstempb=PIDUBrms.out; 
		     Urmstempc=PIDUCrms.out; 
 
		     PQ.A.Paver=PQ.A.Pout; 
	         PQ.A.Qaver=PQ.A.Qout; 
             PQ.B.Paver=PQ.B.Pout; 
		     PQ.B.Qaver=PQ.B.Qout; 
		     PQ.C.Paver=PQ.C.Pout; 
		     PQ.C.Qaver=PQ.C.Qout; 
		    } 
		//   FLAG.D.bit.Candatadeal=1; 
		   FLAG.B.bit.PQReceive8=0; 
        } 
      else 
    	{ 
    	 Uouta_av=ADCRMS.Uinv_a; 
		 Uoutb_av=ADCRMS.Uinv_b; 
		 Uoutc_av=ADCRMS.Uinv_c; 
 
		 Urmstempa=PIDUArms.out; 
		 Urmstempb=PIDUBrms.out; 
		 Urmstempc=PIDUCrms.out; 
 
		 PQ.A.Paver=PQ.A.Pout; 
	     PQ.A.Qaver=PQ.A.Qout; 
         PQ.B.Paver=PQ.B.Pout; 
		 PQ.B.Qaver=PQ.B.Qout; 
		 PQ.C.Paver=PQ.C.Pout; 
		 PQ.C.Qaver=PQ.C.Qout;   	 
    	}   
	} 
   else 
     { 
         Uouta_av=ADCRMS.Uinv_a; 
		 Uoutb_av=ADCRMS.Uinv_b; 
		 Uoutc_av=ADCRMS.Uinv_c; 
 
		 Urmstempa=PIDUArms.out; 
		 Urmstempb=PIDUBrms.out; 
		 Urmstempc=PIDUCrms.out; 
 
		 PQ.A.Paver=PQ.A.Pout; 
	     PQ.A.Qaver=PQ.A.Qout; 
         PQ.B.Paver=PQ.B.Pout; 
		 PQ.B.Qaver=PQ.B.Qout; 
		 PQ.C.Paver=PQ.C.Pout; 
		 PQ.C.Qaver=PQ.C.Qout; 
     } 
*/ 
  
 
/* 
	   if( FLAG.B.bit.GrdConAldy7==0) 
	     {	       
	   
  
             if(FLAG.B.bit.BgPhlock5==1)  
              {  
  
               UREFa=UREF; 
			   UREFb=UREF; 
			   UREFc=UREF; 
              } 
 
  
             if(FLAG.B.bit.BgKMIPClose9==1) 
   
            {             
      
             UREFa=UREF; 
			 UREFb=UREF; 
			 UREFc=UREF; 
            }  
         
	      if(FLAG.D.bit.KMIP0==1) 
            { 
              FLAG.B.bit.GrdConAldy7=1; 
 
			  K.PaverI=0; 
              K.PaverP=0; 
              K.QaverI=0; 
              K.QaverP=0;    //14 
	//	      FlagKn.bit.BgSftinGrid=1; 
           //   Iaver_M_on(); //并入电网了,打开测量 
            } 
		 
 
	            
          if((FLAG.A.bit.Ifilta13==0)||(FLAG.A.bit.Ifiltb14==0)||(FLAG.A.bit.Ifiltc15==0)) 
            { 
           //  FlagKm.bit.SysStopCon=1; 
//			 DisPWM(); 
            }     
         } 
        
//////////////////////////////////////// 
       if(FLAG.B.bit.GrdConAldy7==1) 
         { 
           if(FLAG.B.bit.BgExitGrid13==1)  
	         {  
               Iaver_M_off(); 
	         } 
		   if((ADCRMS.Iload_a<15)&&(ADCRMS.Iload_b<15)&&(ADCRMS.Iload_c<15)&&(FLAG.B.bit.BgExitGrid13==1)) 
            { 
       //      KMIP_off(); //KM1_off() 
			 FLAG.B.bit.BgExitGrid13=0; 
			 FLAG.B.bit.UaloneRef6=0; 
		//	 UREFa=UREF; 
		//	 UREFb=UREF; 
		//	 UREFc=UREF; 
            }  
         }  
       
   */   	            
       p_flag=0;  
//       conts1++; 
    }  
 
} 
 
void KMIP_on() 
{ 
  Urmstempa0=PIDUArms.out; 
  Urmstempb0=PIDUBrms.out; 
  Urmstempc0=PIDUCrms.out; 
  GpioDataRegs.GPCSET.bit.GPIO75=1; 
  FLAG.B.bit.GrdConAldy7=1; 
  FLAG.D.bit.KMIP0=1; 
  FLAG1.Csfw4.bit.KMIPon=1; 
  Iaver_M_on(); 
 // KMSS_off(); 
}  
 
void KMIP_off() 
{ 
  FLAG.B.bit.GrdConAldy7=0; 
  GpioDataRegs.GPCCLEAR.bit.GPIO75=1; 
  FLAG.D.bit.KMIP0=0; 
  FLAG1.Csfw4.bit.KMIPon=0; 
  Iaver_M_off();  
  FLAG1.Csfw4.bit.BgKMIPClose=0; 
  Pref1=0; 
 
  PIDPa.outtempiRem=0; 
  PIDPa.outtempkRem=0; 
  PIDPa.outi=0; 
  PIDQa.outi=0; 
  PIDQa.outtempiRem=0; 
  PIDQa.outtempkRem=0; 
  PIDQa.out=0; 
  PIDPa.out=0; 
 
 
} 
 
void KMSS_on() 
{ 
  GpioDataRegs.GPCSET.bit.GPIO76=1; 
  FLAG.D.bit.KMSS1=1; 
  FLAG.B.bit.GrdConAldy7=1; 
} 
 
void KMSS_off() 
{ 
  GpioDataRegs.GPCCLEAR.bit.GPIO76=1; 
  FLAG.D.bit.KmSSforns4=0; 
} 
 
 
void KM_Timer() 
{ 
/* 
  if(FLAG.D.bit.KMSS1) 
    { 
      if(FLAG.D.bit.KmSSforns4==0) 
         { 
           if((Count_20ms-Km1Ttemp)>=KMSSns) 
	         FLAG.D.bit.KmSSforns4=1; 
         } 
    } 
  else 
    { 
      Km1Ttemp=Count_20ms; 
      FLAG.D.bit.KmSSforns4=0; 
    } 
 
 
 
  if(FLAG.D.bit.KMIP0) 
    { 
      if(FLAG.D.bit.KmIPforns3==0) 
         { 
           if((Count_20ms-Km2Ttemp)>=KMIPns) 
	         FLAG.D.bit.KmIPforns3=1; 
         } 
    } 
  else 
    { 
      Km2Ttemp=Count_20ms; 
      FLAG.D.bit.KmIPforns3=0; 
    } 
*/ 
 
} 
 
 
void Iaver_M_on() 
{ 
 // GpioDataRegs.GPCCLEAR.bit.GPIO79=1;  // 0: switch on 
  GpioDataRegs.GPCCLEAR.bit.GPIO79=1; 
  
//  GpioDataRegs.GPCSET.bit.GPIO74=1;   //KM_BAT_ON 
  
  FLAG.D.bit.Iavermon2=1; 
 
 
} 
 
void Iaver_M_off() 
{ 
//  GpioDataRegs.GPCSET.bit.GPIO79=1;  //1 : switch off 
    GpioDataRegs.GPCSET.bit.GPIO79=1;  
   
//  GpioDataRegs.GPCCLEAR.bit.GPIO74=0; 
  FLAG.D.bit.Iavermon2=0; 
} 
 
 
void Adc_Soft(void) 
{ 
  int i; 
  AdcRegs.ADCTRL2.bit.SOC_SEQ1=1;  
  while(AdcRegs.ADCST.bit.INT_SEQ1==0); 
//  EPwm1Regs.ETCLR.bit.SOCA=1; //clears the ETFLG[OSOCA] flag bit 
  AdcRegs.ADCST.bit.INT_SEQ1_CLR=1; //clear the interrupt flag 
  AdcRegs.ADCTRL2.bit.RST_SEQ1=1; //reset to state conv00 
  for (i=0;i<256;i++) 
  { 
   AdcRegs.ADCTRL2.bit.SOC_SEQ1=1; 
   while (AdcRegs.ADCST.bit.INT_SEQ1==0); 
//   EPwm1Regs.ETCLR.bit.SOCA=1; //clears the ETFLG[OSOCA] flag bit 
   AdcRegs.ADCST.bit.INT_SEQ1_CLR=1; //clear the interrupt flag 
//////////////////////////////////////////////////////////////// 
   //data deal 
///////////////////////////////////////////////////////// 
   DELAY_US(104L);   
//   AdcRegs.ADCST.bit.INT_SEQ1_CLR=1; 
   AdcRegs.ADCTRL2.bit.RST_SEQ1=1; //reset to state conv00 
  } 
} 
 
void Pulse_judge(void) 
{ 
  if((CUNTER.Ery20ms-CUNTER.Fouta1)>2) 
    FLAG1.Sdsiga1.bit.Foutaon=1; 
  else 
    FLAG1.Sdsiga1.bit.Foutaon=0; 
 
  if((CUNTER.Ery20ms-CUNTER.Finva2)>2) 
    FLAG1.Sdsiga1.bit.Finvaon=1; 
  else 
    FLAG1.Sdsiga1.bit.Finvaon=0; 
 
  if((CUNTER.Ery20ms-CUNTER.Fpulse5)>2) 
    FLAG1.Sdsiga1.bit.Fpulseon=1; 
  else 
    FLAG1.Sdsiga1.bit.Fpulseon=0; 
 
} 
 
 
Uint16 Sum_Uga1=0,Sum_Uga2=0, Sum_Ugb1=0,Sum_Ugb2=0,Sum_Ugc1=0,Sum_Ugc2=0; 
/*  
 Ke.Ugrid1P20=1081;  //220*1.2*4.096=1081; 
 Ke.UgridP10=90;  //22*4.096=90 
 Ke.UgridP80=720; //220*0.8*4.096=720 
 */ 
void Ugrid_judge(void) 
{ 
 
/*    
   if(ADCRMS.Uout_a<Ke.UgridP10)  
      { 
       Sum_Ug2=CUNTER.Ery20ms; 
       if((CUNTER.Ery20ms-Sum_Ug1)>50) 
	     {	        
		   FLAG1.Sdsiga2.bit.Uaout=1; 
		   Sum_Ug1=CUNTER.Ery20ms; 
	     } 
       return; 
      } 
   else if((ADCRMS.Uout_a>Ke.UgridP80)&&(ADCRMS.Uout_a<Ke.Ugrid1P20)) 
     { 
        Sum_Ug1=CUNTER.Ery20ms; 
        Sum_Ug3=CUNTER.Ery20ms; 
 
        if((CUNTER.Ery20ms-Sum_Ug2)>50) 
	     {	        
		   FLAG1.Sdsiga2.bit.Uaout=0; 
		   Sum_Ug2=CUNTER.Ery20ms; 
	     } 
     } 
   else 
     { 
         Sum_Ug1=CUNTER.Ery20ms; 
		 Sum_Ug2=CUNTER.Ery20ms; 
 
         if((CUNTER.Ery20ms-Sum_Ug3)>50) 
	      {	        
		    FLAG1.Sdsiga2.bit.Uaout=1; 
		    Sum_Ug3=CUNTER.Ery20ms; 
	      }   
     } 
*/ 
 
   if((ADCRMS.Uout_a>Ke.UgridP80)&&(ADCRMS.Uout_a<Ke.Ugrid1P20)) 
	 { 
	    Sum_Uga1=CUNTER.Ery20ms; 
         
        if((CUNTER.Ery20ms-Sum_Uga2)>3) 
		  { 
		    FLAG1.Sdsiga2.bit.Uaout=0; 
			Sum_Uga2=CUNTER.Ery20ms; 
		  } 
 
	  } 
    else 
	  { 
	    Sum_Uga2=CUNTER.Ery20ms; 
        if((CUNTER.Ery20ms-Sum_Uga1)>3) 
		  { 
		     FLAG1.Sdsiga2.bit.Uaout=1; 
			 Sum_Uga1=CUNTER.Ery20ms; 
		  } 
	  } 
//-------------------------------------------------------------------------- 
    if((ADCRMS.Uout_b>Ke.UgridP80)&&(ADCRMS.Uout_b<Ke.Ugrid1P20)) 
	  { 
	    Sum_Ugb1=CUNTER.Ery20ms; 
         
        if((CUNTER.Ery20ms-Sum_Ugb2)>3) 
		  { 
		    FLAG1.Sdsiga1.bit.Ubout=0; 
			Sum_Ugb2=CUNTER.Ery20ms; 
		  } 
 
	  } 
    else 
	  { 
	    Sum_Ugb2=CUNTER.Ery20ms; 
        if((CUNTER.Ery20ms-Sum_Ugb1)>3) 
		  { 
		     FLAG1.Sdsiga1.bit.Ubout=1; 
			 Sum_Ugb1=CUNTER.Ery20ms; 
		  } 
	  } 
 
//----------------------------------------------------------------------------- 
      if((ADCRMS.Uout_c>Ke.UgridP80)&&(ADCRMS.Uout_c<Ke.Ugrid1P20)) 
	 { 
	    Sum_Ugc1=CUNTER.Ery20ms; 
         
        if((CUNTER.Ery20ms-Sum_Ugc2)>3) 
		  { 
		    FLAG1.Sdsiga1.bit.Ucout=0; 
			Sum_Ugc2=CUNTER.Ery20ms; 
		  } 
 
	  } 
    else 
	  { 
	    Sum_Ugc2=CUNTER.Ery20ms; 
        if((CUNTER.Ery20ms-Sum_Ugc1)>3) 
		  { 
		     FLAG1.Sdsiga1.bit.Ucout=1; 
			 Sum_Ugc1=CUNTER.Ery20ms; 
		  } 
	  } 
 
//------------------------------------------------------------------------------ 
    if(FLAG1.Sdsiga1.bit.Ubout||FLAG1.Sdsiga1.bit.Ucout||FLAG1.Sdsiga2.bit.Uaout) 
	   FLAG1.Sdsiga2.bit.Uout=1; 
    else 
	   FLAG1.Sdsiga2.bit.Uout=0; 
 
 
    if(FLAG1.Sdsiga2.bit.Uout&&FLAG1.Csfw4.bit.KMIPon) 
      {  
        FLAG1.Csfw4.bit.DisWork=1; 
		DisWorkNum=6; 
      } 
 
//------------------------------------------------------------------------------ 
} 
 
 
 
Uint16 Sum_inv, Sum_unbalance,Sum_inva1,Sum_inva2,Sum_invb1,Sum_invb2,Sum_invc1,Sum_invc2; 
void U_Protect(void) 
{ 
   Udc_judge();    
//------------------------------------------------------- 
  if(FLAG1.Csfw4.bit.Usoft==1) 
  return; 
 
  
  if((ADCRMS.Uinv_a<1004)&&(ADCRMS.Uinv_a>819))      //245*4.096=1004   200*4.096=819 
    { 
      Sum_inva2=CUNTER.Ery20ms; 
      if((CUNTER.Ery20ms-Sum_inva1)>=3)  
		  { 
		     Sum_inva1=CUNTER.Ery20ms; 
		     FLAG1.Sdsiga1.bit.Uainv=0; 
		  } 
    } 
  else 
    {  
	  Sum_inva1=CUNTER.Ery20ms;    
      if((CUNTER.Ery20ms-Sum_inva2)>=3)  
		  { 
		     Sum_inva2=CUNTER.Ery20ms; 
		     FLAG1.Sdsiga1.bit.Uainv=1; 
		  } 
    } 
 
 //----------------------------------- 
  if((ADCRMS.Uinv_b<1004)&&(ADCRMS.Uinv_b>819) )     //245*4.096=1004   200*4.096=819 
    { 
      Sum_invb2=CUNTER.Ery20ms; 
      if((CUNTER.Ery20ms-Sum_invb1)>=3)  
		  { 
		     Sum_invb1=CUNTER.Ery20ms; 
		     FLAG1.Sdsiga1.bit.Ubinv=0; 
		  } 
    } 
  else 
    {  
	  Sum_invb1=CUNTER.Ery20ms;    
      if((CUNTER.Ery20ms-Sum_invb2)>=3)  
		  { 
		     Sum_invb2=CUNTER.Ery20ms; 
		     FLAG1.Sdsiga1.bit.Ubinv=1; 
		  } 
    } 
 
//------------------------------------------------- 
  if((ADCRMS.Uinv_c<1004)&&(ADCRMS.Uinv_c>819))      //245*4.096=1004   200*4.096=819 
    { 
      Sum_invc2=CUNTER.Ery20ms; 
      if((CUNTER.Ery20ms-Sum_invc1)>=3)  
		  { 
		     Sum_invc1=CUNTER.Ery20ms; 
		     FLAG1.Sdsiga1.bit.Ucinv=0; 
		  } 
    } 
  else 
    {  
	  Sum_invc1=CUNTER.Ery20ms;    
      if((CUNTER.Ery20ms-Sum_invc2)>=3)  
		  { 
		     Sum_invc2=CUNTER.Ery20ms; 
		     FLAG1.Sdsiga1.bit.Ucinv=1; 
		  } 
    } 
 
//------------------------------------------------------ 
   if(FLAG1.Sdsiga1.bit.Uainv||FLAG1.Sdsiga1.bit.Ubinv||FLAG1.Sdsiga1.bit.Ucinv) 
     FLAG1.Sdsiga1.bit.Uinv=1; 
   else  
     FLAG1.Sdsiga1.bit.Uinv=0;   
      
     
    if(FLAG1.Sdsiga1.bit.Uinv&&FLAG1.Csfw4.bit.KMIPon) 
      {  
        FLAG1.Csfw4.bit.DisWork=1; 
		DisWorkNum=1; 
      } 
   
} 
 
 
 
int32 Udccmp=0; 
int16 BgUdcjudge=0; 
void Udc_judge(void) 
{ 
   
  if(BgUdcjudge==0) 
    { 
      BgUdcjudge=1; 
	  Udccmp=(int32)K_Udg2phy*340/64;   //345 
    } 
 
  if(ADCRMS.Udc_bus>Udccmp) 
   { 
     Udccmp=(int32)K_Udg2phy*330/64;    //340 
	 FLAG1.Sdsiga2.bit.Udc=0; 
   } 
  else 
   { 
     Udccmp=(int32)K_Udg2phy*340/64;    //345 
	 FLAG1.Sdsiga2.bit.Udc=1; 
   } 
 
} 
 
Uint16 Sum_IloadP; 
int32 iaddA,isubA,thermA,iaddB,isubB,thermB,iaddC,isubC,thermC; 
void I_Protect(void) 
{ 
   if(ADCRMS.Iload_a>Ke.Iload1P20) 
      { 
        FLAG1.Sdsiga1.bit.Iaload=1; 
      } 
   else 
        FLAG1.Sdsiga1.bit.Iaload=0; 
    
   if(ADCRMS.Iload_b>Ke.Iload1P20) 
      { 
        FLAG1.Sdsiga1.bit.Ibload=1; 
      //  return; 
      } 
   else 
        FLAG1.Sdsiga1.bit.Ibload=0; 
 
   if(ADCRMS.Iload_c>Ke.Iload1P20) 
      { 
        FLAG1.Sdsiga1.bit.Icload=1; 
      //  return; 
      } 
   else 
        FLAG1.Sdsiga1.bit.Icload=0; 
    
    
   if(FLAG1.Sdsiga1.bit.Iaload||FLAG1.Sdsiga1.bit.Ibload||FLAG1.Sdsiga1.bit.Icload) 
      { 
        FLAG1.Sdsiga1.bit.IOvload=1; 
      //  return; 
      } 
   else 
        FLAG1.Sdsiga1.bit.IOvload=0; 
	  /* 
   if(FLAG1.Csfw4.bit.ALOLPR==0) 
     { 
       FLAG1.Csfw4.bit.BgILoadP=1; 
	   Sum_IloadP=CUNTER.Ery20ms; 
	   return; 
     } 
     
	if((CUNTER.Ery20ms-Sum_IloadP)>=50) 
	  { 
	    Sum_IloadP=CUNTER.Ery20ms; 
        iaddA=ADCRMS.Iload_a+Ke.Iload1P05; 
		isubA=ADCRMS.Iload_a-Ke.Iload1P05; 
		thermA+=iaddA*isubA; 
 
        iaddB=ADCRMS.Iload_b+Ke.Iload1P05; 
		isubB=ADCRMS.Iload_b-Ke.Iload1P05; 
		thermB+=iaddB*isubB; 
 
		iaddC=ADCRMS.Iload_c+Ke.Iload1P05; 
		isubC=ADCRMS.Iload_c-Ke.Iload1P05; 
		thermC+=iaddC*isubC; 
 
        if(thermA>0) 
		  { 
		    if(thermA>Ke.Thermconst) 
			  { 
			    thermA=Ke.Thermconst; 
				FLAG1.Sdsiga1.bit.IOvload=1; 
			  } 
		  } 
		else 
		 thermA=0; 
 
        if(thermB>0) 
		  { 
		    if(thermB>Ke.Thermconst) 
			  { 
			    thermB=Ke.Thermconst; 
				FLAG1.Sdsiga1.bit.IOvload=1; 
			  } 
		  } 
		else 
		 thermB=0; 
 
		if(thermC>0) 
		  { 
		    if(thermC>Ke.Thermconst) 
			  { 
			    thermC=Ke.Thermconst; 
				FLAG1.Sdsiga1.bit.IOvload=1; 
			  } 
		  } 
		else 
		 thermC=0; 
	  } 
*/ 
}   
 
/*   
void cstop(void) 
{ 
  DisPWM(); 
 
} 
*/ 
 
void sync3_configer(void) 
{ 
   EALLOW;  // This is needed to write to EALLOW protected registers 
   PieVectTable.TINT0 = &cpu_timer0_isr; 
   GpioCtrlRegs.GPAPUD.bit.GPIO20 = 0;   // Enable   
   GpioCtrlRegs.GPAMUX2.bit.GPIO20 = 0;   
   GpioCtrlRegs.GPADIR.bit.GPIO20 = 1;  
   EDIS;    // This is needed to disable write to EALLOW protected registers 
   InitCpuTimers();   // For this example, only initialize the Cpu Timers 
   ConfigCpuTimer(&CpuTimer0, 150, 10000);//10ms flip 
   CpuTimer0Regs.TCR.all = 0x4001; // Use write-only instruction to set TSS bit = 0 
   PieCtrlRegs.PIEIER1.bit.INTx7 = 1; 
//   IER|=M_INT1;	 
} 
 
//------------------------------------------------------------------- 
void PQaver_C1(void) 
{ 
   PIDPa.outMax=200; 
   PIDPa.outMin=-200 ; 
   PIDPb.outMax=200 ; 
   PIDPb.outMin=-200; 
   PIDPc.outMax=200; 
   PIDPc.outMin=-200; 
    
   PIDQa.outMax=40 ; 
   PIDQa.outMin=-40; 
   PIDQb.outMax=40; 
   PIDQb.outMin=-40; 
   PIDQc.outMax=40; 
   PIDQc.outMin=-40;   
 
 
   PIDPa.errmax=0; 
   PIDPa.errmin=0; 
   PIDQa.errmax=0; 
   PIDQa.errmin=0; 
 
  // IREF=(PQ.A.Pout+PQ.B.Pout+PQ.C.Pout)/630; 
     IREF=Pref/210; 
 
/* 
	 PQ.A.Pout=(PQ.A.P*9+PQ.A.P0*7+PQ.A.PRem)/16; 
	 PQ.A.PRem=(PQ.A.P*9+PQ.A.P0*7+PQ.A.PRem)%16; 
	 PQ.A.P0=PQ.A.P; 
*/////////////////////////////////// 
      
	 PIDPa.Ref=Pref; 
	 PIDPa.Fdb=(PQ.A.Pout+PQ.B.Pout+PQ.C.Pout)/3; 
   //  pid_reg_calc(&PIDPa);   
 
     PIDPa.err = PIDPa.Ref - PIDPa.Fdb; 
     
     if(PIDPa.err>PIDPa.errmax) 
     PIDPa.err=PIDPa.err-PIDPa.errmax; 
     else if(PIDPa.err<PIDPa.errmin) 
     PIDPa.err=PIDPa.err-PIDPa.errmin; 
     else PIDPa.err=0; 
 
 //    PIDPa.outk=(K.PaverP*PIDPa.err)>>12;  // 1/256 1/4096 
     PIDPa.outtempk=K.PaverP*PIDPa.err+PIDPa.outtempkRem; 
	 PIDPa.outk=PIDPa.outtempk>>12; 
	 PIDPa.outtempkRem=PIDPa.outtempk-(PIDPa.outk<<12); 
 
	 PIDPa.Remtemp=K.PaverI*PIDPa.err+PIDPa.outtempiRem; 
	 PIDPa.Remtemp1=(K.PaverI*PIDPa.err+PIDPa.outtempiRem)>>8; 
     PIDPa.outi=PIDPa.outi+PIDPa.Remtemp1;   
  //  PIDPa.outtempiRem=	PIDPa.Remtemp%64; 
     PIDPa.outtempiRem=	PIDPa.Remtemp-(PIDPa.Remtemp1<<8); 
     if(PIDPa.outi>PIDPa.outMax) PIDPa.outi=PIDPa.outMax; 
	 if(PIDPa.outi<PIDPa.outMin) PIDPa.outi=PIDPa.outMin; 
     PIDPa.out=PIDPa.outk+PIDPa.outi; 
     if(PIDPa.out>PIDPa.outMax) PIDPa.out=PIDPa.outMax; 
	 if(PIDPa.out<PIDPa.outMin) PIDPa.out=PIDPa.outMin; 
 
  ////////////////////////////////////////////////////////////////////////////////////////// 
//	 PIDQa.Ref=PQ.A.Qaver; 
//	 PIDQa.Fdb=PQ.A.Q; 
     PIDQa.Ref=0; 
	 PIDQa.Fdb=(PQ.A.Qout+PQ.B.Qout+PQ.C.Qout)/3; 
 
//	 pid_reg_calc(&PIDQa);   
     PIDQa.err =-(PIDQa.Ref - PIDQa.Fdb); 
     if(PIDQa.err>PIDQa.errmax) 
     PIDQa.err=PIDQa.err-PIDQa.errmax; 
     else if(PIDQa.err<PIDQa.errmin) 
     PIDQa.err=PIDQa.err-PIDQa.errmin; 
	 else PIDQa.err=0; 
      
//   PIDQa.outk=(K.QaverP*PIDQa.err)>>8;  //    
	 
	 PIDQa.outtempk=K.QaverP*PIDQa.err+PIDQa.outtempkRem; 
	 PIDQa.outk=PIDQa.outtempk>>12; 
	 PIDQa.outtempkRem=PIDQa.outtempk-(PIDQa.outk<<12); 
	 
	 PIDQa.Remtemp=K.QaverI*PIDQa.err+PIDQa.outtempiRem; 
	 PIDQa.Remtemp1=(K.QaverI*PIDQa.err+PIDQa.outtempiRem)>>8; 
     PIDQa.outi=PIDQa.outi+PIDQa.Remtemp1;   
  //  PIDQa.outtempiRem=	PIDQa.Remtemp%64; 
     PIDQa.outtempiRem=	PIDQa.Remtemp-(PIDQa.Remtemp1<<8); 
     if(PIDQa.outi>PIDQa.outMax) PIDQa.outi=PIDQa.outMax; 
	 if(PIDQa.outi<PIDQa.outMin) PIDQa.outi=PIDQa.outMin; 
     PIDQa.out=PIDQa.outk+PIDQa.outi; 
     if(PIDQa.out>PIDQa.outMax) PIDQa.out=PIDQa.outMax; 
	 if(PIDQa.out<PIDQa.outMin) PIDQa.out=PIDQa.outMin; 
 
} 
 
 
//------------------------------------------------------------------- 
 
void PQaver_C(void) 
{ 
/* 
	 PQ.A.Pout=(PQ.A.P*9+PQ.A.P0*7+PQ.A.PRem)/16; 
	 PQ.A.PRem=(PQ.A.P*9+PQ.A.P0*7+PQ.A.PRem)%16; 
	 PQ.A.P0=PQ.A.P; 
*/////////////////////////////////// 
      
	 PIDPa.Ref=Pref; 
	 PIDPa.Fdb=PQ.A.Pout; 
   //  pid_reg_calc(&PIDPa);   
 
     PIDPa.err = PIDPa.Ref - PIDPa.Fdb; 
     
     if(PIDPa.err>PIDPa.errmax) 
     PIDPa.err=PIDPa.err-PIDPa.errmax; 
     else if(PIDPa.err<PIDPa.errmin) 
     PIDPa.err=PIDPa.err-PIDPa.errmin; 
     else PIDPa.err=0; 
 
     PIDPa.outk=(K.PaverP*PIDPa.err)>>12;  // 1/256 1/4096 
	 PIDPa.Remtemp=K.PaverI*PIDPa.err+PIDPa.outtempiRem; 
	 PIDPa.Remtemp1=(K.PaverI*PIDPa.err+PIDPa.outtempiRem)>>12; 
     PIDPa.outi=PIDPa.outi+PIDPa.Remtemp1;   
  //  PIDPa.outtempiRem=	PIDPa.Remtemp%64; 
     PIDPa.outtempiRem=	PIDPa.Remtemp-(PIDPa.Remtemp1<<12); 
     if(PIDPa.outi>PIDPa.outMax) PIDPa.outi=PIDPa.outMax; 
	 if(PIDPa.outi<PIDPa.outMin) PIDPa.outi=PIDPa.outMin; 
     PIDPa.out=PIDPa.outk+PIDPa.outi; 
     if(PIDPa.out>PIDPa.outMax) PIDPa.out=PIDPa.outMax; 
	 if(PIDPa.out<PIDPa.outMin) PIDPa.out=PIDPa.outMin; 
 
//	  PIDPb.Ref=0; 
//	  PIDPb.Fdb=PQ.B.Paver; 
      
      PIDPb.Ref=PQ.B.Paver; 
	  PIDPb.Fdb=PQ.B.Pout; 
      //     pid_reg_calc(&PIDPb);  
      PIDPb.err = PIDPb.Ref - PIDPb.Fdb; 
      if(PIDPb.err>PIDPb.errmax) 
      PIDPb.err=PIDPb.err-PIDPb.errmax; 
      else if(PIDPb.err<PIDPb.errmin) 
      PIDPb.err=PIDPb.err-PIDPb.errmin; 
	  else PIDPb.err=0; 
      PIDPb.outk=(K.PaverP*PIDPb.err)>>12;  // 1/256 1/4096 
      PIDPb.Remtemp=K.PaverI*PIDPb.err+PIDPb.outtempiRem; 
	  PIDPb.Remtemp1=(K.PaverI*PIDPb.err+PIDPb.outtempiRem)>>12; 
      PIDPb.outi=PIDPb.outi+PIDPb.Remtemp1;   
  //  PIDPb.outtempiRem=	PIDPb.Remtemp%64; 
      PIDPb.outtempiRem=PIDPb.Remtemp-(PIDPb.Remtemp1<<12); 
      if(PIDPb.outi>PIDPb.outMax) PIDPb.outi=PIDPb.outMax; 
	  if(PIDPb.outi<PIDPb.outMin) PIDPb.outi=PIDPb.outMin; 
      PIDPb.out=PIDPb.outk+PIDPb.outi; 
      if(PIDPb.out>PIDPb.outMax) PIDPb.out=PIDPb.outMax; 
	  if(PIDPb.out<PIDPb.outMin) PIDPb.out=PIDPb.outMin; 
 
	  	   
	  PIDPc.Ref=PQ.C.Paver; 
	  PIDPc.Fdb=PQ.C.Pout; 
 
    // pid_reg_calc(&PIDPc);  
     PIDPc.err=PIDPc.Ref - PIDPc.Fdb; 
     if(PIDPc.err>PIDPc.errmax) 
     PIDPc.err=PIDPc.err-PIDPc.errmax; 
     else if(PIDPc.err<PIDPc.errmin) 
     PIDPc.err=PIDPc.err-PIDPc.errmin; 
	 else PIDPc.err=0; 
     PIDPc.outk=(K.PaverP*PIDPc.err)>>12;  // 1/256 1/4096 
	 PIDPc.Remtemp=K.PaverI*PIDPc.err+PIDPc.outtempiRem; 
	 PIDPc.Remtemp1=(K.PaverI*PIDPc.err+PIDPc.outtempiRem)>>12; 
     PIDPc.outi=PIDPc.outi+PIDPc.Remtemp1;   
  //  PIDPc.outtempiRem=	PIDPc.Remtemp%64; 
     PIDPc.outtempiRem=	PIDPc.Remtemp-(PIDPc.Remtemp1<<12); 
     if(PIDPc.outi>PIDPc.outMax) PIDPc.outi=PIDPc.outMax; 
	 if(PIDPc.outi<PIDPc.outMin) PIDPc.outi=PIDPc.outMin; 
     PIDPc.out=PIDPc.outk+PIDPc.outi; 
     if(PIDPc.out>PIDPc.outMax) PIDPc.out=PIDPc.outMax; 
	 if(PIDPc.out<PIDPc.outMin) PIDPc.out=PIDPc.outMin; 
////////////////////////////////////////////////////////////////////////////////////////// 
//	 PIDQa.Ref=PQ.A.Qaver; 
//	 PIDQa.Fdb=PQ.A.Q; 
     PIDQa.Ref=0; 
	 PIDQa.Fdb=PQ.A.Qout; 
 
 
//	 pid_reg_calc(&PIDQa);   
     PIDQa.err = PIDQa.Ref - PIDQa.Fdb; 
     if(PIDQa.err>PIDQa.errmax) 
     PIDQa.err=PIDQa.err-PIDQa.errmax; 
     else if(PIDQa.err<PIDQa.errmin) 
     PIDQa.err=PIDQa.err-PIDQa.errmin; 
	 else PIDQa.err=0; 
     PIDQa.outk=(K.QaverP*PIDQa.err)>>12;  //    
	 PIDQa.Remtemp=K.QaverI*PIDQa.err+PIDQa.outtempiRem; 
	 PIDQa.Remtemp1=(K.QaverI*PIDQa.err+PIDQa.outtempiRem)>>12; 
     PIDQa.outi=PIDQa.outi+PIDQa.Remtemp1;   
  //  PIDQa.outtempiRem=	PIDQa.Remtemp%64; 
     PIDQa.outtempiRem=	PIDQa.Remtemp-(PIDQa.Remtemp1<<12); 
     if(PIDQa.outi>PIDQa.outMax) PIDQa.outi=PIDQa.outMax; 
	 if(PIDQa.outi<PIDQa.outMin) PIDQa.outi=PIDQa.outMin; 
     PIDQa.out=PIDQa.outk+PIDQa.outi; 
     if(PIDQa.out>PIDQa.outMax) PIDQa.out=PIDQa.outMax; 
	 if(PIDQa.out<PIDQa.outMin) PIDQa.out=PIDQa.outMin; 
 
 
//	 PIDQb.Ref=PQ.B.Qaver; 
//	 PIDQb.Fdb=PQ.B.Q; 
     PIDQb.Ref=0; 
	 PIDQb.Fdb=PQ.B.Qout; 
 
 
//	 pid_reg_calc(&PIDQb); 
     PIDQb.err = PIDQb.Ref - PIDQb.Fdb; 
     if(PIDQb.err>PIDQb.errmax) 
     PIDQb.err=PIDQb.err-PIDQb.errmax; 
     else if(PIDQb.err<PIDQb.errmin) 
     PIDQb.err=PIDQb.err-PIDQb.errmin; 
	 else PIDQb.err=0; 
     PIDQb.outk=(K.QaverP*PIDQb.err)>>12;  // 1/256 
	 PIDQb.Remtemp=K.QaverI*PIDQb.err+PIDQb.outtempiRem; 
	 PIDQb.Remtemp1=(K.QaverI*PIDQb.err+PIDQb.outtempiRem)>>12; 
     PIDQb.outi=PIDQb.outi+PIDQb.Remtemp1;   
  //  PIDQb.outtempiRem=	PIDQb.Remtemp%64; 
     PIDQb.outtempiRem=	PIDQb.Remtemp-(PIDQb.Remtemp1<<12); 
     if(PIDQb.outi>PIDQb.outMax) PIDQb.outi=PIDQb.outMax; 
	 if(PIDQb.outi<PIDQb.outMin) PIDQb.outi=PIDQb.outMin; 
     PIDQb.out=PIDQb.outk+PIDQb.outi; 
     if(PIDQb.out>PIDQb.outMax) PIDQb.out=PIDQb.outMax; 
	 if(PIDQb.out<PIDQb.outMin) PIDQb.out=PIDQb.outMin; 
 
 
//	  PIDQc.Ref=PQ.C.Qaver; 
//	  PIDQc.Fdb=PQ.C.Q; 
      PIDQc.Ref=0; 
	  PIDQc.Fdb=PQ.C.Qout; 
 
 
//	 pid_reg_calc(&PIDQc); 
      PIDQc.err = PIDQc.Ref - PIDQc.Fdb; 
      if(PIDQc.err>PIDQc.errmax) 
      PIDQc.err=PIDQc.err-PIDQc.errmax; 
      else if(PIDQc.err<PIDQc.errmin) 
      PIDQc.err=PIDQc.err-PIDQc.errmin; 
	  else PIDQc.err=0; 
      PIDQc.outk=(K.QaverP*PIDQc.err)>>12;  // 1/256 
	  PIDQc.Remtemp=K.QaverI*PIDQc.err+PIDQc.outtempiRem; 
	  PIDQc.Remtemp1=(K.QaverI*PIDQc.err+PIDQc.outtempiRem)>>12; 
      PIDQc.outi=PIDQc.outi+PIDQc.Remtemp1;   
  //  PIDQc.outtempiRem=	PIDQc.Remtemp%64; 
      PIDQc.outtempiRem=PIDQc.Remtemp-(PIDQc.Remtemp1<<12); 
      if(PIDQc.outi>PIDQc.outMax) PIDQc.outi=PIDQc.outMax; 
      if(PIDQc.outi<PIDQc.outMin) PIDQc.outi=PIDQc.outMin; 
      PIDQc.out=PIDQc.outk+PIDQc.outi; 
      if(PIDQc.out>PIDQc.outMax) PIDQc.out=PIDQc.outMax; 
	  if(PIDQc.out<PIDQc.outMin) PIDQc.out=PIDQc.outMin; 
} 
 
 
 
Uint16 Sum_deltU; 
int16 delt_ua=0,delt_ub=0,delt_uc=0; 
void KMIPOn_C(void) 
{ 
      if(FLAG1.Csfw4.bit.Usoft==1) 
      return; 
 
      if(FLAG1.Sdsiga2.bit.Uaout==0) 
	     //  if((FLAG1.Sdsiga2.bit.UaoutL==0)&&(FLAG1.Sdsiga1.bit.Uinv==0)) 
	    { 
	       delt_ua=abs(ADCRMS.Uinv_a-ADCRMS.Uout_a); 
		   delt_ub=abs(ADCRMS.Uinv_b-ADCRMS.Uout_b); 
		   delt_uc=abs(ADCRMS.Uinv_c-ADCRMS.Uout_c); 
 
	       if((delt_ua<10)&&(delt_ub<10)&&(delt_uc<10))  //10*4.096=60 25*=102 
              { 
                if((CUNTER.Ery20ms-Sum_deltU)>10)    //6 
	              { 
	                 FLAG1.Csfw4.bit.DeltU=1; 
	              }	  
              } 
           else 
             { 
               Sum_deltU=CUNTER.Ery20ms; 
	           FLAG1.Csfw4.bit.DeltU=0; 
              } 	     
	    } 
	  else 
	    { 
		  // if((FLAG1.Sdsiga2.bit.UaoutL==0)&&(FLAG1.Sdsiga1.bit.Uinv==0))  
		     FLAG1.Csfw4.bit.DeltU=0;	 
		  // else 
		  //  FLAG1.Csfw4.bit.DeltU=0;     
	    } 
//-----------------------------------------------------------------------------  
     
 //    if(FLAG1.Csfw4.bit.DeltAngle&&FLAG1.Csfw4.bit.DeltU&&FLAG1.Csfw4.bit.BgKMIPClose&&(FLAG1.Sesiga3.bit.PhABC==0)) 
/* 
  if(FLAG1.Csfw4.bit.DeltAngle&&FLAG1.Csfw4.bit.DeltU&&FLAG1.Csfw4.bit.BgKMIPClose) 
       { 
          KMIP_on(); 
       }     
*/ // 20130425 
      
     
 
  //   if(FLAG1.Csfw4.bit.BgKMIPClose) 
//          KMIP_on();  
          
} 
 
 
void Urmsa_C(void) 
{ 
  /*   
    if(P_NODE.GridConect==1) 
	   { 
	     if(FLAG.B.bit.PQReceive8==1) 
           { 
              Uouta_av=(ADCRMS.Uinv_a+P_NODE.Uinva)/2; 
			  Uoutb_av=(ADCRMS.Uinv_b+P_NODE.Uinvb)/2; 
			  Uoutc_av=(ADCRMS.Uinv_c+P_NODE.Uinvc)/2; 
 
			  Urmstempa=Urmstempa0+(PIDUArms.out-Urmstempa0+P_NODE.PIDUouta)/2; 
		      Urmstempb=Urmstempb0+(PIDUBrms.out-Urmstempb0+P_NODE.PIDUoutb)/2; 
		      Urmstempc=Urmstempc0+(PIDUCrms.out-Urmstempc0+P_NODE.PIDUoutc)/2; 
 
              PQ.A.Paver=(PQ.A.Pout+P_NODE.Pa)/2; 
			  PQ.A.Qaver=(PQ.A.Qout+P_NODE.Qa)/2; 
			  PQ.B.Paver=(PQ.B.Pout+P_NODE.Pb)/2; 
			  PQ.B.Qaver=(PQ.B.Qout+P_NODE.Qb)/2; 
			  PQ.C.Paver=(PQ.C.Pout+P_NODE.Pc)/2; 
			  PQ.C.Qaver=(PQ.C.Qout+P_NODE.Qc)/2; 
 
              FLAG.B.bit.PQReceive8=0; 
           } 
        } 
     else 
       { 
		 Uouta_av=ADCRMS.Uinv_a; 
		 Uoutb_av=ADCRMS.Uinv_b; 
		 Uoutc_av=ADCRMS.Uinv_c; 
 
		 Urmstempa=PIDUArms.out; 
		 Urmstempb=PIDUBrms.out; 
		 Urmstempc=PIDUCrms.out; 
 
		 PQ.A.Paver=PQ.A.Pout; 
	     PQ.A.Qaver=PQ.A.Qout; 
         PQ.B.Paver=PQ.B.Pout; 
		 PQ.B.Qaver=PQ.B.Qout; 
		 PQ.C.Paver=PQ.C.Pout; 
		 PQ.C.Qaver=PQ.C.Qout; 
       }  
*/ 
/* 
    Uouta_av=ADCRMS.Uinv_a; 
    Uoutb_av=ADCRMS.Uinv_b; 
    Uoutc_av=ADCRMS.Uinv_c; 
*/ 
 
 //   PIDUArms.err=(int32)Kmda*524/128-Uouta_av;        
    PIDUArms.err=(int32)Kmda*524/128-ADCRMS.Uinv_a;    // 4.09375   
	 
//	utemp0=4; 
    
    if(PIDUArms.err>PIDUArms.errmax) 
    PIDUArms.err=PIDUArms.errmax; 
    if(PIDUArms.err<PIDUArms.errmin) 
    PIDUArms.err=PIDUArms.errmin; 
    if(PIDUArms.err>utemp0)PIDUArms.err=PIDUArms.err-utemp0; 
    else if(PIDUArms.err<-utemp0)PIDUArms.err=PIDUArms.err+utemp0; 
	else PIDUArms.err=0; 
//    PIDUArms.outk=(PIDUArms.Kp*PIDUArms.err)>>10;  // 1/64(6) 
    PIDUArms.outk=(K.P_urms*PIDUArms.err)>>10;  // 1/64(6)   
    PIDUArms.Remtemp=K.I_urms*PIDUArms.err+PIDUArms.outtempiRem; 
	PIDUArms.Remtemp1=PIDUArms.Remtemp>>10; 
	PIDUArms.outtempiRem=PIDUArms.Remtemp-(PIDUArms.Remtemp1<<10); 
    PIDUArms.outi+=PIDUArms.Remtemp1;     
    if(PIDUArms.outi>PIDUArms.outMax) PIDUArms.outi=PIDUArms.outMax; 
	if(PIDUArms.outi<PIDUArms.outMin) PIDUArms.outi=PIDUArms.outMin; 
    PIDUArms.out=PIDUArms.outk+PIDUArms.outi; 
    if(PIDUArms.out>PIDUArms.outMax) PIDUArms.out=PIDUArms.outMax; 
	if(PIDUArms.out<PIDUArms.outMin) PIDUArms.out=PIDUArms.outMin; 
} 
 
void Urmsb_C(void) 
{ 
 
//  PIDUBrms.err=(int32)Kmdb*524/128-Uoutb_av;  
    PIDUBrms.err=(int32)Kmdb*524/128-ADCRMS.Uinv_b;    // 4.09375  
 
//	utemp0=1; 
	if(PIDUBrms.err>PIDUBrms.errmax) 
    PIDUBrms.err=PIDUBrms.errmax; 
    if(PIDUBrms.err<PIDUBrms.errmin) 
    PIDUBrms.err=PIDUBrms.errmin; 
    if(PIDUBrms.err>utemp0)PIDUBrms.err=PIDUBrms.err-utemp0; 
    else if(PIDUBrms.err<-utemp0)PIDUBrms.err=PIDUBrms.err+utemp0; 
	else PIDUBrms.err=0; 
    PIDUBrms.outk=(K.P_urms*PIDUBrms.err)>>10;  // 1/64(6) 
    PIDUBrms.Remtemp=K.I_urms*PIDUBrms.err+PIDUBrms.outtempiRem; 
	PIDUBrms.Remtemp1=PIDUBrms.Remtemp>>10; 
	PIDUBrms.outtempiRem=PIDUBrms.Remtemp-(PIDUBrms.Remtemp1<<10); 
    PIDUBrms.outi+=PIDUBrms.Remtemp1;     
    if(PIDUBrms.outi>PIDUBrms.outMax) PIDUBrms.outi=PIDUBrms.outMax; 
	if(PIDUBrms.outi<PIDUBrms.outMin) PIDUBrms.outi=PIDUBrms.outMin; 
    PIDUBrms.out=PIDUBrms.outk+PIDUBrms.outi; 
    if(PIDUBrms.out>PIDUBrms.outMax) PIDUBrms.out=PIDUBrms.outMax; 
	if(PIDUBrms.out<PIDUBrms.outMin) PIDUBrms.out=PIDUBrms.outMin; 
} 
 
void Urmsc_C(void) 
{ 
    
//   PIDUCrms.err=(int32)Kmdc*524/128-Uoutc_av;     
 
   PIDUCrms.err=(int32)Kmdc*524/128-ADCRMS.Uinv_c;    // 4.09375    
//	utemp0=1; 
	if(PIDUCrms.err>PIDUCrms.errmax) 
    PIDUCrms.err=PIDUCrms.errmax; 
    if(PIDUCrms.err<PIDUCrms.errmin) 
    PIDUCrms.err=PIDUCrms.errmin; 
    if(PIDUCrms.err>utemp0)PIDUCrms.err=PIDUCrms.err-utemp0; 
    else if(PIDUCrms.err<-utemp0)PIDUCrms.err=PIDUCrms.err+utemp0; 
	else PIDUCrms.err=0; 
    PIDUCrms.outk=(K.P_urms*PIDUCrms.err)>>10;  // 1/64(6) 
    PIDUCrms.Remtemp=K.I_urms*PIDUCrms.err+PIDUCrms.outtempiRem; 
	PIDUCrms.Remtemp1=PIDUCrms.Remtemp>>10; 
	PIDUCrms.outtempiRem=PIDUCrms.Remtemp-(PIDUCrms.Remtemp1<<10); 
    PIDUCrms.outi+=PIDUCrms.Remtemp1;     
    if(PIDUCrms.outi>PIDUCrms.outMax) PIDUCrms.outi=PIDUCrms.outMax; 
	if(PIDUCrms.outi<PIDUCrms.outMin) PIDUCrms.outi=PIDUCrms.outMin; 
    PIDUCrms.out=PIDUCrms.outk+PIDUCrms.outi; 
    if(PIDUCrms.out>PIDUCrms.outMax) PIDUCrms.out=PIDUCrms.outMax; 
	if(PIDUCrms.out<PIDUCrms.outMin) PIDUCrms.out=PIDUCrms.outMin; 
} 
 
 
  
void DispDataCan(void) 
{ 
  
	 if((CUNTER.Ery20ms-CUNTER.Can)>50)           //3 
 	  { 
	   CUNTER.Can=CUNTER.Ery20ms; 
	   InitECana(); 
	   LED4_ON(); 
	  } 
	 else 
	  {LED4_OFF(); 
	  } 
 
} 
/* 
void DispDataSCi(void) 
{ 
  if((CUNTER.Ery20ms-CUNTER.SCI)>=500) 
    { 
      CUNTER.SCI=CUNTER.Ery20ms; 
      InitScia(); 
    //  CUNTER.SCI=CUNTER.Ery20ms; 
      FLAG1.Sedevice5.bit.SciOk=0;   //false    
    } 
  else  
    FLAG1.Sedevice5.bit.SciOk=1; 
   
 
   if(Ksendready==1)	//SCI transmit  
	{ 
	 	Copy_to_TxBuff2(Uinva,Ifiltera,Iloada,TxBuff); 
		Ksendready=0; 
	} 
} 
*/ 
/* 
void Copy_to_TxBuff2(int16 *data1,int16 *data2,int16 *data3,int16 *buff) 
{ 
	Uint16 j; 
	int16 *p1; 
	int16 *p2; 
	int16 *p3; 
	int16 *p;	//buff 
	p1=data1; 
	p2=data2; 
	p3=data3; 
	p=buff; 
 
	for(j=0;j<192;j++)	//data1 24点 
	{ 
		*p=(*p1)>>8;	//高8位 
		p++; 
		*p=(*p1)&0xff;	//低8位 
		p++; 
		p1++; 
	 
	} 
	for(j=0;j<192;j++)	//data2 
	{ 
		*p=(*p2)>>8;	//高8位 
		p++; 
		*p=(*p2)&0xff;	//低8位 
		p++; 
		p2++; 
	 
	} 
	for(j=0;j<192;j++)	//data3 
	{ 
		*p=(*p3)>>8;	//高8位 
		p++; 
		*p=(*p3)&0xff;	//低8位 
		p++; 
		p3++; 
	 
	} 
  
	*p=(UREF+600+4096)>>8;//高8位                         1 
	p++; 
	*p=(UREF+600+4096)&0xff;	//低8位 
	p++; 
 
	*p=(ADCRMS.Uinv_a+4096)>>8;//高8位      //   Uinv_a            2 
	p++; 
	*p=(ADCRMS.Uinv_a+4096)&0xff;	//低8位 
	p++; 
 
	 
	*p=(ADOFFSET[0]+4096)>>8;//高8位   // PQ.A.P   FlagKm.bit.GUFok    PQ.A.Pout/10             3 
	p++; 
	*p=(ADOFFSET[0]+4096)&0xff;	//低8位   PQ.A.Pout 
	p++; 
 
	*p=(ADOFFSET[1]+4096)>>8;//高8位       FlagA.bit.DelPhnorm_out           4 
	p++; 
	*p=(ADOFFSET[1]+4096)&0xff;	//低8位 PQ.A.Qout 
	p++;	 
  
 
	*p=(K.P_u+4096)>>8;//0x83  ntemp  K.Kaver    Kr   K.PaverP    PQ.C.P    K.P_u 5  ntemp K.P_i 
	p++; 
	*p=(K.P_u+4096)&0xff;	//低8位 K.PaverI CAP.Uout1.Tableindex Km.Iav_a 
	p++; 
 
	*p=(K.P_i+4096)>>8;//0x84   K.PaverP   K.P_i 6 
	p++; 
	*p=(K.P_i+4096)&0xff;	//低8 CAP.Uinv2.Tableindex 
	p++; 
 
	*p=(K.P_urms+4096)>>8;//高8位   PIDLOCK.Ki  PQ.C.P   K.PaverI    7 
	p++; 
	*p=(K.P_urms+4096)&0xff;	//低8位  K.QaverP  K.P_phlock 
	p++; 
 
	*p=(K.I_urms+4096)>>8;//高8位   PQ.C.Qout-PQ.C.Qaverout   BgIavermon  ddelta      K.P_phlock          8 
	p++; 
	*p=(K.I_urms+4096)&0xff;	//低8位  K.QaverP   K.P_phlock 
	p++; 
 
	*p=(K.feedforward+4096)>>8;//高8位    FlagKm.bit.BgPhLock   Kr      FlagKm.bit.BgPhLock         9 
	p++; 
	*p=(K.feedforward+4096)&0xff;	//低8位   9  K.QaverI     *p=(deltUa+600+4096)&0xff 
	p++; 
 
	      
	*p=(ADCRMS.Uout_a+4096)>>8;//高8位     Dz2tempP1     10   ADCRMS.Ifilter_a 
	p++; 
	*p=(ADCRMS.Uout_a+4096)&0xff;	//低8位 
	p++; 
 
	*p=(CAP.Uouta1.Freq+4096)>>8;//高8位 ADCRMS.Iload_a           11 
	p++; 
	*p=(CAP.Uouta1.Freq+4096)&0xff;	//低8位 
	p++; 
 
	//12 ADCDCAVER.Iload_a->P_NODE.P 
	*p=(ADOFFSET[Iload_a1]+4096)>>8;//高8位PQ.B.P  PQ.A.Paver  PIDPa.err      PQ.B.Pout  12 
	p++; 
	*p=(ADOFFSET[Iload_a1]+4096)&0xff;	//低8位 
	p++; 
	//13 ADCDCAVER.Iav_a->P_NODE.Q->P_NODE.ReceiveMessageCount 
	*p=(ADOFFSET[3]+4096)>>8;//高8位  PIDQa.err eCAN_PDP_cout       13 
	p++; 
	*p=(ADOFFSET[3]+4096)&0xff;	//低8位  PQ.B.Qout 
	p++; 
 
	*p=(FLAG1.Csfw4.bit.BgPhlock+4096)>>8;//高8位   FlagKm.bit.GrdConAldy  14 
	p++; 
	*p=(FLAG1.Csfw4.bit.BgPhlock+4096)&0xff;	//低8位 
	p++; 
 
	*p=(FLAG1.Csfw4.bit.BgKMIPClose+4096)>>8;//高8位	RefAngle      15 
	p++; 
	*p=(FLAG1.Csfw4.bit.BgKMIPClose+4096)&0xff;	//低8位 
	p++; 
 
	*p=(RefAngle  +15000)>>8;//高8位  K.QaverI     ADCOFFSET.Ifilter_a    RefAngle  utemp0         16 
	p++; 
	*p=(RefAngle  +15000)&0xff;	//低8位 K.PrmsP 
	p++; 
 
 
	*p=(ADCRMS.Uinv_b+4096)>>8;//高8   ADCRMS.Uinv_b            17 
	p++; 
	*p=(ADCRMS.Uinv_b+4096)&0xff;	//低8位 
	p++; 
 
 
	*p=(ADCRMS.Uout_b+4096)>>8;//高8位     18  PIDUArms.err  P_NODE.GridConect  
	p++; 
	*p=(ADCRMS.Uout_b+4096)&0xff;	//低8位   PIDUArms.err ADCRMS.Ifilter_b 
	p++; 
 
	*p=(CAP.Uinva2.Freq+4096)>>8;//高8位      PQ.B.Q    delt  19  TableIndex1 FlagKm.bit.GrdConAldy 
	p++; 
	*p=(CAP.Uinva2.Freq+4096)&0xff;	//低8位  TableIndex PIDQa.err   P_NODE.GridConect 
	p++; 
 
	*p=(ADOFFSET[4]+4096)>>8;//高8位 P_NODE.Urmsb      PIDQa.err    PIDUArms.out    20 
	p++; 
	*p=(ADOFFSET[4]+4096)&0xff;	//低8位    PQ.A.Paver   PQ.C.Pout ADCOFFSET.Iav_a PQ.C.Pout/10 
	p++; 
 
	*p=(ADOFFSET[5]+4096)>>8;//高8位     PIDQa.out   TableIndex1    21 
	p++; 
	*p=(ADOFFSET[5]+4096)&0xff;	//低8位  Dz1  PIDQa.out  PQ.A.Qaver  PQ.C.Qout ADCOFFSET.Iav_b 
	p++; 
 
	*p=(ADCRMS.Uinv_c+4096)>>8;//高8位  FlagA.bit.Fnorm_G    PQ.C.P   ADCRMS.Udc_bus     22 
	p++; 
	*p=(ADCRMS.Uinv_c+4096)&0xff;	//低8位 ADCRMS.Uout_a Uouta_av 
	p++; 
 
	*p=(ADCRMS.Uout_c+4096)>>8;//高8位      ADCRMS.Ifilter_c      23 ADCRMS.Ifilter_c 
	p++; 
	*p=(ADCRMS.Uout_c+4096)&0xff;	//低8位 ADCOFFSET.Iav_c 
	p++; 
 
	*p=(CAP.Pulse5.Freq+4096)>>8;//高8位  ADCRMS.Iload_c   ADCRMS.Iload_c       24 
	p++; 
	*p=(CAP.Pulse5.Freq+4096)&0xff;	//低8位FlagA.bit.Fnorm_G  FLAG.B.bit.DeltPhpulse3 
	p++; 
 
	*p=(ID+4096)>>8;//  ID号      25 
	p++; 
	*p=(ID+4096)&0xff;	  
	p++; 
 
	*p=((FLAG1.Sdsiga1.all+4096)>>8)&0xff;//  Sdsiga1      26 
	p++; 
	*p=(FLAG1.Sdsiga1.all+4096)&0xff;	  
	p++; 
 
	*p=((FLAG1.Sdsiga2.all+4096)>>8)&0xff;//  Sdsiga2      27 
	p++; 
	*p=(FLAG1.Sdsiga2.all+4096)&0xff;	  
	p++; 
 
	*p=((FLAG1.Sesiga3.all+4096)>>8)&0xff;//  Sdsiga3      28 
	p++; 
	*p=(FLAG1.Sesiga3.all +4096)&0xff;	  
	p++; 
 
	*p=((FLAG1.Csfw4.all+4096)>>8)&0xff;//  Csfw4      29 
	p++; 
	*p=(FLAG1.Csfw4.all+4096)&0xff;	 
	p++;  
 
 
 
	*p=0x0D;			//末字节 
	tx_pointer=buff;	//发送指针指向buff[0] 
//	tx_counter=24*2*3+1+2+8+2+2;	//发送TxBuff的字节数192*2*3+1+ddelta+()+Kmd+ADCKC.Uinv_a 
//	tx_counter=24*2*3+16*2+1; 
//    tx_counter=24*2*3+24*2+1;//24点 
 	 
//	tx_counter=1201;   //tx_counter=192*2*3+24*2+1=1201;//192点 
//    tx_counter=1203;   //tx_counter=192*2*3+25*2+1=1203; 
	tx_counter=1211;   //tx_counter=192*2*3+29*2+1=1203; 
 
	SciaRegs.SCICTL2.bit.TXINTENA=1; // Tx IRQ ENABLE 
	SciaRegs.SCITXBUF=0xC7;	//发送头字节 
 
} 
*/