//ADC部分
//2012.6.6 7:39
//采集对象:母线电压、母线电流、电容1电流A相和C相,电容2电流A相和C相;
//结果:母线电压、母线电流、相位角、有功、无功、视在功率、电压谐波畸变率、
//电流谐波畸变率、1-13谐波电流值;电容器1、2的A,C相电流,判断电容缺相、倒送电,
//电容器过流、母线过压、欠压,功率因数;
#include "ad.h"
#pragma data:code
const float sin_tab[Coll_Num]={0,
0.19509032201612826784828486847702,
0.3826834323650897717284599840304,
0.55557023301960222474283081394853,
0.70710678118654752440084436210485,
0.83146961230254523707878837761791,
0.92387953251128675612818318939679,
0.98078528040323044912618223613424,
1,
0.98078528040323044912618223613424,
0.92387953251128675612818318939679,
0.83146961230254523707878837761791,
0.70710678118654752440084436210485,
0.55557023301960222474283081394853,
0.3826834323650897717284599840304,
0.19509032201612826784828486847702,
0,
-0.19509032201612826784828486847702,
-0.3826834323650897717284599840304,
-0.55557023301960222474283081394853,
-0.70710678118654752440084436210485,
-0.83146961230254523707878837761791,
-0.92387953251128675612818318939679,
-0.98078528040323044912618223613424,
-1,
-0.98078528040323044912618223613424,
-0.92387953251128675612818318939679,
-0.83146961230254523707878837761791,
-0.70710678118654752440084436210485,
-0.55557023301960222474283081394853,
-0.3826834323650897717284599840304,
-0.19509032201612826784828486847702
};
const float cos_tab[Coll_Num]={1,
0.98078528040323044912618223613424,
0.92387953251128675612818318939679,
0.83146961230254523707878837761791,
0.70710678118654752440084436210485,
0.55557023301960222474283081394853,
0.3826834323650897717284599840304,
0.19509032201612826784828486847702,
0,
-0.19509032201612826784828486847702,
-0.3826834323650897717284599840304,
-0.55557023301960222474283081394853,
-0.70710678118654752440084436210485,
-0.83146961230254523707878837761791,
-0.92387953251128675612818318939679,
-0.98078528040323044912618223613424,
-1,
-0.98078528040323044912618223613424,
-0.92387953251128675612818318939679,
-0.83146961230254523707878837761791,
-0.70710678118654752440084436210485,
-0.55557023301960222474283081394853,
-0.3826834323650897717284599840304,
-0.19509032201612826784828486847702,
0,
0.19509032201612826784828486847702,
0.3826834323650897717284599840304,
0.55557023301960222474283081394853,
0.70710678118654752440084436210485,
0.83146961230254523707878837761791,
0.92387953251128675612818318939679,
0.98078528040323044912618223613424,
};
const uchar DataItempNum[Coll_Num]={0,16,8,24,4,20,12,28,2,18,10,26,6,22,14,30
,1,17,9,25,5,21,13,29,3,19,11,27,7,23,15,31};
#pragma data:data
uint m_Concord[7];//谐波电流参数
uchar AD_Channel[7];//加上两路电容电流测量保护,分别为基准,主变电压,主变电流,电容1电流A,C相,电容2电流A,C相
uchar m_ADCCont;
/////////////////////
uchar m_IsAdc_C1=0;
////////////////////////////////
int AD_Result[7][Coll_Num];//采样结果,加上4路电容电流测量保护,分别为基准,主变电流,主变电压,电容电流1,电容电流2.
float AD_FinalResult[12];//显示二次值。
//0,主变电流;1,主变电压;2,视在功率;3,谐波电流畸变率;
//4,谐波电压畸变率;5,电容1电流A相;6,电容1电流C相;7,相角度;
//8,电容2电流A相;9,电容2电流C相;10,有功;11,无功;
float dataR[2][Coll_Num]; //FFT分析用变量。
//调试过程中使用的数据
//定义校正角度
uint AngleCorrect=0;/////////////////////////注意初始值。
void addelay()
{
uchar cdk=0;
for(;cdk<8;cdk++);
}
void BegColl()
{
AD_Channel[0]=0X03;//基准
AD_Channel[1]=0X06; //主变电流
AD_Channel[2]=0X07;//主变电压
AD_Channel[3]=0X05; //电容2电流A相
AD_Channel[4]=0X04;//电容2电流C相
AD_Channel[5]=0X00;//电容1电流C相
AD_Channel[6]=0X02;//电容1电流A相
m_ADCCont=0;
TCNT1H=DEF_time1h;
TCNT1L=DEF_time1l;
TIMSK=(1<<TOIE1);
TCCR1B=(1<<CS10);//1分频,4MHZ,0.625mS
TIFR|=0X04;// 清除中断标志.
while(m_ADCCont<=31);
TIMSK=0x00;
}
void Colldata(void) //这里的都是二次值.
{
uchar i;
float temp,fsum;
BegColl();
//-------------------------------------------------
//这里的任务是将互感器的二次电压转化为互感器的一次值
//******************************************************
fsum=0;
for(i=0;i<Coll_Num;i++)
{
temp=AD_Result[2][i]-AD_Result[0][i] ;//母线二次电压
dataR[0][i]=temp;
fsum+=temp*temp;
}
AD_FinalResult[1]=sqrt(fsum/Coll_Num)*k1*UK;//板子电压互感器一次电压
fsum=0;
for(i=0;i<Coll_Num;i++)
{
temp=AD_Result[1][i]-AD_Result[0][i] ;//主变电流
dataR[1][i]=temp;
fsum+=temp*temp;
}
AD_FinalResult[0]=sqrt(fsum/Coll_Num)*k1*IK;//板子上电流互感器一次电流
fsum=0;
for(i=0;i<Coll_Num;i++)
{
temp=AD_Result[3][i]-AD_Result[0][i] ;//电容2电流A相
fsum+=temp*temp;
}
AD_FinalResult[8]=sqrt(fsum/Coll_Num)*k1*IK;//
fsum=0;
for(i=0;i<Coll_Num;i++)
{
temp=AD_Result[4][i]-AD_Result[0][i] ;//电容2电流C相
fsum+=temp*temp;
}
AD_FinalResult[9]=sqrt(fsum/Coll_Num)*k1*IK;//
////////////////////////////////////////////////////
fsum=0;
for(i=0;i<Coll_Num;i++)
{
temp=AD_Result[6][i]-AD_Result[0][i] ;//电容1电流A相
fsum+=temp*temp;
}
AD_FinalResult[5]=sqrt(fsum/Coll_Num)*k1*IK;//
fsum=0;
for(i=0;i<Coll_Num;i++)
{
temp=AD_Result[5][i]-AD_Result[0][i] ;//电容1电流C相
fsum+=temp*temp;
}
AD_FinalResult[6]=sqrt(fsum/Coll_Num)*k1*IK;//
//*******************************************************************
////////////////////////////////////////////
////////////////////////////////////////////调试用部分。
/*temp=AD_FinalResult[1]*100;
uart1_tran(temp/256);
uart1_tran(temp);
temp=AD_FinalResult[5]*100;
uart1_tran(temp/256);
uart1_tran(temp);
temp=AD_FinalResult[6]*100;
uart1_tran(temp/256);
uart1_tran(temp);
temp=AD_FinalResult[8]*100;
uart1_tran(temp/256);
uart1_tran(temp);
temp=AD_FinalResult[9]*100;
uart1_tran(temp/256);
uart1_tran(temp);
temp=AD_FinalResult[7]*1000;
uart1_tran(temp/256);
uart1_tran(temp);
*/
}
void FFT( uchar ui)
{
uchar L,b,j,i,p,k;
float pp;
float TTR,TTI,temp;
float dataI[Coll_Num];
for(i=0;i<Coll_Num;i++)
dataI[DataItempNum[i]]=dataR[ui][i];
for(i=0;i<Coll_Num;i++)
{
dataR[ui][i]=dataI[i];
dataI[i]=0;
}
//following code fft
for(L=1;L<=5;L++)
{
b=1;i=L-1; //for(1)
while(i>0)
{ b=b*2;i--;} //b=2^(L-1)
for(j=0;j<=b-1;j++) //for(2)
{
p=1;i=5-L;
while(i>0) //p=pow(2,5-L)*j;
{ p=p*2; i--; }
p=p*j;
for(k=j;k<Coll_Num;k=k+2*b)//for(3)