📄 pmsm.c
字号:
#include "Cpu.h"
#include "Events.h"
#include "PWM.h"
#include "MFR.h"
#include "ADC1.h"
#include "PB413.h"
#include "TI1.h"
#include "TO1.h"
#include "ACRELAY.h"
#include "LEDSPI.h"
#include "SCIAS.h"
#include "PE_Types.h"
#include "PE_Error.h"
#include "PE_Const.h"
#include "IO_Map.h"
#include "Start.h"
#include "Close.h"
#include "Smopos.h"
#include "Clarke.h"
#include "Iclarke.h"
#include "Park.h"
#include "Ipark.h"
#include "Filter.h"
#include "Normal.h"
#include "Spdcal.h"
#include "Spdreg.h"
/* PMSM */
int count1; /* used in delay time for DSP work */
int count2; /* used in delay time for DSP work */
int count3; /* used in delay time for DSP work */
int count4; /* used in delay time for PWM output */
volatile int cnt2spd=0; /* set the speedref */
volatile int ktempmax; /* Q15 set the ktemp max value */
volatile double numfmax; /* set the numf max value */
/* Start */
volatile int cnt=1; /* set the frequency of speed change */
volatile int cnt2spdadj=1; /* set the frequency of speed change */
volatile int ktemp=0; /* temp value for k */
volatile int k; /* Q16 set the amplitude of field */
volatile double numn=0; /* set the frequency of rotor speed */
volatile double numf=0;
volatile int num=0;
volatile unsigned int sector=1; /* indicate the sector from 1 to 6 */
/* Event */
volatile long int cnt2drtchg=0; /* set the frequency of direction change */
volatile bool updown=1; /* 1 for anticlockwise, 0 for clockwise */
volatile unsigned int Vbus1;
volatile unsigned int Vbusoffset=144; /* Vbus offset value when real value is zero */
volatile int Vbus; /* Q6 */
volatile unsigned int Ia1; /* A current in ADC */
volatile unsigned int Ib1; /* B current in ADC */
volatile int Iaoffset=2097; /* A current when the value is zero */
volatile int Iboffset=2095; /* Theory value=(4096*1.65)/3.3=2048 */
volatile int Ia; /* A current after shift right 3 bits */
volatile int Iaold;
volatile int Iafst=1;
volatile int Ib; /* A current after shift right 3 bits */
volatile int Imax; /* store the max value of current sense */
volatile bool Imaxfst=0; /* flag to indicate the first */
volatile int cnt2Imax=0;
volatile int index=0; /* indicate the subscript of the array to store the max value */
volatile unsigned int cnt2Imaxchg=0; /* indicate when to change the step */
volatile int Imaxtab[28]; /* store the max Imax,28=1400/50 */
volatile int Imin=0; /* store the min Imax */
volatile int idx=0; /* index for search the min Imax */
volatile int Iminflag=0; /* disable some codes and make the motor run */
volatile int d;
volatile int e;
volatile int a; /* Q15 A phase current */
volatile int b; /* Q15 B phase current */
volatile int c; /* Q15 C phase current */
volatile int alfa; /* Q15 alfa axis phase current */
volatile int beta; /* Q15 beta axis phase current */
volatile int isalfa; /* Q15 alfa axis phase current */
volatile int isbeta; /* Q15 beta axis phase current */
volatile bool negative=0;
volatile int d1; /* Q15 d axis phase current */
volatile int q1; /* Q15 q axis phase current */
volatile int theta; /* rotor position anle */
volatile int cnt2autoadj=0;
volatile bool autoadj=0;
volatile int gujun=0; /* for debug */
volatile int isalfae; /* Q15 alfa axis estimated current */
volatile int isbetae; /* Q15 beta axis estimated current */
volatile int vsalfa; /* Q6 alaf axis phase voltage */
volatile int vsbeta; /* Q6 beta axis phase voltage */
volatile int esalfa; /* Q6 alfa axis back EMF */
volatile int esbeta; /* Q6 beta axis back EMF */
volatile int zalfa; /* Q6 alfa axis sliding control */
volatile int zbeta; /* Q6 beta axis sliding control */
volatile int quadrant; /* determine the rotor angle quadrant */
volatile int thetatemp;
volatile int thetae; /* estimated rotor position anle */
volatile int id; /* Q15 d axis current */
volatile int iq; /* Q15 q axis current */
volatile int idref=0; /* Q15 d axis reference current */
volatile int iqref=10840; /* 3700 Q15 q axis reference current */
volatile int udout; /* Q6 d axis voltage command */
volatile int uqout; /* Q6 q axis voltage command */
volatile int ualfa; /* Q6 alfa axis voltage command */
volatile int ubeta; /* Q6 beta axis voltage command */
volatile int iqout; /* current regulate output */
volatile int iqtemp0=0; /* Q1 */
volatile bool tab=0;
volatile int cnt2tabset=0; /* count to time the table set */
volatile bool tabset=0; /* 1 for set the table, 0 for stop */
volatile int cnt2tabstop=0; /* count to stop set the */
volatile bool tabstop=0; /* 1 for stop the table */
volatile int cnt2ctlchg=0; /* count to change the control strategy */
volatile bool clzloop=0; /* 1 for close loop ctrl, 0 for open loop ctrl */
volatile int ktempchg; /* Q15 */
volatile int numtt=0;
volatile int istemp=1;
volatile int omeg; /* rotor speed */
volatile int tomeg; /* time for rotor rotate 1 cycle */
volatile int spdref=240; /* 1000rpm */
volatile bool filter1=0;
volatile int sensebit=0;
volatile bool normalbit=0;
volatile int smobit=0;
volatile bool stnormal;
volatile int delta;
volatile int deltaold;
volatile int xdtemp0=0;
volatile int xqtemp0=0;
volatile int thetatemp1;
volatile int thetae1;
volatile int k1;
volatile int aa,ab,ac,ad;
volatile int e=1;
volatile unsigned int cnt2omg=0;
volatile int thetaa;
volatile int thetab;
volatile bool spdregbit=0;
//for DAC
volatile unsigned int index1,temp,spi_data,spi_data0,n;
volatile int tempp1,tempp2;
volatile int Icurrent;
volatile int sina[180]=
{
0, 572, 1144, 1715, 2286, 2856, 3425, 3993, 4560,
5126, 5690, 6252, 6813, 7371, 7927, 8481, 9032, 9580,
10126,10668,11207,11743,12275,12803,13328,13848,14365,
14876,15384,15886,16384,16877,17364,17847,18324,18795,
19261,19720,20174,20622,21063,21498,21926,22348,22763,
23170,23571,23965,24351,24730,25102,25466,25822,26170,
26510,26842,27166,27482,27789,28088,28378,28660,28932,
29197,29452,29698,29935,30163,30382,30592,30792,30983,
31164,31336,31499,31651,31795,31928,32052,32166,32270,
32365,32449,32524,32588,32643,32688,32723,32748,32763,
32767,32763,32748,32723,32688,32643,32588,32524,32449,
32365,32270,32166,32052,31928,31795,31651,31499,31336,
31164,30983,30792,30592,30382,30163,29935,29698,29452,
29197,28932,28660,28378,28088,27789,27482,27166,26842,
26510,26170,25822,25466,25102,24730,24351,23965,23571,
23170,22763,22348,21926,21498,21063,20622,20174,19720,
19261,18795,18324,17847,17364,16877,16384,15886,15384,
14876,14365,13848,13328,12803,12275,11743,11207,10668,
10126, 9580, 9032, 8481, 7927, 7371, 6813, 6252, 5690,
5126, 4560, 3993, 3425, 2856, 2286, 1715, 1144, 572
};
volatile int cosa[180]=
{
32767,32763,32748,32723,32688,32643,32588,32524,32449,
32365,32270,32166,32052,31928,31795,31651,31499,31336,
31164,30983,30792,30592,30382,30163,29935,29698,29452,
29197,28932,28660,28378,28088,27789,27482,27166,26842,
26510,26170,25822,25466,25102,24730,24351,23965,23571,
23170,22763,22348,21926,21498,21063,20622,20174,19720,
19261,18795,18324,17847,17364,16877,16384,15886,15384,
14876,14365,13848,13328,12803,12275,11743,11207,10668,
10126, 9580, 9032, 8481, 7927, 7371, 6813, 6252, 5690,
5126, 4560, 3993, 3425, 2856, 2286, 1715, 1144, 572,
0, -572, -1144, -1715, -2286, -2856, -3425,-3993, -4560,
-5126, -5690, -6252, -6813, -7371, -7927, -8481, -9032, -9580,
-10126,-10668,-11207,-11743,-12275,-12803,-13328,-13848,-14365,
-14876,-15384,-15886,-16384,-16877,-17364,-17847,-18324,-18795,
-19261,-19720,-20174,-20622,-21063,-21498,-21926,-22348,-22763,
-23170,-23571,-23965,-24351,-24730,-25102,-25466,-25822,-26170,
-26510,-26842,-27166,-27482,-27789,-28088,-28378,-28660,-28932,
-29197,-29452,-29698,-29935,-30163,-30382,-30592,-30792,-30983,
-31164,-31336,-31499,-31651,-31795,-31928,-32052,-32166,-32270,
-32365,-32449,-32524,-32588,-32643,-32688,-32723,-32748,-32763
};
volatile int tana[46]=
{
0, 572, 1144, 1717, 2291, 2867, 3444, 4023, 4605,
5190 , 5778, 6369, 6965, 7565, 8170, 8780, 9396,10018,
10647,11283,11927,12578,13239,13909,14589,15280,15982,
16696,17423,18164,18919,19689,20476,21280,22102,22944,
23807,24692,25601,26535,27496,28485,29504,30557,31644,
32767
};
volatile int table[360];
volatile int thetabuf[5];
volatile int nbuf=0;
/////PFC
extern int value_CMPLD1,value_CMPLD2,soft_start,p_flag,dc_lowlim,dc_highlim,dcref_trgt;
////
/* for LED */
int data1=1,data2=1000;
int Leddata[8];
extern bool change;
///for SCI
extern SCIAS_TComData senddata[12];
void main(void)
{
/* LED variable definition */
int num1,num2,num11,i,j;
int Value[8];
int Code[8];
int a[10]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x67};
int Bitcode[8]={0xf7,0xfb,0xfd,0xfe,0x7f,0xbf,0xdf,0xef};
/*** Processor Expert internal initialization. DON'T REMOVE THIS CODE!!! ***/
PE_low_level_init(); /* 32us */
/*** End of Processor Expert internal initialization. ***/
for(count2=1;count2<100;count2++) /* 0.5us per loop */
{
for(count1=1;count1<30000;count1++)
{
cnt=1;cnt=1;
}
}
ACRELAY_SetVal();
dc_lowlim=dcref_trgt-0x124;//c3-20v//124-30v
dc_highlim=dcref_trgt+0xc3;
soft_start=1;
/*Initialize timer used to pfc*/
TO1_Enable();
setReg(TMR0_CTRL,0x1024);
setReg(TMR0_SCR,0x0001);
setReg(TMR0_COMSCR,0x0086);
setReg(TMR0_CNTR,0x0000);
setReg(TMR0_LOAD,0x0000);
setReg(TMR0_CMP1,value_CMPLD1);
setReg(TMR0_CMP2,value_CMPLD2);
setReg(TMR0_CMPLD1,value_CMPLD1);
setReg(TMR0_CMPLD2,value_CMPLD2);
setReg(TMR0_CTRL,0x3024);
///PFC initialization end
////for LED
/* for(;;)
{
Leddata[0]=0xf706;
Leddata[1]=0xfb00;
Leddata[2]=0xfd00;
Leddata[3]=0xfe00;
Leddata[4]=0x7f3f;
Leddata[5]=0xbf3f;
Leddata[6]=0xdf3f;
Leddata[7]=0xef06;
}/////for LED end*/
///for SCI
senddata[0]=17;
senddata[1]=34;
senddata[2]=51;
senddata[3]=68;
senddata[4]=85;
senddata[5]=102;
senddata[6]=119;
senddata[7]=136;
senddata[8]=153;
senddata[9]=170;
senddata[10]=187;
senddata[11]=204;
numfmax=2.4;
ktempchg=13;
ktempmax=5362;
clrRegBit(ADC_ADCR1,STOP0);
setRegBit(ADC_ADCR1,START0);
for(count2=1;count2<105;count2++)
{
for(count1=1;count1<10;count1++)
{
cnt=1;cnt=1;
}
}
Ia1=(getReg(ADC_ADRSLT0) & 0x7fff);
Ib1=(getReg(ADC_ADRSLT2) & 0x7fff);
Iaoffset=(int)(Ia1>>3);
Iboffset=(int)(Ib1>>3);
for(;;)
{
if(stnormal==1)
{
Normal();
stnormal=0;
}
if(spdregbit==1)
{
Spdreg();
spdregbit=0;
cnt2omg=0;
}
senddata[2]=(unsigned char)(data2%10+(data2/10)%10*10);
senddata[3]=(unsigned char)((data2/100)%10+(data2/1000)*10);
if(data2>=1000)
senddata[1]=1;/////high speed
else
senddata[1]=0;/////low speed
if(data1==1)
senddata[4]=1;////clockwise
else
senddata[4]=0;////anti-clockwise
if(change)
{
num1=data1;
num11=data1;
num2=data2;
for(i=0;i<4;i++)
{
Value[i]=num1%10;
num1=num1/10;
}
for(i=4;i<8;i++)
{
Value[i]=num2%10;
num2=num2/10;
}
for(i=0;i<8;i++)
{
j=Value[i];
Code[i]=a[j];
}
for(i=0;i<8;i++)
Leddata[i]=Bitcode[i]*256+Code[i];
change=0;
if(num11==1)
{
for(i=1;i<4;i++)
Leddata[i]=(int)(Leddata[i]|0xff00);
}
if(num11==0)
{
for(i=2;i<4;i++)
Leddata[i]=(int)(Leddata[i]|0xff00);
Leddata[0]=Bitcode[0]*256+a[1];
Leddata[1]=Bitcode[1]*256+0x40;
}
}
} /* for loop end */
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -