nrf24l01.c
智芯锐电子技术论坛:http://www.zhixinrui.com
#include "nrf24l01.h"
unsigned char flag=0;//状态寄存器(STATUS)标志
unsigned char success=0;//发送接收成功与否标志,0为失败,1为发送成功,2为接收成功
unsigned charTXBuf[TX_PLOAD_WIDTH]={0x68,0x86,0x00};
unsigned char RXBuf[RX_PLOAD_WIDTH]={0x00,0x00,0x00};
unsigned char TX_ADDRESS[TX_ADDR_WIDTH]={0x04,0x01,0x02,0x03,0x00}; //接收节点地址(0通道40位可任意配置)
unsigned char RX_ADDRESS[RX_ADDR_WIDTH]={0x04,0x01,0x02,0x03,0x00}; //接收地址(与接收节点地址相同)
//Function:延时t*ms
void Delayms(unsigned int t)
{SysCtlDelay(t*2000);}
//Function:延时t*us
void Delayus(unsigned int t)
{SysCtlDelay(t*2);}
//Function:系统初始化
void SystemInit(void)
{
SysCtlLDOSet(SYSCTL_LDO_2_50V);//设置LDO输出电压为2.5v
SysCtlClockSet(SYSCTL_SYSdiv_1|SYSCTL_USE_OSC|SYSCTL_OSC_MAIN|SYSCTL_XTAL_6MHZ);//系统时钟设置,采用主振荡器,外晶振6MHZ,不分频
}
//Function:管脚配置初始化
void NrfInitPort(void)
{
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA);
GPIOPinTypeGPIOOutput(CE);
GPIOPinTypeGPIOOutput(CSN);
GPIOPinTypeGPIOOutput(MOSI);
GPIOPinTypeGPIOInput(MISO);//MISO配置为输入
GPIOPinTypeGPIOOutput(SCK);
GPIOPinTypeGPIOInput(IRQ);
GPIOIntTypeSet(IRQ,GPIO_LOW_LEVEL);//低电平触发
GPIOPinIntEnable(IRQ);//开启端口引脚中断
IntEnable(INT_GPIOA);//开启GPIOA总中断
}
//Function:SPI写入一字节,同时读出一字节
unsigned char SPIWriteRead(unsigned char c)
{
unsigned char i;
for(i=0;i<8;i++)
{
GPIOPinWrite(MOSI,(0x80&c)>>5);//SPI写
c<=1;
GPIOPinWrite(SCK,0xFF);
c|=(GPIOPinRead(MISO)>>1);//SPI读
GPIOPinWrite(SCK,0x00);
}
return c;
}
//Function:从寄存器中读取状态字节
unsigned char ReadReg(unsigned char reg)
{
unsigned char regVal;
GPIOPinWrite(CSN,0x00);//片选
SPIWriteRead(R_REGISTER+reg);
regVal=SPIWriteRead(0);//只读,故参数值()内任意取char型值
GPIOPinWrite(CSN,0xFF);
return regVal;
}
//Function:向寄存器写入一字节,同时返回状态字节
unsigned char WriteReadReg(unsigned char reg,unsigned char value)
{
unsigned char status;
GPIOPinWrite(CSN,0x00);
status=SPIWriteRead(reg);//选择寄存器(写指令)
SPIWriteRead(value);//写入值(配置寄存器)
GPIOPinWrite(CSN,0xFF);
return status;
}
//Function:从寄存器读出num字节的数据(pBuf),同时返回状态字节
unsigned char ReadBuf(unsigned char reg,unsigned char *pBuf,unsigned char num)
{
unsigned char i,status;
GPIOPinWrite(CSN,0x00);
status=SPIWriteRead(reg);//选择寄存器(写指令)
for(i=0;i
pBuf[i]=SPIWriteRead(0);//读取num字节数据
GPIOPinWrite(CSN,0xFF);
return status;
}
//Function:向寄存器写入num字节的数据(pBuf),同时返回状态字节
unsigned char WriteBuf(unsigned char reg,unsigned char*pBuf,unsigned char num)
{
unsigned char i,status;
GPIOPinWrite(CSN,0x00);
status=SPIWriteRead(reg);//选择寄存器(写指令)
for(i=0;i
SPIWriteRead(pBuf[i]);//读取num字节数据
GPIOPinWrite(CSN,0xFF);
return status;
}
//Function:接收启动函数
void RXReady(void)
{
WriteReadReg(W_REGISTER+CONFIG,0x0F);//IRQ收发完成中断响应,16位CRC,PWR_UP上电,接收模式
GPIOPinWrite(CE,0xFF);//CE置高使能接收模式
Delayus(130);//待机模式->接收模式用时最长为130us
}
//Function:发送启动函数
void TXReady(unsigned char *txBuf)
{
GPIOPinWrite(CE,0x00);
WriteBuf(W_TX_PAYLOAD,txBuf,TX_PLOAD_WIDTH);//装载发送数据
WriteReadReg(W_REGISTER + CONFIG, 0x0E);//IRQ收发完成中断响应,16位CRC,PWR_UP上电,发送模式
GPIOPinWrite(CE,0xFF);//启动发送
Delayus(10);//CE高电平保持时间至少10us
}
//Function:Nrf寄存器初始化
void NrfInit(void)
{
GPIOPinWrite(CE,0x00);//芯片使能关闭
GPIOPinWrite(CSN,0xFF);//片选关闭
GPIOPinWrite(SCK,0x00);//SPI时钟为低
WriteBuf(W_REGISTER+TX_ADDR, TX_ADDRESS, TX_ADDR_WIDTH);//写本地地址
WriteBuf(W_REGISTER+RX_ADDR_P0, RX_ADDRESS, RX_ADDR_WIDTH);//写接收端地址