' ------------------------------------------------------------------------------------------------------------------------------------- ' File: MiRF_Inc_v4.inc ' ' Include file with SPI routines for Nordic nRF24L01 Transceiver. ' v4 Works great with both Rx & Tx units. ' ------------------------------------------------------------------------------------------------------------------------------------- ' Connections to MiRF Transceiver pins should be made in main program. 'MiRF_CE VAR PORTB.3 'MiRF_CSN VAR PORTA.4 'MiRF_SCK VAR PORTB.4 'MiRF_MOSI VAR PORTA.2 'MiRF_MISO VAR PORTA.3 'MiRF_IRQ VAR PORTB.0 ' ---------------------------------------------- ' SPI (nRF24L01) Commands TX_AW CON $05 ' Address Width = 5 RX_AW CON $05 READ_REG CON $00 ' Define read command to register WRITE_REG CON $20 ' Define write command to register RD_RX_PLOAD CON $61 ' Define RX payload register address WR_TX_PLOAD CON $A0 ' Define TX payload register address FLUSH_TX CON $E1 ' Define flush TX register command FLUSH_RX CON $E2 ' Define flush RX register command REUSE_TX_PL CON $E3 ' Define reuse TX payload register command NOP CON $FF ' Define No Operation, might be used to read status register CONFIG CON $00 'Config' register address EN_AA CON $01 'Enable Auto Acknowledgment' register address EN_RXADDR CON $02 'Enabled RX addresses' register address SETUP_AW CON $03 'Setup address width' register address SETUP_RETR CON $04 'Setup Auto. Retrans' register address RF_CH CON $05 'RF channel' register address RF_SETUP CON $06 'RF setup' register address STATUS_REG CON $07 'Status' register address OBSERVE_TX CON $08 'Observe TX' register address CD CON $09 'Carrier Detect' register address RX_ADDR_P0 CON $0A 'RX address pipe0' register address RX_ADDR_P1 CON $0B 'RX address pipe1' register address RX_ADDR_P2 CON $0C 'RX address pipe2' register address RX_ADDR_P3 CON $0D 'RX address pipe3' register address RX_ADDR_P4 CON $0E 'RX address pipe4' register address RX_ADDR_P5 CON $0F 'RX address pipe5' register address TX_ADDR CON $10 'TX address' register address RX_PW_P0 CON $11 'RX payload width, pipe0' register address RX_PW_P1 CON $12 'RX payload width, pipe1' register address RX_PW_P2 CON $13 'RX payload width, pipe2' register address RX_PW_P3 CON $14 'RX payload width, pipe3' register address RX_PW_P4 CON $15 'RX payload width, pipe4' register address RX_PW_P5 CON $16 'RX payload width, pipe5' register address FIFO_STATUS CON $17 'FIFO Status Register' register address MAX_RT CON $10 ' Max #of TX retrans interrupt TX_DS CON $20 ' TX data sent interrupt RX_DR CON $40 ' RX data received ' ------------------------------------------------------------------------------------------------------------ Setup_MiRF_Address: DataArray[0] = $42 DataArray[1] = $E7 DataArray[2] = $94 DataArray[3] = $0A DataArray[4] = $19 RETURN ' ------------------------------------------------------------------------------------------------------------ ' Shift a byte out the SPI pins, while shifting a byte in at the same time. ' SPI_Rw_Byte: for x = 0 to 7 MiRF_MOSI = theDataByte.7 MiRF_SCK = 1 theDataByte = theDataByte << 1 theDataByte.0 = MiRF_MISO ' read in status byte MiRF_SCK = 0 Next x MiRF_MOSI = 0 ' set back to zero before leaving RETURN ' ------------------------------------------------------------------------------------------------------------- ' Same as Send_Cmd ' SPI_Rw_Reg: MiRF_CSN = 0 ' Initiate SPI transaction theDataByte = theCmd Gosub SPI_Rw_Byte theStatusByte = theDataByte theDataByte = theData Gosub SPI_Rw_Byte MiRF_CSN = 1 ' End SPI RETURN ' ---------------------------------------------------------------------------- ' Enter with theCmd = the Register to read ' Exit with theDataByte holding the value read ' SPI_Read: MiRF_CSN = 0 ' Initiate SPI transaction theDataByte = theCmd Gosub SPI_Rw_Byte theDataByte = 0 Gosub SPI_Rw_Byte ' Byte read is now in "theDataByte" ' theStatusByte = theDataByte MiRF_CSN = 1 ' End of SPI transaction RETURN ' ----------------------------------------------------------------------------------------------------------- ' Enter with theCmd = the Register to write to ' nBytes holds the number of bytes to write (32 mox) ' SPI_Write_Buf: MiRF_CSN = 0 ' Initiate SPI transaction theDataByte = theCmd Gosub SPI_Rw_Byte theStatusByte = theDataByte For j = 0 to (nBytes-1) theDataByte = DataArray[j] Gosub SPI_Rw_Byte Next j MiRF_CSN = 1 ' End SPI RETURN ' ---------------------------------------------------------------------------- ' Enter with theCmd = the Register to read, nBytes holding nBytes to read ' SPI_Read_Buf: MiRF_CSN = 0 ' Initiate SPI transaction theDataByte = theCmd Gosub SPI_Rw_Byte theStatusByte = theDataByte for j = 0 to (nBytes - 1) theDataByte = 0 MiRF_MOSI = 0 for x = 1 to 8 MiRF_SCK = 1 theDataByte = theDataByte << 1 theDataByte.0 = MiRF_MISO MiRF_SCK = 0 Next x DataArray[j] = theDataByte ' Debug "Data: ", theDataByte, 13 Next j MiRF_CSN = 1 ' End of SPI transaction RETURN ' ---------------------------------------------------------------------------------------------------------------------------------------- ' End of File