/************************************************************************
** 文件名:nrf2401.c
** 功 能:nRF2401驱动程序c文件
*************************************************************************/
#include "nrf2401.h"
#include <stdio.h>
static void RF_WriteByte(unsigned char data);
static void Delay(unsigned int time);
/*
** 配置数据
*/
static const unsigned char ConfigData[] = {
(unsigned char)DATA2_W,
(unsigned char)DATA1_W,
(unsigned char)ADDR2_4,
(unsigned char)ADDR2_3,
(unsigned char)ADDR2_2,
(unsigned char)ADDR2_1,
(unsigned char)ADDR2_0,
(unsigned char)ADDR1_4,
(unsigned char)ADDR1_3,
(unsigned char)ADDR1_2,
(unsigned char)ADDR1_1,
(unsigned char)ADDR1_0,
(unsigned char)((ADDR_W << 2) | (CRC_L << 1) | CRC_EN),
(unsigned char)((RX2_EN << 7) | (CM << 6) | (RFDR_SB << 5) | (XO_F << 2) | RF_PWR),
(unsigned char)((RF_CH << 1) | RXEN)
};
/*
** 向nrf2401发送一个字节的数据
** data:要发送的数
*/
static void RF_WriteByte(unsigned char data)
{
unsigned char i;
for(i = 0x80; i > 0; i /= 2)
{
if((data & i) != 0)
DATA_OUT_H;
else
DATA_OUT_L;
CLK1_H;
_NOP();
_NOP();
CLK1_L;
}
}
static void Delay(unsigned int time)
{
while(time--);
}
/*
** Enter Active modes
*/
void RF_Active(void)
{
PWR_UP_H;
CE_H;
CS_L;
}
/*
** Enter Configuration modes
*/
void RF_Config(void)
{
PWR_UP_H;
CE_L;
CS_H;
}
/*
** Enter Stand by modes
*/
void RF_Standby(void)
{
PWR_UP_H;
CE_L;
CS_L;
}
/*
** Power down modes
** Note:When the nRF2401A is in power down it must always settle
** in stand by mode for 3ms before it can enter configuration or one
** of the active modes.
*/
void RF_PowerDown(void)
{
PWR_UP_L;
}
/*
** nRF2401初始化
*/
void RF_Init(void)
{
unsigned int i;
IO_INIT();
RF_Standby();;
Delay(30000);
RF_Config();
for(i = 0; i < 15; i++)
RF_WriteByte(ConfigData[i]);
Delay(2000);
CS_L;
DATA_OUT_L;
}
/*
** 向指定channel指定地址发送指定数据
** channel:接收端的频道(0 - 127)
** addr: 接收端的地址的指针
** alength:地址长度(byte)
** sendbuffer:要发送数据的首指针
** dlength:数据长度
** 注意:接收端地址数组的第一个元素为地址高字节,如接收端地址ADDR1_4 ...ADDR1_0为
** 0x11,0x22,0x33,0x44,0x55,那么接收端地址数组addr[]={0x11,0x22,0x33,0x44,0x55}
*/
void RF_SendData(unsigned char channel, \
unsigned char *addr, \
unsigned char alength, \
unsigned char *sendbuffer,\
unsigned char dlength)
{
unsigned int i;
if(channel > 127)
return;
if(alength > 5)
return;
if(dlength > 32)
return;
if((addr == NULL) || (sendbuffer == NULL))
return;
RF_Config();
RF_WriteByte((unsigned char)(channel << 1));
Delay(2000);
CS_L;
DATA_OUT_L;
RF_Active();
for(i = 0; i < alength; i++)
RF_WriteByte(*addr++);
for(i = 0; i < dlength; i++)
RF_WriteByte(*sendbuffer++);
Delay(2000);
CE_L;
DATA_OUT_L;
}
/*
** 进入接收模式
*/
void RF_RXMode(void)
{
RF_Config();
DATA_OUT_H;
CLK1_H;
_NOP();
_NOP();
CLK1_L;
Delay(2000);
CS_L;
CE_H;
}
/*
** 接收数据
** receivebuffer:接收缓冲区指针
** channel:接收的通道
** 返回接收到得字节数,错误返回-1
*/
int RF_ReceiveData(unsigned char *receivebuffer, unsigned char channel)
{
unsigned char temp = 0;
unsigned int i = 0, j = 0;
if(receivebuffer == NULL)
return -1;
if((channel < 1) || (channel > 2))
return -1;
switch(channel)
{
case 1:
DATA_IN;
while(DR1_IN_H)
{
CLK1_H;
if(DATA_IN_H)
temp |= 1;
i++;
if(i > 7)
{
i = 0;
receivebuffer[j] = temp;
temp = 0;
j++;
}
CLK1_L;
temp = temp << 1;
}
DATA_OUT;
break;
case 2:
while(DR2_IN_H)
{
CLK2_H;
if(DOUT2_IN_H)
temp |= 1;
i++;
if(i > 7)
{
i = 0;
receivebuffer[j] = temp;
temp = 0;
j++;
}
CLK2_L;
temp = temp << 1;
}
break;
default:
break;
}
return (int)j;
}