1. 程式人生 > >NUC140之PLL(頻率合成器LMX2541)

NUC140之PLL(頻率合成器LMX2541)

PLL 是一種集成了鎖相環、鑑相器、振盪器為一體的頻率合成晶片,

以LMX2541為例,它是一款小數分頻的頻率合成器,輸出頻率31.6MHz~4GHz,

具有非常好的相位噪聲和雜散指標。採用SPI控制驅動程式如下:

#include "PLL_Thread.h"
#include "DrvSPI.h"
const int16_t OffSetArray[] = {8000, 7500, 7300, 5000, 4000, 2500, 0, -2500, -4000, -5000, -7300, -7500, -

8000};

//暫存器0
volatile TRegister_R0 Register_R0_TX;	//Register_R0_RX,

//暫存器1
volatile TRegister_R1 Register_R1_TX;	 //Register_R1_RX,

//暫存器2
volatile TRegister_R2 Register_R2_TX; //Register_R2_RX,

//暫存器4
volatile TRegister_R4 Register_R4_TX;	 //Register_R4_RX,

//Register_R2.Map.default0 = Register_R2_default;
//	Register_R2.Map.PLL_DEN = PLL_DEN_RX_VALUE;						
//	LMX2541_Set_Register(Register_R2.Value,LMX2541_REG2);

//暫存器6 extern
 volatile TRegister_R6 Register_R6;


//
////暫存器8
//TRegister_R8 Register_R8;
//
////暫存器9
//TRegister_R9 Register_R9;
//
////暫存器12
//TRegister_R12 Register_R12;
//
////暫存器13
//TRegister_R13 Register_R13;

void IniSPI(void);
//void IniLMX2541_GPIO(void);
void IniLMX2541_Ctrl_GPIO(void);
void IniLMX2541(void);
void LMX2541_Set_Register(uint32_t Register_Value,TLMX2541_REG  Register_Addr);
void LMX2541_Write_Data(uint32_t Value);
uint8_t LMX2541_Modify_FRE(uint32_t Fre_Value,uint8_t OffSet);
uint8_t LMX2541_Modify_RFGain(uint8_t Gain_Value);
void LMX2541_Modify_LoopFilter(TLMX2541_RES  RES_R3,TLMX2541_RES  RES_R4,TLMX2541_CAP3  CAP_C3,TLMX2541_CAP4 CAP_C4,uint8_t vco_div_value);
void PTT_SW(uint8_t ptt);
void EINT1_Callback(void);
void GPIO_GPAB_Callbackfunc(uint32_t portA,uint32_t portB);
//void GPIO_GPCDE_Callbackfunc(uint32_t portC,uint32_t portD,uint32_t portE);


//void LMX2541_Modify_PLL (uint8_t PLL_R,uint32_t PLL_N, uint32_t PLL_NUM, uint32_t PLL_DEN);


void IniSPI(void)
{
	/* Configure SPI0 related multi-function pins */
    DrvGPIO_InitFunction(E_FUNC_SPI0);

    /* Configure SPI0 as a master, 32-bit transaction */
    DrvSPI_Open(eDRVSPI_PORT0,eDRVSPI_MASTER,eDRVSPI_TYPE1,32);
    
	/* Disable the automatic slave select function of SS0. */
	DrvSPI_DisableAutoSS(eDRVSPI_PORT0);

	/* Set the active level of slave select. */
    DrvSPI_SetSlaveSelectActiveLevel(eDRVSPI_PORT0, eDRVSPI_ACTIVE_LOW_FALLING);

	DrvSPI_Set2BitTransferMode(eDRVSPI_PORT0,FALSE);
   
    /* The transfer order of SPI0 is MSB first */ 
	DrvSPI_SetEndian(eDRVSPI_PORT0, eDRVSPI_MSB_FIRST);
	
	/* SPI clock rate 1M */
    DrvSPI_SetClockFreq(eDRVSPI_PORT0,1000000, 0);
}


