首先我调试的是接收,用51单片机作为从机,能够发送出去,但是反过来,用mega8控制NRF2401就接收不到了,不清楚到底是什么情况,在51上调试的挺好的,换到AVR上为什么不行了呢,GPIO的设置应该没有问题,以下是我的程序,各位大神,求指点啊。
#include
//------------------------------------------------------------------------------
//--------------------------------NRF24L01接口定义--------------------------------
//------------------------------------------------------------------------------
//#define NRF24L01_MISO PD5 //输入0
#define Hign_24L01_MISO PORTD |= (1 << PORTD5)
#define Low_24L01_MISO PORTD &= ~(1 << PORTD5)
#define Read_24L01_MISO PIND & (1 << PORTD5)
//#define NRF24L01_MOSI PD6 //输出1
#define Hign_24L01_MOSI PORTD |= (1 << PORTD6)
#define Low_24L01_MOSI PORTD &= ~(1 << PORTD6)
#define Read_24L01_MOSI PIND & (1 << PIND6)
//#define NRF24L01_CSN PB0 //输出1
#define Low_24L01_CSN PORTB &= ~(1 << PORTB0)
#define Hign_24L01_CSN PORTB |= (1 << PORTB0)
//#define NRF24L01_SCK PD7 //输出1
#define Hign_24L01_SCK PORTD |= (1 << PORTD7)
#define Low_24L01_SCK PORTD &= ~(1 << PORTD7)
#define Read_24L01_SCK PIND & (1 << PIND7);
//#define NRF24L01_CE PB1 //输出1
#define Hign_24L01_CE PORTB |= (1 << PORTB1)
#define Low_24L01_CE PORTB &= ~(1 << PORTB1)
#define Read_24L01_CE PINB & (1 << PINB1)
//#define NRF24L01_IRQ PD3 //输入
#define Hign_NRF24L01_IRQ PORTD |= (1 << PORTD3)
#define Low_NRF24L01_IRQ PORTD &= ~(1 << PORTD3)
#define Read_NRF24L01_IRQ PIND & (1 << PIND3)
#define LED PORTB.2
unsigned int sta;
//*********************************************NRF24L01*************************************
#define TX_ADR_WIDTH 5 // 5 uints TX address width
#define RX_ADR_WIDTH 5 // 5 uints RX address width
#define TX_PLOAD_WIDTH 20 // 20 uints TX payload
#define RX_PLOAD_WIDTH 20 // 20 uints TX payload
unsigned char TX_ADDRESS[TX_ADR_WIDTH]= {0x34,0x43,0x10,0x10,0x01}; //本地地址
unsigned char RX_ADDRESS[RX_ADR_WIDTH]= {0x34,0x43,0x10,0x10,0x01}; //接收地址
//***************************************NRF24L01寄存器指令*******************************************************
#define READ_REG 0x00 // 读寄存器指令
#define WRITE_REG 0x20 // 写寄存器指令
#define RD_RX_PLOAD 0x61 // 读取接收数据指令
#define WR_TX_PLOAD 0xA0 // 写待发数据指令
#define FLUSH_TX 0xE1 // 冲洗发送 FIFO指令
#define FLUSH_RX 0xE2 // 冲洗接收 FIFO指令
#define REUSE_TX_PL 0xE3 // 定义重复装载数据指令
#define NOP1 0xFF // 保留
//*************************************SPI(nRF24L01)寄存器地址****************************************************
#define CONFIG 0x00 // 配置收发状态,CRC校验模式以及收发状态响应方式
#define EN_AA 0x01 // 自动应答功能设置
#define EN_RXADDR 0x02 // 可用信道设置
#define SETUP_AW 0x03 // 收发地址宽度设置
#define SETUP_RETR 0x04 // 自动重发功能设置
#define RF_CH 0x05 // 工作频率设置
#define RF_SETUP 0x06 // 发射速率、功耗功能设置
#define STATUS 0x07 // 状态寄存器
#define OBSERVE_TX 0x08 // 发送监测功能
#define CD 0x09 // 地址检测
#define RX_ADDR_P0 0x0A // 频道0接收数据地址
#define RX_ADDR_P1 0x0B // 频道1接收数据地址
#define RX_ADDR_P2 0x0C // 频道2接收数据地址
#define RX_ADDR_P3 0x0D // 频道3接收数据地址
#define RX_ADDR_P4 0x0E // 频道4接收数据地址
#define RX_ADDR_P5 0x0F // 频道5接收数据地址
#define TX_ADDR 0x10 // 发送地址寄存器
#define RX_PW_P0 0x11 // 接收频道0接收数据长度
#define RX_PW_P1 0x12 // 接收频道0接收数据长度
#define RX_PW_P2 0x13 // 接收频道0接收数据长度
#define RX_PW_P3 0x14 // 接收频道0接收数据长度
#define RX_PW_P4 0x15 // 接收频道0接收数据长度
#define RX_PW_P5 0x16 // 接收频道0接收数据长度
#define FIFO_STATUS 0x17 // FIFO栈入栈出状态寄存器设置
//**************************************************************************************
void Delay(unsigned int s);
void init_NRF24L01(void);
unsigned int SPI_RW(unsigned char data);
unsigned char SPI_Read(unsigned char reg);
unsigned int SPI_RW_Reg(unsigned char reg,unsigned char value);
unsigned char SPI_Read_Buf(unsigned char reg, unsigned char *pBuf,unsigned char uchars);
unsigned int SPI_Write_Buf(unsigned char reg, unsigned char *pBuf,unsigned char uchars);
void SetRX_Mode(void);
unsigned char nRF24L01_RxPacket(unsigned char* rx_buf);
void nRF24L01_TxPacket(unsigned char * tx_buf);
unsigned char RxBuf[20]={0};
unsigned char TxBuf[20]={0};
//*****************************************长延时*****************************************
void Delay(unsigned int s)
{
unsigned int i;
for(i=0; i
#asm("nop");
}
//****************************************************************************************************
//*函数:uint SPI_RW(uint uchar)
//*/功能:NRF24L01的SPI写时序
//****************************************************************************************************/
unsigned int SPI_RW(unsigned char data)
{
unsigned int i,temp=0;
for(i=0;i<8;i++) // output 8-bit
{
if(data & 0x80)
{
Hign_24L01_MOSI ; // output 'uchar', MSB to MOSI
}
else
{
Low_24L01_MOSI;
}
data = (data << 1); // shift next bit into MSB..
temp<<=1;
Hign_24L01_SCK ; // Set SCK high..
if(PINB&0x20)temp++; // capture current MISO bit,because the MISO bit is the fourth
Low_24L01_SCK; // ..then set SCK low again
}
return(temp); // return read uchar MISO vaule
}
//****************************************************************************************************
//*函数:uchar SPI_Read(uchar reg)
//*功能:NRF24L01的SPI时序
/****************************************************************************************************/
unsigned char SPI_Read(unsigned char reg)
{
unsigned char reg_val;
Low_24L01_CSN; // CSN low, initialize SPI communication...
SPI_RW(reg); // Select register to read from..
reg_val = SPI_RW(0XFF); // ..then read registervalue
Hign_24L01_CSN; // CSN high, terminate SPI communication
return(reg_val); // return register value
}
//****************************************************************************************************/
//*功能:NRF24L01读写寄存器函数
//****************************************************************************************************/
unsigned int SPI_RW_Reg(unsigned char reg, unsigned char value)
{
unsigned int status;
Low_24L01_CSN; // CSN low, init SPI transaction
status = SPI_RW(reg); // select register
SPI_RW(value); // ..and write value to it..
Hign_24L01_CSN; // CSN high again
return(status); // return nRF24L01 status uchar
}
//****************************************************************************************************/
//*函数:uint SPI_Read_Buf(uchar reg, uchar *pBuf, uchar uchars)
//*功能: 用于读数据,reg:为寄存器地址,pBuf:为待读出数据地址,uchars:读出数据的个数
//****************************************************************************************************/
unsigned char SPI_Read_Buf(unsigned char reg, unsigned char *pBuf, unsigned char uchars)
{
unsigned char status1,uchar_ctr;
Low_24L01_CSN; // Set CSN low, init SPI tranaction
status1 = SPI_RW(reg); // 发送寄存器值(位置),并读取状态值
for(uchar_ctr=0;uchar_ctr
pBuf[uchar_ctr] = SPI_RW(0xff); //
Hign_24L01_CSN;
return(status1); // 返回状态寄存器的值
}
//*********************************************************************************************************
//*函数:uint SPI_Write_Buf(uchar reg, uchar *pBuf, uchar uchars)
//*功能: 用于写数据:为寄存器地址,pBuf:为待写入数据地址,uchars:写入数据的个数
/*********************************************************************************************************/
unsigned int SPI_Write_Buf(unsigned char reg, unsigned char *pBuf, unsigned char uchars)
{
unsigned int status1,uchar_ctr;
Low_24L01_CSN; //SPI使能
status1 = SPI_RW(reg);
for(uchar_ctr=0; uchar_ctr
SPI_RW(*pBuf++);
Hign_24L01_CSN; //关闭SPI
return(status1); //
}
//****************************************************************************************************/
//*函数:void SetRX_Mode(void)
//*功能:数据接收配置
//****************************************************************************************************/
void SetRX_Mode(void)
{
Low_24L01_CE;
//SPI_RW_Reg(FLUSH_RX,0xff);
//SPI_RW_Reg(WRITE_REG + CONFIG, 0x0d);
SPI_RW_Reg(WRITE_REG + CONFIG, 0x0f);
Hign_24L01_CE;
Delay(5000);//注意不能太小 130us
}
//******************************************************************************************************/
//*函数:unsigned char nRF24L01_RxPacket(unsigned char* rx_buf)
//*功能:数据读取后放如rx_buf接收缓冲区中
/******************************************************************************************************/
unsigned char nRF24L01_RxPacket(unsigned char* rx_buf)
{
unsigned char revale=0;
sta=SPI_Read(STATUS); // 读取状态寄存其来判断数据接收状况
SPI_RW_Reg(WRITE_REG+STATUS,sta); //接收到数据后RX_DR,TX_DS,MAX_PT都置高为1,通过写1来清楚中断标志
if(sta&0x40) // 判断是否接收到数据
{
//Low_24L01_CE; //SPI使能
SPI_Read_Buf(RD_RX_PLOAD,rx_buf,TX_PLOAD_WIDTH);// read receive payload from RX_FIFO buffer
//Hign_24L01_CE;
SPI_RW_Reg(FLUSH_RX,0xff);
revale =1; //读取数据完成标志
}
return revale;
}
//***********************************************************************************************************
//*函数:void nRF24L01_TxPacket(char * tx_buf)
//*功能:发送 tx_buf中数据
/**********************************************************************************************************/
void nRF24L01_TxPacket(unsigned char * tx_buf)
{
Low_24L01_CE; //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); // 装载数据
SPI_RW_Reg(WRITE_REG + CONFIG, 0x0e); // IRQ收发完成中断响应,16位CRC,主发送
Hign_24L01_CE; //置高CE,激发数据发送
Delay(600);
}
//****************************************************************************************
//*NRF24L01初始化
//***************************************************************************************/
void init_NRF24L01(void)
{
Low_24L01_CE; // chip enable
Hign_24L01_CSN; // Spi disable
Low_24L01_SCK; // Spi clock line init high
//SPI_Write_Buf(WRITE_REG + TX_ADDR, TX_ADDRESS, TX_ADR_WIDTH); // 写本地地址
SPI_Write_Buf(WRITE_REG + RX_ADDR_P0, RX_ADDRESS, RX_ADR_WIDTH); // 写接收端地址
SPI_RW_Reg(WRITE_REG + EN_AA, 0x01); // 频道0自动 ACK应答允许
SPI_RW_Reg(WRITE_REG + EN_RXADDR, 0x01); // 允许接收地址只有频道0,如果需要多频道可以参考Page21
SPI_RW_Reg(WRITE_REG + RF_CH, 0); // 设置信道工作为2.4GHZ,收发必须一致
SPI_RW_Reg(WRITE_REG + RX_PW_P0, RX_PLOAD_WIDTH); //设置接收数据长度,本次设置为20字节
SPI_RW_Reg(WRITE_REG + RF_SETUP, 0x0f); //设置发射速率为1MHZ,发射功率为最大值0dB
//SPI_RW_Reg(WRITE_REG + CONFIG, 0x0f); // IRQ收发完成中断响应,16位CRC ,主接收
}
void UART_Send(unsigned char i)
{
while (UCSRA.5==0);// waiting for the buffer is empty
UDR=i; //PUT data into buffer,sends the data
}
void ini_devices(void)
{ //initial ports
DDRB = 0x07;
DDRC = 0xff;
DDRD = 0xc0;
SFIOR&=~(1<
//PORTB=0;
LED=1;
PORTD=0x00;
// USART intitial
UBRRH=0;
UBRRL=0x2F; //baud rate:9600
UCSRB=0x18; //enable transmitter and receiver
UCSRC=0x86; //set frame format:8data,1stop bit,enable parity
init_NRF24L01() ;
}
//******************************************************************************
void main(void)
{
ini_devices();
//******************************************************************************
RxBuf[1] = 0x00;
RxBuf[2] = 0x00;
//nRF24L01_TxPacket(TxBuf);
while(1)
{ SetRX_Mode();
RxBuf[1] = 0x00;
RxBuf[2] = 0x00;
nRF24L01_RxPacket(RxBuf);
// nRF24L01_TxPacket(TxBuf);
//SPI_RW_Reg(WRITE_REG + STATUS, 0xff);
// Delay(1000);
if(RxBuf[1]==1)
{
LED=1;
}
//Delay(10000);
RxBuf[1] = 0x00;
RxBuf[2] = 0x00;
LED=0;
}
}
0