PIC to PIC communication between 18F4620 & 10F202


Closed Thread
Results 1 to 9 of 9

Hybrid View

  1. #1
    Join Date
    May 2014
    Posts
    7

    Default PIC to PIC communication between 18F4620 & 10F202

    Hello everyone,

    This is my first post here. I would appreciate if you could show my mistakes.

    I am trying to form a serial communication between 2 PICs: 18F4620 (Let's call this one A) and 10F202 (B)

    18F4620(A) | 10F202 (B)
    ------------------------
    AN0 -------|------ GP0
    AN1 -------|------ GP1

    PIC A has an LCD attached to it, so I can check what is going on, no such thing on B.

    What I am trying to do is send data from A to B, then receive it back: First A -> B then B -> A

    For test purposes I sent data from B to A and confirmed it is working.

    Here is my code for A (18F4620)

    Code:
    include "modedefs.bas"
    
    TRISA = 0 'Set PORTA to all outputs - NOT SURE IF THESE ARE REQUIRED
    TRISB = 0 'Set PORTB to all outputs
    
    
    DEFINE OSC 20
    DEFINE LCD_DREG    PORTB
    DEFINE LCD_DBIT 4
    DEFINE LCD_RSREG PORTB
    DEFINE LCD_RSBIT 2
    DEFINE LCD_EREG    PORTB
    DEFINE LCD_EBIT 3
    RW var portb.1:output portb.1:rw=0      ' LCD RW
    DEFINE LCD_BITS 4
    DEFINE LCD_LINES 4
    DEFINE LCD_COMMANDUS 2000
    DEFINE LCD_DATAUS 50     
    Pause 1000
    DEFINE HSER_BAUD 9600
    ADCON1 = 15                             ' ANALOGS to DIGITAL
    
    
    SI VAR PORTA.0:input PORTA.0
    SO VAR PORTA.1:output PORTA.1
    
    
    GG VAR WORD:GG=18
    TT var byte:TT=4
    
    
    lcdout $FE,130,"SEROUT:[",#gg,"]"
    SERout rout,N9600,[gg]
    
    
    SERIN SI,1000,loop,N9600,tt            
    lcdout $FE,214,"SERIN:[",#tt,"]"
    
    
    end
    My code for B (10F202)
    Code:
    include "modedefs.bas"
    
    TRISA = 0 'Set PORTA to all outputs     ' Are these necessary? 
    TRISB = 0 'Set PORTB to all outputs
    
    
    DEFINE OSC 4
    ADCON1 = 7                              ' ANALOGS to DIGITAL ?
    
    
    ROUT var GPIO.0:output GPIO.0
    RIN VAR GPIO.1:INPUT GPIO.1
    
    
    RR var word
    
    
    SERIN rin,N9600,rr
    pause 1000
    
    
    SERout rout,N9600,[rr]
    END
    I am quite new to PicBasic Pro, just put together some stuff I found but it is not working. I tried the above with loops as well, but since SERIN waits there till it receives something it looks fine to me this way.

    Thanks in advance

  2. #2
    Join Date
    Nov 2005
    Location
    Bombay, India
    Posts
    966


    Did you find this post helpful? Yes | No

    Default Re: PIC to PIC communication between 18F4620 & 10F202

    The way you have coded, it will function like a single shot.

    You may want to repeat the operation infinitely to ensure that it keeps doing the operations over and over again.

    A Do-Loop construct or even a While-Wend might be helpful.
    Something like this
    Code:
          Do
              SERIN rin,N9600,rr
              SERout rout,N9600,[rr]
          Loop
    You will need this on both sides. Eventually though, you will realize, you may need to receive using interrupts. That makes your code much more reliable as the communications you are trying to realize are asynchronous.

    END does nothing other than tell the processor to spin like a dog running after its own tail on the same spot.

  3. #3
    Join Date
    May 2014
    Posts
    7


    Did you find this post helpful? Yes | No

    Default Re: PIC to PIC communication between 18F4620 & 10F202

    Re: Jerson

    Thank you for your answer.

    After reading your post I started testing with loops, but did not work. Now I will try interrupt.

  4. #4
    Join Date
    Nov 2003
    Location
    Wellton, U.S.A.
    Posts
    5,924


    Did you find this post helpful? Yes | No

    Default Re: PIC to PIC communication between 18F4620 & 10F202

    If you can not get it working in a loop you will never get an interrupt to work.
    Take a look at this
    http://melabs.com/samples/PBP-mixed/serin.htm
    Dave
    Always wear safety glasses while programming.

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


    Did you find this post helpful? Yes | No

    Default Re: PIC to PIC communication between 18F4620 & 10F202

    Have you set DEFINE_OSC to proper values on both chips? various speeds can cause devices not to listen each other properly, unless you use HSERIN statement.

  6. #6
    Join Date
    May 2014
    Posts
    7


    Did you find this post helpful? Yes | No

    Default Re: PIC to PIC communication between 18F4620 & 10F202

    Re: CuriousOne

    Yes, OSC has been defined on both chips, however they are different from each other. 18F4620 is using OSC 20 (external), 10F202 is using OSC 4 (internal). Could this be the reason?

    Re: mackrackit

    I tried with loops, interrupts but nothing has worked so far. I think the main problem is synchronization. Let me try to explain:

    In an ideal case it should be (skipping definitions):

    18F4620
    --------
    PAUSE 1000
    SEROUT PORTA.1,N9600,[GG]
    SERIN PORTA.0,N9600,[TT]
    lcdout $FE,214,"SERIN:[",#tt,"]"

    10F202
    -------
    SERIN GPIO.1,N9600,[RR]
    SEROUT GPIO.0,N9600,[RR]

    time analysis:
    TIME | 18F4620 ----------| 10F202
    ---------------------------------------
    0 ---| PAUSE 1000 ------|SERIN (waiting)
    1 ---| SEROUT ----------| message received
    2 ---| SERIN (waiting) ---| SEROUT
    3 ---| message received -|
    4 ---| LCDOUT ----------|

    I think 2-pic communications usually go in 1-way. Pic_1 receives data from Pic_2 and lights a led or something. But in this case both pics sending and receiving messages in a very short time should be causing the problem.

  7. #7
    Join Date
    Jan 2005
    Location
    Montreal, Quebec, Canada
    Posts
    3,154


    Did you find this post helpful? Yes | No

    Default Re: PIC to PIC communication between 18F4620 & 10F202

    I've had PICs talk together with different osc speeds.

    As long as the registers and communication commands are set properly, the hardware wired properly, it should work.

    Robert

  8. #8
    Join Date
    Jan 2005
    Location
    Montreal, Quebec, Canada
    Posts
    3,154


    Did you find this post helpful? Yes | No

    Default Re: PIC to PIC communication between 18F4620 & 10F202

    10F202 should wait to receive data, the master should wait to receive reply.

    I don't see any waiting in the code above.

    But in this case both pics sending and receiving messages in a very short time should be causing the problem.
    Most likely.

    Robert

  9. #9
    Join Date
    May 2014
    Posts
    7


    Did you find this post helpful? Yes | No

    Default Re: PIC to PIC communication between 18F4620 & 10F202

    Code:
    DEFINE OSCCAL_1K 1      'Load oscillator calibration
    This single line of code added to 10F202 fixed all my problems.

Similar Threads

  1. Fast yet reliable PIC-PIC communication?
    By Kamikaze47 in forum mel PIC BASIC Pro
    Replies: 6
    Last Post: - 27th September 2009, 15:34
  2. Pic 18f4620
    By 78Davide in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 6th May 2009, 19:24
  3. Coding Style - PIC 18F4620 config fuses
    By BrianT in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 8th January 2008, 20:05
  4. SPI PIC to PIC Bi-Directional Communication
    By CocaColaKid in forum Serial
    Replies: 5
    Last Post: - 23rd December 2006, 22:44
  5. PIC to PIC RS485 with users the communication
    By x0914667 in forum Serial
    Replies: 2
    Last Post: - 21st July 2005, 00:20

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