收藏 分享(赏)

NRF2401详细注释编程程序.doc

上传人:精品资料 文档编号:11001610 上传时间:2020-01-30 格式:DOC 页数:11 大小:103KB
下载 相关 举报
NRF2401详细注释编程程序.doc_第1页
第1页 / 共11页
NRF2401详细注释编程程序.doc_第2页
第2页 / 共11页
NRF2401详细注释编程程序.doc_第3页
第3页 / 共11页
NRF2401详细注释编程程序.doc_第4页
第4页 / 共11页
NRF2401详细注释编程程序.doc_第5页
第5页 / 共11页
点击查看更多>>
资源描述

1、#include #include typedef unsigned char uchar;typedef unsigned char uint;/*NRF24L01端口定义*sbit MISO =P15;sbit MOSI =P14;sbit SCK =P13;sbit CE =P11;sbit CSN =P12;sbit IRQ =P16;/*按键*sbit KEY1=P26;sbit KEY2=P25;/*数码管位选*sbit led1=P24;sbit led2=P35;/*NRF24L01*#define TX_ADR_WIDTH 5 / 5 uints TX address wid

2、th#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 payloaduint const TX_ADDRESSTX_ADR_WIDTH= 0x34,0x43,0x10,0x10,0x01; /本地地址uint const RX_ADDRESSRX_ADR_WIDTH= 0x34,0x43,0x10,0x10,0x01; /接收地址/*NRF24L01寄存器指令*#define

3、 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)寄存器地址*#define CONFIG 0x00 / 配置收发

4、状态,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 R

5、X_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接收数据

6、长度#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(unsigned char n);void init_NRF24L01(void);uint SPI_RW(uint uchar);uc

7、har 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_RxPacket(unsigned char* rx_buf);void nRF24L01_TxPacket(unsigned char * tx_buf)

8、;/*长延时*void Delay(unsigned int s)unsigned int i;for(i=0; i0;n-)_nop_();/*/*NRF24L01初始化/*/void init_NRF24L01(void)inerDelay_us(100);CE=0; / chip enableCSN=1; / Spi disable SCK=0; / SPI_Write_Buf(WRITE_REG + TX_ADDR, TX_ADDRESS, TX_ADR_WIDTH); / 写本地地址 SPI_Write_Buf(WRITE_REG + RX_ADDR_P0, RX_ADDRESS,

9、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); /设置接收数据长度,本次设置为32字节SPI_RW_Reg(WRITE_REG + R

10、F_SETUP, 0x07); /设置发射速率为1MHZ,发射功率为最大值0dB/*/*函数:uint SPI_RW(uint uchar)/*功能:NRF24L01的 SPI 写时序/*/uint SPI_RW(uint uchar)uint bit_ctr;for(bit_ctr=0;bit_ctr8;bit_ctr+) / output 8-bitMOSI = (uchar / output uchar, MSB to MOSIuchar = (uchar 1); / shift next bit into MSBSCK = 1; / Set SCK highuchar |= MISO;

11、 / capture current MISO bitSCK = 0; / then set SCK low againreturn(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 fromreg_val = SPI_RW(0)

12、; / then read registervalueCSN = 1; / CSN high, terminate SPI communicationreturn(reg_val); / return register value/*/*功能:NRF24L01读写寄存器函数/*/uint SPI_RW_Reg(uchar reg, uchar value)uint status;CSN = 0; / CSN low, init SPI transactionstatus = SPI_RW(reg); / select registerSPI_RW(value); / and write val

13、ue to itCSN = 1; / CSN high againreturn(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, ini

14、t SPI tranactionstatus = SPI_RW(reg); / Select register to write to and read status ucharfor(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)/*功能: 用于写数据:为寄存器地址,pB

15、uf:为待写入数据地址, uchars:写入数据的个数/*/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; /关闭 SPIreturn(status); / /*/*函数:void SetRX_Mode(void)/*功能:数据接收配置 /*/void SetRX_Mod

16、e(void)CE=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); / 读取状态寄存其

17、来判断数据接收状况if(RX_DR) / 判断是否接收到数据CE = 0; /SPI 使能SPI_Read_Buf(RD_RX_PLOAD,rx_buf,TX_PLOAD_WIDTH);/ read receive payload from RX_FIFO bufferrevale =1; /读取数据完成标志SPI_RW_Reg(WRITE_REG+STATUS,sta); /接收到数据后 RX_DR,TX_DS,MAX_PT都置高为1,通过写1 来清楚中断标志return revale;/*/*函数:void nRF24L01_TxPacket(unsigned char * tx_buf)

18、/*功能:发送 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位CRC,主发送CE=1; /置高 CE,激发数据

19、发送inerDelay_us(10);/*主函数*void main(void)unsigned char tf =0;unsigned char TxBuf20=0; / unsigned char RxBuf20=0; init_NRF24L01() ;led1=1;led2=1;P0=0x00;TxBuf1 = 1 ;TxBuf2 = 1 ;nRF24L01_TxPacket(TxBuf); / Transmit Tx buffer dataDelay(6000);P0=0xBF;while(1)if(KEY1 =0 ) TxBuf1 = 1 ;tf = 1 ; led1=0;Delay

20、(120);led1=1;Delay(120);if(KEY2 =0 )TxBuf2 =1 ;tf = 1 ;led2=0;Delay(120); led2=1;Delay(120);if (tf=1) nRF24L01_TxPacket(TxBuf); / Transmit Tx buffer dataTxBuf1 = 0x00;TxBuf2 = 0x00;tf=0;Delay(1000); SetRX_Mode();RxBuf1 = 0x00;RxBuf2 = 0x00;Delay(1000);nRF24L01_RxPacket(RxBuf);if(RxBuf1|RxBuf2) if( RxBuf1=1) led1=0;if( RxBuf2=1)led2=0;Delay(6000); /old is 1000RxBuf1 = 0x00;RxBuf2 = 0x00;led1=1;led2=1;

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 企业管理 > 管理学资料

本站链接:文库   一言   我酷   合作


客服QQ:2549714901微博号:道客多多官方知乎号:道客多多

经营许可证编号: 粤ICP备2021046453号世界地图

道客多多©版权所有2020-2025营业执照举报