/*
	函式名稱:IniLMX2541_Ctrl_GPIO
	函式功能:初始化控制LMX2541	相關引腳功能
	入口函式:無
	出口函式:無
	返回值:無
*/
void IniLMX2541_Ctrl_GPIO(void)
{
	
//	DrvGPIO_Open(E_GPA,3, E_IO_OUTPUT);											//SYTEST
//	DrvGPIO_Open(E_GPA,11, E_IO_INPUT);											
//	DrvGPIO_Open(E_GPB,3, E_IO_OUTPUT);											//VCTL
	

	//IniSPI();
//	DrvGPIO_Open(E_GPC,0, E_IO_OUTPUT);	   										//LE
//	DrvGPIO_Open(E_GPC,1, E_IO_OUTPUT);											//CLK
//	DrvGPIO_Open(E_GPC,7, E_IO_INPUT);											//SYSLOCK_STU
//	DrvGPIO_Open(E_GPE,0, E_IO_OUTPUT);											//RFEN
//    DrvGPIO_Open(E_GPE,1, E_IO_OUTPUT);											//DATA_PLL
//	DrvGPIO_Open(E_GPE,2, E_IO_OUTPUT);											//CLK_PLL
//	DrvGPIO_Open(E_GPE,3, E_IO_OUTPUT);											//EN_PLL	
//	
//	SYTEST(SYNCLOCK);															//預設為頻合正常
	
	//DrvGPIO_EnableInt(E_GPB,2, E_IO_BOTH_EDGE,E_MODE_EDGE);						//開啟LOCK中斷
	
	//DrvGPIO_SetIntCallback(GPIO_GPAB_Callbackfunc, GPIO_GPCDE_Callbackfunc);
	//DrvGPIO_EnableEINT1(E_IO_BOTH_EDGE, E_MODE_EDGE,EINT1_Callback);						
}
/******************************************
 ********初始化送數IO口********************
******************************************/

//void IniLMX2541_GPIO(void)
//{
//	DrvGPIO_Open(E_GPE,3, E_IO_OUTPUT);											//EN_PLL
//	DrvGPIO_Open(E_GPE,2, E_IO_OUTPUT);											//CLK_PLL
//	DrvGPIO_Open(E_GPE,1, E_IO_OUTPUT);											//DATA_PLL
//}
/*
	函式名稱:IniLMX2541
	函式功能:初始化LMX2541	相關暫存器
	入口函式:無
	出口函式:無
	返回值:無
*/
void IniLMX2541(void)
{
	//暫存器2
	TRegister_R2 Register_R2;
	
	//暫存器3
	TRegister_R3 Register_R3;
	
	//暫存器4
//	TRegister_R4 Register_R4;
	
	//暫存器5
	TRegister_R5 Register_R5;
		
	//暫存器7
	TRegister_R7 Register_R7;
	
	TRegister_R8 Register_R8;

	TRegister_R9 Register_R9;

	TRegister_R12 Register_R12;

	TRegister_R13 Register_R13;
	
	IniLMX2541_Ctrl_GPIO();										//初始化控制LMX2541的暫存器引腳
	
	RFEN_PLL(0);												//關閉射頻輸出
		
	//初始化暫存器7
	Register_R7.Map.default0 = Register_R7_default;
	LMX2541_Set_Register(Register_R7.Value,LMX2541_REG7);

	//初始化暫存器13
	Register_R13.Map.default0 = Register_R13_default;
	Register_R13.Map.VCO_DIV_OPT = 0;
	LMX2541_Set_Register(Register_R13.Value,LMX2541_REG13);

	//初始化暫存器12
	Register_R12.Map.default0 = Register_R12_default;
	LMX2541_Set_Register(Register_R12.Value,LMX2541_REG12);

	//初始化暫存器9
	Register_R9.Map.default0 = Register_R9_default;
	LMX2541_Set_Register(Register_R9.Value,LMX2541_REG9);

	//初始化暫存器8
	Register_R8.Map.default0 = Register_R8_default;
	Register_R8.Map.AC_TEMP_COMP = 5;
	LMX2541_Set_Register(Register_R8.Value,LMX2541_REG8);

	//初始化暫存器6
	Register_R6.Map.default0 = Register_R6_default;
//	Register_R6.Map.VCOGAIN = 12;
	Register_R6.Map.OUTTERM = 12;
	Register_R6.Map.DIVGAIN = 12;
	Register_R6.Map.RFOUT = 1;									//射頻開關由RFoutEN控制
	Register_R6.Map.Addr = LMX2541_REG6;
	LMX2541_Write_Data(Register_R6.Value);

	//初始化暫存器5
	Register_R5.Map.default0 = Register_R5_default;
	Register_R5.Map.FL_CPG = 0;
	Register_R5.Map.FL_RF_LF = 0;
	Register_R5.Map.FL_R3_LF = 0;
	Register_R5.Map.FL_TOC = 0;									//禁用FastLock
	LMX2541_Set_Register(Register_R5.Value,LMX2541_REG5);

	//初始化暫存器4(環路濾波器)
//	LMX2541_Modify_LoopFilter(LMX2541_RES_1k,LMX2541_RES_200,LMX2541_CAP3_20,LMX2541_CAP4_100,20);

	
	//初始化暫存器3
	Register_R3.Map.FSK = 0;			//禁用FSK
	Register_R3.Map.default0 = Register_R3_default;
	Register_R3.Map.DLOCK = 0;			//鎖定時間為13.5ns
	Register_R3.Map.CPT = 0;			//禁止使用3態
	Register_R3.Map.DITH = 3;			//禁止使用抖動
	Register_R3.Map.ORDER = 3;			//使用小數分頻
	Register_R3.Map.FDM = 1;			//使用小數分頻時,分子和分母都是22bit
	Register_R3.Map.OSC_2X = 0;			//不使用雙倍晶振
	Register_R3.Map.CPP = 0;			//全功能模式下,電荷泵極性為負極
	Register_R3.Map.MUX = 8;
	Register_R3.Map.CPG = 15;			//選擇最大的充電泵電流 1600uA
	Register_R3.Map.XO = 0;
	Register_R3.Map.POWERDOWN = 0;		//不關閉器件
	Register_R3.Map.MODE = 0;			//全功能模式
//	Register_R3.Map.Addr = LMX2541_REG3;
	LMX2541_Set_Register(Register_R3.Value,LMX2541_REG3);

	//初始化暫存器2
	Register_R2.Map.default0 = Register_R2_default;
	Register_R2.Map.PLL_DEN = PLL_DEN_MAX_VALUE;
	LMX2541_Set_Register(Register_R2.Value,LMX2541_REG2);

	//初始化暫存器1
	//Register_R1_RX.Map.Addr = LMX2541_REG1;							//地址
	//Register_R1_RX.Map.default0 = Register_R1_default;

	Register_R1_TX.Map.Addr = LMX2541_REG1;							//地址
	Register_R1_TX.Map.default0 = Register_R1_default;
	
	//初始化暫存器0
	//Register_R0_RX.Map.Addr = LMX2541_REG0;							//地址
	Register_R0_TX.Map.Addr = LMX2541_REG0;							//地址
	
					
}


