Rs485 Using Hserin/hserout


Results 1 to 28 of 28

Threaded View

  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.

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 : 0

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