初级会员
积分 51
金钱 51
注册时间 2014-6-26
在线时间 0 小时
5 金钱
<div style="background-color:#E8E8E8;">
[mw_shl_code=c,true]NRF24L01的数据手册不停翻看了好多了,确实有点不行了,想请人帮帮忙,谢谢大家[/mw_shl_code]
[mw_shl_code=c,true]
[/mw_shl_code]
[mw_shl_code=c,true]
[/mw_shl_code]
[mw_shl_code=c,true]#include "NRF2401.h"
const unsigned char TX_ADDRESS[TX_ADR_WIDTH] = {0xe7,0xe7,0xe7,0xe7,0xe7}; //本地地址
const unsigned char RX_ADDRESS0[RX_ADR_WIDTH]= {0xe7,0xe7,0xe7,0xe7,0xe7}; //接收地址
const unsigned char RX_ADDRESS1[RX_ADR_WIDTH]= {0xc2,0xc2,0xc2,0xc2,0xc2}; //接收地址
const unsigned char RX_ADDRESS2[RX_ADR_WIDTH]= {0xc2,0xc2,0xc2,0xc2,0xc3}; //接收地址
const unsigned char RX_ADDRESS3[RX_ADR_WIDTH]= {0xc2,0xc2,0xc2,0xc2,0xc4}; //接收地址
const unsigned char RX_ADDRESS4[RX_ADR_WIDTH]= {0xc2,0xc2,0xc2,0xc2,0xc5}; //接收地址
const unsigned char RX_ADDRESS5[RX_ADR_WIDTH]= {0xc2,0xc2,0xc2,0xc2,0xc6}; //接收地址
unsigned int sta; //状态标志
/************************************NRF24L01初始化************************************/
void Set_TxMode(void)
{
DelayMs(5);
CECLR; // chip enable
// CSNSET; // Spi disable
SCKCLR;
SPI_Write_Buf(WRITE_REG + TX_ADDR, TX_ADDRESS, RX_ADR_WIDTH); //写接收端地址
SPI_Write_Buf(WRITE_REG+RX_ADDR_P0,RX_ADDRESS0,RX_ADR_WIDTH);
SPI_RW_Reg(WRITE_REG + CONFIG, 0x0e); // IRQ收发完成中断响应,16位CRC,主发送
SPI_RW_Reg(WRITE_REG + EN_AA, 0x01); //频道0自动,ACK应答允许(应该需要关闭自动应答)
SPI_RW_Reg(WRITE_REG + EN_RXADDR, 0x01);
// SPI_RW_Reg(WRITE_REG + SETUP_AW, 0x03); //收发地址宽度5字节(不知道写不写)
SPI_RW_Reg(WRITE_REG + RF_CH, 0x40); //设置信道工作为2.4GHZ,收发必须一致
SPI_RW_Reg(WRITE_REG + RX_PW_P0, RX_PLOAD_WIDTH); //设置接收数据长度,本次设置为32字节
SPI_RW_Reg(WRITE_REG + RF_SETUP, 0x07); //设置发射速率为1MHZ,发射功率为最大值0dB
}
/****************************************************************************************************
函数:void SetRX_Mode(void)
功能:数据接收配置
****************************************************************************************************/
void Set_RxMode(void)
{[/mw_shl_code]
[mw_shl_code=c,true]<span style="font-family:'sans serif', tahoma, verdana, helvetica;line-height:1.5;">//延时至少10us</span>[/mw_shl_code]
[mw_shl_code=c,true]<span style="font-family:'sans serif', tahoma, verdana, helvetica;line-height:1.5;"> DelayMs(5);</span>[/mw_shl_code]
[mw_shl_code=c,true] CECLR;
SCKCLR;
SPI_Write_Buf(WRITE_REG + RX_ADDR_P0, RX_ADDRESS0, TX_ADR_WIDTH); //写本地地址
SPI_RW_Reg(WRITE_REG + CONFIG, 0x0f);
SPI_RW_Reg(WRITE_REG + EN_AA, 0x01);
SPI_RW_Reg(WRITE_REG + EN_RXADDR, 0x01);
// SPI_RW_Reg(WRITE_REG + SETUP_AW, 0x03); //看好多都没设置
SPI_RW_Reg(WRITE_REG + RF_CH, 0x40);
SPI_RW_Reg(WRITE_REG + RX_PW_P0, RX_PLOAD_WIDTH);
SPI_RW_Reg(WRITE_REG + RF_SETUP, 0x07);
//此处CE可能需要拉高,并延时
}
/**********************************NRF24L01的SPI写时序**********************************/
unsigned int SPI_RW(unsigned int uchar)
{
unsigned int bit_num;
for(bit_num=0;bit_num<8;bit_num++) // output 8-bit
{
if(uchar & 0x80)
MOSISET;
else
MOSICLR; // output 'uchar', MSB to MOSI
uchar = (uchar << 1); // shift next bit into MSB..
SCKSET; // SCK置高,理论上需延时40ns
DelayUs(1);
uchar |= ((GPIOE_IDR & 0x0040)>>6); //uchar已经左移,故最后一位位0,和0或获得MISO状态,且PE6为输入状态
SCKCLR; // 置低,不解释
DelayUs(1);
}
return uchar; // 返回MISO线状态
}
/****************************************************************************************************
函数:uchar SPI_Read(uchar reg)
功能:NRF24L01的SPI时序
****************************************************************************************************/
unsigned char SPI_Read(unsigned char reg)
{
unsigned char reg_val;
CSNSET; // 置低,则可以开始读写操作
CSNCLR;
SPI_RW(reg); // CSN置低后,先给出指令,即寄存器地址
reg_val = SPI_RW(0); // ..then read registervalue
CSNSET; // 置高,读写结束
return reg_val; // return register value
}
/***************************************************************************************************
功能:NRF24L01读写寄存器函数 (感觉应该是写)
****************************************************************************************************/
unsigned int SPI_RW_Reg(unsigned char reg, unsigned char value)
{
unsigned int status;
CSNSET; // CSN low, init SPI transaction
CSNCLR;
status = SPI_RW(reg); //给出指令,返回状态寄存器地址
SPI_RW(value); // ..and write value to it..
CSNSET; // CSN high again
return status; // return nRF24L01 status uchar
}
/***************************************************************************************************
函数:uint SPI_Read_Buf(uchar reg, uchar *pBuf, uchar uchars)
功能: 用于读数据,reg:为寄存器地址,pBuf:为待读出数据地址,uchars:读出数据的个数
****************************************************************************************************/
unsigned int SPI_Read_Buf(unsigned char reg, unsigned char *pBuf, unsigned char uchars)
{
unsigned int status,uchar_ctr;
CSNSET; // Set CSN low, init SPI tranaction
CSNCLR;
status = SPI_RW(reg); // Select register to write to and read status uchar
for(uchar_ctr=0;uchar_ctr<uchars;uchar_ctr++)
pBuf[uchar_ctr] = SPI_RW(0); //多字节则先读出低字节
CSNSET;
return status; // return nRF24L01 status uchar
}
/*********************************************************************************************************
函数:uint SPI_Write_Buf(uchar reg, uchar *pBuf, uchar uchars)
功能: 用于写数据:为寄存器地址,pBuf:为待写入数据地址,uchars:写入数据的个数
*********************************************************************************************************/
unsigned int SPI_Write_Buf(unsigned char reg,const unsigned char *pBuf, unsigned char uchars)
{
unsigned int status,uchar_ctr;
CSNSET; //SPI使能
CSNCLR;
status = SPI_RW(reg);
for(uchar_ctr=0; uchar_ctr<uchars; uchar_ctr++) //
SPI_RW(*pBuf++); //多字节则先写入低字节
CSNSET; //关闭SPI
return status; //
}
/******************************************************************************************************
函数:unsigned char nRF24L01_RxPacket(unsigned char* rx_buf)
功能:数据读取后放如rx_buf接收缓冲区中
******************************************************************************************************/
unsigned char nRF24L01_RxPacket(unsigned char* rx_buf)
{
unsigned char revale=0;
CESET;
DelayMs(30);
sta=SPI_Read(STATUS); // 读取状态寄存其来判断数据接收状况
while(!((sta &= 0x0040)>>6)); // 等待接受到数据
CECLR; //SPI使能
SPI_Read_Buf(RD_RX_PLOAD,rx_buf,TX_PLOAD_WIDTH); // read receive payload from RX_FIFO buffer数据位数设置的32位
revale =1; //读取数据完成标志
SPI_RW_Reg(WRITE_REG+STATUS,sta); //接收到数据后RX_DR,TX_DS,MAX_PT都置高为1,通过写1来清楚中断标志
return revale;
}
/***********************************************************************************************************
函数:void nRF24L01_TxPacket(unsigned char * tx_buf)
功能:发送 tx_buf中数据
**********************************************************************************************************/
void nRF24L01_TxPacket(unsigned char *tx_buf)
{
CECLR; //StandBy I模式
SPI_Write_Buf(WRITE_REG + RX_ADDR_P0, TX_ADDRESS, TX_ADR_WIDTH); // 装载接收端地址
SPI_Write_Buf(WR_TX_PLOAD, tx_buf, TX_PLOAD_WIDTH); // 装载数据
CESET; //置高CE,激发数据发送
DelayMs(1);
}
[/mw_shl_code]
</div>
<br />
<br />
<br />
这粘上主函数<br />
<div style="background-color:#E8E8E8;">
[mw_shl_code=c,true]int main()
{
RCC_HSE_ON();
RCC_APB2ENR=0xFFFFFFFF;
GPIOA_Init();
GPIOB_Init();
GPIOC_Init();
GPIOD_Init();
GPIOE_Init();
LCD_Init();
//以上的初始化都没问题
Set_TxMode();
while(1)
{
TxBuf[0]=1;
nRF24L01_TxPacket(TxBuf);
LCD_WriteString(0,0,"TxBuf[0]="); //在LCD上显示,TurnChar是我写的一个把整数转化成字符串以便显示的函数
LCD_WriteString(0,1,"TxBuf[1]=");
LCD_WriteString(55,0,TurnChar(TxBuf[0]));
LCD_WriteString(55,1,TurnChar(TxBuf[1]));
// LCD_WriteString(0,2,TurnChar(SPI_Read(WR_TX_PLOAD)));
LCD_WriteString(0,3,"STATUS=");
// SPI_RW_Reg(WRITE_REG+STATUS,0x70);
LCD_WriteString(43,3,TurnChar(SPI_Read(STATUS)));
DelayMs(1000);
}
//***********************************************************************************************
/*
Set_RxMode();
while(1)
{
nRF24L01_RxPacket(RxBuf);
LCD_WriteString(0,0,"RxBuf[0]=");
LCD_WriteString(0,1,"RxBuf[1]=");
LCD_WriteString(55,0,TurnChar(RxBuf[0]));
LCD_WriteString(55,1,TurnChar(RxBuf[1]));
LCD_WriteString(0,3,TurnChar(SPI_Read(STATUS)));
if(RxBuf[1]==1)
{
LEDON;
LCD_WriteString(0,2,"Received");
DelayMs(1);
}
if(RxBuf[1]==0)
{
LEDCL;
LCD_WriteString(0,2,"not received");
DelayMs(1);
}
// RxBuf[1] = 0x00;
DelayMs(1000);
}
*/
} [/mw_shl_code]
</div>
<br />
<br />
我来回答