《nrf24l01发送程序》由会员分享,可在线阅读,更多相关《nrf24l01发送程序(8页珍藏版)》请在金锄头文库上搜索。
1、 #include #include typedef unsigned char uchar; typedef unsigned char uint; /*IO端口定义* sbit MISO =P12; sbit MOSI =P32; sbit SCK =P16; sbit CE =P15; sbit CSN =P17; sbit IRQ =P13; /*数码管0-9编码* uchar seg10=0xC0,0xCF,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90; /09段码 uchar TxBuf32= 0x01,0x02,0x03,0x4,0x05,0x0
2、6,0x07,0x08, 0x09,0x10,0x11,0x12,0x13,0x14,0x15,0x16, 0x17,0x18,0x19,0x20,0x21,0x22,0x23,0x24, 0x25,0x26,0x27,0x28,0x29,0x30,0x31,0x32, ; / /*按键* sbit KEY1=P36; sbit KEY2=P37; /*数码管位选* sbit led1=P21; sbit led0=P20; sbit led2=P22; sbit led3=P23; /*NRF24L01* #define TX_ADR_WIDTH 5 / 5 uints TX address
3、 width #define RX_ADR_WIDTH 5 / 5 uints RX address width #define TX_PLOAD_WIDTH 32 / 20 uints TX payload #define RX_PLOAD_WIDTH 32 / 20 uints TX payload uint const TX_ADDRESSTX_ADR_WIDTH= 0x34,0x43,0x10,0x10,0x01; /本地地址 uint const RX_ADDRESSRX_ADR_WIDTH= 0x34,0x43,0x10,0x10,0x01; /接收地址 /*NRF24L01寄存器
4、指令* #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 NOP 0xFF / 保留 /*SPI(nRF24L01)寄存器地址* #defin
5、e 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 / 发送监测功能 #de
6、fine 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
7、 / 接收频道 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 inerDelay_us(
8、unsigned char n); void init_NRF24L01(void); uint SPI_RW(uint uchar); uchar SPI_Read(uchar reg); void SetRX_Mode(void); uint SPI_RW_Reg(uchar reg, uchar value); uint SPI_Read_Buf(uchar reg, uchar *pBuf, uchar uchars); uint SPI_Write_Buf(uchar reg, uchar *pBuf, uchar uchars); unsigned char nRF24L01_Rx
9、Packet(unsigned char* rx_buf); void nRF24L01_TxPacket(unsigned char * tx_buf); /*长延时* void Delay(unsigned int s) unsigned int i; for(i=0; is; i+); for(i=0; i0;n-) _nop_(); /* /*NRF24L01 初始化 /*/ void init_NRF24L01(void) inerDelay_us(100); CE=0; / chip enable CSN=1; / Spi disable SCK=0; / Spi clock li
10、ne 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_R
11、eg(WRITE_REG + RF_CH, 0); / 设置信道工作为 2.4GHZ, 收发必须一致 SPI_RW_Reg(WRITE_REG + RX_PW_P0, RX_PLOAD_WIDTH); /设置接收数据长度,本次设置为 32 字节 SPI_RW_Reg(WRITE_REG + RF_SETUP, 0x07); /设置发射速率为 1MHZ,发射功率为最大值 0dB SPI_RW_Reg(WRITE_REG + CONFIG, 0x0e); / IRQ 收发完成中断响应,16位 CRC,主发送 /* /*函数:uint SPI_RW(uint uchar) /*功能:NRF24L01
12、 的 SPI 写时序 /*/ uint SPI_RW(uint uchar) uint bit_ctr; for(bit_ctr=0;bit_ctr8;bit_ctr+) / output 8-bit MOSI = (uchar & 0x80); / output uchar, MSB to MOSI uchar = (uchar 1); / shift next bit into MSB. SCK = 1; / Set SCK high. uchar |= MISO; / capture current MISO bit SCK = 0; / .then set SCK low again
13、return(uchar); / return read uchar /* /*函数:uchar SPI_Read(uchar reg) /*功能:NRF24L01 的 SPI 时序 /*/ uchar SPI_Read(uchar reg) uchar reg_val; CSN = 0; / CSN low, initialize SPI communication. SPI_RW(reg); / Select register to read from. reg_val = SPI_RW(0); / .then read registervalue CSN = 1; / CSN high,
14、 terminate SPI communication return(reg_val); / return register value /*/ /*功能:NRF24L01 读写寄存器函数 /*/ uint SPI_RW_Reg(uchar reg, uchar value) uint status; CSN = 0; / CSN low, init SPI transaction status = SPI_RW(reg); / select register SPI_RW(value); / .and write value to it. CSN = 1; / CSN high again
15、 return(status); / return nRF24L01 status uchar /*/ /*函数:uint SPI_Read_Buf(uchar reg, uchar *pBuf, uchar uchars) /*功能: 用于读数据,reg:为寄存器地址,pBuf:为待读出数据地址,uchars:读出数据的个数 /*/ uint SPI_Read_Buf(uchar reg, uchar *pBuf, uchar uchars) uint status,uchar_ctr; CSN = 0; / Set CSN low, init SPI tranaction status =
16、 SPI_RW(reg); / Select register to write to and read status uchar for(uchar_ctr=0;uchar_ctruchars;uchar_ctr+) pBufuchar_ctr = SPI_RW(0); / CSN = 1; return(status); / return nRF24L01 status uchar /* /*函数:uint SPI_Write_Buf(uchar reg, uchar *pBuf, uchar uchars) /*功能: 用于写数据:为寄存器地址,pBuf:为待写入数据地址,uchars:
17、写入数据的个数 /*/ uint SPI_Write_Buf(uchar reg, uchar *pBuf, uchar uchars) uint status,uchar_ctr; CSN = 0; /SPI 使能 status = SPI_RW(reg); for(uchar_ctr=0; uchar_ctruchars; uchar_ctr+) / SPI_RW(*pBuf+); CSN = 1; /关闭 SPI return(status); / /*/ /*函数:void SetRX_Mode(void) /*功能:数据接收配置 /*/ void SetRX_Mode(void) C
18、E=0; SPI_RW_Reg(WRITE_REG + CONFIG, 0x0f); / IRQ 收发完成中断响应,16位 CRC ,主接收 CE = 1; inerDelay_us(130); /*/ /*函数: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); / 读取状态
19、寄存其来判断数据接收状况 if(RX_DR) / 判断是否接收到数据 CE = 0; /SPI 使能 SPI_Read_Buf(RD_RX_PLOAD,rx_buf,TX_PLOAD_WIDTH);/ read receive payload from RX_FIFO buffer revale =1; /读取数据完成标志 SPI_RW_Reg(WRITE_REG+STATUS,sta); /接收到数据后 RX_DR,TX_DS,MAX_PT都置高为 1,通过写 1 来清楚中断标志 return revale; /* /*函数:void nRF24L01_TxPacket(unsigned c
20、har * tx_buf) /*功能:发送 tx_buf 中数据 /*/ void nRF24L01_TxPacket(unsigned char * tx_buf) CE=0; /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位 C
21、RC,主发送 CE=1; /置高 CE,激发数据发送 inerDelay_us(10); /*主函数* void main(void) uchar temp =0; init_NRF24L01() ; led0=0;led1=0;led2=0;led3=0; P0=0x00; nRF24L01_TxPacket(TxBuf); / Transmit Tx buffer data Delay(6000); P0=0xBF; while(1) if(temp4) switch(temp) case 1: P0= 0xBF; break; case 2: P0= 0xf7; break; case 3: P0= 0xFE; break; default: break; if(temp=3) temp=0; nRF24L01_TxPacket(TxBuf); / Transmit Tx buffer data Delay(20000); SPI_RW_Reg(WRITE_REG+STATUS,0XFF); temp+;