1、#define RX_ADR_WIDTH 5 / 5 uints RX address width#define TX_PLOAD_WIDTH 32 / 32 uints TX payload#define RX_PLOAD_WIDTH 32 / 32 uints TX payloaduchar const TX_ADDRESSTX_ADR_WIDTH= 0x34,0x43,0x10,0x10,0x01; /本地地址uchar const RX_ADDRESSRX_ADR_WIDTH= 0x34,0x43,0x10,0x10,0x01; /接收地址uchar code Tx_BufTX_PLO
2、AD_WIDTH=0xff,0xee,0x11,0x22,0x33,0xaa,0xbb,0x11,0x22,0x33,0xaa,0xbb,0x11,0x22,0x33,0xaa,0xbb,0x11,0x22,0x33,0xaa,0xbb,0x11,0x22,0x33,0xaa,0xbb,0x11,0x22,0x33,0xee,0xff;/发送数据uchar Rx_BufRX_PLOAD_WIDTH;/接收数据/*NRF24L01寄存器指令*#define READ_REG 0x00 / 读寄存器指令#define WRITE_REG 0x20 / 写寄存器指令#define RD_RX_PLO
3、AD 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 / 配置收发状态,CRC校验模式以及收发状态响应方式#define EN_AA 0x01 / 自动应答功能设置#define EN_RXADDR 0x02 /
4、 可用信道设置#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 RX_ADDR_P0 0x0A / 频道0接收数据地址#define RX_ADDR_P1 0x0B / 频道1接收数据地址#define RX_AD
5、DR_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 / 接收频道1接收数据长度#define RX_PW_P2 0x13 / 接收频道2接收数据长度#define RX_PW_P3 0x14 / 接收频道3接收数据长度#de
6、fine RX_PW_P4 0x15 / 接收频道4接收数据长度#define RX_PW_P5 0x16 / 接收频道5接收数据长度#define FIFO_STATUS 0x17 / FIFO栈入栈出状态寄存器设置/*延时函数*/长延时void Delay(unsigned int s) unsigned int i,j;for(i=0;i1000;i+)for(j=0;js;j+);/短延时void delay_ms(unsigned int x) i=0; for(i=0;x;i+) j=108; while(j-); /*初始化5A spi*/void Init_SPI() SPDA
7、T=0; /初始化数据寄存器 SPSTAT=0XC0; /清除状态寄存器 SPCTL=0XD2;/设置为主机模式 主频不能超过2M/忽略SS 使能spi MSB SCLK空闲为0 第一个时钟边沿开始采集 spi通信的频率为CUP_CLK/16/SPDAT 读写一个字节/TxData:要写入的字节 /返回值:读取到的字节uchar SPI_ReadWriteByte(uchar TxData) SPDAT=TxData; /发送一个byte while(SPSTAT&0x80)=0); /清除状态寄存器 return SPDAT; /返回收到的数据 /读取SPI寄存器值/reg:要读的寄存器uc
8、har SPI_Read_Reg(uchar reg) uchar reg_val; CSN = 0; /使能SPI传输 SPI_ReadWriteByte(reg); /发送寄存器号 reg_val=SPI_ReadWriteByte(0xFF);/读取寄存器内容 CSN = 1; /禁止SPI传输 return(reg_val); /返回状态值 / 向寄存器REG写一个字节,同时返回状态字节 reg寄存器地址 value写入的数据uchar SPI_RW_Reg (uchar reg,uchar value) uchar status; CSN=0; status=SPI_ReadWrit
9、eByte(reg);/发送寄存器号 SPI_ReadWriteByte(value); /写入寄存器的值 CSN=1; return(status);/写一个数据包uchar SPI_Write_Buf(uchar reg, uchar *pBuf, uchar bytes) uchar status,byte_ctr; CSN = 0; for(byte_ctr=0; byte_ctrbytes; byte_ctr+) SPI_ReadWriteByte(*pBuf+); CSN = 1;/读一个数据包uchar SPI_Read_Buf(uchar reg, uchar *pBuf, u
10、char uchars) uchar status,uchar_ctr; status = SPI_ReadWriteByte(reg); for(uchar_ctr=0;uchar_ctruchars;uchar_ctr+) pBufuchar_ctr=SPI_ReadWriteByte(0xFF);/*发*送*模*式*代*码*/void TX_Mode(void) CE=0; SPI_RW_Reg(FLUSH_TX,0x00); SPI_Write_Buf(WRITE_REG + TX_ADDR, TX_ADDRESS, TX_ADR_WIDTH); / 写发送的地址 Writes TX_
11、Address to nRF24L01 SPI_Write_Buf(WRITE_REG + RX_ADDR_P0, TX_ADDRESS, TX_ADR_WIDTH); / 写接受的地址 RX_Addr0 same as TX_Adr for Auto.Ack SPI_RW_Reg(WRITE_REG + EN_AA, 0x01); /*使能自动应答 Enable Auto.Ack:Pipe0 SPI_RW_Reg(WRITE_REG + EN_RXADDR, 0x01); /使能通道0 Enable Pipe0 SPI_RW_Reg(WRITE_REG + SETUP_RETR, 0x1a)
12、; / *自动重发功能设置 500us + 86us, 10 retrans.1a SPI_RW_Reg(WRITE_REG + RF_CH, 40); /收发频率 SPI_RW_Reg(WRITE_REG + RF_SETUP, 0x07); / 发射速率、功耗功能设置 TX_PWR:0dBm, Datarate:1Mbps, LNA:HCURR SPI_RW_Reg(WRITE_REG + RX_PW_P0, (unsigned char)RX_PLOAD_WIDTH); /设置接收数据长度,本次设置为2字节 SPI_RW_Reg(WRITE_REG + CONFIG, 0x0e); CE
13、=1; delay_ms(100);void Transmit(unsigned char * tx_buf) /StandBy I模式 /装载接收端地址 /清除FIFO SPI_Write_Buf(WR_TX_PLOAD, tx_buf, TX_PLOAD_WIDTH); / 装载数据 / IRQ收发完成中断响应,16位CRC,主发送 /置高CE,激发数据发送 delay_ms(150);/*主函数*/uchar NRF24L01_Check(void) uchar buf5=0XA5,0XA5,0XA5,0XA5,0XA5; uchar i; SPI_Write_Buf(WRITE_REG
14、+TX_ADDR,buf,5);/写入5个字节的地址. SPI_Read_Buf(TX_ADDR,buf,5); /读出写入的地址 5;i+) if(bufi!=0XA5) break; if(i!=5) return 1;/检测24L01错误 return 0; /检测到24L01void main() uint i=0; Init_SPI(); while(NRF24L01_Check()/检测不到24L01 delay_ms(500); led=!led; TX_Mode(); while(1) Transmit(Tx_Buf); Delay(10); sta=SPI_Read_Reg(
15、READ_REG + STATUS); if(TX_DS) led=0; /8位LED显示当前STATUS状态 发送中断应使bit5 = 1 灯灭 Delay(100); SPI_RW_Reg(WRITE_REG + STATUS,sta); if(MAX_RT) /如果是发送超时 /发送超时时 8位LED灯 bit4 = 1 灯灭 Delay(150);/发送1#define TX_PLOAD_WIDTH 2 / 32 uints TX payload#define RX_PLOAD_WIDTH 2 / 32 uints TX payloaduchar const TX_ADDRESSTX_ADR_WIDTH= 0xc2,0x43,0x10,0x10,0xc2; /*本地地址uchar const RX_ADDRESSRX_ADR_WIDTH= 0xc2,0x43,0x10,0x10,0xc2; /*接收地址uchar Tx_BufTX_PLOAD_WIDTH=0x11,0x22;
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1