本帖最后由 雪山飞狐D 于 2009-8-31 17:08 编辑
#include <REG52.H>
#include <MATH.H>
#include <INTRINS.H>
typedef unsigned char uchar ;
typedef unsigned int uint;
#define P_262 P1 //口线连线(从高到低):A3,A2,A1,A0,D1,D0,Wr,_
sbit P_wr = P_262^1;
sbit Fclk_S2=P2^2; //CPLD选择Fclk控制线
sbit Fclk_S1=P2^1;
sbit Fclk_S0=P2^0;
sbit PFS0=P2^3;//工作模式工作模式选择0
sbit PFS1=P2^4;//工作模式工作模式选择1
long FLCK = 1000000 ; //MAX262工作频率
uchar Mod=2;//工作模式0,1,2,3
#define PI 3.1415926
#define delay_262() _nop_();_nop_();_nop_()
void delay(unsigned int d)
{unsigned char j;
while(d!=0)
{
d--;
j=70;
do {j--;} while(j!=0);
}
}
//***************************************************************
//写两比特的数据
void write(uchar add,uchar dat2bit)
{
P_262=(P_262&0x0f)|(add<<4);
P_262=(P_262&0xf3)|((dat2bit<<2)&0x0c);
P_wr=0;
delay_262();
P_wr=1;
delay_262();
}
//**************************************************************
//设置A通道工作模式
void Set_AM(uchar mod)
{
write(0,mod);
}
//设置A通道F值
void Set_AF(uchar datF)
{
write(1,datF);
datF=datF>>2;
write(2,datF);
datF=datF>>2;
write(3,datF);
}
//设置A通道Q值
void Set_AQ(uchar datQ)
{
write(4,datQ);
datQ=datQ>>2;
write(5,datQ);
datQ=datQ>>2;
write(6,datQ);
datQ=(datQ>>2)&1;
write(7,datQ);
}
//设置B通道工作模式
void Set_BM(uchar mod)
{
write(8,mod);
}
//设置A通道F值
void Set_BF(uchar datF)
{
write(9,datF);
datF=datF>>2;
write(10,datF);
datF=datF>>2;
write(11,datF);
}
//设置A通道Q值
void Set_BQ(uchar datQ)
{
write(12,datQ);
datQ=datQ>>2;
write(13,datQ);
datQ=datQ>>2;
write(14,datQ);
datQ=(datQ>>2)&1;
write(15,datQ);
}
//***************************************************************
//基本计算
//***************************************************************
//根据截止频率fc计算中心频率f0
//
/*
uchar CopF0(float q, float fc)
{
q = pow(q, 2);
q = (float)q*2;
q = (float)(q-1)/q;//1-(1/q)
fc = (float)fc * sqrt(sqrt(1+pow(q,2))-q);
}
float CopF0_tx(float q, float fc)
{
q = pow(q, 2);
q = (float)q*2;
q = (float)(q-1)/q;//1-(1/q)
fc = fc * sqrt(sqrt(1+pow(q,2))+q);
}
*/
//根据中心频率求设定值FN
float CopFn(uchar mod, float f0)
{
if (1==mod)
{
return ((float)((FLCK*2.8284271247/f0)/PI)-26);
}
else
{
return ((float)((FLCK*2/f0)/PI)-26);
}
}
//根据Q求设定值QN
uchar CopQn(uchar mod, float q)
{
if (1==mod)
{
return (128-(float)(90.51/q));
}
else
{
return (128-(float)(64/q));
}
}
//***************************************
//功能接口函数
//设定频率
Set_F0(uchar mod,float f0 )
{
if(f0>28612)
{
Fclk_S2=0;Fclk_S1=0;Fclk_S0=0;//Fclk 4M
FLCK=4000000;
}
else if(f0>14306)
{
Fclk_S2=0;Fclk_S1=0;Fclk_S0=1;//Fclk 2M
FLCK=2000000;
}
else if(f0>7153)
{
Fclk_S2=0;Fclk_S1=1;Fclk_S0=0; //Fclk 1M
FLCK=1000000;
}
else if(f0>3683)
{
Fclk_S2=0;Fclk_S1=1;Fclk_S0=1;//Fclk 512K
FLCK=512000;
}
else if(f0>1831)
{
Fclk_S2=1;Fclk_S1=0;Fclk_S0=0;//Fclk 256
FLCK=256000;
}
else
{
Fclk_S2=1;Fclk_S1=0;Fclk_S0=1;//Fclk 128
FLCK=128000;
}
Set_AF(CopFn(mod,f0));
Set_BF(CopFn(mod,f0));
}
//设置滤波器模式 , 0为底通、1为高通、2为带通
Set_PFmod(uchar mod)
{
if(mod<3)
{
switch(mod)
{ case 0:
PFS1=0;FS0=0;
Mod=0;
break;
case 1:
PFS1=0;FS0=1;
Mod=2;
break;
case 2:
PFS1=1;FS0=0;
Mod=0;
break;
}
Set_AM(Mod); Set_BM(Mod);
}
}
//uint Tx;
float Tx;
void main(void)
{
/*
Tx=CopFn(Mod,120048);
Tx=CopQn(Mod,1);
Tx=CopF0(0.7,60000);
Tx=CopF0_tx(0.7,Tx); */
delay(10000);delay(10000);delay(10000);
Set_AM(Mod);
Set_AQ(CopQn(Mod,1));
Set_BM(Mod);
Set_BQ(CopQn(Mod,1));
//Set_AF(CopFn(Mod,5000));
//Set_BF(CopFn(Mod,5000));
Set_PFmod(2);
Set_F0(Mod,8000);
for(;;);
} |