This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

三相SRF锁相环IQ格式.h文件有问题,我输出的频率根本不对 这是我的调试程序,请帮我看一下哪里有问题,谢谢



enum {Va,Vb,Vc};
//enum {Alpha,Beta,Zero};
//enum {D,Q,Z};
_iq voltage[3];
_iq theta[3] = {0,120,240};
_iq A = _IQ(325.2691193);  //230*sqrt(2)
_iq Vmeas[3];
_iq pll_theta[2];
_iq pll_grid_freq;
int16 VTimer0[4]; // Virtual Timers slaved off CPU Timer 0 (A events)
int16 VTimer1[4]; // Virtual Timers slaved off CPU Timer 1 (B events)
int16 VTimer2[4]; // Virtual Timers slaved off CPU Timer 2 (C events)  虚拟定时器消耗定时器2
int16 SerialCommsTimer;   //串行通讯计时器        这四个参数状态机中用到
// Global variables used in this system
_iq VdTesting = _IQ(0.2); // Vd reference (pu)  Vd参考
_iq VqTesting = _IQ(0.2); // Vq reference (pu)  Vq参考
_iq Grid_freq =_IQ(50.0);  /
_iq GridMeas1;
_iq GridMeas2;
_iq GridMeas3;
int16 DlogCh1 = 0;        //DLOG函数的初值
int16 DlogCh2 = 0;
int16 DlogCh3 = 0;
int16 DlogCh4 = 0;
volatile Uint16 EnableFlag = FALSE;      //在运行的时候,需要在变量窗口将EnableFlag位置1;
// Instance a SPLL object
  SPLL_3ph_SRF_IQ spll1;
 // Instanse a ABC_DQ0 object
 ABC_DQ0_POS_IQ abc_dq0_pos1;
