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
}
声明:本文内容来自网络转载或用户投稿,文章版权归原作者和原出处所有。文中观点,不代表本站立场。若有侵权请联系本站删除(kf@86ic.com)https://www.86ic.net/qiche/257776.html