#define SIZE 20 //最大数据包长度 32-ADDR4-CRC2=26Byte 25
#define ADDRSIZE 4 //地址长度Byte
#define CH_NO 2 //频段编号
#define BPS_1M //编译控制:1M or 250Kbps
uchar TxBuf[SIZE]={101,102,103,104,105,106,107,108,109,110,
111,112,113,114,115,116,117,118,119,120};
uchar RxBuf[SIZE];
/*-------------------------------------------------------------------------------------------
nrf2401A配置寄存器宏定义
nrf2401A配置寄存器(15Byte)
工作频率f=2.4G+CH_NO*1MHz
-------------------------------------------------------------------------------------------*/
#define RX_ADDRESS 0xCCCCCCCC //接收有效地址(本方)
#define TX_ADDRESS 0xCCCCCCCC //发送有效地址(对方)
#define CH_NO_FREQ CH_NO<<1 //工作频段 Byte1.7-2
#define RX_EN 1 //接收发送切换 Byte1.0 1接收 0发送
#define RX2EN_YES 128 //第2接收通道使能1 Byte2.7
#define RX2EN_NO 0 //第2接收通道使能0 Byte2.7
#define CM_SHOCK 64 //收发模式ShockBurst Byte2.6
#define CM_DIRECT 0 //收发模式DirectMode Byte2.6
#define RFDR_1M 32 //空中Baud=1M Byte2.5
#define RFDR_250K 0 //空中Baud=250K Byte2.5
#define XOF_20MHz 4*4 //晶体振荡器频率20MHz Byte2.4~2
#define XOF_16MHz 3*4 //晶体振荡器频率16MHz
#define XOF_12MHz 2*4 //晶体振荡器频率12MHz
#define XOF_8MHz 1*4 //晶体振荡器频率8MHz
#define XOF_4MHz 0*4 //晶体振荡器频率4MHz
#define PA_PWR_0 0 //输出功率-20dBm Byte2.1~0
#define PA_PWR_1 1 //输出功率-10dBm
#define PA_PWR_2 2 //输出功率-5dBm
#define PA_PWR_3 3 //输出功率 0dBm
#define ADDR_W (ADDRSIZE<<5) //地址位数 Byte3.7~2
#define CRC_MODE_16BIT 2 //CRC16模式 Byte3.1 1
#define CRC_MODE_8BIT 0 //CRC8模式 Byte3.1
#define CRC_EN 1 //CRC使能 Byte3.0 1
#define CRC16_EN 3 //CRC16模式使能
#define CRC8_EN 1 //CRC8模式使能
#define RX_ADDRESS_0 RX_ADDRESS & 0xff //通道1接收有效地址第1字节 Byte4
#define RX_ADDRESS_1 (RX_ADDRESS >> 8) & 0xff //通道1接收有效地址第2字节 Byte5
#define RX_ADDRESS_2 (RX_ADDRESS >> 16) & 0xff //通道1接收有效地址第3字节 Byte6
#define RX_ADDRESS_3 RX_ADDRESS >> 24 //通道1接收有效地址第4字节 Byte7
#define RX_ADDRESS_4 0 //通道1接收有效地址第5字节 Byte8
#define RX_ADDRESS2 0 //Byte9,10,11,12,13字节为通道2接收地址
#define DATA1_W SIZE*8 //接收通道1数据包长度,Bit计算 Byte14
#define DATA2_W SIZE*8 //接收通道2数据包长度,Bit计算 Byte15
#define TX_ADDRESS_4 0 //发送有效地址第5字节
#define TX_ADDRESS_3 TX_ADDRESS >> 24 //发送有效地址第4字节
#define TX_ADDRESS_2 (TX_ADDRESS >> 16) & 0xff //发送有效地址第3字节
#define TX_ADDRESS_1 (TX_ADDRESS >> 8) & 0xff //发送有效地址第2字节
#define TX_ADDRESS_0 TX_ADDRESS & 0xff //发送有效地址第1字节
//写1Byte
void nrf2401A_WR(uchar val)
{uchar temp,i;
//MSB first 高位开始,先输出DATA,然后CLK_Up,再CLK_Dn
SET(DDRC,DATA);//置DATA输出
temp=val;
for (i=0;i<8;i++)
{
if ((temp&0x80)>0)
SET(PORTC,DATA);
else
CLR(PORTC,DATA);
NOP();
SET(PORTC,CLK1);
NOP();
NOP();
NOP();
NOP();
CLR(PORTC,CLK1);
temp=temp<<1;
}
}
//读1Byte
uchar nrf2401A_RD()
{uchar temp=0,i;
//MSB first 高位开始,先读入DATA,然后CLK_Up,再CLK_Dn
CLR(DDRC,DATA);//置DATA输入
temp=0;
for (i=0;i<8;i++)
{
temp=temp<<1;
if ((PINC&(1< temp=temp|0x01;
else temp=temp&0xfe;
SET(PORTC,CLK1);
NOP();
NOP();
NOP();
CLR(PORTC,CLK1);
}
return temp;
}
//PwrDown->StandBy
void nrf2401A_StandBy()
{
SET(PORTB,PWR);
CLR(PORTD,CE);
CLR(PORTB,CS);
}
//->PwrDown
void nrf2401A_Off()
{
CLR(PORTB,PWR);
CLR(PORTD,CE);
CLR(PORTB,CS);
}
//配置模式
void nrf2401A_RwOn()
{
SET(PORTB,PWR);
CLR(PORTD,CE);
SET(PORTB,CS);
}
//工作模式
void nrf2401A_Run()
{
SET(PORTC,PWR);
CLR(PORTB,CS); //first
SET(PORTD,CE);
}
void nrf2401A_Config(void)
{
nrf2401A_RwOn();//配置状态
//nrf2401A_WR(0x8e);
//nrf2401A_WR(0x08);
//nrf2401A_WR(0x1c);
nrf2401A_WR(DATA2_W); //200+16+32+8
nrf2401A_WR(DATA1_W); //200
nrf2401A_WR(RX_ADDRESS2);
nrf2401A_WR(RX_ADDRESS2);
nrf2401A_WR(RX_ADDRESS2);
nrf2401A_WR(RX_ADDRESS2);
nrf2401A_WR(RX_ADDRESS2);
nrf2401A_WR(RX_ADDRESS_4);
nrf2401A_WR(RX_ADDRESS_3);
nrf2401A_WR(RX_ADDRESS_2);
nrf2401A_WR(RX_ADDRESS_1);
nrf2401A_WR(RX_ADDRESS_0);
nrf2401A_WR(CRC16_EN|ADDR_W); //CRC16,AddrWidth
nrf2401A_WR(PA_PWR_3|XOF_16MHz|CM_SHOCK);
//nrf2401A_WR(PA_PWR_3|XOF_16MHz|RFDR_1M|CM_SHOCK);
nrf2401A_WR(CH_NO_FREQ|RX_EN); //RxMode,Chan
//
nrf2401A_StandBy();//执行配置
}
/*
//配置nRF2401A
void nrf2401A_Config(void)
{
nrf2401A_RwOn();//配置状态
nrf2401A_WR(DATA2_W);
nrf2401A_WR(DATA1_W);
nrf2401A_WR(RX_ADDRESS2);
nrf2401A_WR(RX_ADDRESS2);
nrf2401A_WR(RX_ADDRESS2);
nrf2401A_WR(RX_ADDRESS2);
nrf2401A_WR(RX_ADDRESS2);
nrf2401A_WR(RX_ADDRESS_4);
nrf2401A_WR(RX_ADDRESS_3);
nrf2401A_WR(RX_ADDRESS_2);
nrf2401A_WR(RX_ADDRESS_1);
nrf2401A_WR(RX_ADDRESS_0);
nrf2401A_WR(CRC16_EN|ADDR_W);//CRC16,AddrWidth
nrf2401A_WR(PA_PWR_3|XOF_16MHz|RFDR_1M|CM_SHOCK);
//#ifdef BPS_1M
// nrf2401A_WR(PA_PWR_3|XOF_16MHz|RFDR_1M|CM_SHOCK);//0dbm,16M,1Mbps,ShockBurst
// #else
// nrf2401A_WR(PA_PWR_3|XOF_16MHz|CM_SHOCK);//0dbm,16M,250Kbps,ShockBurst
// #endif
// nrf2401A_WR(CH_NO_FREQ|RX_EN); //RxMode,Chan
nrf2401A_StandBy();//执行配置
}
*/
//接收模式
void nrf2401A_RxOn()
{
nrf2401A_RwOn();
nrf2401A_WR(CH_NO_FREQ|RX_EN); //RxMode,Chan
nrf2401A_Run();
}
//发送模式
void nrf2401A_TxOn()
{
nrf2401A_RwOn();
nrf2401A_WR(CH_NO_FREQ); //TxMode,Chan
nrf2401A_Run();
}
//初始化nrf2401A
void nrf2401A_Init()
{ uchar i;
CLR(PORTC,CLK1);//
nrf2401A_StandBy();
// DelayMs(10); //!!!must >3ms
for(i=1;i<5;i++)
delay(1000);
nrf2401A_Config();
nrf2401A_RxOn();//接收状态
}
//写Tx地址
void nrf2401A_SetTxAddr()
{
nrf2401A_WR(TX_ADDRESS_3);
nrf2401A_WR(TX_ADDRESS_2);
nrf2401A_WR(TX_ADDRESS_1);
nrf2401A_WR(TX_ADDRESS_0);
}
//写Tx数据
void nrf2401A_SetData()
{ uchar i;
for (i=0;i nrf2401A_WR(TxBuf);
}
//发送TxBuf中的数据
void nrf2401A_SendData()
{
nrf2401A_TxOn();
SET(PORTD,CE);
nrf2401A_SetTxAddr();//写发送地址
nrf2401A_SetData();//写数据
CLR(PORTD,CE);//启动发送
//发送完毕后进入StandBy
}
//读出接收到的数据
void nrf2401A_ReadData()
{uchar i;
for (i=0;i {
RxBuf=nrf2401A_RD();
}
}
void RfRecvProc()
{uchar i;
if ((PINC&(1< //已经接收到数据
nrf2401A_ReadData();//读出...
}