|
|

楼主 |
发表于 2013-10-21 14:55:05
|
显示全部楼层
发送端程序如下:
#include "NRF24L01.h"
#define TX_ADR_WIDTH 5 // 定义发送寄存器地址宽度
#define TX_PLOAD_WIDTH 32 // 定义发送数据宽度
unsigned char TX_ADDRESS[TX_ADR_WIDTH] =
{
0x34,0x43,0x10,0x10,0x01//定义发送的寄存器地址
};
unsigned char txBuf[TX_PLOAD_WIDTH] =
{
0x11,0xa1,0x12 //定义发送的数据
};
//********************NRF24L01引脚配置***********************
#define CE 9
#define CSN 10
#define SCK 13
#define MOSI 11
#define MISO 12
#define IRQ 8
//********************初始化********************************
void setup()
{
pinMode(CE, OUTPUT);
pinMode(SCK, OUTPUT);
pinMode(CSN, OUTPUT);
pinMode(MOSI, OUTPUT);
pinMode(MISO, INPUT);
pinMode(IRQ, INPUT);
NRF24L01_init();
Serial.begin(9600);
}
//********************执行程序**************************
void loop()
{
SPI_RW_Reg(WRITE_REG+STATUS,0xFF); //状态寄存器清零
unsigned char status=SPI_Read(STATUS);//读状态寄存器值(查看状态寄存器是否清零)
Serial.print("restart:");
Serial.println(status,HEX);//测试值为:E/FF
delay(1000);
int i=digitalRead(IRQ);//读数字IRQ引脚(查看数据是否发送完成,TX_DS是否产生中断,IQO置1)
Serial.print("re IRQ:");
Serial.println(i);//测试值为:1
SPI_Write_Buf(WR_TX_PLOAD,txBuf,TX_PLOAD_WIDTH);//发送数据(测试发送是否正常,未设置CONFIG)
status=SPI_Read(FIFO_STATUS);//读FIFO状态寄存器值(查看TX_EMPTY寄存器是否为0)
Serial.print("Reload over:");
Serial.println(status,HEX);//测试值为:1/FF
delay(2000);
nRF24L01_TxPacket(txBuf);//发送数据
status=SPI_Read(FIFO_STATUS);//读FIFO状态寄存器值(查看TX_EMPTY寄存器是否为0)
Serial.print("fashe over FIFO STATUS=:");
Serial.println(status,HEX);//测试值为:11/FF
i=digitalRead(IRQ);//再次读数字IRQ引脚(查看数据是否发送完成,TX_DS是否产生中断,IQO置1)
Serial.print("over IRQ:");
Serial.println(i);//测试值为:1
status=SPI_Read(FIFO_STATUS);
status=SPI_Read(STATUS);//再次读状态寄存器值(查看状态寄存器值,TX_DS是否产生中断,数据发送完成)
Serial.print("overSTATUS:");
Serial.println(status,HEX);//测试值为:2E/FF
delay(2000);
}
//********************NRF24L01初始化*************************
void NRF24L01_init(void)
{
delay(1);
digitalWrite(CE, 0);//芯片运行禁止
digitalWrite(CSN,1);//寄存器读写禁止
digitalWrite(SCK,0);//时钟初始状态
digitalWrite(IRQ,1);//中断复位
SPI_Write_Buf(WRITE_REG + TX_ADDR, TX_ADDRESS, TX_ADR_WIDTH); // 写入发送端寄存器地址
SPI_Write_Buf(WRITE_REG + RX_ADDR_P0, TX_ADDRESS, TX_ADR_WIDTH); // 写入接收端寄存器地址
SPI_RW_Reg(WRITE_REG + EN_AA, 0x00); // 数据通道0自动ACK应答禁止(一般为允许)
SPI_RW_Reg(WRITE_REG + EN_RXADDR, 0x01); // 接收数据通道0允许
SPI_RW_Reg(WRITE_REG + SETUP_RETR, 0x00); // 自动重发禁止
SPI_RW_Reg(WRITE_REG + RF_CH, 40); // 设置通道工作为2.4GHZ,收发必须一致
SPI_RW_Reg(WRITE_REG + RF_SETUP, 0x07); // 设置发射速率为1MHZ,发射功率为最大值0dB
//SPI_RW_Reg(WRITE_REG + CONFIG, 0x0e); // IRQ收发完成中断响应,16位CRC,发射模式
}
//********************发送tx_buf中的数据*************************
void nRF24L01_TxPacket(unsigned char * tx_buf)
{
digitalWrite(CE,0); //StandBy I模式
SPI_Write_Buf(WR_TX_PLOAD, tx_buf, TX_PLOAD_WIDTH); // 装载发送数据
SPI_RW_Reg(WRITE_REG + CONFIG, 0x0e); // IRQ收发完成中断响应,16位CRC,发射模式(PRIM_RX=0,
digitalWrite(CE,1); //置高CE,激发数据发送
delay(1);
}
/**************************************************
* Function: SPI_RW();
* 模拟SPI写时序
* Description:
* Writes one unsigned char to nRF24L01, and return the unsigned char read
* from nRF24L01 during write, according to SPI protocol
**************************************************/
unsigned char SPI_RW(unsigned char Byte)
{
unsigned char i;
for(i=0;i<8;i++) // output 8-bit
{
if(Byte&0x80)
{
digitalWrite(MOSI, 1);
}
else
{
digitalWrite(MOSI, 0);
}
digitalWrite(SCK, 1);
Byte <<= 1; // shift next bit into MSB..
if(digitalRead(MISO) == 1)
{
Byte |= 1; // capture current MISO bit
}
digitalWrite(SCK, 0);
}
return(Byte); // return read unsigned char
}
/**************************************************/
/**************************************************
* Function: SPI_RW_Reg();
* 读写寄存器
* Description:
* Writes value 'value' to register 'reg'
/**************************************************/
unsigned char SPI_RW_Reg(unsigned char reg, unsigned char value)
{
unsigned char status;
digitalWrite(CSN, 0); // CSN low, init SPI transaction
status = SPI_RW(reg); // select register
SPI_RW(value); // ..and write value to it..
digitalWrite(CSN, 1); // CSN high again
return(status); // return nRF24L01 status unsigned char
}
/**************************************************/
/**************************************************
* Function: SPI_Read();
* 读寄存器
* Description:
* Read one unsigned char from nRF24L01 register, 'reg'
/**************************************************/
unsigned char SPI_Read(unsigned char reg)
{
unsigned char reg_val;
digitalWrite(CSN, 0); // CSN low, initialize SPI communication...
SPI_RW(reg); // Select register to read from..
reg_val = SPI_RW(0); // ..then read register value
digitalWrite(CSN, 1); // CSN high, terminate SPI communication
return(reg_val); // return register value
}
/**************************************************/
/**************************************************
* Function: SPI_Read_Buf();
* 读数据
* Description:
* Reads 'unsigned chars' #of unsigned chars from register 'reg'
* Typically used to read RX payload, Rx/Tx address
/**************************************************/
unsigned char SPI_Read_Buf(unsigned char reg, unsigned char *pBuf, unsigned char bytes)
{
unsigned char status,i;
digitalWrite(CSN, 0); // Set CSN low, init SPI tranaction
status = SPI_RW(reg); // Select register to write to and read status unsigned char
for(i=0;i<bytes;i++)
{
pBuf = SPI_RW(0); // Perform SPI_RW to read unsigned char from nRF24L01
}
digitalWrite(CSN, 1); // Set CSN high again
return(status); // return nRF24L01 status unsigned char
}
/**************************************************/
/**************************************************
* Function: SPI_Write_Buf();
* 写数据
* Description:
* Writes contents of buffer '*pBuf' to nRF24L01
* Typically used to write TX payload, Rx/Tx address
/**************************************************/
unsigned char SPI_Write_Buf(unsigned char reg, unsigned char *pBuf, unsigned char bytes)
{
unsigned char status,i;
digitalWrite(CSN, 0); // Set CSN low, init SPI tranaction
status = SPI_RW(reg); // Select register to write to and read status unsigned char
for(i=0;i<bytes; i++) // then write all unsigned char in buffer(*pBuf)
{
SPI_RW(*pBuf++);
}
digitalWrite(CSN, 1); // Set CSN high again
return(status); // return nRF24L01 status unsigned char
}
|
|