⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 srf.c

📁 制作Zigbee模块的详细电路原理图和C代码
💻 C
📖 第 1 页 / 共 2 页
字号:
/*******************************************************************************

       UBEC (Uniband Electronic Corp.)

       Project: U-NET01, Ubiquitous network platform

       File: srf.c

       Version: 0.1.1

       Usage: Simple Rf Control Function

       Platform: U-NET01 DK with IAR 8051 C compiler

       Reference:

               Silicon Laboratories: C8051F124

               UBEC: UZ2400

       Note :

               Copyright (C) 2007 Uniband Electronic Corporation, All rights reserved

********************************************************************************/

#include ".\zigbee\unet.h"

//extern UINT8 SecKeyPool[16];
extern UNET_SYS UnetSys;

/**************************************************************
	Function Name: UzInit()
	Description: Initial UZ2400
	Parameters: None
	Return Value: None
**************************************************************/

void UzInit(){
	//Soft Reset
    	spi_sw(SOFTRST, 0x03); //If use 0x07 then should wait circuit stability

	//Set CCA mode to ED
	spi_sw(BBREG2, 0x80);

	//ED threshold for CCA
	spi_sw(RSSITHCCA, 0x60);

	//20Mhz clock recovery time
	spi_sw(SLPACK, 0x5f);

	//Apend RSSI value in Rx packets, Tx filter control, 20MHz clock recovery time control
    	spi_sw(BBREG6, 0xd0);

	//RF optimized control
	spi_lw(RFCTRL2, 0x80); //RF-PLL Control

	spi_lw(RFCTRL7, 0x80); //Sleep clock selection, [6:7] = 10 : use internal ring oscillator
	
	spi_lw(RFCTRL8, 0x10); // RF VCO control

	//Sleep clock frequency control
	spi_lw(SCLKDIV, 0x01);

	//Enable the Interrupt
    	spi_sw(INTMSK, 0);

	//Errata Issue
	spi_sw(FFOEN, 0x98);

	spi_sw(TXPEMISP, 0x95);

	//Reset RF state machine
	spi_sw(RFCTL, 0x04);
	spi_sw(RFCTL, 0x00);
	
}

/**************************************************************
	Function Name: UzEnablePA()
	Description: Enable Power Amplifier (UP2202)
	Parameters: None
	Return Value: None
**************************************************************/

void UzEnablePA(){

	spi_lw(TESTMODE,  spi_lr(TESTMODE) |0x0f);
}

/**************************************************************
	Function Name:  UzSetTxPower()
	Description: Set Power Level
	Parameters: dB -> Power Level
	Return Value: None
	Note: If dB > 0 ,will reduce power level
**************************************************************/

void UzSetTxPower(INT8 dB)
{ // Value Range: 0 ~ -40
	UINT8 Value = 0x00;
	INT8  Operator = 0x00;

	if(dB > 40 || dB < 0) return;

	if(dB == 0)
	{ //Maximum Tx Power
		Value = 0;
	}
	else
	{
		if((Operator = dB - 40) >=0)
		{ //dB = 40
			Value |= 0xc0;
			spi_lw(RFCTRL3, Value); //Set Power
			Value &= 0x00;
			Value |=0x40;
		}
		else if((Operator = dB - 30) >=0)
		{//30 <= dB < 40
			Value |= 0xc0;
		}
		else if((Operator = dB - 20) >=0)
		{//20 <= dB < 30
			Value |= 0x80;
		}
		else if((Operator = dB - 10) >=0)
		{//10 <= dB < 20
			Value |= 0x40;
		}
		else
		{                                            // 0 < dB < 10
			Operator = dB;
		}
			
		if(Operator != 0)
		{
			if(Operator == 1)
			{
				Value |= 0x08;
			}
			else if(Operator == 2 || Operator == 3)
			{
				Value |= 0x10;
			}
			else if(Operator == 4)
			{
				Value |= 0x18;
			}
			else if(Operator == 5)
			{
				Value |= 0x20;
			}
			else if(Operator == 6)
			{
				Value |= 0x28;
			}
			else if(Operator == 7)
			{
				Value |= 0x30;
			}
			else
			{//Operator == 8 or 9
				Value |= 0x38;	
			}
		}
	}
	spi_lw(RFCTRL3, Value);
}

/**************************************************************
	Function Name: UzSetChannel()
	Description: Set Channel
	Parameters: NewChannel -> Logical Channel you want modify, Value Range 11~26
	Return Value: None
**************************************************************/

void UzSetChannel(UINT8 NewChannel){ 

	if (NewChannel > 26 || NewChannel < 11) return; //Check Channel Range
	
    	spi_lw(RFCTRL0, ((NewChannel - 11) << 4) + 0x02); //// Shift logic channel, Program Channel

	//Reset RF state machine
	spi_sw(RFCTL, 0x04);
	spi_sw(RFCTL, 0x00);

}

/**************************************************************
	Function Name: UzSetCca()
	Description: Setup CSMA/CA Mode and Threshold
	Parameters: CCAMode -> CSMA/CA mechanism, CS only, ED only or CS |ED hybrid
			     CS_TH -> Threshold for Carrier Sense Mode
			     ED_TH -> Threshold for Energy Detect Mode
	Return Value: None
**************************************************************/

#define CCA_ED_MODE 0x80
#define CCA_CS_MODE 0x40

void UzSetCca(UINT8 CCAMode, UINT8 CS_TH, UINT8 EDT_TH){
	UINT8 Value = 0x00;

	Value |= CCAMode;

	if(CCAMode != CCA_ED_MODE){ // Set Threshold for CS or Hybrid
		Value |= ((CS_TH & 0x0f) << 2);
	}

	if(CCAMode != CCA_CS_MODE){ // Set Threshold for ED or Hybrid
		spi_sw(RSSITHCCA, EDT_TH);
	}

	spi_sw(BBREG2, Value); //Program Channel
}

/**************************************************************
	Function Name: UzReadRSSI()
	Description: Read current RSSI value
	Parameters: None
	Return Value: RSSI Value
**************************************************************/

UINT8 UzReadRSSI(){
	
       spi_sw(BBREG6, spi_sr(BBREG6) | 0x80); // Issue RSSI Request

	while(!(spi_sr(BBREG6) & 0x1)); //Wait Value Ready
         
    	return  spi_lr(RSSI);
}

/**************************************************************
	Function Name: UzSetMacAddress()
	Description: Set Mac Address
	Parameters: MacAddress -> 64bits Mac Address Pointer
	Return Value: None
	Note: Mac Address is used as the hardware filter of RX Normal Mode 
**************************************************************/

void UzSetMacAddress(UINT8 *MacAddress){
	UINT8 i;

	for (i=0; i<8; i++)
       	spi_sw(EADR0+i, *(MacAddress+i)); //Set Value 
	
}

/**************************************************************
	Function Name: UzSetPanId()
	Description: Set Pan Id
	Parameters: PanId -> 16bits PAN identifier
	Return Value: None
	Note: Pan Id is used as the hardware filter of RX Normal Mode 
**************************************************************/

void UzSetPanId(UINT16 PanId){
	UINT8 i;
	
	for(i=0; i<2; i++)
		spi_sw(PANIDL+i, *(((UINT8 *)&PanId)+i)); //Set Value
}

/**************************************************************
	Function Name: UzSetNwkAddr()
	Description: Set Network(16bits) Address
	Parameters: NwkAddr -> 16bits short Address
	Return Value: None
**************************************************************/

void UzSetNwkAddr(UINT16 NwkAddr){
	UINT8 i;

        for(i=0; i<2; i++){
		spi_sw(SADRL+i, *(((UINT8 *)&NwkAddr)+i)); //Set Value
	}
}

/**************************************************************
	Function Name: UzSoftReset()
	Description: Reset UZ2400 by Software
	Parameters: None
	Return Value: None
**************************************************************/

void UzSoftReset(){

	//Reset Power management, Base band, Mac
	spi_sw(SOFTRST, 0x07);
}

/**************************************************************
	Function Name: UzSetUnslotMode()
	Description: Set UZ2400 work in unslot mode
	Parameters: None
	Return Value: None
**************************************************************/

void UzSetUnslotMode(){
	
	spi_sw(ORDER, 0xff);
		        
	spi_sw(TXMCR, spi_sr(TXMCR) & ~0x20); //Set the hardware sloted bit
}


/**************************************************************
	Function Name: 
	Description: 
	Parameters: None
	Return Value: None
**************************************************************/

void UzSetCoordinator(){

    	spi_sw(RXMCR, spi_sr(RXMCR) |0x0c); //Set the PAN coordinator Bit
}

/**************************************************************
	Function Name: UzRxPromiscouosMode()
	Description: Set Promiscouos Mode of Rx
	Parameters: None
	Return Value: None
**************************************************************/

void UzRxPromiscouosMode(){
 
	spi_sw(RXMCR, spi_sr(RXMCR) | 0x01); // Accept all packets with CRC OK
}

/**************************************************************
	Function Name: UzRxErrorMode()
	Description: Set Error Mode of Rx
	Parameters: None
	Return Value: None
**************************************************************/

void UzRxErrorMode(){

	spi_sw(RXMCR, spi_sr(RXMCR) | 0x03); // Accept all kinds of pkt(even CRC error)
} 

/**************************************************************
	Function Name: UzRxNormalMode()
	Description: Set Normal Mode of Rx
	Parameters: None
	Return Value: None
**************************************************************/

void UzRxNormalMode(){

	spi_sw(RXMCR, spi_sr(RXMCR) & ~0x03); // Accept packets with crossponding Pan Id , Mac Address or Network Addrss
}


/**************************************************************
	Function Name: UzEnabTurboMode()
	Description: Enable The Turbo Mode of UZ2400
	Parameters: None
	Return Value: None
**************************************************************/

void UzEnabTurboMode(){

	spi_sw(BBREG0, spi_sr(BBREG0) | 0x01);
	
	spi_sw(BBREG3, spi_sr(BBREG3) | 0x38);

	spi_sw(BBREG4, spi_sr(BBREG4) | 0x5C);
}


/**************************************************************
	Function Name: UzDisTurboMode()
	Description: Disable The Turbo Mode of UZ2400
	Parameters: None
	Return Value: None
**************************************************************/

void UzDisTurboMode(){

	spi_sw(BBREG0, spi_sr(BBREG0) & ~0x01);
 
	spi_sw(BBREG3, spi_sr(BBREG3) & ~0x38);

	spi_sw(BBREG4, spi_sr(BBREG4) & ~0x5C);
}

/**************************************************************
	Function Name: UzForceTxMode()
	Description: 
	Parameters: None
	Return Value: None
**************************************************************/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -