📄 vsi_control.c.bak1
字号:
//
//###########################################################################
//
// FILE: VSI_Control.c
//
// TITLE: Control Program for Single-Phase Voltage Source Inverter
//
// DESCRIPTION:
//
// This program is to control single-phase inverter, repetitive control
// and predictive control method are adopted.
//
//
//
//
//###########################################################################
//
// Ver | dd mmm yyyy | Who | Description of changes
// =====|=============|=========|===============================================
// 0.50| 01 May 2002 | T.CHEN. | for TMX320F2812
// 1.00| 02 Nov 2003 | T.CHEN. | for TMP320F2812
//###########################################################################
// Step 0. Include required header files
// DSP28_Device.h: device specific definitions #include statements for
// all of the peripheral .h definition files.
// DSP28_Example.h is specific for the given example.
#include "DSP281x_Device.h"
#include "DSP281x_Examples.h" // DSP281x Examples Include File
#include "IQMathLib.h"
// Control function software module
// repetitive control parameters
/*
#define kn1 143 // 0.0699 转折频率4500rad/s
#define kn2 107 // 0.0524
#define km1 2664 // 1.301
#define km2 -867 // -0.4232
*/
#define kn1 250 // 0.1219 转折频率6000rad/s
#define kn2 167 // 0.0817
#define km1 2248 // 1.09762
#define km2 -617 // -0.3012
#define LCDDATAADD 0x80004
#define LCDCMDADD 0x80005
#define WAIT_LCD_IDLE delay()
#define LCD_NOP lcd_nop()
// Serial Communications Interface software module
// Man-Machine Interface software module
//* Control software module variable define
unsigned int const LcdInitSYS[8]={0x30,0x87,0x07,0x27,0x39,0xef,0x28,0x00};
unsigned int const LcdInitSCR[10]={0x00,0x00,0xef,0x00,0x30,0xef,0x00,0x00,0x00,0x00};
struct CTR_VARS {
int reset_lock;
int Udc;
int Udc_offset;
int Udc_prescale; // amplify 2^10 times
int Udc_uplimit;
int Udc_lowlimit;
int Udc_aver;
int long Udc_sum;
int Vs;
int Vs_offset;
int Vs_prescale; // amplify 2^10 times
int sinwt; // sinusoid function
int coswt;
int Vs_sinwt_main; // the peak-value of Vs
int long Vs_sinwt_sum; // used to calculate peak-value of Vs
int Vs_rms_ref; // the reference output voltage is Vs_rms_ref*sinwt
int long Vs_sum_20ms; // used to calculate DC component of Vs
int rep[f_point]; // repetitive control variables
int Vyk;
int Vyks1;
int pid_Vyks1; // predictive-PID control variables
int pid_Vyks2;
int Ic;
int Ic_offset; // the sample value of analog signal
int Ic_prescale; // amplify 2^10 times
int long Ic_sum_20ms; // used to calculate DC component of Ic
int Ic_coswt_main;
int long Ic_coswt_sum;
int Il;
int Il_offset; // the sample value of analog signal
int long Il_sum_20ms; // used to calculate DC component of Il
int pianci;
int Il_sinwt_main; // the peak-value of Il
int long Il_sinwt_sum; // used to calculate peak-value of Il
int factor_tmp;
int pre_factor; // feedforward value
int start;
int fault; // indicate the fault status
int run_led; // used to control the flash of run-lamp
int run_stop;
int pwm_enable;
int t2ufint_counter;
int long sum_counter; // used to calculate phase
int thirtysecond;
int KeyValue;
int Udc_low_fault;
int Udc_high_fault;
int Il_fault;
int short_circuit;
int Vs_fault;
int pianci_rec;
};
volatile struct CTR_VARS CtrVars;
//** SCI software variable define
struct SCIA_VARS {
int i;
// int xxx[5];
};
volatile struct SCIA_VARS SciaVars;
struct SCIB_VARS {
int test;
};
volatile struct SCIB_VARS ScibVars;
_iq30 *sintab_pointer;
int tmp[f_point];
// Prototype statements for functions found within this file.
interrupt void eva_t2ufint_isr(void);
interrupt void eva_capint1_isr(void);
interrupt void eva_capint2_isr(void);
interrupt void sciarxint_isr(void);
interrupt void scibrxint_isr(void);
interrupt void xnmi_isr(void);
void initvariable(void); // initialize variables
char * lpstr =(char *)0x80000;
unsigned char ch;
long int count=0;
unsigned int delay()
{
unsigned int i;
unsigned int j;
for(i=0;i<0x3f;i++)
j=i;
return j;
}
unsigned int lcd_nop()
{
unsigned int i=0;
unsigned int j=0;
for(i=0;i<0xf;i++)
j += i;
return j;
}
void OutLCDCmd(unsigned int data)
{
//WAIT_LCD_IDLE;
(*(char *)(lpstr+1))=0x7c;
(*(char *)(lpstr+1))=0x5c;
(*(char *)(LCDCMDADD)) = data;
LCD_NOP;
(*(char *)(lpstr+1))=0x54;
LCD_NOP;
(*(char *)(lpstr+1))=0x5c;
LCD_NOP;
(*(char *)(lpstr+1))=0x7c;
}
void OutLCDData(unsigned int data)
{
// WAIT_LCD_IDLE;
(*(char *)(lpstr+1))=0x6c;
(*(char *)(lpstr+1))=0x4c;
(*(char *)(LCDDATAADD)) = data;
LCD_NOP;
(*(char *)(lpstr+1))=0x44;
LCD_NOP;
(*(char *)(lpstr+1))=0x4c;
LCD_NOP;
(*(char *)(lpstr+1))=0x6c;
}
void AdjustContrast(void)
{
unsigned char i;
// (*(char*)lpstr) = 0xf0;
(*(char*)lpstr) = 0xf4;
for(i=0;i<2;i++)
{
(*(char*)lpstr) = 0x08;
WAIT_LCD_IDLE;
(*(char*)lpstr) = 0xfc;
}
}
void ClearLCDLay1()
{
unsigned int i=0;
OutLCDCmd(0X46);
WAIT_LCD_IDLE;
OutLCDData(0X00);
WAIT_LCD_IDLE;
OutLCDData(0X00);
WAIT_LCD_IDLE;
OutLCDCmd(0X4C);
WAIT_LCD_IDLE;
OutLCDCmd(0X42);
WAIT_LCD_IDLE;
for(i=0;i<0x2fff;i++)
{
OutLCDData(0X00);
// OutLCDData(0Xff);
WAIT_LCD_IDLE;
}
}
void ClearLCDLay2()
{
unsigned int i=0;
OutLCDCmd(0X46);
WAIT_LCD_IDLE;
OutLCDData(0X00);
WAIT_LCD_IDLE;
OutLCDData(0X30);
WAIT_LCD_IDLE;
OutLCDCmd(0X4C);
WAIT_LCD_IDLE;
OutLCDCmd(0X42);
WAIT_LCD_IDLE;
for(i=0;i<0x3000;i++)
{
OutLCDData(0X00);
// OutLCDData(0Xf0);
WAIT_LCD_IDLE;
}
}
/*
void InitLCD1335()
{
unsigned int i=0;
OutLCDCmd(0x40);
for(i=0;i<8;i++)
OutLCDData(LcdInitSYS[i]);
OutLCDCmd(0x44);
for(i=0;i<10;i++)
OutLCDData(LcdInitSCR[i]);
OutLCDCmd(0x5a);//设置点单元卷动位置
OutLCDData(0x00);
OutLCDCmd(0x5b);//设置显示合成方式
OutLCDData(0x01);
OutLCDCmd(0x58);//设置显示开关
// OutLCDData(0x16);
OutLCDData(0x56);
OutLCDCmd(0X4C);
// ClearLCDLay1();
// ClearLCDLay2();
OutLCDCmd(0x46);//设置指针位置
OutLCDData(0x00);
OutLCDData(0x00);
OutLCDCmd(0x5d);//设置光标形状
// OutLCDData(0x01);
OutLCDData(0x04);
OutLCDData(0x86);
OutLCDCmd(0x59);//设置显示开关
OutLCDCmd(0x4c);
OutLCDCmd(0x42);
OutLCDData(0x45);
OutLCDData(0x50);
OutLCDData(0x53);
OutLCDData(0x4f);
OutLCDData(0x4e);
OutLCDData(0x17);
}
*/
void InitLCD1335()
{
unsigned int i=0;
OutLCDCmd(0x40);
for(i=0;i<8;i++)
OutLCDData(LcdInitSYS[i]);
OutLCDCmd(0x44);
for(i=0;i<10;i++)
OutLCDData(LcdInitSCR[i]);
OutLCDCmd(0x5a);//设置点单元卷动位置
OutLCDData(0x00);
OutLCDCmd(0x5b);//设置显示合成方式
OutLCDData(0x01);
OutLCDCmd(0x58);//设置显示开关
OutLCDData(0x16);
OutLCDCmd(0X4C);
ClearLCDLay1();
ClearLCDLay2();
OutLCDCmd(0x46);//设置指针位置
OutLCDData(0x00);
OutLCDData(0x00);
OutLCDCmd(0x5d);//设置光标形状
OutLCDData(0x01);
OutLCDData(0x86);
OutLCDCmd(0x59);//设置显示开关
OutLCDData(0x17);
}
//-------------在固定位置输出点------
void PutPixelLCD(unsigned int PositionX,unsigned int PositionY,unsigned int Pixel)
{
unsigned int TempValue=0;
unsigned char LCDInfo=0;
TempValue=PositionX;
PositionX=320-PositionY-1;
PositionY=TempValue;
TempValue=40*PositionY+PositionX/8+0x3000;
OutLCDCmd(0X46);
WAIT_LCD_IDLE;
OutLCDData(TempValue&0xff);
WAIT_LCD_IDLE;
OutLCDData((TempValue>>8)&0xff);
WAIT_LCD_IDLE;
OutLCDCmd(0X4c);
WAIT_LCD_IDLE;
OutLCDCmd(0X43);
WAIT_LCD_IDLE;
LCDInfo=(*(unsigned char *)LCDCMDADD);
LCDInfo=0;
switch(PositionX%8)
{
case 0:
{
if(Pixel)
LCDInfo=LCDInfo|0x80;
else
LCDInfo=LCDInfo&0x7f;
break;
}
case 1:
{
if(Pixel)
LCDInfo=LCDInfo|0x40;
else
LCDInfo=LCDInfo&0xbf;
break;
}
case 2:
{
if(Pixel)
LCDInfo=LCDInfo|0x20;
else
LCDInfo=LCDInfo&0xdf;
break;
}
case 3:
{
if(Pixel)
LCDInfo=LCDInfo|0x10;
else
LCDInfo=LCDInfo&0xef;
break;
}
case 4:
{
if(Pixel)
LCDInfo=LCDInfo|0x08;
else
LCDInfo=LCDInfo&0xf7;
break;
}
case 5:
{
if(Pixel)
LCDInfo=LCDInfo|0x04;
else
LCDInfo=LCDInfo&0xfb;
break;
}
case 6:
{
if(Pixel)
LCDInfo=LCDInfo|0x02;
else
LCDInfo=LCDInfo&0xfd;
break;
}
case 7:
{
if(Pixel)
LCDInfo=LCDInfo|0x01;
else
LCDInfo=LCDInfo&0xfe;
break;
}
}
TempValue=40*PositionY+PositionX/8+0x3000;
OutLCDCmd(0X46);
WAIT_LCD_IDLE;
OutLCDData(TempValue&0xff);
WAIT_LCD_IDLE;
OutLCDData((TempValue>>8)&0xff);
WAIT_LCD_IDLE;
OutLCDCmd(0X4c);
WAIT_LCD_IDLE;
OutLCDCmd(0X42);
WAIT_LCD_IDLE;
OutLCDData(LCDInfo);
}
//-------------显示一段直线-----------
void DisPlayLine(unsigned int StartX,unsigned int StartY,unsigned int Length,unsigned int Direction)
{
unsigned int i=0;
if(Direction)//X方向
{
if(Length>240||Length<=0)
Length=0;
for(i=0;i<Length;i++)
PutPixelLCD(StartX+i,StartY,1);
}
else //Y方向
{
if(Length>320||Length<=0)
Length=0;
for(i=0;i<Length;i++)
PutPixelLCD(StartX,StartY+i,1);
}
}
void Clear_Lcd()
{
ClearLCDLay1();
ClearLCDLay2();
DisPlayLine(0,0,240,1);
DisPlayLine(0,0,320,0);
DisPlayLine(0,319,240,1);
DisPlayLine(239,0,320,0);
DisPlayLine(0,40,240,1);
DisPlayLine(0,292,240,1);
DisPlayLine(64,0,40,0);
DisPlayLine(136,0,40,0);
DisPlayLine(0,1,240,1);
DisPlayLine(1,0,320,0);
DisPlayLine(0,318,240,1);
DisPlayLine(238,0,320,0);
DisPlayLine(0,40,240,1);
DisPlayLine(0,292,240,1);
DisPlayLine(64,0,40,0);
DisPlayLine(136,0,40,0);
}
void main(void)
{
int i;
(*(char *)(lpstr+1))=0x7c;
// Step 1. Initialize System Control:
// PLL, WatchDog, enable Peripheral Clocks
// This example function is found in the DSP281x_SysCtrl.c file.
InitSysCtrl();
// Step 2. Initalize GPIO:
// This example function is found in the DSP281x_Gpio.c file and
// illustrates how to set the GPIO to it's default state.
InitGpio(); // Skipped for this example
// Step 3. Clear all interrupts and initialize PIE vector table:
// Disable CPU interrupts
DINT;
// Initialize PIE control registers to their default state.
// The default state is all PIE interrupts disabled and flags
// are cleared.
// This function is found in the DSP281x_PieCtrl.c file.
InitPieCtrl();
/*** Copy all FLASH sections that need to run from RAM ***/
// Section ramfuncs contains user defined code that runs from CSM secured RAM
/* memcpy( &ramfuncs_runstart,
&ramfuncs_loadstart,
&ramfuncs_loadend - &ramfuncs_loadstart);
*/
/*** Initialize the FLASH ***/
// InitFlash(); // Initialize the FLASH (FILE: SysCtrl.c)
// Disable CPU interrupts and clear all CPU interrupt flags:
IER = 0x0000;
IFR = 0x0000;
// Initialize the PIE vector table with pointers to the shell Interrupt
// Service Routines (ISR).
// This will populate the entire table, even if the interrupt
// is not used in this example. This is useful for debug purposes.
// The shell ISR routines are found in DSP281x_DefaultIsr.c.
// This function is found in DSP281x_PieVect.c.
InitPieVectTable();
// Step 4. Initialize all the Device Peripherals:
// This function is found in DSP281x_InitPeripherals.c
InitPeripherals();
// Step 5. User specific functions, Reassign vectors (optional), Enable Interrupts:
// Reassign ISRs.
// Reassign the PIE vector for T2UFPINT, CAPINT1 , NMI
// and T4PINT to point to a different
// ISR then the shell routine found in DSP28_DefaultIsr.c.
// This is done if the user does not want to use the shell ISR routine
// but instead wants to use their own ISR. This step is optional:
EALLOW; // This is needed to write to EALLOW protected registers
PieVectTable.T2UFINT = &eva_t2ufint_isr;
PieVectTable.CAPINT1 = &eva_capint1_isr;
PieVectTable.CAPINT2 = &eva_capint2_isr;
PieVectTable.RXAINT = &sciarxint_isr;
PieVectTable.RXBINT = &scibrxint_isr;
PieVectTable.XNMI = &xnmi_isr;
EDIS; // This is needed to disable write to EALLOW protected registers
// Enable PIE group 3 interrupt 3,5 for T2UFINT,CAPINT1
PieCtrlRegs.PIEIER3.all = (M_INT3 | M_INT5);
// Enable PIE group 9 interrupt 1,3 for SCIRXINTA, SCIRXINTB
PieCtrlRegs.PIEIER9.all = (M_INT1 | M_INT3);
// Enable CPU INT3 for T2UFINT, CAPINT1,INT9 for SCIRXINTA,SCIRXINTB:
IER |= (M_INT3 | M_INT9);
// Initialize user variable
initvariable();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -