⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 vsi_control.c

📁 许继的2812开发全套驱动程序,很全很实用的.rar
💻 C
📖 第 1 页 / 共 3 页
字号:
	temp=CHA7>>4;	
	
	CtrVars.Vs_sum_20ms=CtrVars.Vs_sum_20ms+temp;	
	CtrVars.Vs=temp-CtrVars.Vs_offset;	
	temp_32=CtrVars.Vs;
	temp_32=temp_32*CtrVars.sinwt;	// calculate the peak value of output voltage	
	temp=temp_32>>13;
	CtrVars.Vs_sinwt_sum=CtrVars.Vs_sinwt_sum+temp;
	

//Ic

	CHA6 = AdcRegs.ADCRESULT1;
	temp=CHA6>>4;
	
	CtrVars.Ic_sum_20ms=CtrVars.Ic_sum_20ms+temp;	
	CtrVars.Ic=temp-CtrVars.Ic_offset;	
	temp_32=CtrVars.Ic;
	temp_32=temp_32*cosN;//CtrVars.coswt;	// calculate the peak value of output voltage	
	temp=temp_32>>13;
	CtrVars.Ic_coswt_sum=CtrVars.Ic_coswt_sum+temp;	

//Il

	CHA5 = AdcRegs.ADCRESULT2;
	temp=CHA5>>4;
	
	CtrVars.Il=temp-CtrVars.Il_offset;
	CtrVars.Il_sum_20ms=CtrVars.Il_sum_20ms+CtrVars.Il;				
	temp_32=CtrVars.Il;
	temp_32=temp_32*CtrVars.sinwt;	// calculate the peak value of output current
	temp=temp_32>>13;
	CtrVars.Il_sinwt_sum=CtrVars.Il_sinwt_sum+temp;
	
//Udc		

	CHA4 = AdcRegs.ADCRESULT3;
	temp=CHA4>>4;	
		
	CtrVars.Udc=temp-CtrVars.Udc_offset;
	temp_32=CtrVars.Udc;
	temp_32=CtrVars.Udc_prescale*temp_32;
	CtrVars.Udc=temp_32>>10;
		
	CtrVars.Udc_sum=CtrVars.Udc_sum+CtrVars.Udc;	

//------------------------------------------------	
	temp_32=CtrVars.Vs_rms_ref;
	temp_32=pwm_half_per*temp_32;
	temp=CtrVars.Udc_aver*6;	
	pre_value=temp_32/temp;	// pre_value=Vs_peak*pwm_half_per/(4*Udc), ratio=1:2	
	if(abs(CtrVars.Vs)>1100){CtrVars.Il_sinwt_main=0;}
	temp=CtrVars.Il_sinwt_main/3;
	pre_value=pre_value+temp;
	
	if(CtrVars.start==1)
		{
			CtrVars.factor_tmp++;
			CtrVars.pre_factor=CtrVars.factor_tmp>>2;
			if(CtrVars.pre_factor>pre_value)
				{
					CtrVars.pre_factor=pre_value;
					CtrVars.factor_tmp=0;
					CtrVars.start=0;		// finish the process of start					
				}
			Vs_err=0;					
		}
	else if(CtrVars.run_stop==1)
		{				
		    temp_32=CtrVars.Vs_rms_ref;
		    temp_32=temp_32*CtrVars.sinwt;			
			Vs_ref=temp_32>>14;
			Vs_err=Vs_ref-CtrVars.Vs;
			
			pid_output=Vs_err;
			
// low-pass filter
// y(k+1)=y(k)+2*y(k-1)+y(k-2)
			temp=pid_output+CtrVars.pid_Vyks1;
			temp=temp+CtrVars.pid_Vyks1;
			temp=temp+CtrVars.pid_Vyks2;
			pid_output=temp>>2;
			if(pid_output>200)pid_output=200;
			if(pid_output<-200)pid_output=-200;
			CtrVars.pid_Vyks2=CtrVars.pid_Vyks1;
			CtrVars.pid_Vyks1=pid_output;

/** x(k)=e(k)+Q(z)*x(k-N) **/
// Q(z)=0.95
//x(k)=e(k)+0.95*x(k-N)						
			temp_32=CtrVars.rep[CtrVars.t2ufint_counter];
			temp_32=972*temp_32;
			temp=temp_32>>10;
			CtrVars.rep[CtrVars.t2ufint_counter]=Vs_err+temp;
												
			if(CtrVars.rep[CtrVars.t2ufint_counter]>1500)CtrVars.rep[CtrVars.t2ufint_counter]=1500;
			if(CtrVars.rep[CtrVars.t2ufint_counter]<-1500)CtrVars.rep[CtrVars.t2ufint_counter]=-1500;

//y(k+1)=km1*y(k)+km2*y(k-1)+[kn1*x(k+12+9+1-N)+kn2*x(k+12+9-N)
//								+2*kn1*x(k+12+1-N)+2*kn2*x(k+12-N)
//								+kn1*x(k+12-9+1-N)+kn2*x(k+12-9-N)]/4
			temp=CtrVars.t2ufint_counter+21;
			if(temp>=f_point)temp=temp-f_point;
			temp32=kn1;
			temp32=temp32*CtrVars.rep[temp];
			
			temp=CtrVars.t2ufint_counter+20;
			if(temp>=f_point)temp=temp-f_point;
			temp_32=kn2;
			temp_32=temp_32*CtrVars.rep[temp];
			temp32=temp32+temp_32;
			
			temp=CtrVars.t2ufint_counter+12;
			if(temp>=f_point)temp=temp-f_point;
			temp_32=kn1;
			temp_32=temp_32*CtrVars.rep[temp];
			temp32=temp32+temp_32;
			temp32=temp32+temp_32;
			
			temp=CtrVars.t2ufint_counter+11;
			if(temp>=f_point)temp=temp-f_point;
			temp_32=kn2;
			temp_32=temp_32*CtrVars.rep[temp];
			temp32=temp32+temp_32;
			temp32=temp32+temp_32;
			
			temp=CtrVars.t2ufint_counter+3;
			if(temp>=f_point)temp=temp-f_point;
			temp_32=kn1;
			temp_32=temp_32*CtrVars.rep[temp];
			temp32=temp32+temp_32;
			
			temp=CtrVars.t2ufint_counter+2;
			if(temp>=f_point)temp=temp-f_point;
			temp_32=kn2;
			temp_32=temp_32*CtrVars.rep[temp];
			temp32=temp32+temp_32;
			
			temp32=temp32>>2;

//********************************************************
			temp_32=km1;
			temp_32=temp_32*CtrVars.Vyk;
			temp32=temp32+temp_32;
			
			temp_32=km2;
			temp_32=temp_32*CtrVars.Vyks1;
			temp32=temp32+temp_32;
				
			temp=temp32>>11;						
			temp=5*temp;
			temp=temp>>3;
			
			CtrVars.Vyks1=CtrVars.Vyk;
			CtrVars.Vyk=temp;			
						
			Vs_err=temp;
				
// limit the peak-value of output voltage			
			
			temp32=CtrVars.Ic_coswt_main;
			temp32=temp32*cosN; //CtrVars.coswt;
			temp=temp32>>14;
			temp=temp-CtrVars.Ic;
			temp=3*temp;
			temp=temp>>3;
			if(temp>100)temp=100;
			if(temp<-100)temp=-100;
			Vs_err=Vs_err+temp;
	
			Vs_err=Vs_err+pid_output;
			
			CtrVars.pre_factor=pre_value;  // set feedforward variable			

		}
	else
		{
			Vs_err=0;			
		}

	temp_32=CtrVars.pre_factor;
	temp_32=temp_32*sinN;
	temp=temp_32>>14;			
	temp=temp+pwm_half_per/2;
	
	temp=temp+Vs_err;
	temp=temp-CtrVars.pianci;	
		
	if(temp>pwm_half_per)
		{
			temp=pwm_half_per;
		}
	if(temp<0)
		{
			temp=0;
		}

// refresh the full compare registers
	EvaRegs.CMPR1=temp;
	EvaRegs.CMPR2=pwm_half_per-temp;
	
//	if(CtrVars.run_stop==1){tmp[CtrVars.t2ufint_counter]=CtrVars.Ic;}
		
	CtrVars.t2ufint_counter++;
//--------------------------------------------------------------------------------	
	if((CtrVars.t2ufint_counter==f_point/2)||(CtrVars.t2ufint_counter==f_point))
		{
			temp=f_point/2;
			CtrVars.Udc_aver=CtrVars.Udc_sum/temp;
			if(CtrVars.Udc_aver<0){CtrVars.Udc_aver=10;}			
			CtrVars.Udc_sum=0;
		}
