News:

;) This forum is the property of Proton software developers

Main Menu

comunicacion rs 485 pic-pic

Started by krizpin, Jul 26, 2022, 03:11 PM

Previous topic - Next topic

krizpin

hola a todos.
Estoy intentando comunicar 2 pic con protocolo rs485 y no soy capaz. Envio un paquete de datos desde el maestro al esclavo, lo procesoy envio un paquete de datos desde el esclavo que sera un ack ,el maestro lo recibe y si es correcto no envia mas.
El problema es que no soy capaz de sincronizarlo para que funcione bien. Alguien sabe de algun ejemplo???
graciass

Oskar-svr

#1
Hello friend, I recommend that you use these modules, you will only have to send or receive data through serial communication, they work very well, the module is responsible for carrying out the communication flow

https://es.aliexpress.com/item/32705625990.html?spm=a2g0o.order_list.0.0.3c1b194dyNExzd&gatewayAdapt=glo2esp

krizpin

muchas gracias por la informacion. no conocia estos modulos, pero no entiendo muy bien la diferencia entre este modulo y el tipico rs485 que normalmente vemos en ejemplos de arduino (son los modulos que tengo colocados. me puedes aclarar un poco que diferencia hay entre ellos?? graciasss

Frizie

If you want more responses you have to use the English language please.
Ohm sweet Ohm | www.picbasic.nl

krizpin

i`m sorry friend.
Thank you very much for the information. I did not know these modules, but I do not understand very well the difference between this module and the typical rs485 that we normally see in arduino examples (these are the modules that I have placed. Can you clarify a little what is the difference between them?? Thank you

Oskar-svr

I leave you this link so you can understand it

https://es.wikipedia.org/wiki/TIA-485

Hello friend Krizpin I leave you this link so that you can understand it if you like, check the page and later I can help you how you can implement a communication with this communication protocol

krizpin

cool. thank you very much for your help. a greeting

top204

Why not purchase two MAX485 chips, then you can communicate using the USART and the MAX485 devices take care of converting it into RS485.

They have been available for about 20 years and I have used them a few times in the past without problems. As I remember, they are full duplex and each device has a transmitter and receiver inside them.

krizpin

Hello. They are the ones I am currently using, the problem with these chips is that they have a pin that must be enabled at the logical level "0" (reading) or "1" when we want to write. The thing is that since I am using the port with interrupts, when I want to send an ack, sometimes there are times when both the sender and the receiver have that control pin at level "1", which gives errors, etc. I'm having a hard time finding a code that works well

Giuseppe

Below two simple and improvable examples of rs485 test program

'Program Master
'Program to test Rs485 communication between 2 modules, one Master and the other Slave. Communication takes place thanks to 2 Max485
'managed by 2 Micro 12f629. To manage the Max 485 each micro must engage 3 ports. One port is used as an output for
'check the 2 enabling pins of the Max 485.The Max 485 has 2 distinct TX and RX enable pins
'called RE (Pin2 for RX) and DE (Pin3 TX). By joining together the 2 enabling pins if we bring to Gnd we enable RX while if
'we bring to Vdd we enable the TX. The other 2 ports of the 629 are used to manage Pin 1 of the Max 485 which is RX
'and pin 4 which represents the TX.

Device = 12F629
Config INTRC_OSC_NOCLKOUT,WDT_OFF, PWRTE_OFF, MCLRE_OFF, CP_ON, CPD_ON ,BODEN_OFF,
Xtal 4
Set_OSCCAL

'--------register set-----------------------------------------
                       
OPTION_REG=%00001111   
WPU=%00000000 ' 
CMCON = 7  'off comparator

'-------set Port-----------------------------------
TRISIO = %00001100
 
'------set symbol-------------------------------                 
Symbol rx_485 = GPIO.2
Symbol tx_485 = GPIO.1
Symbol en_485 = GPIO.0
Symbol mclr = GPIO.3
Symbol led = GPIO.4
Symbol buzzer = GPIO.5

Dim ed As Byte
Dim id As Byte    'slave selection id in tx
Dim id_rx As Byte 'id_rx received for confirmation from the various slaves
Dim conf As Byte
Dim flag As Byte
Dim temp As Byte

Low en_485
High led

flag = 0
temp = 0
ed = "3"
'------------------------------------------------------------------------
main:

'-----------------------------------Rx Rs485--------------------------------------------------

If flag.0 = 1 Then

SerIn rx_485,84,100,ko_rs485,[id_rx,conf] 'Wait 100mS to receive confirmation from the slave
If id_rx = id And  conf = "Y" Then 
 Toggle led
  flag.0 = 0
End If
End If
'--------------------------------------------------------------------------------------------------

DelayMS 300

If mclr = 0 Then
 DelayMS 200
  If mclr = 0 Then GoTo trasmissione
   tx:
   flag.0 = 1   'to activate the receipt of the command sent
End If


GoTo main

'----------------------------Routine di Tx Rs485 -------------------------------------------------
trasmissione:

If ed = "3" Then
 ed = "2"
 GoTo  trasmissione_1
End If

If ed = "2"  Then
 ed = "3"
 GoTo  trasmissione_1
End If 
 
trasmissione_1:

id = "1"

en_485 = 1
SerOut tx_485,84,[id,ed]     'at 9600 baud it takes 1ms to transmit about 1 Byte (104uS per Bit)
DelayMS 5                    'We need to calculate the transmission time to evaluate the delay to be entered just below
en_485 = 0                   'transmission. This is because If we report en_485 = 0 without having finished the tx it goes to RX and cuts
                             'the Bytes to be transmitted
GoTo tx


'---------------------------------Routine Rs485 Ko-------------------------------------------------
ko_rs485:

Inc temp

If temp = 5 Then     'after about 1500 mS I try to transmit again to see if Rs485 has been restored
 temp = 0            '1500 mS because the Sound buzzer makes 4 activations of 250 ms * 4 = 1000 + 500mS (5temp) = 1500mS
 GoTo trasmissione
End If 

Sound buzzer,[115,20]   'Rs485 warning aborted 
GoTo main


Example reception program :


'Test Slave Rs485. I received 2 commands distinct from the Master to decrease or increase the brightness of a LED via PWM

Device = 12F629
Config INTRC_OSC_NOCLKOUT,WDT_OFF, PWRTE_OFF, MCLRE_OFF, CP_OFF, CPD_OFF ,BODEN_OFF,
Xtal 4
Set_OSCCAL


OPTION_REG=%00001111   
WPU=%00000000 ' 
CMCON = 7 
'-------------set Port  Out  Input-------------------------------------------------------------

TRISIO = %00001100
 
Symbol rx_485 = GPIO.2
Symbol tx_485 = GPIO.1
Symbol en_485 = GPIO.0
Symbol mclr = GPIO.3
Symbol led = GPIO.4
Symbol test = GPIO.5

Dim ed As Byte
Dim id As Byte      '
Dim flag As Byte
Dim indice As Byte
Dim Duty As Byte
Dim inizio As Byte
Dim fine As Byte


Duty = 0
indice = 0
flag = 0

Low led
Low en_485
Low test

'--------------------------------------------------------------------------------------------------------------------------

main:

Toggle test

ed = 0
id = 0

SerIn rx_485,84,100,main,[id,ed]   '

If  ed = "2" Then
 GoSub trasmissione
 inizio = 0
 fine = 255
 
For indice = inizio To fine Step 1

GoSub tabella

PWM led,Duty,10   
                 
Next indice

If Duty = 255 Then High led 

End If

If  ed = "3" Then
 GoSub trasmissione
 inizio = 255
 fine = 0
 
For indice = inizio To fine Step -1

GoSub tabella

PWM led,Duty,10   
                 
Next indice

End If


GoTo main

'-------------------------------------------------------------------------------------------------------------------------

trasmissione:

DelayMS 10               'delay to be sure that the Master has finished transmitting
en_485 = 1
SerOut tx_485,84,["*","Y"]
DelayMS 5                'delay to be sure that the Slave transmits all 2 Bytes. At 9600 baud it takes 1mS per Byte
en_485 = 0

Return
             
'---------------------------------------------------------------------------------------------------------------------------
tabella:

Duty = LookUp indice,[0,0,0,0,0,0,0,0,0,1,1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,5,5,5,6,6,7,7,7,8,_
                     8,9,9,10,10,10,11,11,12,12,13,13,14,14,15,15,16,17,17,18,18,19,19,20,21,21,22,23,23,24,24,25,26,26,_
                     27,28,29,29,30,31,31,32,33,34,34,35,36,37,37,38,39,40,40,41,42,43,44,45,45,46,47,48,49,50,51,51,52,53,_
                     54,55,56,57,58,59,60,61,62,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,79,80,81,82,83,84,85,86,87,_
                     88,89,90,92,93,94,95,96,97,98,99,101,102,103,104,105,107,108,109,110,111,113,114,115,116,117,119,120,_
                     121,122,124,125,126,127,129,130,131,133,134,135,137,138,139,141,142,143,145,146,147,149,150,151,153,_
                     154,156,157,158,160,161,163,164,165,167,168,170,171,173,174,175,177,178,180,181,183,184,186,187,189,_
                     190,192,193,195,196,198,199,201,203,204,206,207,209,210,212,213,215,217,218,220,221,223,225,226,_
                     228,230,231,233,234,236,236,239,241,243,244,246,248,249,251,253,255]
Return









krizpin

good morning. Thank you very much for the examples, they are very well explained