'=== Config hardware ===
Config Spi = Hard , Interrupt = Off , Data Order = Msb , Master = Yes , Polarity = Low , Phase = 0 , Clockrate = 4 , Noss = 1
'Software SPI is NOT working with the nRF24L01, use hardware SPI only, but the SS pin must be controlled by our self
Config Pinc.5 = Output 'CE pin is output
Config Pinb.2 = Output 'SS pin is output
Config Pinc.4 = Input 'IRQ pin is input
Config Pinc.3 = Input 'TX/RX Device _select
Ce Alias Portc.5
Ss Alias Portb.2
Irq Alias Pinc.4
Txrx_device Alias Pinc.3
Spiinit 'init the spi pins
Set Ce
Waitms 10 'Wait a moment until all hardware is stable
Reset Ce 'Set CE pin low
Reset Ss 'Set SS pin low (CSN pin)
Dim D_bytes(33) As Byte , B_bytes(33) As Byte 'Dim the bytes use for SPI, D_bytes = outgoing B_bytes = Incoming
Dim Temp As Byte , W As Word
Dim Packet_count As Byte
If Txrx_device = True Then Goto Main_tx 'Is this the RX or TX device?
'===Main rx==========================================================================================================================
Main_rx:
Call R_register(status , 1) 'Read STATUS register
Print "Rx_device" 'Send to terminal who i'm
Reset Ce 'Set CE low to access the registers
Gosub Setup_rx 'Setup the nRF24L01 for RX
Waitms 2 'Add a delay before going in RX
Set Ce 'Set nRF20L01 in RX mode
Do 'Main loop for RX
If Irq = 0 Then 'Wait until IRQ occurs, pin becomes low on interrupt
Reset Ce 'Receiver must be disabled before reading pload
Do 'Loop until all 3 fifo buffers are empty
Call R_register(rd_rx_pload , 5) 'Read 5 bytes RX pload register
Print "Pload : " ; Hex(b_bytes(1)) ; Hex(b_bytes(2)) ; Hex(b_bytes(3)) ; Hex(b_bytes(4)) ; Hex(b_bytes(5)) 'Print the pload
Call R_register(fifo_status , 1) 'Read FIFO_STATUS
Loop Until B_bytes(1).0 = True 'Test or RX_EMPTY bit is true, RX FIFO empty
D_bytes(1) = Write_reg + Status 'Reset the RX_DR status bit
D_bytes(2) = &B01000000 'Write 1 to RX_DR bit to reset IRQ
Call W_register(2)
Set Ce 'Enable receiver again
Waitms 2
End If
'Gosub Dump_registers 'Unremark me for debugging
Loop
Return
'===Main tx==========================================================================================================================
Main_tx:
Print "TX_device" 'Send to terminal who i'm
D_bytes(1) = Flush_tx 'Flush the TX_fifo buffer
Call W_register(1)
D_bytes(1) = Write_reg + Status 'Reset the IRQ bits
D_bytes(2) = &B00110000
Call W_register(2)
Do 'Main loop for TX
Incr Packet_count 'Increase the send packet counter, for test only
If Packet_count > 254 Then Packet_count = 0
Gosub Setup_tx 'Setup the nrf240l01 for TX
D_bytes(1) = Wr_tx_pload 'Put 5 bytes in the TX pload buffer
D_bytes(2) = &HAA 'Byte 1
D_bytes(3) = &HBB 'Byte 2
D_bytes(4) = &HCC 'Byte 3
D_bytes(5) = &H11 'Byte 4
D_bytes(6) = Packet_count 'Byte 5 will be increase every loop
Call W_register(6) 'Write 6 bytes to register
Waitms 2
Set Ce 'Set CE for a short moment to transmit the fifo buffer
Waitms 1 '
Reset Ce '
Waitms 100 'Some delay to read the output on the terminal, line can be removed for max. speed
W = 0 'Counter for time out
Do
If Irq = 0 Then
Call R_register(status , 1)
Temp = B_bytes(1) And &B01110000 'Mask the IRQ bits out the status byte
Select Case Temp 'Which IRQ occurs
Case Max_rt 'MAX_RT
Print "Maximum number of TX retries, Flussing the TX buffer now !"
D_bytes(1) = Flush_tx 'Flush the TX buffer
Call W_register(1)
D_bytes(1) = Write_reg + Status
D_bytes(2) = &B00010000 'Clear the MAX_RT IRQ bit
Call W_register(2)
Exit Do
Case Tx_ds 'TX_DS
Print "Packet " ; Packet_count ; " send and ACK received."
D_bytes(1) = Write_reg + Status
D_bytes(2) = &B00100000 'Clear the TX_DS IRQ bit
Call W_register(2)
Exit Do
Case Else 'Other IRQ ??
Print "Other irq " ; Bin(temp)
D_bytes(1) = Flush_tx 'Flush the TX buffer
Call W_register(1)
D_bytes(1) = Write_reg + Status
D_bytes(2) = &B00110000 'Clear both MAX_RT, TX_DS bits
Call W_register(2)
End Select
End If
Waitms 1 'Time out waiting for IRQ 1ms * 100
Incr W 'Increment W
If W > 100 Then 'Waited for 100ms
Print "No irq response from RF20L01 within 100ms"
Exit Do 'Exit the wait loop
End If
Loop
Loop
Return
作者: oyt88 时间: 2013-9-22 16:43
'=== Sub routines ===
Sub W_register(byval C_bytes As Byte) 'Write register with SPI
Reset Ss 'Manual control SS pin, set SS low before shifting out the bytes
Spiout D_bytes(1) , C_bytes 'Shiftout the data bytes trough SPI , C_bytes is the amount bytes to be written
Set Ss 'Set SS high
End Sub
Sub R_register(byval Command As Byte , Byval C_bytes As Byte) As Byte 'C_bytes = Count_bytes, number off bytes to be read
Reset Ss 'Manual controle SS pin, set low before shifting in/out the bytes
Spiout Command , 1 'First shiftout the register to be read
Spiin B_bytes(1) , C_bytes 'Read back the bytes from SPI sended by nRF20L01
Set Ss 'Set SS back to high level
End Sub