完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
[C] 纯文本查看 复制代码 NRF24L01的数据手册不停翻看了好多了,确实有点不行了,想请人帮帮忙,谢谢大家 [C] 纯文本查看 复制代码 [C] 纯文本查看 复制代码 [C] 纯文本查看 复制代码 #include "NRF2401.h"const unsigned char TX_ADDRESS[TX_ADR_WIDTH] = {0xe7,0xe7,0xe7,0xe7,0xe7};//本地地址const unsigned char RX_ADDRESS0[RX_ADR_WIDTH]= {0xe7,0xe7,0xe7,0xe7,0xe7};//接收地址const unsigned char RX_ADDRESS1[RX_ADR_WIDTH]= {0xc2,0xc2,0xc2,0xc2,0xc2};//接收地址const unsigned char RX_ADDRESS2[RX_ADR_WIDTH]= {0xc2,0xc2,0xc2,0xc2,0xc3};//接收地址const unsigned char RX_ADDRESS3[RX_ADR_WIDTH]= {0xc2,0xc2,0xc2,0xc2,0xc4};//接收地址const unsigned char RX_ADDRESS4[RX_ADR_WIDTH]= {0xc2,0xc2,0xc2,0xc2,0xc5};//接收地址const unsigned char RX_ADDRESS5[RX_ADR_WIDTH]= {0xc2,0xc2,0xc2,0xc2,0xc6};//接收地址unsigned int sta; //状态标志/************************************NRF24L01初始化************************************/void Set_TxMode(void){ DelayMs(5); CECLR; // chip enable// CSNSET; // Spi disable SCKCLR; SPI_Write_Buf(WRITE_REG + TX_ADDR, TX_ADDRESS, RX_ADR_WIDTH); //写接收端地址SPI_Write_Buf(WRITE_REG+RX_ADDR_P0,RX_ADDRESS0,RX_ADR_WIDTH);SPI_RW_Reg(WRITE_REG + CONFIG, 0x0e); // IRQ收发完成中断响应,16位CRC,主发送SPI_RW_Reg(WRITE_REG + EN_AA, 0x01); //频道0自动,ACK应答允许(应该需要关闭自动应答)SPI_RW_Reg(WRITE_REG + EN_RXADDR, 0x01); //SPI_RW_Reg(WRITE_REG + SETUP_AW, 0x03); //收发地址宽度5字节(不知道写不写)SPI_RW_Reg(WRITE_REG + RF_CH, 0x40); //设置信道工作为2.4GHZ,收发必须一致SPI_RW_Reg(WRITE_REG + RX_PW_P0, RX_PLOAD_WIDTH); //设置接收数据长度,本次设置为32字节SPI_RW_Reg(WRITE_REG + RF_SETUP, 0x07); //设置发射速率为1MHZ,发射功率为最大值0dB}/****************************************************************************************************函数:void SetRX_Mode(void)功能:数据接收配置 ****************************************************************************************************/void Set_RxMode(void){ [C] 纯文本查看 复制代码 tica;line-height:1.5;">//延时至少10us [C] 纯文本查看 复制代码 DelayMs(5); [C] 纯文本查看 复制代码 CECLR;SCKCLR;SPI_Write_Buf(WRITE_REG + RX_ADDR_P0, RX_ADDRESS0, TX_ADR_WIDTH); //写本地地址SPI_RW_Reg(WRITE_REG + CONFIG, 0x0f); SPI_RW_Reg(WRITE_REG + EN_AA, 0x01); SPI_RW_Reg(WRITE_REG + EN_RXADDR, 0x01); //SPI_RW_Reg(WRITE_REG + SETUP_AW, 0x03); //看好多都没设置 SPI_RW_Reg(WRITE_REG + RF_CH, 0x40); SPI_RW_Reg(WRITE_REG + RX_PW_P0, RX_PLOAD_WIDTH); SPI_RW_Reg(WRITE_REG + RF_SETUP, 0x07); //此处CE可能需要拉高,并延时}/**********************************NRF24L01的SPI写时序**********************************/unsigned int SPI_RW(unsigned int uchar){unsigned int bit_num; for(bit_num=0;bit_num<8;bit_num++) // output 8-bit {if(uchar & 0x80)MOSISET;else MOSICLR;// output 'uchar', MSB to MOSIuchar = (uchar << 1); // shift next bit into MSB..SCKSET; // SCK置高,理论上需延时40nsDelayUs(1);uchar |= ((GPIOE_IDR & 0x0040)>>6); //uchar已经左移,故最后一位位0,和0或获得MISO状态,且PE6为输入状态SCKCLR; // 置低,不解释DelayUs(1); } return uchar; // 返回MISO线状态}/****************************************************************************************************函数:uchar SPI_Read(uchar reg)功能:NRF24L01的SPI时序****************************************************************************************************/unsigned char SPI_Read(unsigned char reg){unsigned char reg_val;CSNSET; // 置低,则可以开始读写操作CSNCLR;SPI_RW(reg); // CSN置低后,先给出指令,即寄存器地址reg_val = SPI_RW(0); // ..then read registervalueCSNSET; // 置高,读写结束return reg_val; //return register value}/***************************************************************************************************功能:NRF24L01读写寄存器函数 (感觉应该是写)****************************************************************************************************/unsigned int SPI_RW_Reg(unsigned char reg, unsigned char value){unsigned int status;CSNSET; // CSN low, init SPI transactionCSNCLR;status = SPI_RW(reg); //给出指令,返回状态寄存器地址SPI_RW(value); // ..and write value to it..CSNSET; // CSN high againreturn status; // return nRF24L01 status uchar}/***************************************************************************************************函数:uint SPI_Read_Buf(uchar reg, uchar *pBuf, uchar uchars)功能: 用于读数据,reg:为寄存器地址,pBuf:为待读出数据地址,uchars:读出数据的个数****************************************************************************************************/unsigned int SPI_Read_Buf(unsigned char reg, unsigned char *pBuf, unsigned char uchars){unsigned int status,uchar_ctr;CSNSET; // Set CSN low, init SPI tranactionCSNCLR;status = SPI_RW(reg); // Select register to write to and read status ucharfor(uchar_ctr=0;uchar_ctr 这粘上主函数 [C] 纯文本查看 复制代码 int main(){ RCC_HSE_ON();RCC_APB2ENR=0xFFFFFFFF;GPIOA_Init();GPIOB_Init();GPIOC_Init();GPIOD_Init();GPIOE_Init();LCD_Init();//以上的初始化都没问题Set_TxMode();while(1){TxBuf[0]=1;nRF24L01_TxPacket(TxBuf);LCD_WriteString(0,0,"TxBuf[0]="); //在LCD上显示,TurnChar是我写的一个把整数转化成字符串以便显示的函数LCD_WriteString(0,1,"TxBuf[1]=");LCD_WriteString(55,0,TurnChar(TxBuf[0]));LCD_WriteString(55,1,TurnChar(TxBuf[1]));//LCD_WriteString(0,2,TurnChar(SPI_Read(WR_TX_PLOAD)));LCD_WriteString(0,3,"STATUS=");//SPI_RW_Reg(WRITE_REG+STATUS,0x70);LCD_WriteString(43,3,TurnChar(SPI_Read(STATUS)));DelayMs(1000);}//***********************************************************************************************/*Set_RxMode();while(1){nRF24L01_RxPacket(RxBuf);LCD_WriteString(0,0,"RxBuf[0]=");LCD_WriteString(0,1,"RxBuf[1]=");LCD_WriteString(55,0,TurnChar(RxBuf[0]));LCD_WriteString(55,1,TurnChar(RxBuf[1]));LCD_WriteString(0,3,TurnChar(SPI_Read(STATUS)));if(RxBuf[1]==1){LEDON;LCD_WriteString(0,2,"Received");DelayMs(1);}if(RxBuf[1]==0){LEDCL;LCD_WriteString(0,2,"not received");DelayMs(1);}//RxBuf[1] = 0x00;DelayMs(1000);}*/} |
|
相关推荐
2个回答
|
|
先保证能够正常读写寄存器。
|
|
|
|
|
|
|
|
只有小组成员才能发言,加入小组>>
如何使用STM32+nrf24l01架构把有线USB设备无线化?
2567 浏览 7 评论
请问能利用51单片机和nRF24L01模块实现实时语音无线传输吗?
2358 浏览 5 评论
3203 浏览 3 评论
2832 浏览 8 评论
为什么ucosii上移植lwip后系统进入了HardFault_Handler?
2785 浏览 4 评论
请教各位大咖:有没有接收频率32M左右的芯片推荐的?先感谢啦!
655浏览 1评论
895浏览 0评论
1016浏览 0评论
661浏览 0评论
491浏览 0评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-12-16 02:52 , Processed in 1.038692 second(s), Total 80, Slave 63 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号