📄 myad.c
字号:
#include "LF2407.h"
/*some constants*/
#define N 512
#define M 64
#define MM 6
#define SIZE 500
int buffer1[SIZE];
int buffer2[SIZE];
int buffer3[SIZE];
int buffer4[SIZE];
int buffer5[SIZE];
int buffer6[SIZE];
int buffer7[SIZE];
int buffer8[SIZE];
int buffer9[SIZE];
int buffer10[SIZE];
int buffer11[SIZE];
int buffer12[SIZE];
int buffer13[SIZE];
int buffer14[SIZE];
int buffer15[SIZE];
int buffer16[SIZE];
int buffer17[SIZE];
int buffer18[SIZE];
int buffer19[SIZE];
int buffer20[SIZE];
int buffer21[SIZE];
int buffer22[SIZE];
/*sin,acos table*/
extern const int MySin[513]; //0.33333*sin
extern const int MyAcos[513];
ioport unsigned char port0000;
ioport unsigned char port0001;
ioport unsigned char port0002;
ioport unsigned char port0003;
ioport unsigned char port0004;
ioport unsigned char port0008;
ioport unsigned char port000C;
ioport unsigned int portFFFF;
#define DA0 port0000
#define DA1 port0001
#define DA2 port0002
#define DA3 port0003
#define DA4 port0004
#define DA8 port0008
#define DAC port000C
#define WSGR portFFFF
/*"gd" means specify,"fk" means feedback,"qk" means feedforward*/
int w_gd,w_fk=0,w_pre=0;
int id_gd,id_fk=0;
int iq_gd,iq_fk=0;
int if_gd,if_fk=0; /* 约1.69A */
long ud_qk=0,uq_qk=0;
/*speed-regulater,iq-regulater,id-regulater,if-regulater*/
long int w_out=0,w_sum=0;
long int iq_out=0,iq_sum=0;
long int id_out=0,id_sum=0;
long int if_out=0,if_sum=0;
/*regulator reference*/
int w_kp,w_ki;
int i_kp,i_ki;
int i_kp,i_ki;
int if_kp,if_ki;
/*fundmental variables*/
int ua,ub,uc;
int ud,uq;
int ia,ib,ic;
int theta=0;
int pos=0,pos2;
int i=0,j=0; //用于设置缓冲时间
int ii=0,jj=0; //用于速度给定
int iii=0,jjj=0; //用于求出速度
int iiii=0,jjjj=0; //用于求出id,iq
int bFirstTime=1;
int t=5; /*标示程序是否在运行*/
int bStop=0;
long int temp,temp2,temp3; /*临时的长整数*/
int n; /*临时的整数*/
long int MaxU,MaxI;
int UU;
int error=0;
int K[2][5]={{100,200,300,500,1000},
{10,15,20,30,50} /* i_kp*/
};
int a=0,b=0,c=0,d=0;
int bAuto=0;
int direct=1;
int kk=8;
#define NUM 38
int W[NUM]={64,96,
128,128,192,224,256,288,320,352,
384,416,448,480,512,512,512,544,576,544,512,512,512,480,448,416,384,
352,320,288,256,224,92,128,128,
96,64,0};
long MAXU[NUM]={725216,725216,
1725216,1725216,
6925216,6925216,6925216, 6925216,6925216,6925216,
6925216,6925216,6925216,6925216,6925216,6925216,6925216,6925216,6925216,6925216,6925216,6925216,6925216,6925216,6925216,6925216,6925216,
6925216,6925216,6925216,6925216,6925216,6925216,
1725216,1725216,
725216,725216,725216};
int IKP[NUM]={200,200,
200,200,
100,100,100,100,100,100,
20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,
100,100,100,100,100,100,
200,200,
200,200,200};
int IFGD[NUM]={216,216,
216,216,
216,216,216,216,216,216,
67,67,67,67,67,67,67,67,67,67,67,67,67,67,67,67,67,
216,216,216,216,216,216,
216,216,
216,216,216};
int KK[NUM]={8,8,
8,8,
4,4,4,4,4,4,
8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
4,4,4,4,4,4,
8,8,
8,8,8};
void initial()
{
asm(" clrc SXM"); // 符号位扩展
asm(" clrc OVM"); // 累加器中结果正常溢出
asm(" clrc CNF"); // B0被配置为数据存储空间
*SCSR1=0x83CE; // CLKIN=6M,CLKOUT=4*CLKIN=24M
*WDCR=0x0E8;
*IMR=0x0001; // 允许INT1中断
*IFR=0x0FFFF; // 清除全部中断标志,"写1清0"
}
void ADINIT()
{
*T4CNT=0; // T4计数器清0
*T4CON=0X150C; // T4为连续增计数模式,32分频,且选用内部时钟源
*T4PER=1830; // 设置T4的周期寄存器
*GPTCONB=0X400; // T4周期中断标志触发AD转换
*EVBIMRB=0X01; // 清除EVB中断标志,写"1"清0
*EVBIFRB=0X0FFFF; // 清除EVB中断标志,写"1"清0
*ADCTRL1=0X990; // 采样时间窗口预定标位ACQ PS3-ACQ PS0为0,
// 转换时间预定标位CPS为9,AD为启动/停止模式,排
// 序器为级连工作方式,且禁止特殊的两种工作模式
*ADCTRL2=0X8404; // 可以用EVB的一个事件信号触发AD转换,
// 且用中断模式1
*MAXCONV=3; // 4通道
*CHSELSEQ1=0XA720;
}
// 启动AD转换子程序(通过启动定时器4的方式间接启动)
void ADSOC()
{
*T4CON=*T4CON|0X40;// 启动定时器4
}
// 若是其它中断则直接返回子程序
void interrupt nothing()
{
return;
}
void ValueInit()
{
if_ki=0; /*5411.8->42*//*周期为1/1024秒时,21*/
w_ki=0;/*2032.9->798*/ /*周期为1/1024秒时,399*/
i_ki=0;/*654.7*->82*/ /*周期为1/1024秒时,41*/
if_kp=500; /*779.853->3119*/
w_kp=200;/*34.56->6949*/
i_kp=100;/*37.62->2408*/
UU=970;
w_gd=192;
id_gd=0;
if_gd=216; /* 约1.69A */
MaxU=6925216;
MaxI=10240; /*20480=10A*/
bAuto=0;
kk=4;
}
void interrupt adint()
{
int flag;
int a_cos,a_sin,b_cos,b_sin,c_cos,c_sin;
static int id_buf[M],iq_buf[M],if_buf[M],w_buf[16];
static long int sumId=0,sumIq=0,sumIf=0;
*T4CNT=0X0000;
*MAINSTART=0;
*RECSTART=0;
flag=*EVBIFRB&0X01;
if(flag!=0x01)
{
asm(" clrc INTM"); // 允许总中断
return; // 如果不是定时器1周期中断,则直接返回
}
if(bAuto)
{
ii++;
if(ii==1500)
{
a++;
if(a==5)
{
a=0;
b++;
}
if(b==5)
b=5;
//w_kp=K[0][a];
//i_kp=K[1][b];
ii=0;
if(jj<NUM)
{
w_gd=W[jj]*direct;
if_gd=IFGD[jj];
MaxU=MAXU[jj];
i_kp=IKP[jj];
kk=KK[jj];
jj++;
}
else
{
if(jj==NUM)
{
jj=0;
direct*=-1;
}
}
}
}
*POSITION=0xff00;
pos=*POSITION;
pos2=pos;
/********************AD采样值读取***********************************/
temp=(*RESULT0)>>6;
temp=(temp*325)>>10;
if_fk=temp;
temp=(*RESULT1)>>6;
temp=((temp-449)*609)>>10;
ia=temp;
temp=(*RESULT2)>>6;
temp=((temp-449)*609)>>10;
ib=temp;
temp=(*RESULT3)>>6;
temp=((temp-449)*609)>>10;
ic=temp;
/*****************************得到位置*******************************************/
if(pos>N)pos-=N; //512-1024的数处理
theta=pos-197;
a_sin=theta;
a_cos=theta+128;
b_sin=theta-171;
b_cos=theta-43;
c_sin=theta+171;
c_cos=theta-214;
if(a_sin<0)a_sin+=N;
if(a_cos<0)a_cos+=N;
if(b_cos<0)b_cos+=N;
if(b_sin<0)b_sin+=N;
if(c_cos<0)c_cos+=N;
if(c_sin<0)c_sin+=N;
/*****************************得到速度*******************************************/
if(iii<16)
{
w_fk=0;
w_buf[iii]=pos2;
iii++;
}
else
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -