nrf24l01.c智芯銳電子技術(shù)論壇:http://www.zhixinrui.com
#include "nrf24l01.h"
unsigned char flag=0;//狀態(tài)寄存器(STATUS)標(biāo)志
unsigned char success=0;//發(fā)送接收成功與否標(biāo)志,0為失敗,1為發(fā)送成功,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}; //接收節(jié)點地址(0通道40位可任意配置)
unsigned char RX_ADDRESS[RX_ADDR_WIDTH]={0x04,0x01,0x02,0x03,0x00}; //接收地址(與接收節(jié)點地址相同)
//Function:延時t*ms
void Delayms(unsigned int t)
{SysCtlDelay(t*2000);}
//Function:延時t*us
void Delayus(unsigned int t)
{SysCtlDelay(t*2);}
//Function:系統(tǒng)初始化
void SystemInit(void)
{
SysCtlLDOSet(SYSCTL_LDO_2_50V);//設(shè)置LDO輸出電壓為2.5v
SysCtlClockSet(SYSCTL_SYSDIV_1|SYSCTL_USE_OSC|SYSCTL_OSC_MAIN|SYSCTL_XTAL_6MHZ);//系統(tǒng)時鐘設(shè)置,采用主振蕩器,外晶振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);//低電平觸發(fā)
GPIOPinIntEnable(IRQ);//開啟端口引腳中斷
IntEnable(INT_GPIOA);//開啟GPIOA總中斷
}
//Function:SPI寫入一字節(jié),同時讀出一字節(jié)
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:從寄存器中讀取狀態(tài)字節(jié)
unsigned char ReadReg(unsigned char reg)
{
unsigned char regVal;
GPIOPinWrite(CSN,0x00);//片選
SPIWriteRead(R_REGISTER+reg);
regVal=SPIWriteRead(0);//只讀,故參數(shù)值()內(nèi)任意取char型值
GPIOPinWrite(CSN,0xFF);
return regVal;
}
//Function:向寄存器寫入一字節(jié),同時返回狀態(tài)字節(jié)
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字節(jié)的數(shù)據(jù)(pBuf),同時返回狀態(tài)字節(jié)
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字節(jié)數(shù)據(jù)
GPIOPinWrite(CSN,0xFF);
return status;
}
//Function:向寄存器寫入num字節(jié)的數(shù)據(jù)(pBuf),同時返回狀態(tài)字節(jié)
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字節(jié)數(shù)據(jù)
GPIOPinWrite(CSN,0xFF);
return status;
}
//Function:接收啟動函數(shù)
void RXReady(void)
{
WriteReadReg(W_REGISTER+CONFIG,0x0F);//IRQ收發(fā)完成中斷響應(yīng),16位CRC,PWR_UP上電,接收模式
GPIOPinWrite(CE,0xFF);//CE置高使能接收模式
Delayus(130);//待機模式->接收模式用時最長為130us
}
//Function:發(fā)送啟動函數(shù)
void TXReady(unsigned char *txBuf)
{
GPIOPinWrite(CE,0x00);
WriteBuf(W_TX_PAYLOAD,txBuf,TX_PLOAD_WIDTH);//裝載發(fā)送數(shù)據(jù)
WriteReadReg(W_REGISTER + CONFIG, 0x0E);//IRQ收發(fā)完成中斷響應(yīng),16位CRC,PWR_UP上電,發(fā)送模式
GPIOPinWrite(CE,0xFF);//啟動發(fā)送
Delayus(10);//CE高電平保持時間至少10us
}
評論