/*
	函式名稱:LMX2541_Set_Register
	函式功能:設定暫存器的值
	入口函式:
		Register_Value:暫存器的值;
		Register_Addr:暫存器地址,該值可以為:LMX2541_REG0,LMX2541_REG1,LMX2541_REG2,LMX2541_REG3,LMX2541_REG4,
											LMX2541_REG5,LMX2541_REG6,LMX2541_REG7,LMX2541_REG8,LMX2541_REG9,
											LMX2541_REG12,LMX2541_REG13
	出口函式:無
	返回值:無
*/
void LMX2541_Set_Register(uint32_t Register_Value,TLMX2541_REG  Register_Addr)
{
	uint32_t SendData = 0;
	SendData = Register_Value & 0xFFFFFFF0;
	SendData += Register_Addr;
	LMX2541_Write_Data(SendData);
}


/*
	函式名稱:LMX2541_Write_Data
	函式功能:傳送資料到LMX2541
	入口函式:
		Value:要傳送到LMX2541的值;
		
	出口函式:無
	返回值:無
*/
//void LMX2541_Write_Data(uint32_t Value)
//{
//	DrvSPI_ClrSS(eDRVSPI_PORT0, eDRVSPI_SS0);				   //set high
//	DrvSPI_SetSS(eDRVSPI_PORT0, eDRVSPI_SS0);				   //set low
//	DrvSPI_SingleWrite(eDRVSPI_PORT0,&Value);
//	while(DrvSPI_IsBusy(eDRVSPI_PORT0));
//	DrvSPI_ClrSS(eDRVSPI_PORT0, eDRVSPI_SS0);				   //set high
//	DrvSPI_SetSS(eDRVSPI_PORT0, eDRVSPI_SS0);				   //set low
//}
void LMX2541_Write_Data(uint32_t Value)
{
	uint8_t i;
	EN_PLL(1);
	delay(5);												//set high
	EN_PLL(0);												//set low
	delay(5);
	for(i=0;i<32;i++)
	{
		CLK_PLL(0);
		delay(5);
		if((Value&0x80000000)==0x80000000)
		{
			DATA_PLL(1);	
		}
		else
		{
		    DATA_PLL(0);
		}
		delay(5);
		CLK_PLL(1);
		delay(5);
		Value=Value<<1;
	}
	CLK_PLL(0);	
	delay(5);
	EN_PLL(1);												//set high
	delay(5);
	EN_PLL(0);												//set low
	delay(5);
	RFEN_PLL(1);
	delay(5);
}

/*
	函式名稱:LMX2541_Modify_FRE
	函式功能:修改LMX2541的輸出頻率
	入口函式:

		
	出口函式:無
	返回值:
	1:頻率設定成功
	0:頻率設定失敗


*/
uint8_t LMX2541_Modify_FRE(const uint32_t Fre_Value,const uint8_t OffSet_subscript)
{
	uint8_t ModValue = 0,VCO_DIV_TX = 0;	  //VCO_DIV_RX = 0,
	uint16_t PLL_CNT_N = 0;
	uint32_t VCO_OUT = 0,PLL_CNT_NUM = 0;   	
	float point = 0.0;

	uint32_t RealFre_Tx = 0;	   //RealFre_Rx = 0,
	
	if((Fre_Value>MAXFRE)||(Fre_Value<MINFRE)||(Fre_Value%5 != 0)||(OffSet_subscript>12))
	{
		return 0;				//頻率值或頻偏值超出範圍
	}
	
	ModValue = Fre_Value%100;

	if((ModValue == 20)||(ModValue == 45)||(ModValue == 70)||(ModValue == 95))
	{
		return 0;
	}
	
	if(Fre_Value%25 == 0)	
	{
		RealFre_Tx = Fre_Value * 1000;
	}
	else				
	{

		RealFre_Tx *= 1000;

		
		if( OffSet_subscript>12 )
		{
			return 0;
		}

	}
   	

	//RealFre_Rx = RealFre_Tx + 35250000;
	RealFre_Tx += 35250000;
	RealFre_Tx += OffSetArray[OffSet_subscript];


	//計算接收頻率值
	//VCO_DIV_RX = VCO_MAX_VALUE/RealFre_Rx;					//計算VCO_DIV

	//VCO_OUT = RealFre_Rx * VCO_DIV_RX;						//計算VCO的值

	PLL_CNT_N = VCO_OUT / FPD_VALUE;						//計算N值

	point = ((float)(VCO_OUT % FPD_VALUE)/FPD_VALUE);		//計算小數值

	PLL_CNT_NUM = point * PLL_DEN_MAX_VALUE;				//計算分子值


//	Register_R0_RX.Map.PLL_NUM_L = PLL_CNT_NUM & 0x0000ffff;			//PLL_NUM低16位
//	Register_R0_RX.Map.PLL_N_L = PLL_CNT_N & 0x00000fff;				//PLL_N低12位
			

	//Register_R1_RX.Map.default0 = Register_R1_default;
//	Register_R1_RX.Map.PLL_NUM_H = ((PLL_CNT_NUM >> 16)&0x0000003f);	//PLL_NUM高6位
//	Register_R1_RX.Map.PLL_N_H = ((PLL_CNT_N >> 12)&0x0000003f);		//PLL_N高6位
//	Register_R1_RX.Map.PLL_R = PLL_R_VALUE;								//PLL_R
	//Register_R1_RX.Map.Addr = LMX2541_REG1;							//地址

	//Register_R4_RX.Map.VCO_DIV = VCO_DIV_RX;

	//計算髮射機頻率
	VCO_DIV_TX = VCO_MAX_VALUE/RealFre_Tx;					//計算VCO_DIV

	VCO_OUT = RealFre_Tx * VCO_DIV_TX;						//計算VCO的值

	PLL_CNT_N = VCO_OUT / FPD_VALUE;						//計算N值

	point = ((float)(VCO_OUT % FPD_VALUE)/FPD_VALUE);		//計算小數值


	PLL_CNT_NUM = point * PLL_DEN_MAX_VALUE;				//計算分子值


	Register_R0_TX.Map.PLL_NUM_L = PLL_CNT_NUM & 0x0000ffff;			//PLL_NUM低16位
	Register_R0_TX.Map.PLL_N_L = PLL_CNT_N & 0x00000fff;				//PLL_N低12位
				

	//Register_R1_TX.Map.default0 = Register_R1_default;
	Register_R1_TX.Map.PLL_NUM_H = ((PLL_CNT_NUM >> 16)&0x0000003f);	//PLL_NUM高6位
	Register_R1_TX.Map.PLL_N_H = ((PLL_CNT_N >> 12)&0x0000003f);		//PLL_N高6位
	Register_R1_TX.Map.PLL_R = PLL_R_VALUE;								//PLL_R
	//Register_R1_TX.Map.Addr = LMX2541_REG1;							//地址

	Register_R4_TX.Map.VCO_DIV = VCO_DIV_TX;

	Register_R6.Map.RFOUT = 0;		
	LMX2541_Write_Data(Register_R6.Value);							//關閉射頻輸出
//	RFEN_PLL(0);													//關閉射頻輸出

	LMX2541_Modify_LoopFilter(LMX2541_RES_1k,LMX2541_RES_200,LMX2541_CAP3_20,LMX2541_CAP4_100,VCO_DIV_TX);
//	LMX2541_Write_Data(Register_R1_TX.Value);					//修改R1的值
//	LMX2541_Write_Data(Register_R0_TX.Value);					//修改R0的值

//	LMX2541_Modify_LoopFilter(LMX2541_RES_1k,LMX2541_RES_200,LMX2541_CAP3_20,LMX2541_CAP4_100,VCO_DIV_RX);
	LMX2541_Write_Data(Register_R4_TX.Value);					//修改R4的值
	LMX2541_Write_Data(Register_R1_TX.Value);					//修改R1的值
	LMX2541_Write_Data(Register_R0_TX.Value);					//修改R0的值
//	RFEN_PLL(1);												//開啟射頻輸出

	Register_R6.Map.RFOUT = 2;
	LMX2541_Write_Data(Register_R6.Value);							//開啟射頻輸出	

	//VCTL(FOUT_RX);
	//RFEN_PLL(FOUT_TX);
	return 1;
}





