📄 timer_trigger_adc.c
字号:
#include "2407c.h"
/*adc为高优先级中断模式*/
/*定时器TIMER1触发排序*/
static int inte(long inte_input,int *inte_output);
static int multi_var_filter(int ualpha,int ubeta,int *u_out_alpha,int *u_out_beta);
static int sin_cal(int angle,int *sin_value);
static int cos_cal(int angle,int *cos_value);
const int K=20480; /*20~Q6.10*/
const int OMEGA=20096; /*314~Q10.6*/
const int CONST1=0x24F3;/*Q2.14~1/sqrt3*/
const int CONST2=0x49e6;/*Q2.14~2/sqrt3*/
unsigned int adresult_ua_0_ch;/*定义一个变量 用来保存转换结果0_channel*/
unsigned int adresult_ub_4_ch;/*4_channel*/
unsigned int j = 0;
int theta;/*phase_Q4.12*/
/*Q1.15*/
/*2*pi/256*/
int sin_array_fixed_point[]={0,
804, 1608, 2411, 3212, 4011, 4808, 5602,
6393, 7180, 7962, 8740, 9512, 10279, 11039,
11793, 12540, 13279, 14010, 14733, 15447,
16151, 16846, 17531, 18205, 18868, 19520,
20160, 20788, 21403, 22006, 22595, 23170,
23732, 24279, 24812, 25330, 25833, 26320,
26791, 27246, 27684, 28106, 28511, 28899,
29269, 29622, 29957, 30274, 30572 , 30853,
31114, 31357, 31581, 31786, 31972, 32138,
32286, 32413, 32522, 32610, 32679, 32729,
32758, 32767, 32758, 32729, 32679, 32610,
32522, 32413, 32286, 32138, 31972, 31786,
31581, 31357, 31114, 30853, 30572, 30274,
29957, 29622, 29269, 28899, 28511, 28106,
27684, 27246, 26791, 26320, 25833, 25330,
24812, 24279, 23732, 23170, 22595, 22006,
21403, 20788, 20160, 19520, 18868, 18205,
17531, 16846, 16151, 15447, 14733, 14010,
13279, 12540, 11793, 11039, 10279, 9512, 8740,
7962, 7180, 6393, 5602, 4808, 4011, 3212, 2411,
1608, 804, 0 -804, -1608, -2411, -3212, -4011,
-4808, -5602, -6393, -7180, -7962, -8740, -9512,
-10279, -11039, -11793, -12540, -13279, -14010,
-14733, -15447, -16151, -16846, -17531, -18205,
-18868, -19520, -20160, -20788, -21403, -22006,
-22595,-23170, -23732, -24279, -24812, -25330,
-25833, -26320, -26791, -27246, -27684, -28106,
-28511, -28899, -29269, -29622, -29957, -30274,
-30572, -30853, -31114, -31357, -31581, -31786,
-31972, -32138, -32286, -32413, -32522, -32610,
-32679, -32729, -32758, -32768, -32758, -32729,
-32679, -32610, -32522, -32413, -32286, -32138,
-31972, -31786, -31581, -31357, -31114, -30853,
-30572, -30274, -29957, -29622, -29269, -28899,
-28511, -28106, -27684, -27246, -26791, -26320,
-25833, -25330, -24812, -24279, -23732, -23170,
-22595, -22006, -21403, -20788, -20160, -19520,
-18868, -18205, -17531, -16846, -16151, -15447, -14733,
-14010, -13279, -12540, -11793, -11039, -10279, -9512,
-8740, -7962, -7180, -6393, -5602, -4808, -4011, -3212,
-2411, -1608, -804};
int cos_array_fixed_point[]={32767, 32758, 32729, 32679,
32610, 32522, 32413, 32286, 32138, 31972, 31786,
31581, 31357, 31114, 30853, 30572, 30274,
29957, 29622, 29269, 28899, 28511, 28106,
27684, 27246, 26791, 26320, 25833, 25330,
24812, 24279, 23732, 23170, 22595, 22006,
21403, 20788, 20160, 19520, 18868, 18205,
17531, 16846, 16151, 15447, 14733, 14010,
13279, 12540, 11793, 11039, 10279, 9512, 8740,
7962, 7180, 6393, 5602, 4808, 4011, 3212, 2411,
1608, 804, 0 -804, -1608, -2411, -3212, -4011,
-4808, -5602, -6393, -7180, -7962, -8740, -9512,
-10279, -11039, -11793, -12540, -13279, -14010,
-14733, -15447, -16151, -16846, -17531, -18205,
-18868, -19520, -20160, -20788, -21403, -22006,
-22595,-23170, -23732, -24279, -24812, -25330,
-25833, -26320, -26791, -27246, -27684, -28106,
-28511, -28899, -29269, -29622, -29957, -30274,
-30572, -30853, -31114, -31357, -31581, -31786,
-31972, -32138, -32286, -32413, -32522, -32610,
-32679, -32729, -32758, -32768, -32758, -32729,
-32679, -32610, -32522, -32413, -32286, -32138,
-31972, -31786, -31581, -31357, -31114, -30853,
-30572, -30274, -29957, -29622, -29269, -28899,
-28511, -28106, -27684, -27246, -26791, -26320,
-25833, -25330, -24812, -24279, -23732, -23170,
-22595, -22006, -21403, -20788, -20160, -19520,
-18868, -18205, -17531, -16846, -16151, -15447, -14733,
-14010, -13279, -12540, -11793, -11039, -10279, -9512,
-8740, -7962, -7180, -6393, -5602, -4808, -4011, -3212,
-2411, -1608, -804, 0, 804, 1608, 2411, 3212,
4011, 4808, 5602, 6393, 7180, 7962, 8740,
9512, 10279, 11039, 11793, 12540, 13279,
14010, 14733, 15447, 16151, 16846, 17531,
18205, 18868, 19520, 20160, 20788, 21403,
22006, 22595, 23170, 23732, 24279, 24812,
25330, 25833, 26320, 26791, 27246, 27684,
28106, 28511, 28899, 29269, 29622, 29957,
30274, 30572 , 30853, 31114, 31357, 31581,
31786, 31972, 32138, 32286, 32413, 32522,
32610, 32679, 32729, 32758};
static int multi_var_filter(int ualpha,int ubeta,int *u_out_alpha,int *u_out_beta)
{
static int previous_u_out_alpha;/*Q2.14*/
static int previous_u_out_beta;/*Q2.14*/
int temp_beta;/*Q2.14*/
int temp_alpha;/*Q2.14*/
long temp_k_beta;
long temp_k_alpha;
long temp_omega_alpha;
long temp_omega_beta;
long temp_inte_alpha_input;/*Q11.21*/
long temp_inte_beta_input;/*Q11.21*/
temp_beta=ubeta+~previous_u_out_beta+1;/*Q2.14*/
temp_alpha=ualpha+~previous_u_out_alpha+1;/*Q2.14*/
temp_k_alpha=(long)K*temp_alpha;/*Q8.24*/
temp_k_alpha=temp_k_alpha<<1;/*Q7.25*/
temp_k_alpha=temp_k_alpha>>4;/*Q11.21*/
temp_k_beta=(long)K*temp_beta;/*Q8.24*/
temp_k_beta=temp_k_beta<<1;/*Q7.25*/
temp_k_beta=temp_k_beta>>4;/*Q11.21*/
temp_omega_alpha=(long)previous_u_out_alpha*OMEGA;/*Q12.20*/
temp_omega_alpha=temp_omega_alpha<<1;/*Q11.21*/
temp_omega_beta=(long)previous_u_out_beta*OMEGA;/*Q12.20*/
temp_omega_beta=temp_omega_beta<<1;/*Q11.21*/
temp_inte_alpha_input=temp_k_alpha+~temp_omega_beta+1;/*Q11.21*/
temp_inte_beta_input=temp_k_beta+temp_omega_alpha;/*Q11.21*/
inte(temp_inte_alpha_input,u_out_alpha);
inte(temp_inte_beta_input,u_out_beta);
/*update*/
previous_u_out_alpha=*u_out_beta;
previous_u_out_beta=*u_out_alpha;
return 0;
}
/*inte_input~Q11.21~x(n)*/
/*inte_output~Q2.14~y(n)*/
static int inte(long inte_input,int *inte_output)
{
static long previous_inte_input;/*Q11.21~x(n-1)*/
static long previous_inte_output;/*Q11.21~y(n-1)*/
long inte_genuine_output;/*Q11.21~y(n)*/
inte_genuine_output = inte_input+previous_inte_input+previous_inte_output;/*Q11.21*/
previous_inte_output=inte_genuine_output;/*update~y(n-1)*/
previous_inte_input = inte_input;/*update~x(n-1)*/
inte_genuine_output=inte_genuine_output<<9;/*Q2.30*/
inte_genuine_output=inte_genuine_output>>16;/*Q18.14*/
*inte_output=(int)inte_genuine_output;/*Q2.14*/
return 0;
}
/*lf_input~Q2.14*/
/*lf_output~Q2.14*/
static int loop_filter(int lf_input,int *lf_output)
{
static int previous_lf_input;/*Q2.14~x(n-1)*/
static int previous_lf_output;/*Q2.14~y(n-1)*/
long temp_x_n;
long temp_x_n_1;
long temp_y_n_1;
long temp_y_n;
temp_y_n_1=(long)previous_lf_output*9216;/*Q8.24*/
temp_y_n_1=temp_y_n_1>>10;/*Q18.24*/
temp_x_n=(long)lf_input*22835;/*Q8.24*/
temp_x_n=temp_x_n>>10;/*Q18.14*/
temp_x_n_1=(long)lf_output*18268;/*Q8.24*/
temp_x_n_1=temp_x_n_1>>10;/*Q18.14*/
temp_y_n=temp_x_n+~temp_x_n_1+1+temp_y_n_1;/*Q18.14*/
*lf_output=(int)temp_y_n;/*trucated~Q2.14*/
/*update*/
previous_lf_input=lf_input;/*x(n)->x(n-1)*/
previous_lf_output=*lf_output;/*y(n)->y(n-1)*/
return 0;
}
/*vco_input~Q2.14*/
/*vco_output~Q4.12*/
/*2*pi~Q4.12~25735*/
/*-2.5*pi<theta<2.5*pi*/
/*2.5*pi~Q4.12~32196*/
static int VCO(int vco_input,int *vco_output)
{
int temp_vco_input=vco_input>>2;/*Q4.12*/
static int previous_output;/*Q4.12~y_n_1*/
static int previous_input;/*Q4.12~x_n_1*/
int _2PI = 25735;/*2*pi*/
int _2_5_PI = 32196;
int neg_2_5_PI = ~_2_5_PI+1;
int neg_2PI;
neg_2PI = ~_2PI+1;
*vco_output=temp_vco_input+previous_output+previous_input;/*Q4.12*/
/*consider overflow state*/
if((*vco_output>=neg_2PI)&&(*vco_output<=_2PI))
{
previous_output=*vco_output;/*Q4.12*/
previous_input=temp_vco_input;/*Q4.12*/
return 0;
}
if((*vco_output>=25735)&&(*vco_output<_2_5_PI))
{
*vco_output=*vco_output+~_2PI+1;/*2*pi is mod*/
previous_output=*vco_output;/*Q4.12*/
previous_input=temp_vco_input;/*Q4.12*/
return 0;
}
else if((*vco_output<=neg_2PI)&&(*vco_output>neg_2_5_PI))
{
*vco_output=*vco_output+_2PI;
previous_output=*vco_output;/*Q4.12*/
previous_input=temp_vco_input;/*Q4.12*/
return 0;
}
else
{
return -1;
}
}
/*ua,ub~Q1.15*/
/*ualpha,ubeta,Q2.14*/
static int clarke_trans(int ua,int ub,int *ualpha,int *ubeta)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -