📄 vsi_control.c.bak1
字号:
//
//test
ch = (unsigned char)(*lpstr);
(*(char*)lpstr) = 0xf0;
WAIT_LCD_IDLE;
(*(char*)lpstr) = 0x08;
WAIT_LCD_IDLE;
(*(char*)lpstr) = 0xfc;
WAIT_LCD_IDLE;
(*(char *)(lpstr+1))=0x7c;
WAIT_LCD_IDLE;
(*(char *)(lpstr+1))=0x71;
WAIT_LCD_IDLE;
(*(char *)(lpstr+8))=0x00;
(*(char *)(lpstr+8))=0xFF;
WAIT_LCD_IDLE;
(*(char *)(lpstr+8))=0x55;
WAIT_LCD_IDLE;
(*(char *)(lpstr+1))=0x6c;
InitLCD1335();
Clear_Lcd();
DisPlayLine(10,10,50,1);
DisPlayLine(20,20,50,0);
DisPlayLine(30,30,150,1);
// Enable global Interrupts and higher priority real-time debug events:
EINT; // Enable Global interrupt INTM
ERTM; // Enable Global realtime interrupt DBGM
// Step 6. IDLE loop. Just sit and loop forever:
while(1)
{
/*
if(1)//SciaRegs.SCICTL2.bit.TXRDY == 1)
{
SciaRegs.SCITXBUF = 0xeb;
SciaRegs.SCITXBUF = 0x90;
SciaRegs.SCITXBUF = 0x0a;
SciaRegs.SCITXBUF = 0x0b;
SciaRegs.SCITXBUF = 0x0c;
}
if(ScibRegs.SCICTL2.bit.TXRDY == 1)
{
ScibRegs.SCITXBUF = 0x90;
}*/
if(CtrVars.reset_lock==1)
{
if(CtrVars.Udc_aver>CtrVars.Udc_uplimit)
{
CtrVars.Udc_high_fault=1;
CtrVars.fault=1;
}
else if(CtrVars.Udc_aver<CtrVars.Udc_lowlimit)
{
CtrVars.Udc_low_fault=1;
CtrVars.fault=1;
}
else if(CtrVars.Vs_sinwt_main>1250) // over voltage
{
CtrVars.Vs_fault=1;
CtrVars.fault=1;
}
else if(abs(CtrVars.Il>1900)) // overload or over-current
{
CtrVars.Il_fault=1;
CtrVars.fault=1;
}
else
{
CtrVars.pwm_enable=1;
}
}
if((CtrVars.pwm_enable==1)&&(CtrVars.fault==0))
{
if(CtrVars.run_stop==0)
{
CtrVars.pianci=0;
for(i=0;i<f_point;i++)CtrVars.rep[i]=0;
CtrVars.Vyk=0; CtrVars.Vyks1=0;/* reset some important variables */
CtrVars.pid_Vyks1=0; CtrVars.pid_Vyks2=0;
CtrVars.start=1; CtrVars.run_stop=1;
EvaRegs.CMPR1=pwm_half_per/2; EvaRegs.CMPR2=pwm_half_per/2;
EvaRegs.COMCONA.bit.FCOMPOE=1; /* enable PWM output */
// StatusLed(1); // light it
CtrVars.pianci_rec=0;
}
else
{
CtrVars.run_stop=0;
EvaRegs.COMCONA.bit.FCOMPOE=0; /* disable PWM output */
// StatusLed(0); // black out
//
}
}
else
{
EvaRegs.COMCONA.bit.FCOMPOE=0; /* disable PWM output */
if(CtrVars.run_stop==1)
{
CtrVars.fault=1;
CtrVars.run_stop=0;
} /* don't allow self-restart after detecting fault */
}
KickDog(); /* reset watchdog */
}; /* endless loop, wait for interrupt */
}
// Step 7. Insert all local Interrupt Service Routines (ISRs) and functions here:
// If local ISRs are used, reassign vector addresses in vector table as
// shown in Step 5
interrupt void eva_t2ufint_isr(void)
{
unsigned int CHA7 , CHA6 , CHA5 , CHA4;
int offset , temp , Vs_err , Vs_ref , pre_value , pid_output , N , sinN , cosN;
long temp_32 , temp32;
// Note: To be safe, use a mask value to write to the entire
// EVAIFRA 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.EVAIFRB.bit.T2UFINT = 1;
//led
/*
count++;
if(count >= 10000)
{
ch = (unsigned char)(*lpstr);
(*(char*)lpstr) = ~ch;
(*(char *)(lpstr+4))=ch;
ch = (unsigned char)(*(lpstr+2));
count=0;
}
*/
//--- calculate the phase angle-------------------------
offset=CtrVars.sum_counter/f_point;
CtrVars.sum_counter=CtrVars.sum_counter+512;
if(offset>=512)
{
offset=0;
}
CtrVars.sinwt=*(sintab_pointer+offset)>>16;
CtrVars.coswt=*(sintab_pointer+offset+128)>>16;
N=offset+8;
sinN=*(sintab_pointer+N)>>16;
cosN=*(sintab_pointer+offset+116)>>16;
//---- read the sample values-----------------------------
//Vs
CHA7 = AdcRegs.ADCRESULT0;
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 + -