/*****************************
code name: MAX262
code function: the code is used to control the MAX262 with msp430.
coder: Mark Ma
last updated : 09-8-6
*******************************/
/************pin definition********
P2.7~P2.4 A3~A0
P2.3~P2.2 D1~D0
P2.1 WR
**********************************/
#include "msp430x44x.h"
long FCLK=1000000; //MAX262输入频率
unsigned char Mod=0; //工作模式0,1,2,3
#define PI 3.1415926
void delay_262()
{
for(int i=20;i>0;i--);
}
void set_8Mfreq()
{
SCFI0 |= FN_4; // x2 DCO frequency, 8MHz nominal DCO
SCFQCTL = 121; // (121+1) x 32768 x 2 = 7.99 Mhz
FLL_CTL0 = DCOPLUS + XCAP18PF; // DCO+ set so freq = xtal x D x N+1
P1DIR = 0x22; // P1.1 & P1.5 to output direction
P1SEL = 0x22; // P1.1 & P1.5 to output MCLK & ACLK
}
//端口操作
//***************************************************************
//写两比特的数据
void write(unsigned char add,unsigned char dat2bit)
{
unsigned char temp,temp2;
temp=add<<4;
P2OUT=((P2OUT&0x0f)|temp);
temp=dat2bit<<2;
temp2=temp&0x0c;
P2OUT=((P2OUT&0xf3)|temp2);
P2OUT&=~BIT1;
delay_262();
P2OUT|=BIT1;
delay_262();
}
//设置A通道工作模式
void Set_AM(unsigned char mod)
{
write(0,mod);
}
//设置A通道F 值
void Set_AF(unsigned char datF)
{
write(1,datF);
datF=datF>>2;
write(2,datF);
datF=datF>>2;
write(3,datF);
}
//设置A通道Q值
void Set_AQ(unsigned char 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(unsigned char mod)
{
write(8,mod);
}
//设置B通道F 值
void Set_BF(unsigned char datF)
{
write(9,datF);
datF=datF>>2;
write(10,datF);
datF=datF>>2;
write(11,datF);
}
//设置B通道Q值
void Set_BQ(unsigned char datQ)
{
write(12,datQ);
datQ=datQ>>2;
write(13,datQ);
datQ=datQ>>2;
write(14,datQ);
datQ=(datQ>>2)&1;
write(15,datQ);
}
//***************************************************************
//基本计算
//***************************************************************
//根据中心频率求设定值FN
float CopFn(unsigned char mod, float f0)
{
if (1==mod)
{
return ((float)((FCLK*2.8284271247/f0)/PI)-26);
}
else
{
return ((float)((FCLK*2/f0)/PI)-26);
}
}
//根据Q求设定值QN
unsigned char CopQn(unsigned char mod, float q)
{
if (1==mod)
{
return (128-(float)(90.51/q));
}
else
{
return (128-(float)(64/q));
}
}
//*************************************************************
//功能接口函数
//*************************************************************
//设定频率
Set_F0(unsigned char mod,float f0 )
{
if(f0>28612)
{
//Fclk 4M
SCFI0 |= FN_4;
SCFQCTL = SCFQ_4M;
FLL_CTL0 = XCAP18PF; // DCO+ set so freq = xtal x D x N+1
FCLK=4000000;
}
else if(f0>14306)
{
//Fclk 2M
SCFI0 |= FN_2;
SCFQCTL = SCFQ_2M;
FLL_CTL0 = XCAP18PF; // DCO+ set so freq = xtal x D x N+1
FCLK=2000000;
}
else if(f0>7153)
{
//Fclk 1M
SCFQCTL = SCFQ_1M;
FLL_CTL0 = XCAP18PF; // DCO+ set so freq = xtal x D x N+1
FCLK=1000000;
}
else if(f0>3683)
{
//Fclk 512K
SCFQCTL = SCFQ_512K;
//FLL_CTL0 = XCAP18PF; // DCO+ set so freq = xtal x D x N+1
FCLK=512000;
}
else if(f0>1831)
{
//Fclk 256K
FCLK=256000;
set_8Mfreq();
TACTL = TASSEL1 + TACLR; // ACLK, clear TAR
CCTL0 = CCIE; // CCR0 interrupt enabled
CCR0 = 15;
P5DIR |= 0x02; // Set P5.1 to output direction
TACTL |= MC0; // Start Timer_a in upmode
_EINT();
}
else
{
//Fclk 128K
FCLK=128000;
set_8Mfreq();
TACTL = TASSEL1 + TACLR; // ACLK, clear TAR
CCTL0 = CCIE; // CCR0 interrupt enabled
CCR0 = 30;
P5DIR |= 0x02; // Set P5.1 to output direction
TACTL |= MC0; // Start Timer_a in upmode
_EINT(); // Enable interrupts
}
Set_AF(CopFn(mod,f0));
Set_BF(CopFn(mod,f0));
}
//设置滤波器模式 , 0为底通、1为高通、2为带通
Set_PFmod(unsigned char mod)
{
if(mod<3)
{
switch(mod)
{
case 0:
Mod=0;
break;
case 1:
Mod=2;
break;
case 2:
Mod=0;
break;
}
Set_AM(Mod); Set_BM(Mod);
}
}
//*************************************************************
void main()
{
P2DIR = 0xff;
P2OUT = 0;
P1SEL = BIT1+BIT4;
P1DIR = BIT1+BIT4;
WDTCTL = WDTPW + WDTHOLD;
Set_AM(Mod);
Set_AQ(CopQn(Mod,0.5));
Set_BM(Mod);
Set_BQ(CopQn(Mod,2.0));
Set_PFmod(2); //set function to bandpass
Set_F0(Mod,1009);
for(;;);
}
#pragma vector=TIMERA0_VECTOR
__interrupt void Timer_A (void)
{
P5OUT ^= 0x02; // Toggle P5.1 using exclusive-OR
}
|