//-------------------------------------------------------------------------------		
		
	if(CtrVars.t2ufint_counter>=f_point)
		{
			CtrVars.t2ufint_counter=0;
			CtrVars.sum_counter=0;				
	
			CtrVars.Vs_offset=CtrVars.Vs_sum_20ms/f_point;
			if(CtrVars.run_stop==0)
				{
					CtrVars.reset_lock=1;
					CtrVars.Ic_offset=CtrVars.Ic_sum_20ms/f_point;
					temp=CtrVars.Il_sum_20ms/f_point;
					CtrVars.Il_offset=temp+CtrVars.Il_offset;
				}
			
			if(abs(CtrVars.pianci_rec)>300 && abs(CtrVars.Il_sum_20ms)>300)
				{
					if(CtrVars.Il_sum_20ms>3000)CtrVars.pianci=CtrVars.pianci+5;
					else if(CtrVars.Il_sum_20ms>2000)CtrVars.pianci=CtrVars.pianci+3;
					else if(CtrVars.Il_sum_20ms>400)CtrVars.pianci++;
					else if(CtrVars.Il_sum_20ms<-3000)CtrVars.pianci=CtrVars.pianci-5;
					else if(CtrVars.Il_sum_20ms<-2000)CtrVars.pianci=CtrVars.pianci-3;			
					else if(CtrVars.Il_sum_20ms<-400)CtrVars.pianci--;			
					else{;}
				}
			CtrVars.pianci_rec=CtrVars.Il_sum_20ms;
			
			if(CtrVars.pianci>30)CtrVars.pianci=30;
			if(CtrVars.pianci<-30)CtrVars.pianci=-30;

			CtrVars.Vs_sum_20ms=0;
			CtrVars.Ic_sum_20ms=0;
			CtrVars.Il_sum_20ms=0;
				
			CtrVars.Vs_sinwt_main=CtrVars.Vs_sinwt_sum/f_point;
			CtrVars.Vs_sinwt_sum=0;
			
			CtrVars.Ic_coswt_main=CtrVars.Ic_coswt_sum/f_point;
			if(CtrVars.Ic_coswt_main<0){CtrVars.Ic_coswt_main=0;}
			CtrVars.Ic_coswt_sum=0;
			
			CtrVars.Il_sinwt_main=CtrVars.Il_sinwt_sum/f_point;
			if(CtrVars.Il_sinwt_main<20){CtrVars.Il_sinwt_main=0;}
			CtrVars.Il_sinwt_sum=0;						

		}

//used to control the flash of run_led, 0:off 1:on
	CtrVars.run_led=CtrVars.run_led+1;
	if(CtrVars.run_led==pwm_frequency)        /* second twinkle*/
    	{     		
//			RunLed();
			CtrVars.run_led=0;
			CtrVars.thirtysecond--;
			if(CtrVars.fault==1)
				{
//					StatusLed(2); /* twinkle */					
				}
    	}
    
    // Acknowledge interrupt to recieve more interrupts from PIE group 3
	PieCtrlRegs.PIEACK.all |= PIEACK_GROUP3;
}


/***trace the phase and frequency of the system voltage***/
interrupt void eva_capint1_isr(void)
{
    // Note: To be safe, use a mask value to write to the entire
	// EVAIFRC register.  Writing to one bit will cause a read-modify-write
	// operation that may have the result of writing 1's to clear 
	// bits other then those intended. 
    EvaRegs.EVAIFRC.all = BIT0;
    	
		
	// Acknowledge interrupt to recieve more interrupts from PIE group 3
	PieCtrlRegs.PIEACK.all |= PIEACK_GROUP3;
}

interrupt void eva_capint2_isr(void)
{
    // Note: To be safe, use a mask value to write to the entire
	// EVAIFRC register.  Writing to one bit will cause a read-modify-write
	// operation that may have the result of writing 1's to clear 
	// bits other then those intended. 
    EvaRegs.EVAIFRC.all = BIT1;
	
	
	
	// Acknowledge interrupt to recieve more interrupts from PIE group 3
	PieCtrlRegs.PIEACK.all |= PIEACK_GROUP3;
}

//** communication software module
interrupt void sciarxint_isr(void)
{
/*	switch(SciaVars.i)
	{
	case 0:SciaVars.xxx[SciaVars.i]=SciaRegs.SCIRXBUF.bit.RXDT;break;	
	case 1:SciaVars.xxx[SciaVars.i]=SciaRegs.SCIRXBUF.bit.RXDT;break;
	case 2:SciaVars.xxx[SciaVars.i]=SciaRegs.SCIRXBUF.bit.RXDT;break;
	case 3:SciaVars.xxx[SciaVars.i]=SciaRegs.SCIRXBUF.bit.RXDT;break;
	case 4:SciaVars.xxx[SciaVars.i]=SciaRegs.SCIRXBUF.bit.RXDT;break;
    }
    SciaVars.i++;
*/    
       
    //When a maskable interrupt is acknowledged, only the IFR bit is cleared
	//automatically. The flag bit in the corresponding peripheral control register
	//is not cleared. If an application requires that the control register flag
	//be cleared, the bit must be cleared by software.
	SciaRegs.SCIFFRX.bit.RXFFINTCLR = 1;
	// Acknowledge interrupt to recieve more interrupts from PIE group 9
	PieCtrlRegs.PIEACK.all |= PIEACK_GROUP9;
}

interrupt void scibrxint_isr(void)
{

	
    ScibVars.test = ScibRegs.SCIRXBUF.bit.RXDT;
    
    //When a maskable interrupt is acknowledged, only the IFR bit is cleared
	//automatically. The flag bit in the corresponding peripheral control register
	//is not cleared. If an application requires that the control register flag
	//be cleared, the bit must be cleared by software.
	ScibRegs.SCIFFRX.bit.RXFFINTCLR = 1;
	// Acknowledge interrupt to recieve more interrupts from PIE group 9
	PieCtrlRegs.PIEACK.all |= PIEACK_GROUP9;
}

//*** MMI software module
interrupt void xnmi_isr(void)
{
	int temp;
	temp=INCS3 & 0x3f; // short-circuit fault protection
	if(temp!=0x3f)
		{
			EvaRegs.COMCONA.bit.FCOMPOE = 0; // set full compare output in Hi-Z state	
			CtrVars.fault=1;				 // don't allow self-restart after detecting fault
			CtrVars.short_circuit=temp;
		}
	else
		{
			CtrVars.Il_fault=1;
		}
}


// initialize variables
void initvariable(void)
{	
	int i;
	CtrVars.reset_lock=0;
	CtrVars.pwm_enable=0;
	CtrVars.Udc_offset=2261;
	CtrVars.Udc_prescale=980;//900; 	// amplify 2^10 times
	CtrVars.Udc_uplimit=1000;    // 450V
	CtrVars.Udc_lowlimit=350;
	CtrVars.Udc_sum=0;	
	CtrVars.Udc_aver=1000;
	
	CtrVars.Vs_offset=2254;
	CtrVars.Vs_prescale=1024;  	// amplify 2^10 times	
	
	for(i=0;i<f_point;i++)CtrVars.rep[i]=0;
	CtrVars.Vyk=0;	CtrVars.Vyks1=0;
		
	CtrVars.Vs_sinwt_sum=0;
	CtrVars.Vs_rms_ref=1030;  	// the reference output voltage is Vs_rms_ref*sinwt
	
	CtrVars.Vs_sum_20ms=0;
	
	CtrVars.Ic_offset=2244; 	// the sample value of analog signal
	CtrVars.Ic_prescale=450; 	// amplify 2^10 times
	CtrVars.Ic_sum_20ms=0;
	
	CtrVars.Il_offset=2239; 	// the sample value of analog signal
	CtrVars.Il_sum_20ms=0;
	CtrVars.Il_sinwt_sum=0;	
	CtrVars.pianci=0;
				
	CtrVars.pre_factor=0;
	CtrVars.factor_tmp=0;
		
	CtrVars.start=0;	
	CtrVars.fault=0;
	CtrVars.run_led=0;			//used to control run_lamp
	CtrVars.run_stop=0;
	
	CtrVars.t2ufint_counter=0;
	CtrVars.sum_counter=0; 		// used to calculate phase
		
	CtrVars.pid_Vyks1=0;	CtrVars.pid_Vyks2=0;

	sintab_pointer=(_iq30 *)0x3ff000;
	
	CtrVars.short_circuit=0;
	CtrVars.Vs_fault=0;
	CtrVars.Il_fault=0;
	CtrVars.Udc_high_fault=0;
	CtrVars.Udc_low_fault=0;

}

//===========================================================================
// No more.
//===========================================================================

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -