它的一些物理特性如作业频段、供电电压、数据传输速率就不具体介绍了,直接上代码。
1.首先是发送端:
// Define SPI pins#include#define uchar unsigned char/***************************************************/#define TX_ADR_WIDTH 5 // 5字节宽度的发送/接纳地址#define TX_PLOAD_WIDTH 4 // 数据通道有用数据宽度sbit LED = P2^1;sbit KEY1 = P3^1;sbit KEY2 = P3^2;uchar code TX_ADDRESS[TX_ADR_WIDTH] = {0x34,0x43,0x10,0x10,0x01}; // 界说一个静态发送地址uchar RX_BUF[TX_PLOAD_WIDTH];uchar TX_BUF[TX_PLOAD_WIDTH];uchar flag;uchar DATA = 0x01;uchar bdata sta;sbit RX_DR = sta^6;sbit TX_DS = sta^5;sbit MAX_RT = sta^4;sbit CE = P1^5;sbit CSN= P1^4;sbit SCK= P1^3;sbit MOSI= P1^2;sbit MISO= P1^1;sbit IRQ = P1^0;// 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 addressvoid blink(char i);/**************************************************函数: init_io()描绘:初始化IO/**************************************************/void init_io(void){CE = 0; // 待机CSN = 1; // SPI制止SCK = 0; // SPI时钟置低IRQ = 1; // 中止复位LED = 1; // 封闭指示灯}/**************************************************//**************************************************函数:delay_ms()描绘:推迟x毫秒/**************************************************/void delay_ms(uchar x){uchar i, j;i = 0;for(i=0; i 射频通道0x40SPI_RW_Reg(WRITE_REG + RX_PW_P0, TX_PLOAD_WIDTH); // 接纳通道0挑选和发送通道相同有用数据宽度SPI_RW_Reg(WRITE_REG + RF_SETUP, 0x07); // 数据传输率1Mbps,发射功率0dBm,低噪声放大器增益SPI_RW_Reg(WRITE_REG + CONFIG, 0x0f); // CRC使能,16位CRC校验,上电,接纳形式delay_ms(150);CE = 1; // 拉高CE发动接纳设备}/**************************************************//**************************************************函数:TX_Mode()描绘:这个函数设置nRF24L01为发送形式,(CE=1继续至少10us),130us后发动发射,数据发送结束后,发送模块主动转入接纳形式等候应对信号。/**************************************************/void TX_Mode(uchar * BUF){CE = 0;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); // 为了应对接纳设备,接纳通道0地址和发送地址相同SPI_Write_Buf(WR_TX_PLOAD, BUF, TX_PLOAD_WIDTH); // 写数据包到TX FIFOSPI_RW_Reg(WRITE_REG + EN_AA, 0x01); // 使能接纳通道0主动应对SPI_RW_Reg(WRITE_REG + EN_RXADDR, 0x01); // 使能接纳通道0SPI_RW_Reg(WRITE_REG + SETUP_RETR, 0x0a); // 主动重发延时等候250us+86us,主动重发10次SPI_RW_Reg(WRITE_REG + RF_CH, 40); // 挑选射频通道0x40SPI_RW_Reg(WRITE_REG + RF_SETUP, 0x07); // 数据传输率1Mbps,发射功率0dBm,低噪声放大器增益SPI_RW_Reg(WRITE_REG + CONFIG, 0x0e); // CRC使能,16位CRC校验,上电delay_ms(150);CE = 1;}/**************************************************//**************************************************函数:Check_ACK()描绘:查看接纳设备有无接纳到数据包,设定没有收到应对信号是否重发/**************************************************/uchar Check_ACK(bit clear){delay_ms(200);while(IRQ);sta = SPI_RW(NOP); // 回来状况寄存器if(TX_DS){blink(3);}//blink(5);if(MAX_RT)if(clear) // 是否铲除TX FIFO,没有铲除在复位MAX_RT中止标志后重发SPI_RW(FLUSH_TX);SPI_RW_Reg(WRITE_REG + STATUS, sta); // 铲除TX_DS或MAX_RT中止标志IRQ = 1;if(TX_DS)return(0x00);elsereturn(0xff);}/**************************************************//**************************************************函数:CheckButtons()描绘:查看按键是否按下,按下则发送一字节数据/**************************************************/void CheckButtons(){if(KEY1 == 0){delay_ms(20);if(KEY1 == 0){TX_BUF[0] = 1; // 数据送到缓存//TX_BUF[0] = 0xff; // 数据送到缓存TX_Mode(TX_BUF); // 把nRF24L01设置为发送形式并发送数据 //LED = ~DATA; // 数据送到LED显现Check_ACK(0); // 等候发送结束,铲除TX FIFOdelay_ms(250);delay_ms(250);}}if(KEY2 == 0){delay_ms(20);if(KEY2 == 0){TX_BUF[0] = 2; // 数据送到缓存//TX_BUF[0] = 0xff; // 数据送到缓存TX_Mode(TX_BUF); // 把nRF24L01设置为发送形式并发送数据 //LED = ~DATA; // 数据送到LED显现Check_ACK(0); // 等候发送结束,铲除TX FIFOdelay_ms(250);delay_ms(250);}}}/**************************************************/void blink(char i){while(i--){LED = 1;delay_ms(500);LED = 0;delay_ms(500);}}/**************************************************函数:main()描绘:主函数/**************************************************/void main(void){init_io(); // 初始化IOwhile(1){CheckButtons(); // 按键扫描}}
这段代码首要先看大局变量,经过对IO口的赋值(如按键、led、无线模块的端口CE/IRQ等)能够知道电路图的制作。
然后进入main函数,初始化后便是一段按键扫描函数,晒干依据界说的按键,触发后发送相应的数据,不必过分深究晒干对寄存器的装备是完结了什么意图。其间晒干的blink函数是用来判别TX_DS是否为真而业绩led灯的,这个标志位为真阐明数据发送成功。
2.然后是接纳端
// Define SPI pins#include#include #define uchar unsigned char#define uint unsigned int#define ulong unsigned long/***************************************************/#define TX_ADR_WIDTH 5 // 5字节宽度的发送/接纳地址#define TX_PLOAD_WIDTH 4 // 数据通道有用数据宽度sbit LED = P2^1;uchar code TX_ADDRESS[TX_ADR_WIDTH] = {0x34,0x43,0x10,0x10,0x01}; // 界说一个静态发送地址uchar RX_BUF[TX_PLOAD_WIDTH];uchar TX_BUF[TX_PLOAD_WIDTH];uchar flag;uchar DATA = 0x01;uchar bdata sta;sbit RX_DR = sta^6;sbit TX_DS = sta^5;sbit MAX_RT = sta^4;sbit CE = P1^5;sbit CSN= P1^4;sbit SCK= P1^3;sbit MOSI= P1^2;sbit MISO= P1^1;sbit IRQ = P1^0;// 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//--界说SPI要运用的 IO--//sbit MOSIO = P3^4;sbit R_CLK = P3^5;sbit S_CLK = P3^6;void blink(char i);//--大局函数声明--///**************************************************函数: init_io()描绘:初始化IO/**************************************************/void init_io(void){CE = 0; // 待机CSN = 1; // SPI制止SCK = 0; // SPI时钟置低IRQ = 1; // 中止复位LED = 1; // 封闭指示灯}/**************************************************//**************************************************函数:delay_ms()描绘:推迟x毫秒/**************************************************/void delay_ms(uchar x){uchar i, j;i = 0;for(i=0; i
经过main函数能够知道晒干经过对RX_BUF[0]值的判别做相应的LED业绩,业绩的次数不同。
假如要收发字符串直接往TX_BUF数组晒干寄存巨细为TX_PLOAD_WIDTH的字符,在RX_BUF就能够接纳到相应的数据了。
代码有删减过一点,不过经过用KEIL进行的编译调试,收发基本是没有问题的。。鄙人亲侧过。