Forum - MCS Electronics

 

FAQFAQ SearchSearch RegisterRegister Log inLog in

nRF24 simple to use, multilink library

 
Post new topic   Reply to topic    www.mcselec.com Forum Index -> Share your working BASCOM-AVR code here
View previous topic :: View next topic  
Author Message
EDC

Bascom Expert



Joined: 26 Mar 2014
Posts: 672

poland.gif
PostPosted: Wed Mar 28, 2018 2:59 pm    Post subject: nRF24 simple to use, multilink library Reply with quote

Hello. For some new project I need send more than 32 nRF24 FiFo bytes. So I made this library/routines.
Feel free to improve cause I need it same way too
Very Happy

'*[CREDIT]*
'
' 1.Based on #AN151 from Evert Dekker (Extended to variable/flexible payload lenght)
' LINK https://www.mcselec.com/index.php?option=com_content&task=view&id=212&Itemid=57
'
' 2.modified FIFO routines from forum MCS user "ollopa"
' LINK https://www.mcselec.com/index2.php?option=com_forum&Itemid=59&page=viewtopic&t=7941&highlight=ring+buffer
'
'*[DESCRIPTION]*
'
' nRF24 have FIFO buffer but only for 32bytes. With this library you can send larger buffers/strings.
' Library gives opportunity to easy and quick setup and transmission handling
'.
' Up to six different incomming adressess can be easy programmed in the receive device.Use "Set_rx_address" for that.
' In this sub you must choose pipe nr and show six byte array with address. For simple two device link adress can be
' same for both.
'
' Be careful, and set power to the minimum if you test devices near each other. Use "Set_radio(1 , 0)" for that.
' Which means "channel 1, minimum power"
'
' Sub "Call Rfm24_routines() " should be frequently called in the loop. It set "Got_msg" flag if message with Enter arrive.
'
' If you want to send something then call "Tx_mode", fill "data_out" string and call "Send_string"
' Software FIFO buffer will chunk message into 32bytes max and will be sending it till the end, and switch to Rx_mode.
'
' Attached DEMO code will send 53 bytes every 2sec if switch/jumper is short to the ground.

Code:

$regfile = "m328pdef.dat"
$crystal = 16000000
$baud = 115200
$hwstack = 32
$swstack = 16
$framesize = 32

Config Submode = New

'-[switch for test purposes]-
  Switch Alias Pinc.0 : Config Switch = Input : Set Portc.0 'enable pullup on PORT not PIN!

  '-[Timer for 10ms time base]-
  Config Timer2 = Timer , Prescale = 1024 , Compare_a = Disconnect , Compare_b = Disconnect , Clear_timer = 1
   Compare2a = 155                                          '10ms @16MHz/1024

$include "nRF24L01routines.inc"
 Call Set_tx_address(device_tx_addr(1))
 Call Set_rx_address(device_tx_addr(1) , 0 )                'for only two nodes address here can be same as tx
 Call Set_radio(1 , 0)                                      'Ch1 to Ch125 ,power  0min 3max



Do


 If Tifr2.ocf2a = 1 Then                                    '10ms
  Tifr2.ocf2a = 1

   Call Rfm24_routines()

  '---------------------[NEW MESSAGE]------
   If Got_msg = 1 Then

    Print Data_in

    Got_msg = 0 : Data_idx = 0 : Data_in = ""
   End If
  '----------------------------------------


   If Miliseconds < 100 Then                                '1s timer  100 x10ms
    Incr Miliseconds
   Else
    Miliseconds = 0

    If Rxtx_mode = 0 Then
     If Switch = 0 Then                                     'switch for test purposes

         Call Tx_mode()                                     'want to send something
        Data_out = "12345678901234567890123456789012345678901234567890-53"       'fill string
         Call Send_string()                                 'fill FIFO

     End If
    End If


   End If

 'end 10ms
 End If

Loop
Back to top
View user's profile Visit poster's website
albertsm

Administrator



Joined: 09 Apr 2004
Posts: 4967
Location: Holland

blank.gif
PostPosted: Wed Mar 28, 2018 8:11 pm    Post subject: Reply with quote

Thank you for sharing your project.
RF24 is still very popular.

Good that you included the data-sheet.

_________________
Mark
Back to top
View user's profile Visit poster's website
EDC

Bascom Expert



Joined: 26 Mar 2014
Posts: 672

poland.gif
PostPosted: Wed Mar 28, 2018 9:15 pm    Post subject: Reply with quote

This days you can buy 2$ version with PowerAmplifier and then have link with range up to 1km and this range is the key of popularity because nothing compares to this in this price Very Happy
Back to top
View user's profile Visit poster's website
albertsm

Administrator



Joined: 09 Apr 2004
Posts: 4967
Location: Holland

blank.gif
PostPosted: Thu Mar 29, 2018 9:27 am    Post subject: Reply with quote

you mean the module with PA and LNA ?
You actual get 1 km distance?

