1、sbit SCK =P12;sbit CE =P10;sbit CSN =P11;sbit IRQ =P15;/*按键*/sbit KEY1=P36;/sbit KEY2=P37;/*数码管位选*/sbit led3=P20;/sbit led2=P21;/sbit led1=P22;/sbit led0=P23;/*蜂明器*/sbit BELL=P34;/*数码管0-9编码*/uchar seg10=0xC0,0xCF,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90; /09段码/*NRF24L01*#define TX_ADR_WIDTH 5 / 5 uin
2、ts TX address width#define RX_ADR_WIDTH 5 / 5 uints RX address width#define TX_PLOAD_WIDTH 10 / 20 uints TX payload#define RX_PLOAD_WIDTH 10 / 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; /接收地址/*NRF2
3、4L01寄存器指令*#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)寄存器地址*#define CON
4、FIG 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 / 发送监测功能#define CD 0x09 /
5、地址检测#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 / 接收频道0接收数据长度#define RX_PW_P1 0x12
6、 / 接收频道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(unsigned char n);void init_NRF24L01(void);uint SPI_RW(uin
7、t 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_RxPacket(unsigned char* rx_buf);void nRF24L01_TxPacket(unsigned cha
8、r * tx_buf);/*长延时*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 enable CSN=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,
9、 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_Reg(WRITE_REG + RF_CH, 0);/ 设置信道工作为2.4GHZ,收发必须一致 SPI_RW_Reg(WRITE_REG + RX_PW_P0, RX_PLOAD_WIDTH);/设置接收数据长度,本次设置为32字节 SPI_RW_Reg(WRIT
10、E_REG + RF_SETUP, 0x07); /设置发射速率为1MHZ,发射功率为最大值0dB/*/*函数:uint SPI_RW(uintuchar)/*功能:NRF24L01的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 return(uchar); / return read uchar/*
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1