NRF24L01多路通讯调试成功的关键__附程序 下载本文

内容发布更新时间 : 2024/11/17 2:36:26星期一 下面是文章的全部内容请认真阅读。

NRF24L01多路通讯调试成功的关键

(附基于串口助手的无线通讯工具源代码)

本文档部分内容摘自网络,由于按照网上教程调试总不成功,特此分享自己的失败经验(红字加重)。 一、收发端共同的设置 1、设置信道工作频率(收发必须一致) 如:SPI_RW_Reg(WRITE_REG+RF_CH,40); 2、设置发射速率(2mbps或1mbps)和发射功率(收发必须一致);

如:SPI_RW_Reg(WRITE_REG+RF_SETUP,0x0f); //发射速率为2Mbps,发射功率 最大为0dB

二、接收端的设置(最关键)

1、设置频道0-5,自动ACK应答允许

如: SPI_RW_Reg(WRITE_REG+EN_AA,0x3f); 2、设置接收通道全部允许

如: SPI_RW_Reg(WRITE_REG+EN_RXADDR,0x3f); 3、向发送地址寄存器写入本地地址(5byte)

4、向各个频道的接收地址寄存器写入接收地址(调试成不成功的关键) 频道0:5个字节的地址

频道1:5个字节的地址(和频道0的地址必须不同)

频道2:1个字节的地址(为该通道发射机地址的最后一个字节·) 有一个配置为发射模式的24l01要通过该通道与接收机通信,发射机的本地地址为{0x37,0xa1,0xb3,0xc9,0xda};则接收机频道2的地址为(0x37) 频道3:1个字节的地址(同上) 频道4:1个字节的地址(同上) 频道5:1个字节的地址(同上)

5、向每个频道(用那个写那个,需要在上面配置允许通道接收和ack·)接收数据长度寄存器写入接收数据宽度(最快均为32)

频道n:SPI_RW_Reg(WRITE_REG + RX_PW_Pn, RX_PLOAD_WIDTH); 如:

频道5:SPI_RW_Reg(WRITE_REG + RX_PW_P5, RX_PLOAD_WIDTH); 6、配置为接收模式 如:

SPI_RW_Reg(WRITE_REG+CONFIG,0x0f);

下面附上我的程序

/***************************头文件******************************/

#ifndef __NRF24L01_H__ #define __NRF24L01_H__ sbit CE = P1^2; sbit CSN = P1^3;

sbit IRQ = P1^4; sbit MOSI = P1^5; sbit MISO = P1^6; sbit SCK = P1^7;

void NRF24L01_init(); //初始化模块

ucharTX_packet(uchar *tx_buf); //返回值判断是否成功 ucharRX_packet(uchar *rx_buf); //返回值判断是否成功 void TX_MODE(); //发送模式 void RX_MODE(); //接收模式

//****************************************************************// // SPI(nRF24L01) commands

#define READ_REG 0x00 // Define read command to register #define WRITE_REG 0x20 // Define write command to register #define RD_RX_PLOAD 0x61 // Define RX payload register address #define WR_TX_PLOAD 0xA0 // Define TX payload register address #define FLUSH_TX 0xE1 // Define flush TX register command #define FLUSH_RX 0xE2 // Define flush RX register command

#define REUSE_TX_PL 0xE3 // Define reuse TX payload register command

#define NOP 0xFF // Define No Operation, might be used to read status register

//***************************************************// // SPI(nRF24L01) registers(addresses)

#define CONFIG 0x00 // 'Config' register address

#define EN_AA 0x01 // 'Enable Auto Acknowledgment' register address #define EN_RXADDR 0x02 // 'Enabled RX addresses' register address #define SETUP_AW 0x03 // 'Setup address width' register address #define SETUP_RETR 0x04 // 'Setup Auto. Retrans' register address #define RF_CH 0x05 // 'RF channel' register address #define RF_SETUP 0x06 // 'RF setup' register address #define STATUS 0x07 // 'Status' register address #define OBSERVE_TX 0x08 // 'Observe TX' register address #define CD 0x09 // 'Carrier Detect' register address #define RX_ADDR_P0 0x0A // 'RX address pipe0' register address #define RX_ADDR_P1 0x0B // 'RX address pipe1' register address #define RX_ADDR_P2 0x0C // 'RX address pipe2' register address #define RX_ADDR_P3 0x0D // 'RX address pipe3' register address #define RX_ADDR_P4 0x0E // 'RX address pipe4' register address #define RX_ADDR_P5 0x0F // 'RX address pipe5' register address #define TX_ADDR 0x10 // 'TX address' register address

#define RX_PW_P0 0x11 // 'RX payload width, pipe0' register address #define RX_PW_P1 0x12 // 'RX payload width, pipe1' register address #define RX_PW_P2 0x13 // 'RX payload width, pipe2' register address

#define RX_PW_P3 0x14 // 'RX payload width, pipe3' register address #define RX_PW_P4 0x15 // 'RX payload width, pipe4' register address #define RX_PW_P5 0x16 // 'RX payload width, pipe5' register address #define FIFO_STATUS 0x17 // 'FIFO Status Register' register address

//***************************************************************// #endif

/*********************************程序***********************************/ #include #include\#include\#include\

uchar code TX_ADDRESS[5] = {0,1,1,1,1}; uchar code RX_ADDRESS_P0[5] = {0,1,1,1,1}; uchar code RX_ADDRESS_P1[5] = {1,1,1,1,1}; uchar code RX_ADDRESS_P2[1] = {2}; uchar code RX_ADDRESS_P3[1] = {3}; uchar code RX_ADDRESS_P4[1] = {4}; uchar code RX_ADDRESS_P5[1] = {5}; uchar code Data_width = 1; uchar code Data_rt = 15;

uchar SPI_RW(uchardat) // SPI读写指令 {

uchar i;

for(i=0;i<8;i++) {

SCK = 0;

MOSI = (dat&0x80); dat<<= 1; SCK = 1;