_________________
Mark
Back to top
View user's profile Visit poster's website
EDC

Bascom Expert



Joined: 26 Mar 2014
Posts: 672

poland.gif
PostPosted: Thu Mar 29, 2018 5:26 pm    Post subject: Reply with quote

Yes and no Very Happy
I have modules with PA and LNA but I dont made long distance tests.
In the Home /wall obstacle it work without any dropped/retransmitted packets.(they can be checked - see pdf).
..but I wrote simple "EKG/Live_line code" Very Happy and want to do some tests in the terrain.
When receiver, battery powered/mobile, receive the expected code then it wil beep shortly..if no message arrive within two seconds then buzzer will constantly signaling...I know that it is similar to the live pulse Very Happy
So after weekend I let know how test pass or fail Very Happy
I often use newest Bascom feautures/capabilities like Compare functions So sometimes for back compability with some users I must write some functions to workaroud for them...
Hint for Tomy :New Compare function is not highlighted Very Happy
Code:
Do


 If Tifr2.ocf2a = 1 Then                                    '10ms
  Tifr2.ocf2a = 1

   If Timeout_for_rx > 0 Then
    Set Buzzer
    Decr Timeout_for_rx
   Else
    Reset Buzzer                                            'constant signal
   End If


   Call Rfm24_routines()

  '---------------------[NEW MESSAGE]------
   If Got_msg = 1 Then

    Print "RX " ; Data_in
    'Print "TX " ; Data_out

    Helpb = Compare (data_in , Data_out , 53)
    If Helpb = 0 Then
     Reset Buzzer                                           'beep for 10ms cause it will be OFF in the next loop
     Timeout_for_rx = 250
    Else
     Debug "ERR at " ; Helpb
    End If


    Got_msg = 0 : Data_idx = 0 : Data_in = ""
   End If
  '----------------------------------------

   If Miliseconds < 100 Then                                '1s timer  100 x10ms
    Incr Miliseconds
   Else
    Miliseconds = 0
     Set Buzzer

    If Rxtx_mode = 0 Then
     If Switch = 0 Then                                     'switch for test purposes

         Call Tx_mode()                                     'want to send something
          'Data_out was fill earlier for compare testing
         Call Send_string()                                 'fill FIFO

     End If
    End If


   End If

 'end 10ms
 End If

Loop


Last edited by EDC on Thu Mar 29, 2018 5:55 pm; edited 1 time in total
Back to top
View user's profile Visit poster's website
EDC

Bascom Expert



Joined: 26 Mar 2014
Posts: 672

poland.gif
PostPosted: Thu Mar 29, 2018 5:49 pm    Post subject: Reply with quote

I find Bug(in my code) that will do the Compare fail. Picture show the problem. I update first post shortly but there I`m showing how one of the sub should be modified.
Code:
Sub Load_nrf24()
 Local Idx As Byte

 Tx_bytes(1) = Wr_tx_pload
  Idx = 1

  While Head <> Tail

   Incr Idx
   If Idx > 32 Then Exit While

   Tx_bytes(idx) = Pop_fifo()

   Debug Chr(tx_bytes(idx));

  Wend
  Jump_here:

  Call W_register(idx)

   Set Ce                                                   'Set CE for a short moment to transmit the fifo buffer
  Waitus 500                                                '
   Reset Ce
  Rxtx_mode = 20                                            'wait x10ms
End Sub
Back to top
View user's profile Visit poster's website
EDC

Bascom Expert



Joined: 26 Mar 2014
Posts: 672

poland.gif
PostPosted: Mon Apr 02, 2018 2:47 pm    Post subject: Reply with quote

I found that all my devices are nRF24L01plus versions that have additional baudrate 250kbps. So I add possibility to choose baudrate simply because lower baudrate should give longer range.

Code:
Call Set_radio(20 , 3 , 2mega)                             'channel 1-126, power 0-3, Baudrate "250kbps","1mega", "2mega"


This is done by setting 5 bit of the RF_SETUP. I attach new datasheet.

I was do some tests but I`m living in the "national park" so it is a forest, and range in the very bad weather (rain/snow and trees) is about 200m (measured with google maps).

Changing baudrate from 2mega to 250kbps change nothing in my case. I also do shielding for modules (pics attached) and this also change nothing.
Link is down exactly in the same place.

In the free time I do some tests in the open area.

There is one thing which I have not done yet. A scanner for all channels. Device can check if channel is occupied by another device and then our channel should be changed to another to avoid collisions/jamming.
Back to top
View user's profile Visit poster's website
albertsm

Administrator



Joined: 09 Apr 2004
Posts: 4967
Location: Holland

blank.gif
PostPosted: Tue Apr 03, 2018 11:53 am    Post subject: Reply with quote

thanks for the update.
i think shielding will not help when you live in the forest.
maybe a good old external amplifier will help. and of course the perfect antenna/connectors will.

