پاسخ : راه اندازی ماژول +nRF24L01
اینطور که من تا الان در مورد این ماژول مطالعه کردم ،، آره میشه
نوشته اصلی توسط atrobot
'------------------------------------------------------------------------------- ' ' IN THE NAME OF GOD ' '------------------------------------------------------------------------------- '=============================================================================== 'Project : Recive nRF24L01 'Version : 1.0.0 'copyright : (c) 2013 'Date : 1392/08/20 'Chip type : ATmega8 'Clock frequency : 11,059,200 Hz '=============================================================================== $regfile = "m8Adef.dat" '$crystal = 11059200 $crystal = 8000000 $romstart = &H0 $baud = 19200 ' use baud rate $hwstack = 40 ' default use 32 for the hardware stack $swstack = 20 ' default use 10 for the SW stack '------------------------------------------------------------------------------- 'Config LCD 16 * 2 '------------------------------------------------------------------------------- Config Lcd = 16 * 2 Config Lcdpin = Pin , Db4 = Portd.6 , Db5 = Portd.7 , Db6 = Portb.0 , _ Db7 = Portb.1 , E = Portd.3 , Rs = Portd.2 Cursor Off Cursor Noblink Cls '------------------------------------------------------------------------------- ' Declare Sub Directory '------------------------------------------------------------------------------- Declare Sub R_register(byval Command As Byte , Byval C_bytes As Byte) Declare Sub W_register(byval C_bytes As Byte) '------------------------------------------------------------------------------- 'SPI (nRF24L01) team '------------------------------------------------------------------------------- Const Write_reg = &H20 ' Define command entry in the register Const Rd_rx_pload = &H61 ' Define Rx Payload Address Register '------------------------------------------------------------------------------- 'SPI (nRF24L01) registers (address) '------------------------------------------------------------------------------- Const Config_nrf = &H00 ' Config register address Const En_aa = &H01 ' Enable Automatic confirmation email address Const En_rxaddr = &H02 ' Rx Enabled address " address" Const Rf_ch = &H05 ' RF channel' register address Const Rf_setup = &H06 ' RF Setup "(Settings) address" Const Status = &H07 ' Status " address" Const Rx_addr_p0 = &H0A ' rx address Pipe0 " address register " Const Rx_pw_p0 = &H11 ' rx payload width , Pipe0 " address register " Const Fifo_status = &H17 ' fifo - Register Status " check address" Dim D_bytes(6) As Byte , B_bytes(5) As Byte '------------------------------------------------------------------------------- 'Config equipment '------------------------------------------------------------------------------- Config Spi = Hard , Interrupt = Off , Data Order = Msb , Master = Yes , _ Polarity = Low , Phase = 0 , Clockrate = 4 , Noss = 1 '=============================================================================== ' Software SPI does not work with nRF24L01, SPI only use the equipment ' but the SS pin- code should be managed by our function '=============================================================================== Config Portc.5 = Output : Ce Alias Portc.5 'CE pin is output Config Portb.2 = Output : Ss Alias Portb.2 'SS pin is output Config Pinc.4 = Input : Irq Alias Pinc.4 'IRQ pin is input '------------------------------------------------------------------------------- ' connection Global Pines '------------------------------------------------------------------------------- Config Pind.5 = Output : Backlight Alias Portd.5 ' Backlight pin is output Set Backlight Config Portc.1 = Output : Read_led Alias Portc.1 '------------------------------------------------------------------------------- Spiinit '-------------------- Set Ce Waitms 10 Reset Ce Reset Ss Call R_register(status , 1) ' Read the status register Reset Ce ' Set CE is low, access to the registers Gosub Setup_rx ' Setting nRF24L01 for RX Waitms 2 Set Ce ' Set nRF20L01 in receive mode '------------------------------------------------------------------------------- Cls Locate 1 , 1 : Lcd " RX_device " Wait 2 '=============================================================================== ' Wait for IRQ occurs , pin- code becomes low interrupt '=============================================================================== Do If Irq = 0 Then Set Read_led Reset Ce ' The receiver must be enabled to read pload Call R_register(rd_rx_pload , 5) ' Read 5 bytes RX pload registration '-------------------------------------------------------------------------- ' Show Recived Data '-------------------------------------------------------------------------- ' Locate 1 , 1 : Lcd Hex(b_bytes(1)) ; "_" ; Hex(b_bytes(2)) ; "_" ; Hex(b_bytes(3)) ; " " ' Locate 2 , 1 : Lcd Hex(b_bytes(4)) ; "_" ; Hex(b_bytes(5)) ; " " Locate 1 , 1 : Lcd B_bytes(1) ; " _ " ; B_bytes(2) ; " _ " ; B_bytes(3) ; " " Locate 2 , 1 : Lcd B_bytes(4) ; " _ " ; B_bytes(5) ; " " D_bytes(1) = Write_reg + Status ' Reset status bit RX_DR D_bytes(2) = &B01000000 ' Write 1 to RX_DR bit to reset IRQ Call W_register(2) Set Ce ' Enable receiver again Reset Read_led Waitms 100 End If Loop 'Sub routines '=============================================================================== ' Write to register with SPI '=============================================================================== Sub W_register(byval C_bytes As Byte) Reset Ss ' Guide CC control contact, use SS low to offset bytes Spiout D_bytes(1) , C_bytes ' Shiftout data bytes SPI trough , C_bytes is the number of bytes to be written Set Ss ' Set SS high End Sub '=============================================================================== 'C_bytes = Count_bytes, the number of bytes to read off '=============================================================================== Sub R_register(byval Command As Byte , Byval C_bytes As Byte) As Byte Reset Ss ' Manual control SS contact, set at a low level until the transition to / from Byte Spiout Command , 1 ' First shiftout in the register , which will be read Spiin B_bytes(1) , C_bytes ' read back bytes from SPI sent to nRF20L01 Set Ss ' Set SS back at a high level End Sub '=============================================================================== ' Preparing for the RX '=============================================================================== Setup_rx: D_bytes(1) = Write_reg + Rx_addr_p0 ' RX address pipe0 D_bytes(2) = &H34 D_bytes(3) = &H43 D_bytes(4) = &H10 D_bytes(5) = &H10 D_bytes(6) = &H01 Call W_register(6) ' Send 6 bytes for SPI D_bytes(1) = Write_reg + En_aa ' Enable auto ACK on pipe0 D_bytes(2) = &H01 Call W_register(2) D_bytes(1) = Write_reg + En_rxaddr ' Enable RX address pipe0 D_bytes(2) = &H01 Call W_register(2) D_bytes(1) = Write_reg + Rf_ch ' Channel Setup D_bytes(2) = 40 ' frequency 2440 MHz Call W_register(2) D_bytes(1) = Write_reg + Rx_pw_p0 ' Set RX pload width for pipe0 D_bytes(2) = 5 Call W_register(2) D_bytes(1) = Write_reg + Rf_setup ' Setting the RF -> output power of 0 dBm , datarate 2 Mbit / s and the LNA gain on D_bytes(2) = &H0F Call W_register(2) D_bytes(1) = Write_reg + Config_nrf ' Setting CONFIG -> I = 1 (RX_device), PWR_UP = 1 , CRC 2bytes, Enable CRC D_bytes(2) = &H0F Call W_register(2) Return
'------------------------------------------------------------------------------- ' ' IN THE NAME OF GOD ' '------------------------------------------------------------------------------- '=============================================================================== 'Project : Send nRF24L01 'Version : 1.0.0 'copyright : (c) 2013 'Date : 1392/08/20 'Chip type : ATmega8 'Clock frequency : 11,059,200 Hz '=============================================================================== $regfile = "m8Adef.dat" $crystal = 11059200 '$crystal = 8000000 $romstart = &H0 $baud = 19200 ' use baud rate $hwstack = 40 ' default use 32 for the hardware stack $swstack = 10 ' default use 10 for the SW stack '------------------------------------------------------------------------------- 'Config LCD 16 * 2 '------------------------------------------------------------------------------- Config Lcd = 16 * 2 Config Lcdpin = Pin , Db4 = Portd.6 , Db5 = Portd.7 , Db6 = Portb.0 , _ Db7 = Portb.1 , E = Portd.3 , Rs = Portd.2 Cursor Off Cursor Noblink Cls '------------------------------------------------------------------------------- ' Declare Sub Directory '------------------------------------------------------------------------------- Declare Sub W_register(byval C_bytes As Byte) '------------------------------------------------------------------------------- 'SPI (nRF24L01) team '------------------------------------------------------------------------------- Const Write_reg = &H20 ' Define command entry in the register Const Wr_tx_pload = &HA0 ' Define Tx Payload Address Register Const Flush_tx = &HE1 ' Detect Flash Tx Command Register '------------------------------------------------------------------------------- 'SPI (nRF24L01) registers (address) '------------------------------------------------------------------------------- Const Config_nrf = &H00 ' Config register address Const En_aa = &H01 ' Enable Automatic confirmation email address Const En_rxaddr = &H02 ' Rx Enabled address " address" Const Rf_ch = &H05 ' RF channel' register address Const Rf_setup = &H06 ' RF Setup "( Settings) address" Const Status = &H07 ' Status " address" Const Rx_addr_p0 = &H0A ' rx address Pipe0 " address register " Const Tx_addr = &H10 ' TX address ( address) Dim D_bytes(6) As Byte , W As Byte '------------------------------------------------------------------------------- 'Config equipment '------------------------------------------------------------------------------- Config Spi = Hard , Interrupt = Off , Data Order = Msb , Master = Yes , Polarity = Low , Phase = 0 , Clockrate = 4 , Noss = 1 '------------------------------------------------------------------------------- ' Software SPI does not work with nRF24L01, SPI only use the equipment ' but the SS pin- code should be managed by our function '------------------------------------------------------------------------------- Config Portc.5 = Output : Ce Alias Portc.5 'CE pin is output Config Portb.2 = Output : Ss Alias Portb.2 'SS pin is output Config Pinc.4 = Input : Irq Alias Pinc.4 'IRQ pin is input '------------------------------------------------------------------------------- ' connection Global Pines '------------------------------------------------------------------------------- Config Pind.5 = Output : Backlight Alias Portd.5 ' Backlight pin is output Set Backlight Config Portc.1 = Output : Send_led Alias Portc.1 '------------------------------------------------------------------------------- Spiinit Cls Locate 1 , 1 : Lcd " TX_device " Wait 2 '=============================================================================== ' '=============================================================================== Do Incr W If W = 250 Then W = 0 Set Ce Waitms 10 Reset Ce Reset Ss Send_led = 1 D_bytes(1) = Flush_tx ' Flush the TX_fifo buffer Call W_register(1) D_bytes(1) = Write_reg + Status ' Reset IRQ bit D_bytes(2) = &B00110000 Call W_register(2) Gosub Setup_tx ' Setting nrf240l01 for TX D_bytes(1) = Wr_tx_pload ' Put 5 bytes in 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) = W ' Byte 5 will increase every cycle Call W_register(6) ' Record 6 Bytes For Registration Waitms 2 Set Ce ' Set CE for a brief moment to transmit buffer fifo Waitms 1 Reset Ce Send_led = 0 '-------------------------------------------------------------------------- ' Show Sended Data '-------------------------------------------------------------------------- ' Locate 1 , 1 : Lcd Hex(d_bytes(1)) ; " _ " ; Hex(d_bytes(2)) ; " _ " ; Hex(d_bytes(3)) ; " " ' Locate 2 , 1 : Lcd Hex(d_bytes(4)) ; " _ " ; Hex(d_bytes(5)) ; " _ " ; Hex(d_bytes(6)) ; " " Locate 1 , 1 : Lcd D_bytes(1) ; " _ " ; D_bytes(2) ; " _ " ; D_bytes(3) ; " " Locate 2 , 1 : Lcd D_bytes(4) ; " _ " ; D_bytes(5) ; " _ " ; D_bytes(6) ; " " Wait 1 Loop 'Sub routines '=============================================================================== ' Write to register with SPI '=============================================================================== Sub W_register(byval C_bytes As Byte) Reset Ss ' Guide CC control contact, use SS low to offset bytes Spiout D_bytes(1) , C_bytes ' Shiftout data bytes SPI trough , C_bytes is the number of bytes to be written Set Ss ' Set SS high End Sub '=============================================================================== ' Preparing for TX '=============================================================================== Setup_tx: D_bytes(1) = Write_reg + Tx_addr 'TX address D_bytes(2) = &H34 D_bytes(3) = &H43 D_bytes(4) = &H10 D_bytes(5) = &H10 D_bytes(6) = &H01 Call W_register(6) D_bytes(1) = Write_reg + Rx_addr_p0 'RX address pipe0 D_bytes(2) = &H34 D_bytes(3) = &H43 D_bytes(4) = &H10 D_bytes(5) = &H10 D_bytes(6) = &H01 Call W_register(6) D_bytes(1) = Write_reg + En_aa 'Enable auto ACK on pipe0 D_bytes(2) = &H01 Call W_register(2) D_bytes(1) = Write_reg + En_rxaddr ' Enable RX address pipe0 D_bytes(2) = &H01 Call W_register(2) D_bytes(1) = Write_reg + Rf_ch ' Channel Setup D_bytes(2) = 40 ' frequency 2440 MHz Call W_register(2) D_bytes(1) = Write_reg + Rf_setup ' Setting the RF -> output power of 0 dBm , datarate 2 Mbit / s and the LNA gain on D_bytes(2) = &H0F Call W_register(2) D_bytes(1) = Write_reg + Config_nrf ' Setting CONFIG -> I = 0 (TX_device), PWR_UP = 1 , CRC 2bytes, Enable CRC D_bytes(2) = &H0E Call W_register(2) Return
Const Rf_setup = &H06 ' RF Setup "( Settings) address" D_bytes(1) = Write_reg + Rf_setup ' Setting the RF -> output power of 0 dBm , datarate 2 Mbit / s and the LNA gain on
دیدگاه