PICs in a RS-485 network?


Closed Thread
Results 1 to 22 of 22
  1. #1
    atomski's Avatar
    atomski Guest

    Question PICs in a RS-485 network?

    Hello all,

    I've searched the forum for past threads
    regarding this, but came out emptyhanded
    Anyone has any experience with
    PicBasic Pro and RS-485 protocol? I'd like
    to be able to control multiple slave devices
    from one master (bi-directional comm.) as
    well as couple of RS-232 units over RS-485
    network. I've looked at MAX485 / MAX491
    and it looks they are just right for the job.
    Any ideas? Thanks in advance.

    --
    Sincerest regards,

    YZ7REA Vladimir M Skrbic
    4N7ATV Repeater Administrator
    YU7GHZ Radio Club President

  2. #2
    Join Date
    Oct 2003
    Location
    Australia
    Posts
    257


    Did you find this post helpful? Yes | No

    Default

    Atomski,

    Need more info on your application to give a good answer.
    ie are you after help on interfacing the pic to the Max driver or other something else?

    Cheers

  3. #3
    atomski's Avatar
    atomski Guest


    Did you find this post helpful? Yes | No

    Default

    What I'm trying to do here is make a HAM
    repeater controller for ATV (amateur tv). I
    need to send and receive data to/from
    various RS-485 devices as well to/from
    RS-232 devices (without device ID) i.e.
    use RS-485 just as a transport media.
    So, I need advice on how to send data
    trough MAX RS-485 driver to RS-485
    devices with their own IDs and RS-232
    devices that have no ID, respectively. An
    example would be appreciated.

    --
    Sincerest regards,

    YZ7REA Vladimir M Skrbic
    4N7ATV Repeater Administrator
    YU7GHZ Radio Club President

  4. #4
    Join Date
    Jul 2003
    Posts
    2,358


    Did you find this post helpful? Yes | No

    Default

    When searching on PBP matters, you really should use this forum in conjunction with the MeLabs PBP archives…

    http://list.picbasic.com/cgi-bin/board-search.cgi

    If you do that, you will discover there have been many discussions (over 170 pages) on RS-485 therein.

  5. #5
    Join Date
    Oct 2003
    Location
    Australia
    Posts
    257


    Did you find this post helpful? Yes | No

    Default

    Mel,

    Looks like that site has a lot of info to find! I am sure I will find it useful in the future.

    Too bad it doesn't have a similar look to this forum.

    Squibs

  6. #6
    Salutatous's Avatar
    Salutatous Guest


    Did you find this post helpful? Yes | No

    Default

    have a look here for the hardware
    http://www.parallax.com/dl/docs/prod...munication.pdf
    For soft in picbasic i can post a routine for modbus/jbus protocol if you meed

  7. #7
    atomski's Avatar
    atomski Guest


    Did you find this post helpful? Yes | No

    Default

    Hello and thanks for the reply!
    Yes I am interested in MODBUS protocol as it will make my project more compatible with existing RS-485 products out there. That way I could use some of the factory made modules for interconnection with our repeater controller if needed. I would appreciate if you could give me an working example of MODBUS protocol over RS-485 network. Thank you in advance.

    --
    Sincerest regards,


    YZ7REA Vladimir M Skrbic
    4N7ATV Repeater Administrator
    YU7GHZ Radio Club President

  8. #8
    Salutatous's Avatar
    Salutatous Guest


    Did you find this post helpful? Yes | No

    Default ModBus rs485

    Sorry but comments are in French
    This prog run with other industrial modbus material! (tested)

    'Programme VigiBar pour pic 16F84A

    'Adresse EEPROM 00=Version du logiciel =>3.0

    'Adresse EEPROM 01=Adresse station de 1 à 255 => var:Station / question 2

    'Adresse EEPROM 02=Vitesse en bauds => var: BR /question 3

    'Vitesse de com 19200 = 32 = $20
    'Vitesse de com 9600 = 84 = $54
    'Vitesse de com 4800 =188 = $BC

    'Adresse EEPROM 03/04=A pour correction de type ax+b => var:A /question 4

    'Adresse EEPROM 05/06=B pour correction de type ax+b => var:B / question 5

    '----------------------------
    DEFINE OSC 8


    CRC_Ok VAR BIT
    BRx_Ok VAR BIT

    i VAR BYTE 'Boucles
    j VAR BYTE 'Boucles
    Val_AD VAR BYTE 'Valeur AD
    Station VAR BYTE 'N° de la station, changer dans EEPROM par Write 1,x
    BR VAR BYTE 'Vitesse de transmission, changer dans EEPROM par Write 2,x
    Nbr VAR BYTE 'Nbre des byte à recevoir ou transmettre
    CRCL VAR BYTE 'pour entrer low crc
    CRCH VAR BYTE 'pour entrer high crc

    CRC16 VAR WORD 'Valeur du CRC
    A VAR WORD 'A pour correction ax+b
    B VAR WORD 'B pour correction ax+b
    hPa VAR WORD 'Résultat apres correction de AD par ax+b

    BufRx VAR BYTE[8] 'Buffer de réception
    BufTx VAR BYTE[8] 'Buffer de transmission

    'led VAR PORTB.7 'Led sur PORTB.7
    RS485 VAR PORTB.0 'Bascule de rs485
    InOut VAR PORTB.1 'Com i/o de rs485
    AD VAR PORTB.2 'Résultat analogique/digital
    Clk VAR PORTB.3 'Horloge du convertisseur AD
    CS VAR PORTB.4 'Bascule convertisseur AD

    INCLUDE "modedefs.bas"

    EEPROM 0,[30,2,84,1,1,0,0] 'Version, N° station, Vitesse de transmission, aH, aL, bH, bL

    Init:
    Low RS485 'RS en attente reception
    For i=0 to 7 'Initialise le tableau de communication
    BufRx[i]=0
    BufTx[i]=0
    Next i
    High CS
    Read 1,Station
    Read 2,BR
    Read 3,A.HighByte
    Read 4,A.LowByte
    Read 5,B.HighByte
    Read 6,B.LowByte

    GoTo Principal


    Principal:
    'Boucle principale
    Nbr=8
    SerIn2 InOut,BR,5,Principal,[STR BufRx\Nbr] 'max 5ms entre car timeout

    IF (BufRx[0]=Station) Then
    For i=0 to 7
    BufTx[i]=BufRx[i] 'Permet economie de code, CRC & prépa réponse
    Next i
    BRx_Ok=1
    GoSub Calcul_CRC16
    IF (CRC16.LowByte<>BufRx[6]) OR (CRC16.HighByte<>BufRx[7]) Then
    CRC_Ok=1
    Else
    CRC_Ok=0
    EndIF
    IF (BufRx[1]=3)OR (BufRx[1]=4) Then 'Question lecture /It's read
    'Err pas traité (BufRx[2]<>0) AND (BufRx[4]<>0)
    ***************Some work************************

    IF (BufRx[1]=6)AND (BufRx[2]=0) Then 'C'est une écriture /It's write

    ******************Some work*********************
    EndIF

    IF (BRx_Ok=1) Then 'Toute autre valeur de BufRx[1] #3,4,6
    Nbr=5 'Erreur
    BufTx[1]=BufRx[1]+128
    BufTx[2]=1
    EndIF
    IF (CRC_OK=0) Then 'Réponse si CRC est OK
    High RS485
    GoSub Calcul_CRC16
    CRCL=Nbr-2
    CRCH=Nbr-1
    BufTx[CRCL]=CRC16.LowByte
    BufTx[CRCH]=CRC16.HighByte
    SerOut2 InOut,BR,[STR BufTx\Nbr]
    'High led
    For i=0 to 7 'Evite la boucle avec anciennes valeurs
    BufRx[i]=0
    Next i
    Low RS485

    EndIF
    EndIF


    GoTo Principal




    Calcul_CRC16:

    CRC16=$FFFF
    For i=0 to Nbr-3
    CRC16=CRC16^BufTx[i]
    For j=1 to 8
    IF CRC16.Bit0=1 Then
    CRC16=$A001^(CRC16>>1)
    Else
    CRC16=CRC16>>1
    EndIF
    Next j
    Next i
    Return



    End

  9. #9
    atomski's Avatar
    atomski Guest


    Did you find this post helpful? Yes | No

    Default

    Thank you very much for the MODBUS via rs-485 code! I will give it a try and let you know how it worked for me. I've also downloaded the MODBUS protocol reference manual so I'll be able to debug everything to the last bit. Thank you one again! 73!

    --
    Sincerest regards,

    YZ7REA Vladimir M Skrbic
    4N7ATV Repeater Administrator
    YU7GHZ Radio Club President

  10. #10
    Join Date
    Apr 2004
    Posts
    34


    Did you find this post helpful? Yes | No

    Exclamation collision detect

    Hi,

    I'm also working on a RS485 based system for my home automation.
    I do have a question on how to avoid a data collision being that 2 or more nodes start transmitting at the same time.
    The RS485 system I use is bidirectional so all node can receive and transmit.
    Is there a good way of checking whether the bus is free to start transmitting?

    Best regards,

  11. #11
    Salutatous's Avatar
    Salutatous Guest


    Did you find this post helpful? Yes | No

    Default

    Modbus is a master/slave system. Each slave does its job, and the master collect the informations and give orders. Therefore there is no collision as slaves can only transmit when the master orders them to

  12. #12
    Join Date
    Oct 2003
    Location
    Australia
    Posts
    257


    Did you find this post helpful? Yes | No

    Default

    Found this interesting site with a bit of info on RS-485 and other bits.

    http://www.mikroelektronika.co.yu/en...m#7.5%20RS-485

  13. #13
    Join Date
    Feb 2008
    Posts
    8


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by Salutatous View Post
    Sorry but comments are in French
    This prog run with other industrial modbus material! (tested)

    'Programme VigiBar pour pic 16F84A

    'Adresse EEPROM 00=Version du logiciel =>3.0

    'Adresse EEPROM 01=Adresse station de 1 à 255 => var:Station / question 2

    'Adresse EEPROM 02=Vitesse en bauds => var: BR /question 3

    'Vitesse de com 19200 = 32 = $20
    'Vitesse de com 9600 = 84 = $54
    'Vitesse de com 4800 =188 = $BC

    'Adresse EEPROM 03/04=A pour correction de type ax+b => var:A /question 4

    'Adresse EEPROM 05/06=B pour correction de type ax+b => var:B / question 5

    '----------------------------
    DEFINE OSC 8


    CRC_Ok VAR BIT
    BRx_Ok VAR BIT

    i VAR BYTE 'Boucles
    j VAR BYTE 'Boucles
    Val_AD VAR BYTE 'Valeur AD
    Station VAR BYTE 'N° de la station, changer dans EEPROM par Write 1,x
    BR VAR BYTE 'Vitesse de transmission, changer dans EEPROM par Write 2,x
    Nbr VAR BYTE 'Nbre des byte à recevoir ou transmettre
    CRCL VAR BYTE 'pour entrer low crc
    CRCH VAR BYTE 'pour entrer high crc

    CRC16 VAR WORD 'Valeur du CRC
    A VAR WORD 'A pour correction ax+b
    B VAR WORD 'B pour correction ax+b
    hPa VAR WORD 'Résultat apres correction de AD par ax+b

    BufRx VAR BYTE[8] 'Buffer de réception
    BufTx VAR BYTE[8] 'Buffer de transmission

    'led VAR PORTB.7 'Led sur PORTB.7
    RS485 VAR PORTB.0 'Bascule de rs485
    InOut VAR PORTB.1 'Com i/o de rs485
    AD VAR PORTB.2 'Résultat analogique/digital
    Clk VAR PORTB.3 'Horloge du convertisseur AD
    CS VAR PORTB.4 'Bascule convertisseur AD

    INCLUDE "modedefs.bas"

    EEPROM 0,[30,2,84,1,1,0,0] 'Version, N° station, Vitesse de transmission, aH, aL, bH, bL

    Init:
    Low RS485 'RS en attente reception
    For i=0 to 7 'Initialise le tableau de communication
    BufRx[i]=0
    BufTx[i]=0
    Next i
    High CS
    Read 1,Station
    Read 2,BR
    Read 3,A.HighByte
    Read 4,A.LowByte
    Read 5,B.HighByte
    Read 6,B.LowByte

    GoTo Principal


    Principal:
    'Boucle principale
    Nbr=8
    SerIn2 InOut,BR,5,Principal,[STR BufRx\Nbr] 'max 5ms entre car timeout

    IF (BufRx[0]=Station) Then
    For i=0 to 7
    BufTx[i]=BufRx[i] 'Permet economie de code, CRC & prépa réponse
    Next i
    BRx_Ok=1
    GoSub Calcul_CRC16
    IF (CRC16.LowByte<>BufRx[6]) OR (CRC16.HighByte<>BufRx[7]) Then
    CRC_Ok=1
    Else
    CRC_Ok=0
    EndIF
    IF (BufRx[1]=3)OR (BufRx[1]=4) Then 'Question lecture /It's read
    'Err pas traité (BufRx[2]<>0) AND (BufRx[4]<>0)
    ***************Some work************************

    IF (BufRx[1]=6)AND (BufRx[2]=0) Then 'C'est une écriture /It's write

    ******************Some work*********************
    EndIF

    IF (BRx_Ok=1) Then 'Toute autre valeur de BufRx[1] #3,4,6
    Nbr=5 'Erreur
    BufTx[1]=BufRx[1]+128
    BufTx[2]=1
    EndIF
    IF (CRC_OK=0) Then 'Réponse si CRC est OK
    High RS485
    GoSub Calcul_CRC16
    CRCL=Nbr-2
    CRCH=Nbr-1
    BufTx[CRCL]=CRC16.LowByte
    BufTx[CRCH]=CRC16.HighByte
    SerOut2 InOut,BR,[STR BufTx\Nbr]
    'High led
    For i=0 to 7 'Evite la boucle avec anciennes valeurs
    BufRx[i]=0
    Next i
    Low RS485

    EndIF
    EndIF


    GoTo Principal




    Calcul_CRC16:

    CRC16=$FFFF
    For i=0 to Nbr-3
    CRC16=CRC16^BufTx[i]
    For j=1 to 8
    IF CRC16.Bit0=1 Then
    CRC16=$A001^(CRC16>>1)
    Else
    CRC16=CRC16>>1
    EndIF
    Next j
    Next i
    Return



    End
    Dear Salutatous

    thank you very much for the modbus code?
    but i am not sure that i understand the part regarding the RS485 handling,do i have to use the RTS pin or no?

    can you please post a schematic diagram for this circuit?
    Best Regards

  14. #14
    Join Date
    Oct 2005
    Posts
    74


    Did you find this post helpful? Yes | No

    Default

    I guess you see that was from 2004 right?

    The Max 485 has two enable pins.
    #2 /RE
    #3 DE

    Tie these two pins together and run them to an output on the pic, call it RS485 for an example. To Transmit Data do this.

    High RS485 ' Put into TX Mode
    Send Data like you would via 232
    Low RS485 'Put into RX Mode

    Nothing to it. 485 is nothing more than the hardware layer or communication line.
    The protocol/format can be anything you want it to be. In the case above (french Code), Modbus RTU.

    A good schematic....
    http://www.mikroe.com/pdf/rs485_board_schematic.pdf

  15. #15
    Join Date
    Oct 2003
    Location
    holland
    Posts
    251


    Did you find this post helpful? Yes | No

    Default

    This is what I did a few years ago.The upper schema is the master, the rest is slave. Al programmed in PBP with the max speed possible at 4 Mhz. Connect all "A" 's to eachother, all "B" 's to eachother and also the gnd's to eachother.
    Each "ontvanger" and "input" is adressable for and adress between 0 and 255.
    So everything is possible. The program is so that an output adress follows the anput adres with the same adresnumber.
    Attached Images Attached Images  

  16. #16
    Join Date
    Jan 2006
    Posts
    31


    Did you find this post helpful? Yes | No

    Question Right Cabling for RS485 network

    Hi everyone, I'm new to RS485 technology and as I read the datasheets of RS485 chips, I noticed that it is recommended to use a twisted pair cable. I am only familiar with 8-wire CAT 5 UTP cable. Locally, I cannot find any cabling technology provider that sells 2-wire twisted pair cable. My question is, is there any alternative solution to this? Will it be possible to use an ordinary telephone cord?


    emavil

  17. #17
    skimask's Avatar
    skimask Guest


    Did you find this post helpful? Yes | No

    Default

    Take 2 wires and twisted them together with a drill!
    Ordinary telephone cord? The stuff I see around the house isn't twisted.
    A quick read on RS485 at wiki says that '485 uses differential balanced lines rather than the twisted pair.
    I haven't tried RS485 directly. I've done similar with RS232, a couple of 7404 inverters and some CAT5 ethernet cable between the house and the garage (about 270ft) and it seemed to work just fine.

  18. #18
    Join Date
    Aug 2006
    Location
    Look, behind you.
    Posts
    2,818


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by emavil View Post
    Hi everyone, I'm new to RS485 technology and as I read the datasheets of RS485 chips, I noticed that it is recommended to use a twisted pair cable. I am only familiar with 8-wire CAT 5 UTP cable. Locally, I cannot find any cabling technology provider that sells 2-wire twisted pair cable. My question is, is there any alternative solution to this? Will it be possible to use an ordinary telephone cord?


    emavil
    Home Centers sell it as doorbell wire. Downside is it usually is solid core , non-stranded wire.
    If you do not believe in MAGIC, Consider how currency has value simply by printing it, and is then traded for real assets.
    .
    Gold is the money of kings, silver is the money of gentlemen, barter is the money of peasants - but debt is the money of slaves
    .
    There simply is no "Happy Spam" If you do it you will disappear from this forum.

  19. #19
    Join Date
    Apr 2007
    Location
    Santiago, Chile
    Posts
    77


    Did you find this post helpful? Yes | No

    Default Rs485

    I've worked quite a bit with RS485 with various protocols for the security camera business.
    As far as the cabling is concerned CAT5 or CAT6 is a pre/requisite if you want more that 50 metres. Less you can get away with twisted just about anything. Also, from experience I have found that over 100m you should not have an earth connected on each side (only 1 side) as you can get earth differential and as RS485 & RS422 only allow around 1.4 volt differential the difference in earth can cause false data very easily.
    As far as multi slaves is concerned you can have up to 31 slaves all talking on the same time but you may will have to have to make the master poll each one's address and allow him to answer in turn or just let each one repeat his message over and over until the master hears it without a clash (camacazi way but can work with out 4 -5 nodes).
    I found the best cable to use (noise resistant and reliable) is the CAT5 shielded type where each twisted pair is separately shielded. I've never had problems with that and I have send data, sound, video & power over the same line without problems.
    I usually use the very inexpensive 75176 from Texas Inst., an old but reliable chip which is very much cheaper than the MAX. For dual direction you just use 2, one for each direction.
    One thing that cannot be done without a real intelligent RS485 repeater with it's own handshaking system is BI/directional communication with multiple masters and slaves mixed together. (In theory RS485 can have up to 31 masters & 1 slave or 31 slaves and 1 master for a wire length up to 300m, any more and you need a repeater) this is very different to RS422 which is a single master with slaves system. Many people lump them together even though their logic voltages are also slightly different.

    I hope that little 485 lecture helped a bit.

    Best Regards

    Chris

  20. #20
    Join Date
    Jan 2006
    Posts
    31


    Did you find this post helpful? Yes | No

    Default

    Thank you guys for the support.

  21. #21
    mtstorm's Avatar
    mtstorm Guest


    Did you find this post helpful? Yes | No

    Default Re: PICs in a RS-485 network?

    Quote Originally Posted by mat janssen View Post
    This is what I did a few years ago.The upper schema is the master, the rest is slave. Al programmed in PBP with the max speed possible at 4 Mhz. Connect all "A" 's to eachother, all "B" 's to eachother and also the gnd's to eachother.
    Each "ontvanger" and "input" is adressable for and adress between 0 and 255.
    So everything is possible. The program is so that an output adress follows the anput adres with the same adresnumber.
    I'm looking for what you have build, is your solution open source? I'm pretty new in pic programming and I hope I may use and learn from your code.

  22. #22
    Join Date
    Oct 2003
    Location
    holland
    Posts
    251


    Did you find this post helpful? Yes | No

    Default Re: PICs in a RS-485 network?

    In the attached file the source for the transmitter and the receiver.
    The receiver software fits in the input and output card.
    Attached Files Attached Files

Similar Threads

  1. RS 485 network
    By mychangl in forum mel PIC BASIC Pro
    Replies: 12
    Last Post: - 25th November 2008, 09:35
  2. Retrieving infos from multiple PICs on a bus/chain
    By flotulopex in forum mel PIC BASIC Pro
    Replies: 7
    Last Post: - 15th October 2007, 04:42
  3. RS 485 wireless communication
    By Armadus in forum mel PIC BASIC Pro
    Replies: 22
    Last Post: - 26th January 2006, 19:30
  4. Internal EEPROM Read/write Addressing Errors with 18F PIC's
    By Melanie in forum FAQ - Frequently Asked Questions
    Replies: 18
    Last Post: - 12th July 2005, 19:42
  5. RS 485 using 16F84A
    By ngeronikolos in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 18th May 2005, 14:46

Members who have read this thread : 1

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts