Rs485 Using Hserin/hserout


Closed Thread
Results 1 to 28 of 28
  1. #1

    Default Rs485 Using Hserin/hserout

    Good day

    I have a simple setup with <b>2 x PIC 16F877</b> and want to do <b>RS485 Comms</b> using <b>2 x SN75176</b> chips.
    The one PIC is the <b>Master</b> and the other the <b>Slave</b>.

    For debug purposes I send messages to my <b>PC using RS232</b> from the Master.


    <font color="#993300">Below is my code for the MASTER:</font>
    <hr>
    <code>
    &nbsp;&nbsp;Include "Modedefs.bas"
    &nbsp;&nbsp;&nbsp;&nbsp;
    &nbsp;&nbsp;DEFINE&nbsp;&nbsp;&nbsp;&nbsp; OSC 4

    &nbsp;&nbsp;DE_OR_RE&nbsp;&nbsp;&nbsp;&nbsp;VAR PORTC.5&nbsp;&nbsp;&nbsp;&nbsp; ' DE and RE Pin of SN75176
    &nbsp;&nbsp;RS232_DEBUG_PIN VAR PORTB.5&nbsp;&nbsp;&nbsp;&nbsp; ' Debugging to PC
    &nbsp;&nbsp;LEDPIN&nbsp;&nbsp;&nbsp;&nbsp; VAR PORTD.2&nbsp;&nbsp;&nbsp;&nbsp; ' LED to confirm PIC is running
    &nbsp;&nbsp;DATARECEIVED&nbsp;&nbsp;VAR BYTE

    &nbsp;&nbsp;'======== HSEROUT, HSERIN SETTINGS ==========&nbsp;&nbsp;
    &nbsp;&nbsp;DEFINE HSER_RCSTA 90h
    &nbsp;&nbsp;define HSER_TXSTA 20h
    &nbsp;&nbsp;define HSER_SPBRG 103
    &nbsp;&nbsp;DEFINE HSER_CLOERR 1
    &nbsp;&nbsp;RCIF&nbsp;&nbsp;&nbsp;&nbsp;VAR PIR1.5
    &nbsp;&nbsp;TXIF&nbsp;&nbsp;&nbsp;&nbsp;VAR PIR1.4
    &nbsp;&nbsp;
    &nbsp;&nbsp;ADCON1 = 7
    &nbsp;&nbsp;TRISC = %10000000
    &nbsp;&nbsp;
    Main:
    &nbsp;&nbsp;HIGH LEDPIN&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&n bsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs p;&nbsp;&nbsp; ' Indicate PIC is running
    &nbsp;&nbsp;HIGH DE_OR_RE&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&n bsp; ' Make ready for TX
    &nbsp;&nbsp;PAUSE 100
    &nbsp;&nbsp;HSEROUT ["S0"]&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&n bsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ' Send "S0"
    &nbsp;&nbsp;PAUSE 100
    &nbsp;&nbsp;LOW LEDPIN
    &nbsp;&nbsp;LOW DE_OR_RE&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&n bsp;&nbsp;&nbsp;' Make ready for RX
    &nbsp;&nbsp;PAUSE 100
    &nbsp;&nbsp;SEROUT RS232_DEBUG_PIN,N2400,["WAITING FOR REPLY..", 13, 10] ' DEBUG TO PC
    &nbsp;&nbsp;if RCIF then
    &nbsp;&nbsp;&nbsp;&nbsp;hserin 100,Main,[WAIT ("R"),DATARECEIVED]
    &nbsp;&nbsp;&nbsp;&nbsp;PAUSE 100
    &nbsp;&nbsp;ENDIF
    &nbsp;&nbsp;SEROUT RS232_DEBUG_PIN,N2400,["REC:",DATARECEIVED, 13, 10] ' DEBUG TO PC
    &nbsp;&nbsp;PAUSE 100
    &nbsp;&nbsp;
    GOTO Main

    END
    </code>
    <hr>

    <font color="#993300">Here is the code for the SLAVE:</font>
    <hr>
    Include "Modedefs.bas"

    DEFINE&nbsp;&nbsp;&nbsp;&nbsp; OSC 4

    &nbsp;&nbsp;DE_OR_RE&nbsp;&nbsp;&nbsp;&nbsp;VAR PORTC.5&nbsp;&nbsp;&nbsp;&nbsp; ' DE and RE Pin of SN75176
    &nbsp;&nbsp;LEDPIN&nbsp;&nbsp;&nbsp;&nbsp; VAR PORTD.2&nbsp;&nbsp;&nbsp;&nbsp; ' LED to confirm PIC is running
    &nbsp;&nbsp;DATARECEIVED&nbsp;&nbsp;VAR BYTE
    &nbsp;&nbsp;
    &nbsp;&nbsp;'======== HSEROUT, HSERIN SETTINGS ==========
    &nbsp;&nbsp;DEFINE HSER_RCSTA 90h
    &nbsp;&nbsp;define HSER_TXSTA 20h
    &nbsp;&nbsp;define HSER_SPBRG 103
    &nbsp;&nbsp;DEFINE HSER_CLOERR 1
    &nbsp;&nbsp;RCIF&nbsp;&nbsp;&nbsp;&nbsp;VAR PIR1.5
    &nbsp;&nbsp;TXIF&nbsp;&nbsp;&nbsp;&nbsp;VAR PIR1.4

    &nbsp;&nbsp;ADCON1 = 7
    &nbsp;&nbsp;TRISC = %10000000
    &nbsp;&nbsp;
    Main:

    &nbsp;&nbsp;HIGH LEDPIN&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&n bsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs p;&nbsp;&nbsp; ' Indicate PIC is running
    &nbsp;&nbsp;LOW DE_OR_RE&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&n bsp;&nbsp;&nbsp;' Make ready for RX
    &nbsp;&nbsp;PAUSE 100&nbsp;&nbsp;
    &nbsp;&nbsp;if RCIF then
    &nbsp;&nbsp;&nbsp;&nbsp;hserin 100,Main,[WAIT ("S"),DATARECEIVED] ' Looking for "S0"
    &nbsp;&nbsp;&nbsp;&nbsp;PAUSE 100
    &nbsp;&nbsp;&nbsp;&nbsp;IF DATARECEIVED = "0" THEN
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;HIGH DE_OR_RE&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ' Make ready for RX
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;PAUSE 100
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;HSEROUT ["R1"]&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&n bsp;&nbsp;&nbsp;&nbsp; ' Send R1
    &nbsp;&nbsp;&nbsp;&nbsp;ENDIF
    &nbsp;&nbsp;endif
    &nbsp;&nbsp;LOW LEDPIN
    &nbsp;&nbsp;PAUSE 100
    GOTO MAIN

    END
    <hr>


    <font color="#993300">Please find attached my schematic!</font>

    <hr>
    MY QUESTION:
    <hr>
    <font color="#993300">
    The first time I connected the Slave to the 5v power and then after about 5 seconds the Master I get the following on the PC Side:
    </font>
    <b>
    WAITING FOR REPLY..
    WAITING FOR REPLY..
    REC:˙
    WAITING FOR REPLY..
    REC:˙
    WAITING FOR REPLY..
    REC:˙
    WAITING FOR REPLY..
    REC:˙
    WAITING FOR REPLY..
    REC:˙
    WAITING FOR REPLY..
    REC:˙
    </b>

    <font color="#993300">
    Then when I done it again I get the following:
    </font>
    <b>
    WAITING FOR REPLY..
    REC:1
    WAITING FOR REPLY..
    WAITING FOR REPLY..
    REC:1
    WAITING FOR REPLY..
    REC:1
    WAITING FOR REPLY..
    REC:1
    WAITING FOR REPLY..
    REC:1
    WAITING FOR REPLY..
    REC:1
    WAITING FOR REPLY..
    REC:1
    WAITING FOR REPLY..
    REC:1
    </b>

    <font color="#993300">
    When I startup the Master first and then after a few seconds the Slave I get the following
    </font>
    <b>
    WAITING FOR REPLY..
    WAITING FOR REPLY..
    WAITING FOR REPLY..
    WAITING FOR REPLY..
    WAITING FOR REPLY..
    WAITING FOR REPLY..
    REC:˙
    WAITING FOR REPLY..
    WAITING FOR REPLY..
    WAITING FOR REPLY..
    WAITING FOR REPLY..
    WAITING FOR REPLY..
    WAITING FOR REPLY..
    WAITING FOR REPLY..
    WAITING FOR REPLY..
    WAITING FOR REPLY..
    </b>

    <font color="#993300">
    When I connect the Slave first, then the Master and then after a few seconds disconnect and connect the master again, it just keep on returning <b>"WAITING FOR REPLY.."</b>
    </font>
    <b>
    WAITING FOR REPLY..
    REC:1
    WAITING FOR REPLY..
    REC:1
    WAITING FOR REPLY..
    REC:1
    WAITING FOR REPLY..
    REC:1
    WAITING FOR REPLY..
    REC:1
    WAITING FOR REPLY..
    WAITING FOR REPLY..
    WAITING FOR REPLY..
    WAITING FOR REPLY..
    WAITING FOR REPLY..
    WAITING FOR REPLY..
    WAITING FOR REPLY..
    WAITING FOR REPLY..
    </b>


    <font color="#993300">
    It is very unreliable, sometimes it is working and sometimes not.

    Am I doing something stupid???
    </font>


    Thank you
    Koossa
    Attached Images Attached Images
    Last edited by koossa; - 25th January 2008 at 11:35.

  2. #2


    Did you find this post helpful? Yes | No

    Default

    Could this happen when the 2 pics are on seperate power supplies?

  3. #3
    skimask's Avatar
    skimask Guest


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by koossa View Post
    Could this happen when the 2 pics are on seperate power supplies?
    Might have something to do with all of those pauses you've got sprinkled in the code. If the pauses don't line up right (the moon, saturn, mars, etc), one part of the project, either the master or the slave, misses a reception at one time or another, and/or gets caught in the middle of one or the other...basically a sync'ing issue.

  4. #4


    Did you find this post helpful? Yes | No

    Default

    Hi Skimask

    Must I remove all the pauses?
    I am working on 2400 Baud and want to give enough time to transmit the data.

    Thank you
    Koossa

  5. #5
    skimask's Avatar
    skimask Guest


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by koossa View Post
    Hi Skimask
    Must I remove all the pauses?
    I am working on 2400 Baud and want to give enough time to transmit the data.
    Thank you
    Koossa
    That's just it, you're giving it enough time to TX the data, but in the process of Pausing for 100ms afterwards, you might've missed 200-ish bits of serial data. The receive buffer is only 2 bytes deep (1 if you want be conservative). I'd say pause for 1 byte time, 4ms (2400 baud = .416ms/bit = 4.16ms/byte) instead of 100ms.

    The SERIN and SEROUT commands won't let the program continue until they're finished transmitting or receiving.
    HSERIN/HSEROUT, not sure about them, as I generally access the serial port registers directly rather than using the commands.
    I would assume that they would act the same as SERIN and SEROUT for continuity purposes, but, I would hope that they would be 'non-blocking' commands (unless WAIT is used with HSERIN of course).

  6. #6


    Did you find this post helpful? Yes | No

    Default

    Don't know if I understand you correctly, but here is my new code

    MASTER PIC
    <HR>
    <p>&nbsp;&nbsp; Include &quot;Modedefs.bas&quot;&nbsp;&nbsp;
    &nbsp;&nbsp;
    &nbsp;&nbsp; DEFINE OSC 4&nbsp;&nbsp;
    &nbsp;&nbsp;
    &nbsp;&nbsp; DE_OR_RE&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; VAR&nbsp;&nbsp;&nbsp;&nbsp; PORTC.5&nbsp;&nbsp;&nbsp;&nbsp; ' DE and RE Pin of SN75176&nbsp;&nbsp;
    &nbsp;&nbsp; RS232_DEBUG_PIN&nbsp;&nbsp;&nbsp;&nbsp; VAR&nbsp;&nbsp;&nbsp;&nbsp; PORTB.5&nbsp;&nbsp;&nbsp;&nbsp; ' Debugging to PC
    &nbsp;&nbsp; LEDPIN&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&n bsp;&nbsp; VAR&nbsp;&nbsp;&nbsp;&nbsp; PORTD.2&nbsp;&nbsp;&nbsp;&nbsp;' LED to confirm PIC is running&nbsp;&nbsp;
    &nbsp;&nbsp; DATARECEIVED&nbsp;&nbsp;&nbsp;&nbsp; VAR&nbsp;&nbsp;&nbsp;&nbsp; BYTE&nbsp;&nbsp;
    &nbsp;&nbsp;
    &nbsp;&nbsp; '======== HSEROUT, HSERIN SETTINGS ========== &nbsp;&nbsp;
    &nbsp;&nbsp; DEFINE&nbsp;&nbsp;&nbsp;&nbsp; HSER_RCSTA&nbsp;&nbsp;&nbsp; 90h&nbsp;&nbsp;
    &nbsp;&nbsp; define&nbsp;&nbsp;&nbsp;&nbsp; HSER_TXSTA&nbsp;&nbsp;&nbsp; 20h&nbsp;&nbsp;
    &nbsp;&nbsp; define&nbsp;&nbsp;&nbsp;&nbsp; HSER_SPBRG&nbsp;&nbsp;&nbsp; 103&nbsp;&nbsp;
    &nbsp;&nbsp; DEFINE&nbsp;&nbsp;&nbsp;&nbsp; HSER_CLOERR&nbsp;&nbsp;&nbsp; 1&nbsp;&nbsp;
    &nbsp;&nbsp; RCIF&nbsp;&nbsp;&nbsp;&nbsp; VAR&nbsp;&nbsp;&nbsp;&nbsp; PIR1.5&nbsp;&nbsp;
    &nbsp;&nbsp; TXIF&nbsp;&nbsp;&nbsp;&nbsp; VAR&nbsp;&nbsp;&nbsp;&nbsp; PIR1.4&nbsp;&nbsp;
    &nbsp;&nbsp;
    &nbsp;&nbsp; ADCON1 = 7&nbsp;&nbsp;
    &nbsp;&nbsp; TRISC = %10000000&nbsp;&nbsp;
    &nbsp;&nbsp;
    Main:&nbsp;&nbsp;
    &nbsp;&nbsp; HIGH LEDPIN ' Indicate PIC is running&nbsp;&nbsp;
    &nbsp;&nbsp; HIGH DE_OR_RE ' Make ready for TX&nbsp;&nbsp;
    &nbsp;&nbsp; PAUSE 4&nbsp;&nbsp;
    &nbsp;&nbsp; HSEROUT [&quot;S0&quot;] ' Send &quot;S0&quot;&nbsp;&nbsp;
    &nbsp;&nbsp; LOW LEDPIN&nbsp;&nbsp;
    &nbsp;&nbsp; LOW DE_OR_RE ' Make ready for RX&nbsp;&nbsp;
    &nbsp;&nbsp; PAUSE 4&nbsp;&nbsp;
    &nbsp;&nbsp; SEROUT RS232_DEBUG_PIN,N2400,[&quot;WAITING FOR REPLY..&quot;, 13, 10] ' DEBUG TO PC&nbsp;&nbsp;
    &nbsp;&nbsp; if RCIF then&nbsp;&nbsp;
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; hserin 100,Main,[WAIT (&quot;R&quot,DATARECEIVED]&nbsp;&nbsp;
    &nbsp;&nbsp; ENDIF&nbsp;&nbsp;
    &nbsp;&nbsp; SEROUT RS232_DEBUG_PIN,N2400,[&quot;REC:&quot;,DATARECEIVED, 13, 10] 'DEBUG TO PC&nbsp;&nbsp;
    &nbsp;&nbsp;
    &nbsp;&nbsp; GOTO Main&nbsp;&nbsp;
    &nbsp;&nbsp;
    END</p>

    <HR>




    SLAVE PIC
    <hr>
    <p>&nbsp;&nbsp; Include &quot;Modedefs.bas&quot;&nbsp;&nbsp;
    &nbsp;&nbsp;
    &nbsp;&nbsp; DEFINE OSC 4&nbsp;&nbsp;
    &nbsp;&nbsp;
    &nbsp;&nbsp; DE_OR_RE&nbsp;&nbsp;&nbsp;&nbsp; VAR&nbsp;&nbsp;&nbsp;&nbsp; PORTC.5&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ' DE and RE Pin of SN75176&nbsp;&nbsp;
    &nbsp;&nbsp; LEDPIN&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&n bsp; VAR&nbsp;&nbsp;&nbsp;&nbsp; PORTD.2&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ' LED to confirm PIC is running&nbsp;&nbsp;
    &nbsp;&nbsp; DATARECEIVED&nbsp; VAR&nbsp;&nbsp;&nbsp;&nbsp; BYTE&nbsp;&nbsp;
    &nbsp;&nbsp;
    &nbsp;&nbsp; '======== HSEROUT, HSERIN SETTINGS ==========&nbsp;&nbsp;
    &nbsp;&nbsp; DEFINE HSER_RCSTA 90h&nbsp;&nbsp;
    &nbsp;&nbsp; define HSER_TXSTA 20h&nbsp;&nbsp;
    &nbsp;&nbsp; define HSER_SPBRG 103&nbsp;&nbsp;
    &nbsp;&nbsp; DEFINE HSER_CLOERR 1&nbsp;&nbsp;
    &nbsp;&nbsp; RCIF VAR PIR1.5&nbsp;&nbsp;
    &nbsp;&nbsp; TXIF VAR PIR1.4&nbsp;&nbsp;
    &nbsp;&nbsp;
    &nbsp;&nbsp; ADCON1 = 7&nbsp;&nbsp;
    &nbsp;&nbsp; TRISC = %10000000&nbsp;&nbsp;
    &nbsp;&nbsp;
    Main:&nbsp;&nbsp;
    &nbsp;&nbsp;
    &nbsp;&nbsp; HIGH LEDPIN ' Indicate PIC is running&nbsp;&nbsp;
    &nbsp;&nbsp; LOW DE_OR_RE ' Make ready for RX&nbsp;&nbsp;
    &nbsp;&nbsp; PAUSE 4 &nbsp;&nbsp;
    &nbsp;&nbsp; LOW LEDPIN&nbsp;&nbsp;
    &nbsp;&nbsp; if RCIF then&nbsp;&nbsp;
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; hserin 100,Main,[WAIT (&quot;S&quot,DATARECEIVED] ' Looking for &quot;S0&quot;&nbsp;&nbsp;
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; IF DATARECEIVED = &quot;0&quot; THEN&nbsp;&nbsp;
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&n bsp; HIGH DE_OR_RE ' Make ready for RX&nbsp;&nbsp;
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&n bsp; PAUSE 4&nbsp;&nbsp;
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&n bsp; HSEROUT [&quot;R1&quot;] ' Send R1
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ENDIF&nbsp;&nbsp;
    &nbsp;&nbsp; endif&nbsp;&nbsp;
    &nbsp;&nbsp; GOTO MAIN&nbsp;&nbsp;
    &nbsp;&nbsp;
    END</p>
    <hr>


    As soon as I connect the Master pic to the power I get the following, does not matter if the Slave pic is running or not, it return the same data.

    <b>
    WAITING FOR REPLY..
    REC:˙
    WAITING FOR REPLY..
    REC:˙
    WAITING FOR REPLY..
    REC:˙
    WAITING FOR REPLY..
    REC:˙
    WAITING FOR REPLY..
    REC:˙
    WAITING FOR REPLY..
    REC:˙
    WAITING FOR REPLY..
    REC:˙
    WAITING FOR REPLY..
    REC:˙
    WAITING FOR REPLY..
    REC:˙
    WAITING FOR REPLY..
    REC:˙
    WAITING FOR REPLY..
    REC:˙
    WAITING FOR REPLY..
    REC:˙
    WAITING FOR REPLY..
    REC:˙
    WAITING FOR REPLY..
    REC:˙
    WAITING FOR REPLY..
    REC:˙
    WAITING FOR REPLY..
    REC:˙
    WAITING FOR REPLY..
    REC:˙
    WAITING FOR REPLY..
    REC:˙
    WAITING FOR REPLY..
    REC:˙
    WAITING FOR REPLY..
    REC:˙
    WAITING FOR REPLY..
    </b>

    Thank you
    Koossa

  7. #7
    skimask's Avatar
    skimask Guest


    Did you find this post helpful? Yes | No

    Default

    I think you've got the idea...but one other thing I just thought about...

    You're checking RCIF in the code. If you're checking RCIF, that means there's already a byte in the receive buffer. HSERIN probably waits for the next byte to come along (basically ignoring what's already in the buffer). If you're already WAITing for a specific character, then why also wait for a flag in hardware? Also, you can make use of the TIMEOUT in the SERIN/HSERIN to keep your code from getting stuck.

  8. #8


    Did you find this post helpful? Yes | No

    Default

    Thank you, I will remove the RCIF and see what happens.

    There is a 100ms Timeout in my code.

    Koossa

  9. #9


    Did you find this post helpful? Yes | No

    Default

    I now get the following on the PC Side, so I assume it does not receive the "R"
    (<b>hserin 100,Main,[WAIT ("R"),DATARECEIVED]</b>) and reaches the Timeout of 100 milliseconds?


    <b>
    WAITING FOR REPLY..
    WAITING FOR REPLY..
    WAITING FOR REPLY..
    WAITING FOR REPLY..
    WAITING FOR REPLY..
    WAITING FOR REPLY..
    WAITING FOR REPLY..
    WAITING FOR REPLY..
    WAITING FOR REPLY..
    WAITING FOR REPLY..
    WAITING FOR REPLY..
    WAITING FOR REPLY..
    WAITING FOR REPLY..
    WAITING FOR REPLY..
    WAITING FOR REPLY..
    WAITING FOR REPLY..
    WAITING FOR REPLY..
    WAITING FOR REPLY..
    WAITING FOR REPLY..
    WAITING FOR REPLY..
    WAITING FOR REPLY..
    WAITING FOR REPLY..
    WAITING FOR REPLY..
    WAITING FOR REPLY..
    WAITING FOR REPLY..
    WAITING FOR REPLY..
    WAITING FOR REPLY..
    WAITING FOR REPLY..
    WAITING FOR REPLY..
    </b>


    Any ideas?

    Thank you
    Koossa

  10. #10
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    Yup that's it!

    PS: careful with your DEFINEs. DEFINE HSER_CLOERR 1 have to be DEFINE HSER_CLROERR 1
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

  11. #11


    Did you find this post helpful? Yes | No

    Default

    Thank you very much Mister_e!

    I have implement it

    <hr>
    Code of My Master PIC
    <br>
    <img src="http://www.picbasic.co.uk/forum/attachment.php?attachmentid=2296&stc=1&d=120129233 2" border="1">
    <hr>
    Code of My Slave PIC
    <br>
    <img src="http://www.picbasic.co.uk/forum/attachment.php?attachmentid=2297&stc=1&d=120129234 2" border="1">



    But it still only returns:
    <b>
    WAITING FOR REPLY..
    WAITING FOR REPLY..
    WAITING FOR REPLY..
    WAITING FOR REPLY..
    WAITING FOR REPLY..
    WAITING FOR REPLY..
    WAITING FOR REPLY..
    WAITING FOR REPLY..
    WAITING FOR REPLY..
    WAITING FOR REPLY..
    WAITING FOR REPLY..
    WAITING FOR REPLY..
    WAITING FOR REPLY..
    WAITING FOR REPLY..
    WAITING FOR REPLY..
    WAITING FOR REPLY..
    WAITING FOR REPLY..
    WAITING FOR REPLY..
    WAITING FOR REPLY..
    WAITING FOR REPLY..
    WAITING FOR REPLY..
    WAITING FOR REPLY..
    WAITING FOR REPLY..
    WAITING FOR REPLY..
    WAITING FOR REPLY..
    WAITING FOR REPLY..
    WAITING FOR REPLY..
    WAITING FOR REPLY..
    WAITING FOR REPLY..
    WAITING FOR REPLY..
    WAITING FOR REPLY..
    WAITING FOR REPLY..
    WAITING FOR REPLY..
    WAITING FOR REPLY..
    WAITING FOR REPLY..
    WAITING FOR REPLY..
    WAITING FOR REPLY..
    WAITING FOR REPLY..
    WAITING FOR REPLY..
    WAITING FOR REPLY..
    WAITING FOR REPLY..
    WAITING FOR REPLY..
    WAITING FOR REPLY..
    WAITING FOR REPLY..
    WAITING FOR REPLY..
    WAITING FOR REPLY..
    WAITING FOR REPLY..
    WAITING FOR REPLY..
    WAITING FOR REPLY..
    WAITING FOR REPLY..
    WAITING FOR REPLY..
    WAITING FOR REPLY..
    WAITING FOR REPLY..
    WAITING FOR REPLY..
    WAITING FOR REPLY..
    WAITING FOR REPLY..
    WAITING FOR REPLY..
    WAITING FOR REPLY..
    WAITING FOR REPLY..
    WAITING FOR REPLY..
    WAITING FOR REPLY..
    WAITING FOR REPLY..
    WAITING FOR REPLY..
    WAITING FOR REPLY..
    WAITING FOR REPLY..
    WAITING FOR REPLY..
    WAITING FOR REPLY..
    WAITING FOR REPLY..
    WAITING FOR REPLY..
    WAITING FOR REPLY..
    WAITING FOR REPLY..
    WAITING FOR REPLY..
    WAITING FOR REPLY..
    WAITING FOR REPLY..
    WAITING FOR REPLY..
    WAITING FOR REPLY..
    WAITING FOR REPLY..
    WAITING FOR REPLY..
    WAITING FOR REPLY..
    WAITING FOR REPLY..
    WAITING FOR REPLY..
    WAITING FOR REPLY..
    WAITING FOR REPLY..
    WAITING FOR REPLY..
    WAITING FOR REPLY..
    WAITING FOR REPLY..
    WAITING FOR REPLY..
    WAITING FOR REPLY..
    WAITING FOR REPLY..
    WAITING FOR REPLY..
    WAITING FOR REPLY..
    WAITING FOR REPLY..
    WAITING FOR REPLY..
    WAITING FOR REPLY..
    WAITING FOR REPLY..
    </b>
    Attached Images Attached Images   

  12. #12
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    what's your expected baudrate? I feel 2400 bauds right?

    so you should use...
    Code:
    DEFINE HSER_RCSTA 90h ' Enable serial port & continuous receive
    DEFINE HSER_TXSTA 20h ' Enable transmit, BRGH = 0
    DEFINE HSER_SPBRG 25  ' 2400 Baud @ 4MHz, 0.17%
    DEFINE HSER_CLROERR 1 ' Clear overflow automatically
    OR
    Code:
    DEFINE HSER_RCSTA 90h ' Enable serial port & continuous receive
    DEFINE HSER_TXSTA 24h ' Enable transmit, BRGH = 1
    DEFINE HSER_SPBRG 103  ' 2400 Baud @ 4MHz, 0.17%
    DEFINE HSER_CLROERR 1 ' Clear overflow automatically
    Unless your current USART setting show 600 baud.. and i'm not sure how god/bad it may works with your current hardware.

    I would tent to use Usart interrupt for that.. but you can still remove the TimeOut to see what happen. Your code work as design so far.

    Still looking...
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

  13. #13
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    Try adding "Fail-Safe" resistors to the RS-485 buss.

    2.2k from A to VDD
    2.2k from B to gnd

    When both ends are in receive mode, the buss is left floating.

    If you're going to have multiple slaves on long distance wires, you may also need 4 schottky diodes on each device.
    <br>
    DT

  14. #14


    Did you find this post helpful? Yes | No

    Default

    Thank you very much Mister_e, I have changed it to the second option, but still receives the :
    <b>
    WAITING FOR REPLY..
    WAITING FOR REPLY..
    WAITING FOR REPLY..
    WAITING FOR REPLY..
    WAITING FOR REPLY..
    WAITING FOR REPLY..
    WAITING FOR REPLY..
    WAITING FOR REPLY..
    WAITING FOR REPLY..
    WAITING FOR REPLY..
    WAITING FOR REPLY..
    WAITING FOR REPLY..
    WAITING FOR REPLY..
    WAITING FOR REPLY..
    WAITING FOR REPLY..
    </b>


    Darrel, the 2.2k from A to VDD and 2.2k from B to gnd, must I put them on the Master and all the Slaves?

    I will have long distance cables and multiple slaves.
    Where will I put the diodes?

    Thank you
    Koossa

  15. #15
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    The resistors and diodes should be on each device.

    I like the Schottky-Diode Termination method because it allows you the have a "Star" configuration on the 485 buss, and it uses less power than other methods.

    Other types of termination require the devices to be in a line with a single termination at the farthest end. This is not always practical, and invariably the farthest end device gets disconnected somewhere along the way, and there goes your termination. Unless someone remembers to terminate the next farthest device. (never happens)

    See figure 6 in this page for the diodes.
    http://www.maxim-ic.com/appnotes.cfm/an_pk/1090
    <br>
    DT

  16. #16


    Did you find this post helpful? Yes | No

    Default

    Darrel, thank you very much!!!

    I have updated my Schematic, could you please confirm that I understand it correctly (see attachment)?

    Koossa
    Attached Images Attached Images

  17. #17
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Thumbs up

    Yup!

    That looks good.
    <br>
    DT

  18. #18


    Did you find this post helpful? Yes | No

    Default

    I have updated everything now, but when I start it up it return the following, no matter if the Slave is running or not.

    <b>
    WAITING FOR REPLY..
    WAITING FOR REPLY..
    WAITING FOR REPLY..
    WAITING FOR REPLY..
    WAITING FOR REPLY..
    WAITING FOR REPLY..
    WAITING FOR REPLY..
    WAITING FOR REPLY..
    WAITING FOR REPLY..
    WAITING FOR REPLY..
    WAITING FOR REPLY..
    WAITING FOR REPLY..
    WAITING FOR REPLY..
    WAITING FOR REPLY..
    WAITING FOR REPLY..
    WAITING FOR REPLY..
    WAITING FOR REPLY..
    WAITING FOR REPLY..
    WAITING FOR REPLY..
    </b>


    Could this be a wiring problem or maybe something wrong in my code?

    Thx
    Koossa

  19. #19
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    You might try removing the handshaking part.
    Have the slave continuously transmit to the master, and see if you're getting anything.

    If not, then yeah, it could be a wiring problem.
    Or, 10-20 other things.
    <br>
    DT

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


    Did you find this post helpful? Yes | No

    Default Background Question

    Hi Koossa,
    Background Question:
    How much distance between master and slave? I am thinking maybe baud rate is too high due to line capacitance, I just had that problem with an ethernet connection, couldn't use 100 had to tell computer to use 10mbs.
    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.

  21. #21


    Did you find this post helpful? Yes | No

    Default

    Joe, the distance between my Master and slave is only about 1.5 meters.

    <hr>
    Darrel

    When I remove the handshacking it is working!!

    <b>
    WAITING FOR REPLY..
    REC:1
    WAITING FOR REPLY..
    REC:1
    WAITING FOR REPLY..
    REC:1
    WAITING FOR REPLY..
    REC:1
    WAITING FOR REPLY..
    REC:1
    WAITING FOR REPLY..
    REC:1
    WAITING FOR REPLY..
    REC:1
    </b>

    Do I need to play arround with "pause"?

  22. #22
    skimask's Avatar
    skimask Guest


    Did you find this post helpful? Yes | No

    Default

    See Posts #2 & #5.
    Failing that, post the program as it sits now...

  23. #23


    Did you find this post helpful? Yes | No

    Default

    Skimask

    Thank you for your reply!

    Whith the following code I only get..
    <b>
    WAITING FOR REPLY..
    WAITING FOR REPLY..
    WAITING FOR REPLY..
    WAITING FOR REPLY..
    WAITING FOR REPLY..
    WAITING FOR REPLY..
    WAITING FOR REPLY..
    WAITING FOR REPLY..
    WAITING FOR REPLY..
    WAITING FOR REPLY..
    WAITING FOR REPLY..
    WAITING FOR REPLY..
    WAITING FOR REPLY..
    WAITING FOR REPLY..
    WAITING FOR REPLY..
    </b>


    SLAVE
    <hr>
    <CODE>
    &nbsp;Include "Modedefs.bas"

    &nbsp;DEFINE&nbsp;&nbsp; OSC 4

    &nbsp;DE_OR_RE&nbsp;&nbsp;VAR PORTC.5&nbsp;&nbsp; ' DE and RE Pin of SN75176
    &nbsp;LEDPIN&nbsp;&nbsp; VAR PORTD.2&nbsp;&nbsp; ' LED to confirm PIC is running
    &nbsp;DATARECEIVED&nbsp;VAR BYTE
    &nbsp;
    &nbsp;'======== HSEROUT, HSERIN SETTINGS ==========
    &nbsp;DEFINE HSER_RCSTA 90h
    &nbsp;define HSER_TXSTA 24h
    &nbsp;define HSER_SPBRG 103
    &nbsp;DEFINE HSER_CLROERR 1
    &nbsp;RCIF&nbsp;&nbsp;VAR PIR1.5
    &nbsp;TXIF&nbsp;&nbsp;VAR PIR1.4

    &nbsp;ADCON1 = 7
    &nbsp;TRISC = %10000000
    &nbsp;
    &nbsp;pause 200
    &nbsp;HIGH LEDPIN
    Main:
    &nbsp;LOW DE_OR_RE&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;' Make ready for RX
    &nbsp;PAUSE 4
    &nbsp;hserin 100,Main,[WAIT ("S"),DATARECEIVED] ' Looking for "S0"
    &nbsp;IF DATARECEIVED = "0" THEN
    &nbsp;&nbsp;&nbsp;&nbsp;HIGH DE_OR_RE&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ' Make ready for RX
    &nbsp;&nbsp;&nbsp;&nbsp;PAUSE 4
    &nbsp;&nbsp;&nbsp;&nbsp;HSEROUT ["R1"]&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ' Send R1
    &nbsp;ENDIF
    &nbsp;GOTO MAIN

    END
    </CODE>
    <hr>

    MASTER
    <hr>
    <CODE>
    &nbsp;Include "Modedefs.bas"
    &nbsp;&nbsp;
    &nbsp;DEFINE&nbsp;&nbsp; OSC 4

    &nbsp;DE_OR_RE&nbsp;&nbsp;VAR PORTC.5&nbsp;&nbsp; ' DE and RE Pin of SN75176
    &nbsp;RS232_DEBUG_PIN VAR PORTB.5&nbsp;&nbsp; ' Debugging to PC
    &nbsp;LEDPIN&nbsp;&nbsp; VAR PORTD.2&nbsp;&nbsp; ' LED to confirm PIC is running
    &nbsp;DATARECEIVED&nbsp;VAR BYTE

    &nbsp;'======== HSEROUT, HSERIN SETTINGS ==========&nbsp;
    &nbsp;DEFINE HSER_RCSTA 90h
    &nbsp;define HSER_TXSTA 24h
    &nbsp;define HSER_SPBRG 103
    &nbsp;DEFINE HSER_CLROERR 1
    &nbsp;RCIF&nbsp;&nbsp;VAR PIR1.5
    &nbsp;TXIF&nbsp;&nbsp;VAR PIR1.4
    &nbsp;
    &nbsp;ADCON1 = 7
    &nbsp;TRISC = %10000000

    &nbsp;PAUSE 200
    &nbsp;HIGH LEDPIN
    &nbsp;
    Main:
    &nbsp;HIGH DE_OR_RE&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; ' Make ready for TX
    &nbsp;PAUSE 4
    &nbsp;HSEROUT ["S0"]&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp ; ' Send "S0"
    &nbsp;LOW DE_OR_RE&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;' Make ready for RX
    &nbsp;PAUSE 4
    &nbsp;SEROUT RS232_DEBUG_PIN,N2400,["WAITING FOR REPLY..", 13, 10] ' DEBUG TO PC
    &nbsp;hserin 100,Main,[WAIT ("R"),DATARECEIVED]
    &nbsp;SEROUT RS232_DEBUG_PIN,N2400,["REC:",DATARECEIVED, 13, 10] ' DEBUG TO PC
    GOTO Main
    END
    </CODE>
    <hr>


    Any idea what I am doing wrong?

    Thank you very much!!!!
    Koossa

  24. #24
    skimask's Avatar
    skimask Guest


    Did you find this post helpful? Yes | No

    Default

    It takes at least 87.5ms for each of those SEROUT debug statements to execute at 2400 baud. My guess is that both sides are getting sync'd up into not receiving each others inputs and they both end up missing each others outputs.
    How about getting rid of them and try to use LEDs to see if either program is getting what it should? Maybe use a couple of switches on one, and LEDs on the other, and get one PIC to follow the other PIC.

    Failing that, how about this:

    SLAVE
    ----------------------------------------------------------
    Include "Modedefs.bas"
    DEFINE OSC 4
    DE_OR_RE VAR PORTC.5 ' DE and RE Pin of SN75176
    LEDPIN VAR PORTD.2 ' LED to confirm PIC is running
    DATARECEIVED VAR BYTE
    '======== HSEROUT, HSERIN SETTINGS ==========
    DEFINE HSER_RCSTA 90h
    define HSER_TXSTA 24h
    define HSER_SPBRG 103
    DEFINE HSER_CLROERR 1
    RCIF VAR PIR1.5:TXIF VAR PIR1.4:ADCON1 = 7:TRISC = %10000000
    pause 200:HIGH LEDPIN
    Main:
    hserin 10,MAIN,[WAIT ("S"),DATARECEIVED] ' Looking for "S0"
    IF DATARECEIVED = "0" THEN
    HSEROUT ["R1"] ' Send R1
    else
    hserout ["RZ"] ' Send something to signify SOMETHING received
    ENDIF
    GOTO MAIN
    END
    --------------------------------------------------------------------------------

    MASTER
    --------------------------------------------------------------------------------
    Include "Modedefs.bas"
    DEFINE OSC 4
    DE_OR_RE VAR PORTC.5 ' DE and RE Pin of SN75176
    RS232_DEBUG_PIN VAR PORTB.5 ' Debugging to PC
    LEDPIN VAR PORTD.2 ' LED to confirm PIC is running
    DATARECEIVED VAR BYTE
    '======== HSEROUT, HSERIN SETTINGS ==========
    DEFINE HSER_RCSTA 90h
    define HSER_TXSTA 24h
    define HSER_SPBRG 103
    DEFINE HSER_CLROERR 1
    RCIF VAR PIR1.5:TXIF VAR PIR1.4:ADCON1 = 7:TRISC = %10000000
    PAUSE 200:HIGH LEDPIN
    Main:
    HSEROUT ["S0"] ' Send "S0"
    SEROUT RS232_DEBUG_PIN,N2400,["WAITING FOR REPLY..", 13, 10] ' DEBUG TO PC
    hserin 100,Main,[WAIT ("R"),DATARECEIVED]
    SEROUT RS232_DEBUG_PIN,N2400,["REC:",DATARECEIVED, 13, 10] ' DEBUG TO PC
    GOTO Main
    END

  25. #25


    Did you find this post helpful? Yes | No

    Default

    Here is my updated code.
    Still getting..
    <b>
    WAITING FOR REPLY..
    WAITING FOR REPLY..
    WAITING FOR REPLY..
    WAITING FOR REPLY..
    WAITING FOR REPLY..
    </b>


    Hope I am not missing something?


    Slave
    <img src="http://www.picbasic.co.uk/forum/attachment.php?attachmentid=2316&stc=1&d=120172226 9">


    Master
    <img src="http://www.picbasic.co.uk/forum/attachment.php?attachmentid=2317&stc=1&d=120172226 9">
    Attached Images Attached Images   

  26. #26
    skimask's Avatar
    skimask Guest


    Did you find this post helpful? Yes | No

    Default

    You've got LEDs on both modules, Yes?
    Well, use them!
    Make them blink whenever they 'receive a message' or whatever.
    Forget about a specific message format for now, just get the things to recognize data in general.
    And again, lose the SEROUT debug messages, make the LEDs blink or something.

    Unless of course your TX and RX lines are backwards or something...

  27. #27
    Join Date
    Jan 2006
    Location
    Istanbul
    Posts
    1,185


    Did you find this post helpful? Yes | No

    Default

    1. I would put a 1 second pause to Master at initial startup (no pause to slave)
    2. I would also make a minor modification as below:

    Instead of HSEROUT ["S0"] I would use HSEROUT ["XYZ","0"]

    and then
    instead of hserin 10,MAIN,[WAIT ("S"),DATARECEIVED] ' Looking for "S0"

    I would use HSERIN 10,MAIN,[WAIT ("XYZ"),DATARECEIVED] ' Looking for "0"


    ------------------
    "If the Earth were a single state, Istanbul would be its capital." Napoleon Bonaparte

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


    Did you find this post helpful? Yes | No

    Default

    Where is the interrupt routine? I see if rcif in the early code and it is now missing,
    look at this thread especially posts 8, 16.
    http://www.picbasic.co.uk/forum/showthread.php?t=4972
    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.

Similar Threads

  1. RS485 splitter
    By The Master in forum Off Topic
    Replies: 0
    Last Post: - 30th August 2009, 06:04
  2. RS485 Vs Wireless (TWS-434A)
    By koossa in forum Off Topic
    Replies: 3
    Last Post: - 11th April 2009, 12:40
  3. Using 16f676 with comms
    By Peter1960 in forum mel PIC BASIC Pro
    Replies: 9
    Last Post: - 28th October 2005, 20:01
  4. RS485 - ibutton network
    By ccsparky in forum mel PIC BASIC Pro
    Replies: 7
    Last Post: - 6th June 2005, 21:48
  5. RS485 Extender
    By SergioRM in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 28th December 2004, 00:09

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