Load_NRF:
nrf_channel = 0x02 ' Channel #2
nrf_address = 0xCFE7E7CF ' some address
nrf_rfsetup = 0x26 ' 00100110 => 250kbps, 0dBm
nrf_setretr = 0x38 ' 00100100 => ARD = 1000uS, ARC = 8
Return
'
'---[SPI sub for the NRF24L01+]-------------
SPI_NRF:
nrf_inx = 0
Repeat
shDATA=0
shDATA=spi_array[nrf_inx]
For s=0 To 7
nrf_MOSI=GetBit shDATA,(7-s)
LoadBit shDATA,(7-s),nrf_MISO
spi_array[nrf_inx]=shDATA
Nop
nrfSCK=1:Nop:nrfSCK=0:Nop
Next s
Inc nrf_inx
Until nrf_inx > nrf_ptr ' nrf_ptr must be sent with the command so we know how many bytes to transfer
Return
'=========================================
Init_PRX:
nrfCE = 0 ' standby mode
'----------------------------------------
'REG 00 CONFIG
nrfCSN = 0
spi_array[0] = W_REGISTER ' W_REGISTER at address $00 => CONFIG
spi_array[1] = 0x39 ' PRX, CRC enabled
nrf_ptr = 1
GoSub SPI_NRF
nrfCSN = 1
'----------------------------------------
'REG 01 EN_AA
nrfCSN = 0
spi_array[0] = W_REGISTER | 0x01 ' W_REGISTER at address $01 => EN_AA
spi_array[1] = 0x01 ' enable auto-ack only for pipe 0
nrf_ptr = 1
GoSub SPI_NRF
nrfCSN = 1
'----------------------------------------
'REG 02 EN_RXADDR
nrfCSN = 0
spi_array[0] = W_REGISTER | 0x02 ' W_REGISTER at address $02 => EN_RXADDR
spi_array[1] = 0x01 ' enable only pipe 0
nrf_ptr = 1
GoSub SPI_NRF
nrfCSN = 1
'----------------------------------------
'REG 03 SETUP AW
nrfCSN = 0
spi_array[0] = W_REGISTER | 0x03 ' W_REGISTER at address $03 => SETUP AW
spi_array[1] = 0x02 ' address width = 4 bytes
nrf_ptr = 1
GoSub SPI_NRF
nrfCSN = 1
'----------------------------------------
'REG 05 RF_CH
nrfCSN = 0
spi_array[0] = W_REGISTER | 0x05 ' W_REGISTER at address $05 => RF_CH
spi_array[1] = nrf_channel ' set channel to variable
nrf_ptr = 1
GoSub SPI_NRF
nrfCSN = 1
'----------------------------------------
'REG 06 RF_SETUP ' note - reduce power and increase speed in final app
nrfCSN = 0
spi_array[0] = W_REGISTER | 0x06 ' W_REGISTER at address $06 => RF_SETUP
spi_array[1] = nrf_rfsetup ' 0x02 = DR = 1Mbps, RF_POWER -12dBm, 0x26 = DR = 250kbps, RF_POWER 0dBm
nrf_ptr = 1
GoSub SPI_NRF
nrfCSN = 1
'----------------------------------------
'REG 07 STATUS
nrfCSN = 0
spi_array[0] = W_REGISTER | 0x07 ' W_REGISTER at address $27 => STATUS
spi_array[1] = 0x7E ' Clear Interrupts
nrf_ptr = 1
GoSub SPI_NRF
nrfCSN = 1
'----------------------------------------
'REG 0A RX_ADDR_P0
nrfCSN = 0
spi_array[0] = W_REGISTER | 0x0A ' W_REGISTER at address $0A => RX_ADDR_P0
spi_array[1] = nrf_address.Byte0 ' RX address byte 0 LSByte
spi_array[2] = nrf_address.Byte1 ' RX address byte 1
spi_array[3] = nrf_address.Byte2 ' RX address byte 2
spi_array[4] = nrf_address.Byte3 ' RX address byte 3
nrf_ptr = 4
GoSub SPI_NRF
nrfCSN = 1
'----------------------------------------
'REG 11 RX_PW_PO
nrfCSN = 0
spi_array[0] = W_REGISTER | 0x11 ' W_REGISTER at address $11 => RX_PW_PO
spi_array[1] = 0x04 ' 4 byte payload width on Pipe0
nrf_ptr = 1
GoSub SPI_NRF
nrfCSN = 1
'----------------------------------------
'REG 0x1C DYNPD
nrfCSN = 0
spi_array[0] = W_REGISTER | 0x1C ' W_REGISTER at address $1C => DYNPD (Dynamic Payload)
spi_array[1] = 0x01 ' enable dynamic payload on Pipe0
nrf_ptr = 1
GoSub SPI_NRF
nrfCSN = 1
'----------------------------------------
;REG 0x1D FEATURE
nrfCSN = 0
spi_array[0] = W_REGISTER | 0x1D ' W_REGISTER at address $1D => FEATURE
spi_array[1] = 0x04 ' set bit 2 (EN_DPL) to enable dynamic payload.
nrf_ptr = 1
GoSub SPI_NRF
nrfCSN = 1
'----------------------------------------
'REG 00 CONFIG
nrfCSN = 0
spi_array[0] = W_REGISTER ' W_REGISTER at address $00 => CONFIG
spi_array[1] = 0x3B ' PRX, PWR_UP = 1,CRC = 1 byte, CRC enable, RX_DR INT
nrf_ptr = 1
GoSub SPI_NRF
nrfCSN = 1
DelayUS 2000 ' Critical! min 1500uS Tpd2stby delay
Return
'=========================================
'
RX_DPL_LEN:
nrfCSN = 0
spi_array[0] = R_RX_PL_WID ' Read Rx payload length
nrf_ptr = 1 ' index of 2 bytes
GoSub SPI_NRF
nrfCSN = 1
nrf_ptr = spi_array[1] ' the payload length is returned and can be used to determine hthe number of data bytes
If nrf_ptr > 0x20 Then ' max length = 32 byte so error
nrf_ptr = 0
nrf_errf = 1
Clear spi_array
GoSub FLUSH_RX_FIFO
Return
EndIf
Return
'
RX_RECIEVE:
nrf_errf = 0
GoSub RX_DPL_LEN ' get the dynamic payload length
If nrf_errf = 1 Then Return ' return error from dynamic payload length
nrfCSN = 0
Clear spi_array
spi_array[0] = R_RX_PAYLOAD ' Read Rx payload command
GoSub SPI_NRF
nrfCSN = 1
Return
'
READ_NRF_STATUS: ' read STATUS for Interrupt source
spi_array[0] = R_REGISTER | 0x07 ' Read Register $07 => STATUS
nrf_ptr = 0
nrfCSN = 0
GoSub SPI_NRF
nrfCSN = 1
NRF_Status = spi_array[0] ' bit6=RX_DR, bit5 = TX_DS, bit4 = MAX_RT
Return
'
CLR_NRF_INT: ' Clear Interrupts
spi_array[0] = W_REGISTER | 0x07 ' W_REGISTER at address $07 => STATUS
spi_array[1] = 0x70 ' Clear all Interrupts
nrf_ptr = 1
nrfCSN = 0
GoSub SPI_NRF
nrfCSN = 1
Return
'
FLUSH_RX_FIFO: ' flush all RX FIFO slots
spi_array[0] = FLUSH_RX
nrf_ptr = 0
nrfCSN = 0
GoSub SPI_NRF
nrfCSN = 1
Return
'
NRF_RX_READY:
GoSub FLUSH_RX_FIFO
Clear DataIn
Clear spi_array
nrfCE = 1 ' Active Rx mode
DelayUS 200 ' min 130uS RX settle delay
Return
'********************************************
'********************************************
'********************************************
Init_PTX:
nrfCE = 0 ' Clear nrfCE so we can Write to NRF in Standby mode
'----------------------------------------
'REG 00 CONFIG
nrfCSN = 0
spi_array[0] = W_REGISTER ' W_REGISTER at address 0x00 => CONFIG
spi_array[1] = 0x48 ' 01001000 PTX, PWR_Dn, CRC 1 byte, CRC enabled, MASK_MAX_RT, MASK_TX_DS intterupts reflected on NRF_IRQ
nrf_ptr = 1
GoSub SPI_NRF
nrfCSN = 1
'----------------------------------------
'REG 01 EN_AA
nrfCSN = 0
spi_array[0] = W_REGISTER | 0x01 ' W_REGISTER At address 0x01 => EN_AA
spi_array[1] = 0x01 ' Enable auto-ack on pipes 0 , TX mode
nrf_ptr = 1
GoSub SPI_NRF
nrfCSN = 1
'----------------------------------------
'REG 03 SETUP AW
nrfCSN = 0
spi_array[0] = W_REGISTER | 0x03 ' W_REGISTER at address 0x03 => SETUP AW
spi_array[1] = 0x02 ' Address width = 4 bytes
nrf_ptr = 1
GoSub SPI_NRF
nrfCSN = 1
'----------------------------------------
'REG 04 SETUP RETR
nrfCSN = 0
spi_array[0] = W_REGISTER | 0x04 ' W_REGISTER at address 0x04 => SETUP RETR
spi_array[1] = nrf_setretr ' auto retransmit values loaded frm LUT
nrf_ptr = 1
GoSub SPI_NRF
nrfCSN = 1
'----------------------------------------
'REG 05 RF_CH
nrfCSN = 0
spi_array[0] = W_REGISTER | 0x05 ' W_REGISTER at address 0x05 => RF_CH
spi_array[1] = nrf_channel ' default channel = 2
nrf_ptr = 1
GoSub SPI_NRF
nrfCSN = 1
'----------------------------------------
'REG 06 RF_SETUP
nrfCSN = 0
spi_array[0] = W_REGISTER | 0x06 ' W_REGISTER at address 0x06 => RF_SETUP
spi_array[1] = nrf_rfsetup ' 0x02 = DR 1Mbps, RF_POWER -12dBm, 0x26 = DR 250kbps, RF_POWER 0dBm
nrf_ptr = 1
GoSub SPI_NRF
nrfCSN = 1
'----------------------------------------
'REG 07 STATUS
nrfCSN = 0
spi_array[0] = W_REGISTER | 0x07 ' W_REGISTER at address 0x07 => STATUS
spi_array[1] = 0x70 ' Clear RX_DR, TX_DS and MAX_RT Interrupts %01110000
nrf_ptr = 1
GoSub SPI_NRF
nrfCSN = 1
'----------------------------------------
'REG 10 TX_ADDR
nrfCSN = 0
spi_array[0] = W_REGISTER | 0x10 ' W_REGISTER at address 0x10 => TX_ADDR
spi_array[1] = nrf_address.Byte0 ' RX address byte 0 LSByte
spi_array[2] = nrf_address.Byte1 ' RX address byte 1
spi_array[3] = nrf_address.Byte2 ' RX address byte 2
spi_array[4] = nrf_address.Byte3 ' RX address byte 3
nrf_ptr = 4
GoSub SPI_NRF
nrfCSN = 1
'----------------------------------------
'REG 0A RX_ADDR_P0 (Only if ACK is to be received into pipe 0)
nrfCSN = 0
spi_array[0] = W_REGISTER | 0x0A ' W_REGISTER at address 0x0A => RX_ADDR_P0
spi_array[1] = nrf_address.Byte0 ' RX address byte 0 LSByte
spi_array[2] = nrf_address.Byte1 ' RX address byte 1
spi_array[3] = nrf_address.Byte2 ' RX address byte 2
spi_array[4] = nrf_address.Byte3 ' RX address byte 3
nrf_ptr = 4
GoSub SPI_NRF
nrfCSN = 1
'----------------------------------------
'REG 0x1C DYNPD
nrfCSN = 0
spi_array[0] = W_REGISTER | 0x1C ' W_REGISTER at address $1C => DYNPD (Dynamic Payload)
spi_array[1] = 0x01 ' enable dynamic payload on Pipe0
nrf_ptr = 1
GoSub SPI_NRF
nrfCSN = 1
'----------------------------------------
;REG 0x1D FEATURE
nrfCSN = 0
spi_array[0] = W_REGISTER | 0x1D ' W_REGISTER at address $1D => FEATURE
spi_array[1] = 0x04 ' 0x04 If we don't need ack with payload
nrf_ptr = 1
GoSub SPI_NRF
nrfCSN = 1
'----------------------------------------
Return
'
FLUSH_TX_FIFO: ' flush all TX FIFO slots
spi_array[0] = FLUSH_TX
nrf_ptr = 0
nrfCSN = 0
GoSub SPI_NRF
nrfCSN = 1
Return
'
NRF_PWRDN: ' power Down
nrfCSN = 0 ' enable Slave
spi_array[0] = W_REGISTER ' W_REGISTER at address $00 => CONFIG
spi_array[1] = 0x48 ' PTX, PWR_DN, CRC 1 Byte, CRC enabled, MASK_MAX_RT, MASK_TX_DS intterupts reflected On NRF_IRQ
nrf_ptr = 1
GoSub SPI_NRF
nrfCSN = 1
Return
'
TX_POWERUP: ' Power up
spi_array[0] = W_REGISTER ' CONFIG Register
spi_array[1] = 0x4A ' PTX, PWR_UP, CRC 1 Byte, CRC enabled, MASK_MAX_RT, MASK_TX_DS intterupts reflected On NRF_IRQ
nrf_ptr = 1
nrfCSN = 0
GoSub SPI_NRF
nrfCSN = 1
DelayUS 2000 ' Critical! min 1500uS Tpd2stby delay
GoSub FLUSH_TX_FIFO
Return
'
READ_NRF_TX_STATUS: ' read STATUS for Interrupt source
spi_array[0] = R_REGISTER | 0x07 ' Read Register $07 => STATUS
nrf_ptr = 0
nrfCSN = 0
GoSub SPI_NRF
nrfCSN = 1
NRF_Status = (spi_array[0] >> 4) & 7 ' shift and mask so we have bits 6:4 in place [2:0] bit2=RX_DR, bit1 = TX_DS, bit0 = MAX_RT
Return
'
'
NRF_Transmit:
'
spi_array[0] = W_TX_PAYLOAD ' TX payload command
spi_array[1] = "1"
nrf_ptr = 1
nrfCSN = 0
GoSub SPI_NRF
nrfCSN = 1
High nrfCE ' transmit
DelayUS 20
Low nrfCE
'DelayMS 50
GoSub READ_NRF_TX_STATUS ' read STATUS for Interrupt source and shif >> 4 to lower nible NRF_Status.2 = RX_DR, NRF_Status.1 = TX_DS, NRF_Status.0 = MAX_RT
GoSub CLR_NRF_INT ' Clear Interrupts
If NRF_Status = 0x02 Then Return ' good Ack no payload = 0x02
If NRF_Status.0 = 0x01 Then nrf_errf = 4 ' MAX_RT no ack LED flash twice
Return