Dim D_bytes(33) As Byte , B_bytes(33) As Byte , Data0(32) As Byte '定义函数类型
Dim Temp As Byte , Run As Byte , Tudr As Byte
Dim Packet_count As Byte , Cntr As Byte
Dim Ok As Bit , W As Word , I As Word
Run = 1 : Reset Ok
Call R_register(status , 1) '读状态寄存器
Print "Rx_device" '发送到终端"Rx_device"
Reset Ce 'CE引脚设置低访问寄存器
Gosub Setup_rx '设置为接收nRF24L01
Waitms 2 '在RX之前延迟2毫秒
Set Ce '置CE为高电平,NRF2401为接收模式
Do '主循环接收
If Irq = 0 Then '等到IRQ中断引脚变低
Reset Ce 'CE置低电平,禁用
Do '循环直到所有3个FIFO缓冲区是空的
Call R_register(rd_rx_pload , 32) '读5个字节RX PLOAD寄存器
If Irq = 0 Then
Call R_register(status , 1)
Temp = B_bytes(1) And &B01110000 ' IRQ 中断状态字节位
Select Case Temp 'IRQ发生
Case Max_rt '最多重发中断
Print "Maximum number of TX retries, Flussing the TX buffer now !"
D_bytes(1) = Flush_tx '清除发送缓冲区
Call W_register(1)
D_bytes(1) = Write_reg + Status '写状态寄存器
D_bytes(2) = &B00010000 '明确max_rt中断点
Call W_register(2)
Exit Do
Case Tx_ds 'TX数据发送中断
Print "Packet " ; Packet_count ; " send and ACK received."
D_bytes(1) = Write_reg + Status '写状态寄存器
D_bytes(2) = &B00100000 '明确tx_ds中断点
Call W_register(2)
Exit Do
Waitms 1
'时间等待IRQ 1ms×100
Incr W
'W加一
If W > 100 Then '等待100ms
Print "No irq response from RF20L01 within 100ms"
Exit Do '退出等待循环
End If
Loop
Goto Main_rx
Loop
Return
'=================================SPI读写子过程 ====================================================
Sub W_register(byval C_bytes As Byte) '写SPI寄存器
Reset Ss '手动控制SS引脚设置低,
Spiout D_bytes(1) , C_bytes '移出的数据字节通过SPI
Set Ss 'SS置高电平
End Sub
Sub R_register(byval Command As Byte , Byval C_bytes As Byte) As Byte '读SPI寄存器
Reset Ss '手动控制SS引脚设置低之前,移动/字节
Spiout Command , 1 '第一移出寄存器被读取
Spiin B_bytes(1) , C_bytes '读取的字节从SPI发送到nrf2401
Set Ss 'SS回到高电平
End Sub