/*
	函式名稱:LMX2541_Modify_RFGain
	函式功能:修改LMX2541的輸出增益
	入口函式:
		Gain_Value:輸出的增益值,取值範圍為0~15
		
	出口函式:無
	返回值:
	1:修改成功
	0:修改失敗
*/
uint8_t LMX2541_Modify_RFGain(uint8_t Gain_Value)
{	
	if(Gain_Value<16)
	{
//		Register_R6.Map.RFOUT = 0;									//關閉射頻輸出
//		LMX2541_Write_Data(Register_R6.Value);
	
		Register_R6.Map.OUTTERM = Gain_Value;
//		Register_R6.Map.DIVGAIN = 12;
//		Register_R6.Map.RFOUT = 1;									//開啟射頻輸出
		LMX2541_Write_Data(Register_R6.Value);

		return 1;
	}
	else
	{
		return 0;
	}	
}



/*
	函式名稱:LMX2541_Modify_LoopFilter
	函式功能:修改LMX2541內部環路濾波器的引數
	入口函式:
		RES_R3:電阻R3的阻值,該值可以為 LMX2541_RES_200,LMX2541_RES_1k,LMX2541_RES_2k,LMX2541_RES_4k,LMX2541_RES_16k,分別對應的阻值為
				200,1k,2k,4k,16k

		RES_R4:電阻R4的阻值,該值可以為 LMX2541_RES_200,LMX2541_RES_1k,LMX2541_RES_2k,LMX2541_RES_4k,LMX2541_RES_16k,分別對應的阻值為
				200,1k,2k,4k,16k

		CAP_C3:電阻C3的電容值,該值可以為 LMX2541_CAP3_0,LMX2541_CAP3_1,LMX2541_CAP3_5,LMX2541_CAP3_6,LMX2541_CAP3_10,LMX2541_CAP3_11,
							LMX2541_CAP3_15,LMX2541_CAP3_16,LMX2541_CAP3_20,LMX2541_CAP3_21,LMX2541_CAP3_25,LMX2541_CAP3_26,
							LMX2541_CAP3_30,LMX2541_CAP3_31,LMX2541_CAP3_35,LMX2541_CAP3_36

				對應的電容值為(pF): 0,1,5,6,10,11,
							15,16,20,21,25,26,
							30,31,35,36

		CAP_C4:電阻C4的電容值,該值可以為 LMX2541_CAP4_0=0,LMX2541_CAP4_5,LMX2541_CAP4_20,LMX2541_CAP4_25,LMX2541_CAP4_40,LMX2541_CAP4_45,
							LMX2541_CAP4_60,LMX2541_CAP4_65,LMX2541_CAP4_100,LMX2541_CAP4_105,LMX2541_CAP4_120,LMX2541_CAP4_125,
							LMX2541_CAP4_140,LMX2541_CAP4_145,LMX2541_CAP4_160,LMX2541_CAP4_165

				對應的電容值為(pF): 0,5,20,25,40,45,
							60,65,100,105,120,125,
							140,145,160,165
		
		vco_div_value:VCO的分頻值 0~63

	出口函式:無
	返回值:無
*/
void LMX2541_Modify_LoopFilter(TLMX2541_RES  RES_R3,TLMX2541_RES  RES_R4,TLMX2541_CAP3  CAP_C3,TLMX2541_CAP4 CAP_C4,uint8_t vco_div_value)
{	
	//接收暫存器4
//	Register_R4_RX.Map.C4_LF = CAP_C4;
//	Register_R4_RX.Map.C3_LF = CAP_C3;
//	Register_R4_RX.Map.R4_LF = RES_R4;
//	Register_R4_RX.Map.R3_LF = RES_R3;
//	Register_R4_RX.Map.VCO_DIV = vco_div_value;
//	Register_R4_RX.Map.OSC_FREQ = OSC_FREQ_VALUE;
//	Register_R4_RX.Map.Addr = LMX2541_REG4;
	

	//發射暫存器4
	Register_R4_TX.Map.C4_LF = CAP_C4;
	Register_R4_TX.Map.C3_LF = CAP_C3;
	Register_R4_TX.Map.R4_LF = RES_R4;
	Register_R4_TX.Map.R3_LF = RES_R3;
	Register_R4_TX.Map.VCO_DIV = vco_div_value;
	Register_R4_TX.Map.OSC_FREQ = OSC_FREQ_VALUE;
	Register_R4_TX.Map.Addr = LMX2541_REG4;
//	LMX2541_Write_Data(Register_R4.Value);							//修改環路濾波器的值
				
}

