PDA

View Full Version : PICs in a RS-485 network?



atomski
- 23rd March 2004, 10:51
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

Squibcakes
- 24th March 2004, 00:56
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

atomski
- 24th March 2004, 07:50
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

Melanie
- 30th March 2004, 11:43
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.

Squibcakes
- 31st March 2004, 05:15
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

Salutatous
- 20th April 2004, 17:36
have a look here for the hardware
http://www.parallax.com/dl/docs/prod/appkit/rs485Communication.pdf
For soft in picbasic i can post a routine for modbus/jbus protocol if you meed

atomski
- 20th April 2004, 19:10
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

Salutatous
- 21st April 2004, 13:51
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

atomski
- 21st April 2004, 20:33
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

jrt4fun
- 22nd April 2004, 09:02
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,

Salutatous
- 24th April 2004, 18:37
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

Squibcakes
- 4th May 2004, 00:03
Found this interesting site with a bit of info on RS-485 and other bits.

http://www.mikroelektronika.co.yu/english/product/books/picbasicbook/07.htm#7.5%20RS-485

mech962784
- 27th February 2008, 10:06
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

rwskinner
- 4th March 2008, 22:56
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

mat janssen
- 5th March 2008, 18:27
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.

emavil
- 29th September 2008, 01:38
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

skimask
- 29th September 2008, 04:49
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.

Archangel
- 29th September 2008, 06:33
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.

crhomberg
- 29th September 2008, 13:23
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

emavil
- 2nd October 2008, 00:59
Thank you guys for the support.

mtstorm
- 12th November 2011, 01:32
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.

mat janssen
- 12th November 2011, 10:52
In the attached file the source for the transmitter and the receiver.
The receiver software fits in the input and output card.