Serial Pic to Pic using HSER


Closed Thread
Results 1 to 6 of 6
  1. #1
    Chadhammer's Avatar
    Chadhammer Guest

    Default Serial Pic to Pic using HSER

    I have a simple project going to get a toe hold on Pic to Pic serial comm. Trying to keep it simple and going one way com for now.

    by pushing button 1 or 2, PIC #1 sends a Binary number (1 or 2) to = "Hello" or "Goodbye" on the PIC #2.

    PIC #1's LCD indicates that the messages have been sent to PIC #2.

    I believe I have PIC #1 working but PIC #2 is not.

    Further, With PIC# 2 running, the LCD on PIC #1 will display then scramble itself up.

    <div style="margin:20px; margin-top:5px">
    <div class="smallfont" style="margin-bottom:2px">PIC #1 Code:</div>
    <pre class="alt2" style="margin:0px; padding:6px; border:1px inset; width:640px; height:300px; overflow:auto"><div dir="ltr" style="text-align:left;">
    '_______________Setup PIC 16F870___________________

    ADCON0.0=0 'disable analog comparator
    TRISA = %00000011 'Setup Port A
    define LCD_DREG PORTB ' Set data pin of LCD to
    define LCD_DBIT 0 ' PORTB.0-PORTB.3
    define LCD_RSREG PORTB ' Set RS bit of LCD to
    define LCD_RSBIT 4 ' PORTB.4
    define LCD_EREG PORTB ' Set E bit of LCD to
    define LCD_EBIT 5 ' PORTB.5
    DEFINE HSER_RCSTA 90h

    ADCON1=6 'disable analog comparator
    SW1 VAR PORTA.0
    SW2 VAR PORTA.1
    Chkswitch VAR BYTE
    '------------Main-----------------------------------
    Readpins:
    Chkswitch=PORTA & $03
    Select case Chkswitch
    case 1
    Goto Mess1
    case 2
    goto Mess2
    end select
    goto readpins
    '------------Subroutines----------------------------
    Mess1:
    Pause 500
    Lcdout $fe, 1, "Hello" ' Clear LCD screen
    Pause 500
    LCDOUT $fe, $c0, "message sent"
    Hserout [%00000001]
    return
    Mess2:
    Pause 500
    Lcdout $fe, 1, "Goodbye" ' Clear LCD screen
    Pause 500
    Lcdout $fe, $c0, "message sent"
    Hserout [%00000010]
    return
    End
    </div></pre>
    </div></div>

    I am using 1 pair of Cat 5 cable (10 feet) with no connectors

    <div style="margin:20px; margin-top:5px">
    <div class="smallfont" style="margin-bottom:2px">PIC #2 Code:</div>
    <pre class="alt2" style="margin:0px; padding:6px; border:1px inset; width:640px; height:300px; overflow:auto"><div dir="ltr" style="text-align:left;">
    '_______________PIC Setup__________________________

    intcon = %11000000 ' enable global and Peripheral interrupt
    ADCON1=6 'disable analog comparator
    TRISA = %11111111 'Set Port A to inputs
    pie1.5 = 1 ' enable interrupt on usart receive
    pir1.5 = 0 ' clearing interrupt flags
    define LCD_DREG PORTB ' Set data pin of LCD to
    define LCD_DBIT 0 ' PORTB.0-PORTB.3
    define LCD_RSREG PORTB ' Set RS bit of LCD to
    define LCD_RSBIT 4 ' PORTB.4
    define LCD_EREG PORTB ' Set E bit of LCD to
    define LCD_EBIT 5 ' PORTB.5
    Mess VAR PORTA
    '____________________Main_________________________ ___
    Start:
    PicRecieve:
    If PIR1.5 = 1 then Pic_serin
    Goto PicRecieve
    Pic_serin:
    Hserin [Mess]
    Goto PicRecieve
    IncomingM:
    Select case Mess
    case %00000001
    Goto Mess1
    case %00000010
    goto Mess2
    end select
    goto incomingM
    '___________________Subroutines___________________ ____
    Mess1:
    Pause 500
    LCDOUT $fe, 1, "Hello"
    return
    Mess2:
    Pause 500
    LCDOUT $fe, 1, "Goodbye"
    return
    end

    </div></pre>
    </div></div>

    Pretty new to PICs and programming. I appreciate greatly what I am learning from here.

    Thanks for all the help and any you can give.
    ChadMan

  2. #2
    Join Date
    Feb 2004
    Location
    Germany
    Posts
    762


    Did you find this post helpful? Yes | No

    Default

    Have you searched the Forum?

    this has been discussed many, many... times.
    regards

    Ralph

    _______________________________________________
    There are only 10 types of people:
    Those who understand binary, and those who don't ...
    _______________________________________________



  3. #3
    Chadhammer's Avatar
    Chadhammer Guest


    Did you find this post helpful? Yes | No

    Talking PIC to PIC using HSER IN/OUT fixed mostly

    In answer to your question: Yes I read every post I could find for about 3 days now.

    I found the problem was in the interupt I believe and some variable handling.

    My LCDs on both PICs seem to scramble after a little while. They come up mostly blacked out Characters with some garbage characters showing up randomly. I put .1 caps across the power in at the LCD and it seemed to slow it down some. Any ideas?

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


    Did you find this post helpful? Yes | No

    Default

    Hi ChadMan,

    More than likely, the problem is related to the "Context Saving" in your interrupt routine.

    Most often it's caused by saving the registers "Again".

    If the PIC has more than 2K of program space then PBP automatically inserts the Context Save routine, then it's up to you to restore them at the end of the interrupt.

    So, if you're saving them again with something like this.
    Code:
    myint
        movwf  wsave
        swapf  STATUS, W
        clrf   STATUS
        movwf  ssave
        movf   PCLATH, W
        movwf  psave
    Then you are overwriting the values that have already been saved. And, when they get restored, PBP get's very confused.

    If that's the case, simply remove that section, and it should work much better.

    HTH,
    &nbsp;&nbsp;&nbsp;Darrel

  5. #5
    Chadhammer's Avatar
    Chadhammer Guest


    Did you find this post helpful? Yes | No

    Default Thanks for the reply


    If that's the case, simply remove that section, and it should work much better.

    HTH,
    &nbsp;&nbsp;&nbsp;Darrel
    Looking thru the ASM generated by PBP, I could not find anything related to the PCLATH. Here is what I have working with the Help of MELabs of course (Thanks Mr. Leo!):

    Code:
            
            Adcon1 = 6 'disable analog comparator
            TRISA = %11111111 'Set Port A to inputs
    
            pir1.5 = 0 ' clearing interrupt flags
            Mess Var Byte
    '____________________Main_____________________
     
    Start:
    
    PicRecieve:      
          If PIR1.5 = 1 then Pic_serin
          Goto PicRecieve
     
          Pic_serin:
          Hserin [Mess]
    
    IncomingM:
            If Mess= %00000001 Then mess1
            If mess= %00000010 Then mess2
    I am using MPasm to assemble the PBP code. The only thing I see related to the inerupt in the ASM is here:
    <div class="smallfont" style="margin-bottom:2px">Code=</div>
    <pre class="alt2" style="margin:0px; padding:6px; border:1px inset; width:600px; height:225px; overflow:auto">PicBasic Pro Compiler 2.45a, (c) 1998, 2004 microEngineering Labs, Inc. All Rights Reserved.
    _USED EQU 1

    INCLUDE "C:\PBP\16F870.INC"


    ; Define statements.
    #define CODE_SIZE 2
    #define LCD_DREG PORTB
    #define LCD_DBIT 0
    #define LCD_RSREG PORTB
    #define LCD_RSBIT 4
    #define LCD_EREG PORTB
    #define LCD_EBIT 5

    RAM_START EQU 00020h
    RAM_END EQU 000BFh
    RAM_BANKS EQU 00002h
    BANK0_START EQU 00020h
    BANK0_END EQU 0007Fh
    BANK1_START EQU 000A0h
    BANK1_END EQU 000BFh
    EEPROM_START EQU 02100h
    EEPROM_END EQU 0213Fh

    R0 EQU RAM_START + 000h
    R1 EQU RAM_START + 002h
    R2 EQU RAM_START + 004h
    R3 EQU RAM_START + 006h
    R4 EQU RAM_START + 008h
    R5 EQU RAM_START + 00Ah
    R6 EQU RAM_START + 00Ch
    R7 EQU RAM_START + 00Eh
    R8 EQU RAM_START + 010h
    FLAGS EQU RAM_START + 012h
    GOP EQU RAM_START + 013h
    RM1 EQU RAM_START + 014h
    RM2 EQU RAM_START + 015h
    RR1 EQU RAM_START + 016h
    RR2 EQU RAM_START + 017h
    _Mess EQU RAM_START + 018h
    _PORTL EQU PORTB
    _PORTH EQU PORTC
    _TRISL EQU TRISB
    _TRISH EQU TRISC
    #define _PIR1_5 PIR1, 005h
    INCLUDE "R2_PIC.MAC"
    INCLUDE "C:\PBP\PBPPIC14.LIB"

    MOVE?CB 006h, ADCON1
    MOVE?CB 0FFh, TRISA
    MOVE?CT 000h, _PIR1_5

    LABEL?L _Start

    LABEL?L _PicRecieve
    CMPEQ?TCL _PIR1_5, 001h, _Pic_serin
    GOTO?L _PicRecieve

    LABEL?L _Pic_serin
    HSERIN?B _Mess

    LABEL?L _IncomingM
    CMPEQ?BCL _Mess, 001h, _mess1
    CMPEQ?BCL _Mess, 002h, _mess2

    LABEL?L _mess1
    PAUSE?C 001F4h
    LCDOUT?C 0FEh
    LCDOUT?C 001h
    LCDOUT?C 048h
    LCDOUT?C 065h
    LCDOUT?C 06Ch
    LCDOUT?C 06Ch
    LCDOUT?C 06Fh
    PAUSE?C 005DCh
    LCDOUT?C 0FEh
    LCDOUT?C 001h
    RETURN?

    LABEL?L _mess2
    PAUSE?C 001F4h
    LCDOUT?C 0FEh
    LCDOUT?C 001h
    LCDOUT?C 047h
    LCDOUT?C 06Fh
    LCDOUT?C 06Fh
    LCDOUT?C 064h
    LCDOUT?C 062h
    LCDOUT?C 079h
    LCDOUT?C 065h
    PAUSE?C 005DCh
    LCDOUT?C 0FEh
    LCDOUT?C 001h
    RETURN?
    END?

    END</div></pre>

    How long a cable do you think I can get away with? I am trying to evolving this into a paging system (functional learning project) and was thinking of using 100' of Telephone wire. Too far? Or would it be better to use a twisted pair like the Cat5 wire?

    Thanks Darrel
    ChadMan

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


    Did you find this post helpful? Yes | No

    Default

    Hmmm,

    Your first message stated that you had a problem with the Interrupt routine.

    Now I can see that you didn't have any interrupt routines, even though you had enabled Interrupts. So what I said previously, doesn't apply.

    As for the Maximum RS232 distance. The spec says 50ft max. But it really depends on the baud rate. I've run it up to 200' with 2400 baud.

    If you put an RS485 convertor on both ends, you can go up to 4000'

    Darrel

Similar Threads

  1. Serial VB 2005 pic 16f877a problems
    By Snap in forum mel PIC BASIC Pro
    Replies: 28
    Last Post: - 8th July 2013, 01:52
  2. PIC to PIC "wired" serial one-way communication - SERIN2/SEROUT2
    By flotulopex in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 14th April 2008, 21:02
  3. Automatic VB6 to pic serial connection
    By arniepj in forum Code Examples
    Replies: 13
    Last Post: - 10th January 2008, 08:57
  4. Serial Com Pic to Pic
    By uludere72 in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 31st May 2005, 11:06
  5. Serial communication PIC to PIC help.
    By Rubicon in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 24th January 2005, 16:45

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