/*
	函式名稱:PTT_SW
	函式功能:收發頻率切換
	入口函式:ptt,收發狀態 1:收狀態 ; 0:髮狀態		
	出口函式:無
	返回值:無
*/
void PTT_SW(uint8_t ptt)
{
//	Register_R6.Map.RFOUT = 0;		
//	LMX2541_Write_Data(Register_R6.Value);							//關閉射頻輸出
	if(ptt)
	{
		RFEN_PLL(1);													//關閉射頻輸出 PTT反相
	}
	else
	{
		RFEN_PLL(0);													//開啟射頻輸出
	}
	
	/*
	if((ptt)||(VOP_ERR))							//收狀態
	{		
		LMX2541_Write_Data(Register_R4_RX.Value);					//修改R4的值
		LMX2541_Write_Data(Register_R1_RX.Value);					//修改R1的值
		LMX2541_Write_Data(Register_R0_RX.Value);					//修改R0的值
		//VCTL(FOUT_RX);
		//RFEN_PLL(FOUT_RX);		
	}
	else		//髮狀態
	{
		LMX2541_Write_Data(Register_R4_TX.Value);					//修改R4的值
		LMX2541_Write_Data(Register_R1_TX.Value);					//修改R1的值
		LMX2541_Write_Data(Register_R0_TX.Value);					//修改R0的值
		//VCTL(FOUT_TX);
		//RFEN_PLL(FOUT_TX);	
	}
	*/
//	Register_R6.Map.RFOUT = 1;
//	LMX2541_Write_Data(Register_R6.Value);							//開啟射頻輸出
}

/*
	函式名稱:EINT1_Callback
	函式功能:PTT中斷回撥函式
	入口函式:param:無意義		
	出口函式:無
	返回值:無
*/
void EINT1_Callback(void)
{
//	PTT_SW(PTT_STU);
}

/*
	函式名稱: GPIO_GPAB_Callbackfunc
	函式功能:鎖定指示中斷
	入口函式:
		portA:埠A的中斷引腳指示
		portB:埠B的中斷引腳指示		
	出口函式:無
	返回值:無
*/
void GPIO_GPAB_Callbackfunc(uint32_t portA,uint32_t portB)
{
//	if(portB & (1<<2))
//	{
//		if(LOCK_STU)
//		{
//			SYTEST(SYNCLOCK);							//頻合鎖定
//			Register_R6.Map.RFOUT = 1;		
//			LMX2541_Write_Data(Register_R6.Value);							//開啟射頻輸出
//		}
//		else
//		{
//			SYTEST(SYNCUNLOCK);							//頻合失鎖
//			Register_R6.Map.RFOUT = 0;		
//			LMX2541_Write_Data(Register_R6.Value);							//關閉射頻輸出
//		}	
//	}
}


void LMX2541_Set_Register_R0(uint32_t Register_Value)
{
	DrvSPI_ClrSS(eDRVSPI_PORT0, eDRVSPI_SS0);				   //set high
	DrvSPI_SetSS(eDRVSPI_PORT0, eDRVSPI_SS0);				   //set low
	
	DrvSPI_SingleWrite(eDRVSPI_PORT0,&Register_Value);
	while(DrvSPI_IsBusy(eDRVSPI_PORT0));
	DrvSPI_ClrSS(eDRVSPI_PORT0, eDRVSPI_SS0);
	DrvSPI_SetSS(eDRVSPI_PORT0, eDRVSPI_SS0);	
}
/*
	函式名稱: GPIO_GPCDE_Callbackfunc
	函式功能:無
	入口函式:
		portC:埠C的中斷引腳指示
		portD:埠D的中斷引腳指示
		portE:埠E的中斷引腳指示		
	出口函式:無
	返回值:無
*/
//void GPIO_GPCDE_Callbackfunc(uint32_t portC,uint32_t portD,uint32_t portE)
//{
//	
//}