national forest? sounds beautiful:D

_________________
Mark
Back to top
View user's profile Visit poster's website
EDC

Bascom Expert



Joined: 26 Mar 2014
Posts: 672

poland.gif
PostPosted: Tue Apr 03, 2018 3:41 pm    Post subject: Reply with quote

albertsm wrote:
national forest? sounds beautiful:D


Hahaha yest it is. I moved 3 years ago and learn about peace and quiet. I moved from Silesia where noise is very big but it was imperceptible for me that time.
Now whenI visit my young years friends I hear that noise..but I can live wit that again. I like it Very Happy

People can adapt to any enviroment Very Happy.. but after a fiew days ..I miss about this quiet Very Happy

I attach two pics. How much snow we have (snow chain for the wheels is a first aid kit here) and second pics is the deer/ roe-deer roe that came for apples which we do not gather ... because they come for apples in the winter when they are hungry Very Happy

But I must add that we have here LTE ~20MBps and I have only 10min to near town So not bad Very Happy
Back to top
View user's profile Visit poster's website
albertsm

Administrator



Joined: 09 Apr 2004
Posts: 4967
Location: Holland

blank.gif
PostPosted: Tue Apr 03, 2018 7:15 pm    Post subject: Reply with quote

it looks indeed as it sounds : beautiful. I always like the snow. This year we had to drive 800 km to Germany to get some decent snow. But it looks you have loads of it. Especial the trees look great.
Nice that you have deers in your back yard Smile . I hope it remains that beautiful.

(sorry for off topic posts)

_________________
Mark
Back to top
View user's profile Visit poster's website
lslabs2005

Bascom Member



Joined: 28 Dec 2004
Posts: 57

PostPosted: Sun Apr 08, 2018 3:23 am    Post subject: can I use one Nrf24l01 as master and another 110 as slaves? Reply with quote

can I use one Nrf24l01 as master and another 110 as slaves?
to replace standard RS485 cable network!
Back to top
View user's profile
sielcon

Bascom Member



Joined: 16 Mar 2006
Posts: 62
Location: Argentina

argentina.gif
PostPosted: Fri Nov 30, 2018 1:41 pm    Post subject: Reply with quote

Hello EDC, first of all I admire and appreciate the excellent work you have done with this lib. I want to ask you about a problem I have, I'm doing a development where there are several devices that speak only with one, everything works perfect while the TX and RX addresses are the same, when I change addresses what the terminal images show.
I need to use two RX addresses, one in all the devices and a different one in each, according to what I have read in the data sheet, it seems possible.
I would thank you if you can help me see what I'm doing wrong.
Best regards.


Compiler version :2.0.8.1
Compiler build :2.0.8.1.000
IDE version :2.0.8.1.001


Code:

  '- Setea address
   Call Set_tx_address(device_tx_addr(1))
   Call Set_rx_address(device_rx_addr_1(1) , 0 )            'for only two nodes address here can be same as tx
 '  Call Set_rx_address(device_tx_addr(1) , 1 )              'for only two nodes address here can be same as tx


   'Setea canal, potencia y data rate
   Call Set_radio(20 , 0 , 250kbps)                         'Ch1 to Ch125 ,power  0min 3max
 

[img]
https://www.dropbox.com/s/685kzvao1qmkpv6/Image2.jpg?dl=0
[/img]

[img]
https://www.dropbox.com/s/9rwuwybkq5tnv6p/Image3.jpg?dl=0
[/img]
Back to top
View user's profile Visit poster's website
sielcon

Bascom Member



Joined: 16 Mar 2006
Posts: 62
Location: Argentina

argentina.gif
PostPosted: Fri Nov 30, 2018 1:45 pm    Post subject: Reply with quote

It is better to see it like this
Back to top
View user's profile Visit poster's website
Paulvk

Bascom Member



Joined: 28 Jul 2006
Posts: 1178
Location: SYDNEY

australia.gif
PostPosted: Sat May 25, 2019 9:25 am    Post subject: Reply with quote

I am having problems with the multilink library
I can get #AN151 working ok
It seems to be passing an array in a sub I did not think this possible?

regards Paul

Code:

Sub Set_tx_address(byref Addr_array() As Byte)
   Reset Ce                                                 'to get access to the registers
 Tx_bytes(1) = Write_reg + Tx_addr                          'TX adress
  M = Memcopy(addr_array(1) , Tx_bytes(2) , 5)
   Call W_register(6)
End Sub
 
Back to top
View user's profile
albertsm

Administrator



Joined: 09 Apr 2004
Posts: 4967
Location: Holland

blank.gif
PostPosted: Tue May 28, 2019 11:37 am    Post subject: Reply with quote

sure you can pass an array to a sub. that is no problem.
_________________
Mark
Back to top
View user's profile Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic    www.mcselec.com Forum Index -> Share your working BASCOM-AVR code here All times are GMT + 1 Hour
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You cannot download files in this forum