nrf24l01带数据返回的ack,等价于主从式被动全双工收发(不用切换收发),并且可以实现变数据长度收发
之前求助过,没能解决,最近闲,研究了下。ack数据很多汉语的参考手册没有介绍,看的话看原版的英语nrf24l01 手册
1:变数据长度是指0-32长度,可以发送的时候指定
2:带数据返回的ack,就是开启应答,一般情况是通过应答看是否发送成功,这个ack可以带返回数据,实现不切换收发模式的伪双工
3:nrf24l01有三个缓冲区,例子中是收到数据才装载ack缓冲区,所以前两次收到的ack数据是随机的,第三次才是发送过去的数据ack回来,;实际应用的时候可以提前装载ack数据,那么收到数据就会自动把ack缓冲区的数据联通ack发出去(程序内有说明)
三个缓冲区,先进先出 first in – first out,第一个截图最后说了,要是保证缓存的就是要发送的,请清除FLUSH_TX 但是实际没用好像,待测试
If the TX FIFO (PRX) contains more than one payload to a PTX, payloads are handled using the first in –
first out principle. The TX FIFO (PRX) is blocked if all pending payloads are addressed to a PTX where the
link is lost. In this case, the MCU can flush the TX FIFO (PRX) by using the FLUSH_TX command.也就是修改下面这个函数
void NRF24L01_PacketAckData(u8*pBuf,u8 len)//发送数据包,用于发送模式2/4
{
NRF24L01_CE=0;
NRF24L01_Write_Buf(W_ACK_PAYLOAD,pBuf,len);
NRF24L01_CE=1;
}
void NRF24L01_PacketAckData(u8*pBuf,u8 len)//发送数据包,用于发送模式2/4
{
NRF24L01_CE=0;
NRF24L01_Write_Reg(FLUSH_TX, 0xff);
NRF24L01_Write_Buf(W_ACK_PAYLOAD,pBuf,len);
NRF24L01_CE=1;
}
不过也仅限于提前装载ack数据的方案
发送程序:
- #include "sys.h"
- #include "delay.h"
- #include "usart1.h"
- #include "nRF24L01.h"
- #include<stdlib.h> //包含函数rand()声明的头文件
- int main()
- {
- u8 i,sta,tx_len=1;
- NVIC_PriorityGroupConfig(NVIC_PriorityGroup_4); //设置NVIC中断分组4: 16位抢占优先级0,15最好不用
- delay_init();
- USART1_Init(115200);
- printf("OK...\r\n\r\n\r\n");
- NRF24L01_Init(); //初始化NRF24L01
- while(NRF24L01_Check())
- {
- printf("NRF24L01 Error\r\n");
- delay_ms(1000);
- }
- printf("TX NRF24L01 OK\r\n");
-
- NRF24L01_TX_Mode();
-
- for(i=1;i<32;i++)
- TX_BUF[i]=i;
- TX_BUF[1]=10;
- while(1)
- {
- sta=NRF24L01_TxPacket(TX_BUF,tx_len,RX_BUF,&RX_ACK_NUM);//发送数据
- if(sta&TX_OK)//显示要发送的数据
- {
- //发送成功打印发送的数据
- printf("TX_OK TX_BUF:");
- for(i=0;i<tx_len;i++)printf("%2d,",TX_BUF[i]);
- printf("\r\n");
-
- //发送成功一次变化一次长度
- tx_len++;
- if(tx_len>32)tx_len=1;//测试变数据长度发送,每次长度加1,最大32,变为1
- //填充新数据
- TX_BUF[0]=tx_len;//第一个代表发送长度
- TX_BUF[1]++; //加加数 用于看ack响应延时帧数
-
- //长度大于1最后一个随机个数 用于看数据是否变化
- if (tx_len>=2)
- TX_BUF[tx_len-1]=rand()%10; //赋值发送的数据 ,第一个数每次加1改变,最后一个数随机,中间的1 2 3、、、
- }
- if(sta&RX_OK)//显示接收到的ack数据 连续证明不丢帧
- {
- printf("RX_ACK_OK RX_BUF:");
- for(i=0;i<RX_ACK_NUM;i++)printf("%2d,",RX_BUF[i]);
- printf("\r\n");
- }
- delay_ms(10);
- }
- }
复制代码
注意看说明书有的旧的nrf24l01不支持ack带data
代码下载:
nrf24l01_ack调通带备注.7z
(206.84 KB, 下载次数: 54)
|