Can't reliably transfer over serial connection between two PICs


+ Reply to Thread
Results 1 to 6 of 6
  1. #1
    Join Date
    Feb 2013
    Posts
    1,141

    Default Can't reliably transfer over serial connection between two PICs

    Hello.
    Having a strange issue.

    PIC18F45K80 and PIC16F1936.

    they are connected to each other via 22K resistor. PORTC.3 on both.

    Trying to run the simple code:

    TX: (1936)

    Code:
    barka:
    serout2 portc.3,84,[345]
    pause 100
    goto barka
    RX (45K80)

    Code:
    taka:
    serin2 portc.3,84,[x]
    if x=345 then stop
    lcdout $fe, $c0, "X=", dec x, "    "
    goto taka
    It does not work, it never receives "345". received value is always random, but quite often it is 89.
    Variable types are proper, port configs also.
    I tried to change direction and send from 18F to 16F - same issue.
    Checked signals with scope - no issues.
    Tried to various resistors - 1K, 10K, 22K - no difference.

    Yes I know that hardware port should be used, but PCB is already made.
    Distance from one chip to another is about 10 centimeters and they're both on the same PCB and same power supply. I've checked signals with scope and there are no issues, like low levels or noise.

    I tried to make code more complicated, like sending 8 characters + control character (enter) -this improves it a bit, might say that at least one try from 10 delivers proper result. Sometimes, it starts to work fine, but then again messes up. Also tried reducing port speed to 1200bps - no change.


    Any ideas?

  2. #2
    Join Date
    Nov 2003
    Location
    Greece
    Posts
    4,148


    Did you find this post helpful? Yes | No

    Default Re: Can't reliably transfer over serial connection between two PICs

    1. How can you be sure that the receiving PIC will start "listening" just before the sending PIC? Better use a start character on the sender and use this character as a WAIT one on the receiving PIC to synchronize both.

    2. How is variable x defined? BYTE or WORD? 345 is more than a BYTE can store. 345 in Binary is 101011001. So if x is byte, it will randomly store 01011001 which happens to be, guess what?
    Exactly 89...!!!

    Ioannis

  3. #3
    Join Date
    Feb 2013
    Posts
    1,141


    Did you find this post helpful? Yes | No

    Default Re: Can't reliably transfer over serial connection between two PICs

    Yes, X is word type, and it sometimes get values over 8 bits, but never 345.
    I tried sending lower numbers, like 4 or 16 or whatever - they also got distorted.

    I also tried like this:

    arraywrite textline, ["ABCD1234"]serout2 portc.3,84,[STR textline\8,13]

    Sometimes whole string is delivered fine, sometimes it is all garbled.

    As I understand, you suggest to use WAIT/WAITSTR statements?

    Meanwhile, by help of AI, this crude bit-banging "protocol" was developed. By hand tuning PAUSEUS values, it works, but I'd prefer "factory" solution if possible.

    Sender:
    Code:
    Main:
        FOR TxValue = 0 TO 254
            GOSUB SendNumber
            PAUSE 300      ' Wait before sending next number
        NEXT
        GOTO Main
    
    
    ' --- Subroutine: SendNumber ---
    SendNumber:
        ' Start bit (LOW pulse)
        LOW DataPin
        PAUSEUS 500
        HIGH DataPin
        PAUSEUS 500
    
    
        ' Send 4 bits (LSB first)
        FOR BitMask = 0 TO 7
            IF (TxValue >> BitMask) & 1 THEN
                HIGH DataPin
            ELSE
                LOW DataPin
            ENDIF
            PAUSEUS 500
            HIGH DataPin                 ' Return line HIGH between bits
            PAUSEUS 500
        NEXT
        RETURN
    Receiver

    Code:
    Mainx:
        ' --- Wait for start bit: must see line LOW ---
    WaitForStart:
        IF DataPin = 1 THEN WaitForStart  ' Keep waiting while idle is HIGH
        PAUSEUS 490                      ' Wait a bit into LOW
        IF DataPin = 0 THEN              ' Still LOW? good start
            GOSUB ReadBits
        ENDIF
        GOTO Mainx
    
    
    ' --- Subroutine: ReadBits ---
    ReadBits:
        ' Wait until line goes back HIGH before first bit
        DO
        LOOP UNTIL DataPin = 1
        PAUSEUS 1000                      ' Move to middle of first bit period
    
    
        RxValue = 0
        FOR BitMask = 0 TO 6
            IF DataPin = 1 THEN
                RxValue = RxValue | (1 << BitMask)
            ENDIF
            PAUSEUS 1020                ' Move to middle of next bit slot
        NEXT
    
    
        ' --- Ensure line is HIGH again before next frame ---
        DO
        LOOP UNTIL DataPin = 1
    
    
        ' --- Display received value on LCD ---
        LCDOUT $FE,1                     ' Clear display
        LCDOUT "Received: ", DEC RxValue
    
    
        RETURN

  4. #4
    Join Date
    Feb 2013
    Posts
    1,141


    Did you find this post helpful? Yes | No

    Default Re: Can't reliably transfer over serial connection between two PICs

    I just tried this code, and it does not work:


    TX:

    Code:
    Main:
        FOR TxValue = 0 TO 255
          
            SEROUT2 DataPin, 84, [100,5,200, TxValue]
            PAUSE 500      
        NEXT
        GOTO Main
    RX:

    Code:
    LCDOUT $FE,1
    LCDOUT "Waiting data..."
    
    
    
    
    
    
    RxValue VAR BYTE
    
    
    Mainx:
    
    
        SERIN2 RxPin, 84, [WAIT (100,5,200), RxValue]
    
    
    LCDOUT $FE, $C0, "Rx=", dec RXvalue, "   "
    
    
        GOTO Mainx

  5. #5
    Join Date
    Feb 2013
    Posts
    1,141


    Did you find this post helpful? Yes | No

    Default Re: Can't reliably transfer over serial connection between two PICs

    if I reduce control values to 1 or two, then it works, but totally wrong data is being decoded.

  6. #6
    Join Date
    Nov 2003
    Location
    Greece
    Posts
    4,148


    Did you find this post helpful? Yes | No

    Default Re: Can't reliably transfer over serial connection between two PICs

    How about this code:

    Code:
    x vart word
    x=345
    barka:
    serout2 portc.3,84,["s",x]
    pause 100
    goto barka
    RX (45K80)

    Code:
    x var word
    taka:
    serin2 portc.3,84,[wait("s"), x]
    if x=345 then lcdout $fe, $c0, "X=", dec x, "    "
    goto taka
    Ioannis

Similar Threads

  1. Csv file transfer directly into excel using vb for serial coms
    By longpole001 in forum mel PIC BASIC Pro
    Replies: 6
    Last Post: - 20th May 2016, 08:43
  2. Usb to ttl modules - connection to pics at 3.3v
    By longpole001 in forum mel PIC BASIC Pro
    Replies: 24
    Last Post: - 12th March 2015, 20:03
  3. Replies: 1
    Last Post: - 23rd December 2006, 10:38
  4. large file transfer petween pics
    By Ron Marcus in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 3rd July 2005, 17:04
  5. Can serial transfer binary data?
    By TurboLS in forum mel PIC BASIC Pro
    Replies: 6
    Last Post: - 12th March 2005, 08:27

Members who have read this thread : 6

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