// Instance a few transform objects (ICLARKE is added in SVGEN module)实例几个转换对象(SVGEN模块中添加了ICLARKE)
CLARKE clarke1 = CLARKE_DEFAULTS;
PARK park1 = PARK_DEFAULTS;
IPARK ipark1 = IPARK_DEFAULTS;
// Instance a PWM driver instance   实例PWM驱动程序实例
PWMGEN pwm1 = PWMGEN_DEFAULTS;
// Instance a Space Vector PWM modulator. This modulator generates a, b and c
// phases based on the d and q stationery reference frame inputs
//实例一个空间向量PWM调制器。这个调制器产生a b c,相位基于d和q的文具参考框架输入
SVGEN svgen1 = SVGEN_DEFAULTS;
// Instance a ramp(sawtooth) generator to simulate an Anglele 实例一个斜坡(锯齿)发生器模拟一个
RAMPGEN rg1 = RAMPGEN_DEFAULTS;
// Create an instance of DATALOG Module   创建DATALOG模块
DLOG_4CH dlog = DLOG_4CH_DEFAULTS;      
void main(void)
{
DeviceInit(); // Device Life support & GPIO 设备生命支持和GPIO
InitFlash(); // Call the flash wrapper init function
   // Waiting for enable flag set   等待启用标志设置
   while (EnableFlag==FALSE) 
    { 
      BackTicker++;
    }
// Timing sync for background loops      背景循环的定时同步
// Timer period definitions found in device specific PeripheralHeaderIncludes.h   特定于设备的定时器周期定义
CpuTimer0Regs.PRD.all =  mSec1; // A tasks   1ms 
CpuTimer1Regs.PRD.all =  mSec5; // B tasks   5ms
CpuTimer2Regs.PRD.all =  mSec50; // C tasks   50ms
// Tasks State-machine init     任务状态机初始化
Alpha_State_Ptr = &A0;
A_Task_Ptr = &A1;
B_Task_Ptr = &B1;
C_Task_Ptr = &C1;
//初始化abc_dq模块
ABC_DQ0_POS_IQ_init(&abc_dq0_pos1);
//  初始化SPLL模块
SPLL_3ph_SRF_IQ_init(Grid_freq,_IQ21((float)(1.0/ISR_FREQUENCY)), &spll1);  //l
// Initialize PWM module    PWM模块初始化
    pwm1.PeriodMax = SYSTEM_FREQUENCY*1000000*T/2;  // 
    pwm1.HalfPerMax=pwm1.PeriodMax/2;
    pwm1.Deadband  = 2.0*SYSTEM_FREQUENCY;        // 120 counts -> 2.0 usec for TBCLK = SYSCLK/1
    PWM_INIT_MACRO(1,2,3,pwm1)
    
// Initialize DATALOG module
    dlog.iptr1 = &DlogCh1;
    dlog.iptr2 = &DlogCh2;
    dlog.iptr3 = &DlogCh3;
    dlog.iptr4 = &DlogCh4;
    dlog.trig_value = 0x1;
    dlog.size = 0x0C8;
    dlog.prescalar = 5;
    dlog.init(&dlog);
    
// Initialize ADC for DMC Kit Rev 1.1 为DMC Kit Rev 1.1初始化ADC
//ChSel[0]=1; // Dummy meas. avoid 1st sample issue Rev0 Picollo*/
//ChSel[1]=1; // ChSelect: ADC A1-> Phase A Current    A相电流
//ChSel[2]=9; // ChSelect: ADC B1-> Phase B Current
//ChSel[3]=3; // ChSelect: ADC A3-> Phase C Current
//ChSel[4]=15; // ChSelect: ADC B7-> Phase A Voltage    A相电压
//ChSel[5]=14; // ChSelect: ADC B6-> Phase B Voltage
//ChSel[6]=12; // ChSelect: ADC B4-> Phase C Voltage
//ChSel[7]=7; // ChSelect: ADC A7-> DC Bus  Voltage   直流总线电压
 
// Initialize the RAMPGEN module    初始化RAMPGEN模块
  rg1.StepAngleMax = _IQ(BASE_FREQ*T);
//Call HVDMC Protection function   调用HVDMC保护功能
HVDMC_Protection();
// Reassign ISRs.     重新分配的isr
EALLOW; // This is needed to write to EALLOW protected registers  这是写EALLOW保护寄存器所需要的
PieVectTable.EPWM1_INT = &OffsetISR;
EDIS;
// Enable PIE group 3 interrupt 1 for EPWM1_INT    为EPWM1_INT启用PIE组3中断1
    PieCtrlRegs.PIEIER3.bit.INTx1 = 1;
// Enable CNT_zero interrupt using EPWM1 Time-base    时基模块  使用EPWM1时基启用CNT_zero中断    事件触发模块ETSEl
    EPwm1Regs.ETSEL.bit.INTEN = 1;   // Enable EPWM1INT generation     使能中断产生
    EPwm1Regs.ETSEL.bit.INTSEL = 2;  // Enable interrupt period event    使能中断周期事件
    EPwm1Regs.ETPS.bit.INTPRD = 1;   // Generate interrupt on the 1st event   在第一类事件中产生中断
EPwm1Regs.ETCLR.bit.INT = 1;     // Enable more interrupts    使能更多中断
// Enable CPU INT3 for EPWM1_INT:          CPU响应中断
IER |= M_INT3;
// Enable global Interrupts and higher priority real-time debug events: 启用全局中断和更高优先级的实时调试事件
EINT;   // Enable Global interrupt INTM  启用全局中断INTM
ERTM; // Enable Global realtime interrupt DBGM    启用全局实时中断DBGM
//spll1.theta[0] = DEG_TO_RAD*0;
//spll1.theta[1] = DEG_TO_RAD*0;
// IDLE loop. Just sit and loop forever:  空闲循环,持续等待
for(;;)  //infinite loop   无限循环
{
// State machine entry & exit point    状态机进入和退出点
//===========================================================
(*Alpha_State_Ptr)(); // jump to an Alpha state (A0,B0,...)   跳转到Alpha状态(A0,B0,…)
//===========================================================
}
} //END MAIN CODE
//=================================================================================
// STATE-MACHINE SEQUENCING AND SYNCRONIZATION FOR SLOW BACKGROUND TASKS
//   对缓慢的后台任务进行状态机排序和同步
//=================================================================================
//--------------------------------- FRAMEWORK -------------------------------------
void A0(void)
{
// loop rate synchronizer for A-tasks  a任务的循环速率同步器
if(CpuTimer0Regs.TCR.bit.TIF == 1)
{
CpuTimer0Regs.TCR.bit.TIF = 1; // clear flag   清标志位
//-----------------------------------------------------------
(*A_Task_Ptr)(); // jump to an A Task (A1,A2,A3,...)  跳转到Alpha状态(A0,B0,…)
//-----------------------------------------------------------
VTimer0[0]++; // virtual timer 0, instance 0 (spare)  虚拟定时器0,实例0(备)
SerialCommsTimer++;
}
Alpha_State_Ptr = &B0; // Comment out to allow only A tasks  注释只能允许一个任务
}
void B0(void)
{
// loop rate synchronizer for B-tasks  b任务的循环速率同步器
if(CpuTimer1Regs.TCR.bit.TIF == 1)
{
CpuTimer1Regs.TCR.bit.TIF = 1; // clear flag  清标志位
//-----------------------------------------------------------
(*B_Task_Ptr)(); // jump to a B Task (B1,B2,B3,...)   跳到B任务(B1 B2 B3…)
//-----------------------------------------------------------
VTimer1[0]++; // virtual timer 1, instance 0 (spare)  虚拟定时器1,实例0(备)
}
Alpha_State_Ptr = &C0; // Allow C state tasks
}
void C0(void)
{
// loop rate synchronizer for C-tasks
if(CpuTimer2Regs.TCR.bit.TIF == 1)
{
CpuTimer2Regs.TCR.bit.TIF = 1; // clear flag
//-----------------------------------------------------------
(*C_Task_Ptr)(); // jump to a C Task (C1,C2,C3,...)   跳转到一个C任务(C1,C2,C3,…)
//-----------------------------------------------------------
VTimer2[0]++; //virtual timer 2, instance 0 (spare)  虚拟定时器2,实例0(备)
}
Alpha_State_Ptr = &A0; // Back to State A0     返回状态0
}
//=================================================================================
// A - TASKS (executed in every 1 msec)     A -任务(每1毫秒执行一次)
//=================================================================================
//--------------------------------------------------------
void A1(void) // SPARE (not used)    备用(不使用)
//--------------------------------------------------------
{
if(EPwm1Regs.TZFLG.bit.OST==0x1)
TripFlagDMC=1;      // Trip on DMC (halt and IPM fault trip )   DMC跳闸(停机和IPM故障跳闸)
//-------------------
//the next time CpuTimer0 'counter' reaches Period value go to A2
//下一次当CpuTimer0 'counter'到达周期值时,转到A2
A_Task_Ptr = &A2;
//-------------------
}
//-----------------------------------------------------------------
void A2(void) // SPARE (not used)
//-----------------------------------------------------------------
{
//-------------------
//the next time CpuTimer0 'counter' reaches Period value go to A3
A_Task_Ptr = &A3;
//-------------------
}
//-----------------------------------------
void A3(void) // SPARE (not used)
//-----------------------------------------
{
//-----------------
//the next time CpuTimer0 'counter' reaches Period value go to A1
A_Task_Ptr = &A1;
//-----------------
}
//=================================================================================
// B - TASKS (executed in every 5 msec)     B -任务(每5毫秒执行一次)
//=================================================================================
//----------------------------------- USER ----------------------------------------
//----------------------------------------
void B1(void) // Toggle GPIO-00    切换GPIO-00
//----------------------------------------
{
//-----------------
//the next time CpuTimer1 'counter' reaches Period value go to B2
B_Task_Ptr = &B2;
//-----------------
}
//----------------------------------------
void B2(void) //  SPARE
//----------------------------------------
{
//-----------------
//the next time CpuTimer1 'counter' reaches Period value go to B3
B_Task_Ptr = &B3;
//-----------------
}
//----------------------------------------
void B3(void) //  SPARE
//----------------------------------------
{
//-----------------
//the next time CpuTimer1 'counter' reaches Period value go to B1
B_Task_Ptr = &B1;
//-----------------
}
//=================================================================================
// C - TASKS (executed in every 50 msec)      (每50秒执行一次)
//=================================================================================
//--------------------------------- USER ------------------------------------------
//----------------------------------------
void C1(void) // Toggle GPIO-34     切换GPIO-34
//----------------------------------------
{
if(EPwm1Regs.TZFLG.bit.OST==0x1) // TripZ for PWMs is low (fault trip)PWMs的TripZ值很低(故障跳闸)
 { TripFlagDMC=1;         
 }
 
GpioDataRegs.GPBTOGGLE.bit.GPIO34 = 1;   // Turn on/off LD3 on the controlCARD 打开/关闭控制卡上的LD3
//-----------------
//the next time CpuTimer2 'counter' reaches Period value go to C2
C_Task_Ptr = &C2;
//-----------------
}
//----------------------------------------
void C2(void) //  SPARE
//----------------------------------------
{
//-----------------
//the next time CpuTimer2 'counter' reaches Period value go to C3
C_Task_Ptr = &C3;
//-----------------
}
//-----------------------------------------
void C3(void) //  SPARE
//-----------------------------------------
{
//-----------------
//the next time CpuTimer2 'counter' reaches Period value go to C1
C_Task_Ptr = &C1;
//-----------------
}
//MainISR 
interrupt void MainISR(void)
{
// Verifying the ISR
    IsrTicker++;   //中断指示器
// =============================== LEVEL 1 ======================================
//  Checks target independent modules, duty cycle waveforms and PWM update
//  Keep the motors disconnected at this level
    //目标独立模块、占空比波形和PWM更新
// ==============================================================================
#if (BUILDLEVEL==LEVEL1)  
    rg1.Freq = (float32)(AC_FREQ); //
    rg1.StepAngleMax = (float32)(1.0/INV_PWM_SWITCHING_FREQUENCY);
    RG_MACRO(rg1)
    //  使用SPLL模块
       int i;
       voltage[Va] = A*_IQsin(DEG_TO_RAD*theta[Va]);
       voltage[Vb] = A*_IQsin(DEG_TO_RAD*theta[Vb]);
       voltage[Vc] = A*_IQsin(DEG_TO_RAD*theta[Vc]);
       for(i=0;i<3;i++)
       {
           theta[i] = theta[i] + 0.9;
           if(theta[i] >= 360)
           {
               theta[i] = theta[i] - 360;
           }
       }
       for(i=0;i<3;i++)
       {
           Vmeas[i] = voltage[i]/230;
       }
       GridMeas1=(Vmeas[Va]);
       GridMeas2=(Vmeas[Vb]);
       GridMeas3=(Vmeas[Vc]);
         abc_dq0_pos1.a = _IQmpy(GridMeas1,_IQ(0.5));
         abc_dq0_pos1.b = _IQmpy(GridMeas2,_IQ(0.5));
         abc_dq0_pos1.c = _IQmpy(GridMeas3,_IQ(0.5));
         abc_dq0_pos1.sin=_IQsin((spll1.theta[1])<<3);  //Q24 to Q21
         abc_dq0_pos1.cos=_IQcos((spll1.theta[1])<<3);
        ABC_DQ0_POS_IQ_MACRO(abc_dq0_pos1);//Q24 to Q21
       spll1.v_q[0] = (int32)(_IQtoIQ21(abc_dq0_pos1.q)); //SPLL CALL    这里貌似不能使用常量,这好像是错误的起因;你能瞎给值
       SPLL_3ph_SRF_IQ_FUNC(&spll1);               //使用这个不会报错           vq0是输入AC电压瞬时采样值为归一化值,在[-1,1]之内。
      pll_theta[0] = spll1.theta[0];
      pll_theta[1] = spll1.theta[1];
      pll_grid_freq = spll1.fo;