📄 nrf905_spiio.c
字号:
#include <C8051F020.h>
#include "nRF905_SPIIO.h"
#define WC 0x00 // Write configuration register command
#define RC 0x10 // Read configuration register command
#define WTP 0x20 // Write TX Payload command
#define RTP 0x21 // Read TX Payload command
#define WTA 0x22 // Write TX Address command
#define RTA 0x23 // Read TX Address command
#define RRP 0x24 // Read RX Payload command
/*******************************************************************************************/
typedef struct RFConfig
{
unsigned char n;
unsigned char buf[10];
}RFConfig;
code RFConfig RxTxConf =
{
10,
0x01, 0x0c, 0x44, 0x20, 0x20, 0xcc, 0xcc, 0xcc,0xcc, 0x58
};
// The content of this struct is nRF905's initialize data.初始化数据
// CH_NO=1;433MHZ;Normal Opration,No Retrans;RX,TX Address is 4 Bytes
// RX TX Payload Width is 32 Bytes;Disable Extern Clock;Fosc=16MHZ
// 8 Bits CRC And enable
/*******************************************************************************************/
unsigned char xdata TxBuf[32];
unsigned char xdata RxBuf[32];
/*******************************************************************************************/
unsigned char bdata DATA_BUF;
sbit flag =DATA_BUF^7;
sbit flag1 =DATA_BUF^0;
/*******************************************************************************************/
void nRF905_Delay(unsigned char N);
void SpiWrite(unsigned char byte);
unsigned char SpiRead(void);
void SpiCmdWrite(unsigned char command,unsigned char WriteBuf[],unsigned char len);
void SpiCmdRead(unsigned char command,unsigned char ReadBuf[],unsigned char len);
void nRF905Init(void);
void Config905(void);
void SetTxMode(void);
void SetRxMode(void);
bit CheckCD(void);
bit CheckDR(void);
void TxPacket(void);
void RxPacket(void);
/*******************************************************************************************
**函数名称:nRF905_Delay
**函数功能:Delay N*100us
**输入参数:N
**输出参数:无
*******************************************************************************************/
void nRF905_Delay(unsigned char N)
{
unsigned int i;
N*=20;
while(N--)
for(i=0;i<80;i++);
}
/*******************************************************************************************
**函数名称:SpiWrite
**函数功能:Spi总线写数据
**输入参数:待写数据byte
**输出参数:无
*******************************************************************************************/
void SpiWrite(unsigned char byte)
{
unsigned char i;
DATA_BUF=byte; // Put function's parameter into a bdata variable
for (i=0;i<8;i++) // Setup byte circulation bits
{
if (flag) // Put DATA_BUF.7 on data line
MOSI=1;
else
MOSI=0;
SCK=1; // Set clock line high
DATA_BUF=DATA_BUF<<1; // Shift DATA_BUF
SCK=0; // Set clock line low
}
}
/*******************************************************************************************
**函数名称:SpiRead()
**函数功能:Spi总线读数据函数
**输入参数:无
**输出参数:读出数据DATA_BUF
*******************************************************************************************/
unsigned char SpiRead(void)
{
unsigned char i;
for (i=0;i<8;i++) // Setup byte circulation bits
{
DATA_BUF=DATA_BUF<<1; // Right shift DATA_BUF
SCK=1; // Set clock line high
if (MISO)
flag1=1; // Read data
else
flag1=0;
SCK=0; // Set clock line low
}
return DATA_BUF; // Return function parameter
}
/*******************************************************************************************
**函数名称:SpiCmdWrite
**函数功能:附带主机指令的SPI写操作
**输入参数:命令字command,待写数据数组头指针WriteBuf[],待写数据字节长度len
**输出参数:无
*******************************************************************************************/
//<附带主机指令的SPI写操作>
void SpiCmdWrite(unsigned char command,unsigned char WriteBuf[],unsigned char len)
{
unsigned char i;
CSN=0; // Spi enable for write a spi command
SpiWrite(command); // Write config command写放配置命令
for (i=0;i<len;i++) // Write configration words 写放配置字
{
SpiWrite(WriteBuf[i]);
}
CSN=1; // Disable Spi
}
/*******************************************************************************************
**函数名称:SpiCmdWrite
**函数功能:附带主机指令的SPI读操作
**输入参数:命令字command,待读出数据存放数组头指针WriteBuf[],待读出数据字节长度len
**输出参数:读出数据
*******************************************************************************************/
//<附带主机指令的SPI读操作>
void SpiCmdRead(unsigned char command,unsigned char ReadBuf[],unsigned char len)
{
unsigned char i;
CSN=0; // Spi enable for write a spi command
SpiWrite(command); // Read payload command
for (i = 0 ;i < len ;i++)
{
ReadBuf[i]=SpiRead(); // Read data and save to buffer
}
CSN=1; // Disable spi
}
/*******************************************************************************************
**函数名称:nRF905Init
**函数功能:nRF905端口初始化
**输入参数:无
**输出参数:无
*******************************************************************************************/
void nRF905Init(void)
{
/*SPI init*/
CSN=1; // Spi disable
SCK=0; // Spi clock line init low
/*ICO*/
DR=1; // Init DR for input
AM=1; // Init AM for input
CD=1; // Init CD for input
/*模式设置*/
P4|=PWR_UP;//PWR_UP=1; // nRF905 power on
P4&=~TRX_CE;//TRX_CE=0; // Set nRF905 in standby mode
P4&=~TX_EN; //TX_EN=0; // set radio in Rx mode
Config905();
}
/*******************************************************************************************
**函数名称:Config905
**函数功能:nRF905模式配置
**输入参数:配置数据结构题数组
**输出参数:无
*******************************************************************************************/
void Config905(void)
{
unsigned char i;
CSN=0; // Spi enable for write a spi command
SpiWrite(WC); // Write config command 写放配置命令
for (i=0;i<RxTxConf.n;i++) // Write configration words 写放配置字
{
SpiWrite(RxTxConf.buf[i]);
}
CSN=1; // Disable Spi
}
/*******************************************************************************************
**函数名称:SetTxMode
**函数功能:设置nRF905为发送模式
**输入参数:无
**输出参数:无
*******************************************************************************************/
void SetTxMode(void)
{
P4|=TX_EN; //TX_EN=1;
P4&=~TRX_CE;//TRX_CE=0;
nRF905_Delay(1); // delay for mode change(>=650us)
}
/*******************************************************************************************
**函数名称:SetRxMode
**函数功能:设置nRF905为接收模式
**输入参数:无
**输出参数:无
*******************************************************************************************/
void SetRxMode(void)
{
P4&=~TX_EN;//TX_EN=0;
P4|=TRX_CE;//TRX_CE=1;
nRF905_Delay(1); // delay for mode change(>=650us)
}
/*******************************************************************************************
**函数名称:CheckCD
**函数功能:检查是否已存在同频率载波
**输入参数:无
**输出参数:存在返回'1',不存在返回'0'
*******************************************************************************************/
bit CheckCD(void) //Pin->检查是否已存在同频率载波
{
if (CD==1)
{
return 1;
}
else
{
return 0;
}
}
/*******************************************************************************************
**函数名称:CheckDR
**函数功能:检查是否有新数据传入 Data Ready
**输入参数:无
**输出参数:有则返回'1',无则返回'0'
*******************************************************************************************/
bit CheckDR(void) //检查是否有新数据传入 Data Ready
{
if (DR==1 && (P4&TRX_CE) && (P4&~TX_EN))//(DR==1 && TRX_CE==1 && TX_EN==0)
{
return 1;
}
else
{
return 0;
}
}
/*******************************************************************************************
**函数名称:TxPacket
**函数功能:向nRF905发送32bye数据
**输入参数:发送数据存放数组TxBuf[32]
**输出参数:无
*******************************************************************************************/
void TxPacket(void)
{
unsigned char i;
CSN=0; // Spi enable for write a spi command
SpiWrite(WTP); // Write payload command
for (i=0;i<32;i++)
{
SpiWrite(TxBuf[i]); // Write 32 bytes Tx data
}
CSN=1; // Spi disable
nRF905_Delay(1);
CSN=0; // Spi enable for write a spi command
SpiWrite(WTA); // Write address command
for (i=0;i<4;i++) // Write 4 bytes address
{
SpiWrite(RxTxConf.buf[i+5]);
}
CSN=1; // Spi disable
P4|=TRX_CE;//TRX_CE=1; // Set TRX_CE high,start Tx data transmission
nRF905_Delay(1); // while (DR!=1);
P4&=~TRX_CE;//TRX_CE=0; // Set TRX_CE low
}
/*******************************************************************************************
**函数名称:RxPacket
**函数功能:向nRF905接收寄存器中读取32bye数据
**输入参数:无
**输出参数:读取的接收寄存器中的32bey数据
*******************************************************************************************/
void RxPacket(void)
{
unsigned char i;
P4&=~TRX_CE;//TRX_CE=0; // Set nRF905 in standby mode
CSN=0; // Spi enable for write a spi command
SpiWrite(RRP); // Read payload command
for (i=0;i<32;i++)
{
RxBuf[i]=SpiRead(); // Read data and save to buffer
}
CSN=1; // Disable spi
while(DR||AM);
//CSN=1;
P4|=TRX_CE;//TRX_CE=1;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -