svm.c

来自「这是英飞凌公司基于xc166开发的foc方式控制的pmsm电机例程 非常有参加价」· C语言 代码 · 共 128 行

C
128
字号
#include "MAIN.H"
#include "DEF.H"
#include "TableQ15.h"

unsigned int tk,tk1,to;					

const int PulseMin_Div2 = 15;

//****************************************************************************
// @Function      void SVM (TComplex  *m, char *Sector) 
//
//----------------------------------------------------------------------------
// @Description   This Function calculate the compare values t0, tk and tk+1  
//                for the SVM. Then this values are written in the Shadow-
//                Registers of CC6-Unit
//					
//                Execution time 4.2us @ 40MHz 
//
//----------------------------------------------------------------------------
// @Returnvalue   None
//
//----------------------------------------------------------------------------
// @Parameters    None
//
//----------------------------------------------------------------------------
// @Date          15.10.2003
//
//****************************************************************************
void SVM(TComplex  *m, char *Sector)					
{
	int x;
	bit Abort=1;

	// determiination of the sector
	x=((long)m->imag*(long)SQRT3INV) >> 15;	
	if (m->imag>0)
	{	// m.imag>0
		if (m->real>0)
		{	// Quadrant 1
			if (x<m->real)	{*Sector=0;} else	{*Sector=1;}
		}
		else
		{	// Quadrant 2
			if (x< -m->real)	{*Sector=2;} else	{*Sector=1;}
		}
	}
	else
	{	// m.imag<0
		if (m->real<0)
		{	// Quadrant 3
			if (x<m->real)	{*Sector=4;} else	{*Sector=3;}
		}
		else
		{	// Quadrant 4
			if (x< -m->real)	{*Sector=4;} else	{*Sector=5;}
		}
	}    
	// Sector contain the Sector number minus 1 (from 0 to 5)
	// Calul of Tk and Tk+1 
	// Tk=		sin(k)*m.Re		-	Cos(k)*m.Im			k is the sector number
	// Tk+1=	Cos(k-1)*m.Im	-	Sin(k-1)*m.Re

	tk1	=	((long)CosVectorQ1_15[*Sector]*(long)m->imag - (long)SinVectorQ1_15[*Sector]*(long)m->real) >> 15;
	tk1	=	((long)tk1 * (long)TPWM) >> 15;

	tk	=	((long)SinVectorQ1_15[*Sector+1]*(long)m->real -	(long)CosVectorQ1_15[*Sector+1]*(long)m->imag) >> 15;
	tk	=	((long)tk * (long)TPWM) >> 15;

// lengthen the states to measure the currents
/*
	if (tk<PulseMin_Div2)	
	{
		tk=PulseMin_Div2;
	}
	if (tk1<PulseMin_Div2)
	{
		tk1=PulseMin_Div2;
	}
*/

	to=1001-tk-tk1;						

	// Write t0, 1k and tk1 to the shadow registers of CC6
	// Set Timer 13 Event Control, for ADC start
	switch (*Sector)
	{
	case 0:
		CCU6_TCTR2     =  0x006A;
		CCU6_CC60SR=to>>2;
		CCU6_CC61SR=500-(to>>2)-(tk1>>1);
		CCU6_CC62SR=500-(to>>2);
		break;	
	case 1:
		CCU6_TCTR2     =  0x0066;
		CCU6_CC60SR=(to>>2)+(tk1>>1);
		CCU6_CC61SR=to>>2;
		CCU6_CC62SR=500-(to>>2);
		break;	
	case 2:
		CCU6_TCTR2     =  0x006E;
		CCU6_CC60SR=500-(to>>2);
		CCU6_CC61SR=to>>2;
		CCU6_CC62SR=500-(to>>2)-(tk1>>1);
		break;	
	case 3:
		CCU6_TCTR2     =  0x006A;		
		CCU6_CC60SR=500-(to>>2);
		CCU6_CC61SR=(to>>2)+(tk1>>1);
		CCU6_CC62SR=to>>2;
		break;	
	case 4:
		CCU6_TCTR2     =  0x0066;
		CCU6_CC60SR=500-(to>>2)-(tk1>>1);
		CCU6_CC61SR=500-(to>>2);
		CCU6_CC62SR=to>>2;
		break;	
	case 5:
		CCU6_TCTR2     =  0x006E;
		CCU6_CC60SR=(to>>2);
		CCU6_CC61SR=500-(to>>2);
		CCU6_CC62SR=(to>>2)+(tk1>>1);
		break;	
	}

}


⌨️ 快捷键说明

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