void PLL_control_entry(void *parameter)
{
    /* 排程器上鎖,上鎖後,將不再切換到其他執行緒,僅響應中斷 */
  rt_enter_critical();	
	DrvGPIO_DisableEINT0();
	DrvGPIO_DisableEINT0();

	DrvGPIO_Open(E_GPC, 0, E_IO_OUTPUT); 
 	DrvGPIO_Open(E_GPC, 1, E_IO_OUTPUT);
	DrvGPIO_Open(E_GPC, 3, E_IO_OUTPUT);
	DrvGPIO_Open(E_GPD, 15, E_IO_INPUT);


	IniLMX2541();
	LMX2541_Modify_FRE(118000,6);


    /* 排程器解鎖 */
    rt_exit_critical();
	DrvGPIO_EnableEINT0(E_IO_BOTH_EDGE, E_MODE_EDGE, 

						PTT_Mic_Callback); 
	DrvGPIO_EnableEINT1(E_IO_BOTH_EDGE, E_MODE_EDGE, 

						PTT_Rem_Callback);

	while(1)
	{
		rt_thread_delay(200);
	}
}


 

標頭檔案如下

#ifndef PLL_THREAD_H
#define PLL_THREAD_H
#include "stdint.h"
#include "DrvGPIO.h"
#include "Globe_Value.h"
#include "GPIO_Irq_Thread.h"
#include <rtthread.h>

typedef enum {LMX2541_REG0=0,LMX2541_REG1,LMX2541_REG2,LMX2541_REG3,LMX2541_REG4,
						LMX2541_REG5,LMX2541_REG6,LMX2541_REG7,LMX2541_REG8,LMX2541_REG9,
						LMX2541_REG12=12,LMX2541_REG13=13}TLMX2541_REG;

typedef enum {LMX2541_RES_200=0,LMX2541_RES_1k,LMX2541_RES_2k,LMX2541_RES_4k,LMX2541_RES_16k}TLMX2541_RES;

typedef enum {LMX2541_CAP3_0=0,LMX2541_CAP3_1,LMX2541_CAP3_5,LMX2541_CAP3_6,LMX2541_CAP3_10,LMX2541_CAP3_11,
							LMX2541_CAP3_15,LMX2541_CAP3_16,LMX2541_CAP3_20,LMX2541_CAP3_21,LMX2541_CAP3_25,LMX2541_CAP3_26,
							LMX2541_CAP3_30,LMX2541_CAP3_31,LMX2541_CAP3_35,LMX2541_CAP3_36}TLMX2541_CAP3;

typedef enum {LMX2541_CAP4_0=0,LMX2541_CAP4_5,LMX2541_CAP4_20,LMX2541_CAP4_25,LMX2541_CAP4_40,LMX2541_CAP4_45,
							LMX2541_CAP4_60,LMX2541_CAP4_65,LMX2541_CAP4_100,LMX2541_CAP4_105,LMX2541_CAP4_120,LMX2541_CAP4_125,
							LMX2541_CAP4_140,LMX2541_CAP4_145,LMX2541_CAP4_160,LMX2541_CAP4_165}TLMX2541_CAP4;


//暫存器0
typedef union {
	struct{
		uint32_t Addr:4;		//地址
		uint32_t PLL_N_L:12;	//PLL_N低12位
		uint32_t PLL_NUM_L:16;	//PLL_NUM低16位		
	}Map;
	uint32_t Value;
}TRegister_R0;

//暫存器1
typedef union {
	struct {
		uint32_t Addr:4;		//地址
		uint32_t PLL_R:12;			//PLL_R
		uint32_t PLL_N_H:6;			//PLL_N高6位
		uint32_t PLL_NUM_H:6;		//PLL_NUM高6位
		uint32_t default0:4;		
	}Map;
	uint32_t Value;
}TRegister_R1;

//暫存器2
typedef union {
	struct {
		uint32_t Addr:4;		//地址
		uint32_t PLL_DEN:22;	//PLL_DEN高6位
		uint32_t default0:6;		
	}Map;
	uint32_t Value;
}TRegister_R2;

//暫存器3
typedef union {
	struct{
		uint32_t Addr:4;		//地址
		uint32_t MODE:2;
		uint32_t POWERDOWN:1;
		uint32_t XO:1;
		uint32_t CPG:5;
		uint32_t MUX:4;
		uint32_t CPP:1;
		uint32_t OSC_2X:1;
		uint32_t FDM:1;
		uint32_t ORDER:3;
		uint32_t DITH:2;
		uint32_t CPT:1;
		uint32_t DLOCK:3;
		uint32_t default0:2;
		uint32_t FSK:1;											
	}Map;
	uint32_t Value;
}TRegister_R3;

//暫存器4
typedef union {
	struct{
		uint32_t Addr:4;		//地址
		uint32_t OSC_FREQ:8;
		uint32_t VCO_DIV:6;
		uint32_t R3_LF:3;
		uint32_t R4_LF:3;
		uint32_t C3_LF:4;
		uint32_t C4_LF:4;		
	}Map;
	uint32_t Value;
}TRegister_R4;


//暫存器5
typedef union {
	struct{
		uint32_t Addr:4;		//地址
		uint32_t FL_TOC:14;
		uint32_t FL_R3_LF:3;
		uint32_t FL_RF_LF:3;
		uint32_t FL_CPG:5;
		uint32_t default0:3;
		
	}Map;
	uint32_t Value;
}TRegister_R5;

//暫存器6
typedef union {
	struct{
		uint32_t Addr:4;		//地址
		uint32_t RFOUT:2;
		uint32_t DIVGAIN:4;
		uint32_t OUTTERM:4;
		uint32_t VCOGAIN:4;
		uint32_t default0:14;						
	}Map;
	uint32_t Value;
}TRegister_R6;

//暫存器7
typedef union {
	struct{
		uint32_t Addr:4;		//地址
		uint32_t default0:28;		
	}Map;
	uint32_t Value;
}TRegister_R7;

//暫存器8
typedef union {
	struct{
		uint32_t Addr:4;		//地址
		uint32_t AC_TEMP_COMP:5;
		uint32_t default0:23;		
	}Map;
	uint32_t Value;
}TRegister_R8;

//暫存器9
typedef union {
	struct{
		uint32_t Addr:4;		//地址
		uint32_t default0:28;		
	}Map;
	uint32_t Value;
}TRegister_R9;

//暫存器12
typedef union {
	struct{
		uint32_t Addr:4;		//地址
		uint32_t default0:28;		
	}Map;
	uint32_t Value;
}TRegister_R12;

//暫存器13
typedef union {
	struct{
		uint32_t Addr:4;		//地址
		uint32_t VCO_DIV_OPT:3;
		uint32_t default0:25;		
	}Map;
	uint32_t Value;
}TRegister_R13;

#define Register_R1_default		0
#define Register_R2_default		1
#define	Register_R3_default		0
#define	Register_R5_default		5
#define	Register_R6_default		7
#define	Register_R7_default		1
#define	Register_R8_default		0x88E7
#define	Register_R9_default		0x2800140
#define	Register_R12_default	1
#define	Register_R13_default	1

#define VCO_MAX_VALUE			4000000000							//VCO的最大值
#define FPD_VALUE				1600000//400000 							//鑑相頻率值

#define PLL_DEN_MAX_VALUE		4194303					 	        //分母的最大值
//#define PLL_DEN_CNT_VALUE		73100								//用於計算的 N值的分母
//#define PLL_DEN_RX_VALUE		365500								分母  5*73100
//#define PLL_DEN_TX_VALUE		146200								分母  2*73100
#define PLL_R_VALUE				8//32
//#define VCO_DIV_RX_VALUE		20									
//#define VCO_DIV_TX_VALUE		25								
#define OSC_FREQ_VALUE			13									//參考源的頻率值(MHz)
#define FOUT_RX					1									
#define FOUT_TX					0									
#define SYNCLOCK				0									//頻合鎖定
#define SYNCUNLOCK				1									//頻合失鎖

#define MAXFRE	143975			//允許的最大頻率值(kHz)
#define MINFRE	118000			//允許的最小頻率值(kHz)

#define PLL_DIS        DrvGPIO_SetBit(E_GPD, 7)
#define PLL_EN         DrvGPIO_ClrBit(E_GPD, 7)
#define PLL_CLK_HIGH   DrvGPIO_SetBit(E_GPD, 14)
#define PLL_CLK_LOW    DrvGPIO_ClrBit(E_GPD, 14)
#define PLL_DATA_HIGH  DrvGPIO_SetBit(E_GPD, 15)
#define PLL_DATA_LOW   DrvGPIO_ClrBit(E_GPD, 15)
#define PLL_RF_EN      DrvGPIO_SetBit(E_GPC, 5)
#define PLL_RF_DIS     DrvGPIO_ClrBit(E_GPC, 5)

#define RFEN_PLL(x)	((x)?(GPIOC->DOUT |= 1<<5):(GPIOC->DOUT &= ~(1<<5)))
#define DATA_PLL(x)	((x)?(GPIOC->DOUT |= 1<<3):(GPIOC->DOUT &= ~(1<<3)))
#define CLK_PLL(x)	((x)?(GPIOC->DOUT |= 1<<1):(GPIOC->DOUT &= ~(1<<1)))
#define EN_PLL(x)	((x)?(GPIOC->DOUT |= 1<<0):(GPIOC->DOUT &= ~(1<<0)))

extern void PLL_control_entry(void *parameter);

#endif