Serial comm between two PICs


Closed Thread
Results 1 to 25 of 25
  1. #1
    Join Date
    Feb 2006
    Posts
    20

    Unhappy Serial comm between two PICs

    Hi, I am trying to interface two PICs together via serial connection. I am using two PIC16F88s running at 4mhz. PortB.0 on the tx chip is connected to PortB.0 on the rx chip. I have a button connected to PortB.1 on the tx that when pushed causes the pin to go high. I have an Led on PortB.1 of the rx. My goal is to transmit the state of the button to the rx chip to turn the LED on or off. This is just an experiment with serial communications and the actual application will be much more complex. here is my code:

    Tx:

    Include "modedefs.bas"
    b0 VAR BYTE

    Start:
    IF PortB.1 = 1 THEN LET b0 = 1
    LET b0 = 0
    SEROUT PortB.0, N2400, [b0]
    GOTO Start



    Rx:

    Include "modedefs.bas"
    b0 VAR BYTE

    Start:
    SERIN PortB.0, N2400, [B0]
    IF b0 = 1 THEN HIGH PortB.1
    LOW PortB.1
    GOTO Start

    When I push the button nothing happens. I am confident that the circuit works so I am convinced that the problem is in the software. I do not fully understand the SERIN SEROUT commands and am interested in what happens when a serial command is executed. Does the Rx wait for the data or if there isn't any go to the next line of code? Do the SERIN SEROUT commands have to be executed at the same time for the communication to be successful.
    I appreciate all the help i can get on this.

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

    Default

    Hi ALFRED,

    When the program gets to the SEROUT statement, the value of b0 is always 0.
    Code:
    Tx:
    
    Include "modedefs.bas"
    b0 VAR BYTE
    
    Start:
    IF PortB.1 = 1 THEN LET b0 = 1
    LET b0 = 0
    SEROUT PortB.0, N2400, [b0]
    GOTO Start
    >> Does the Rx wait for the data or if there isn't any go to the next line of code?
    Yes, the way you have it now, it will wait forever to receive a byte. If you wanted it to continue if it doesn't receive anything, you can add a timeout to the SERIN statement.

    >> Do the SERIN SEROUT commands have to be executed at the same time for the communication to be successful.
    YES!

    DT

  3. #3
    Join Date
    Feb 2006
    Posts
    20

    Question

    Hi, i just realized that I typed my code in wrong. I had it the way you suggested but it still doesn't work. any other ideas of what could be wrong?
    Thanks for any more help.

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

    Default

    Well, it wasn't really a suggestion. I was just pointing out part of the problem.

    My suggestion would be to place the b0 = 0 line just prior to the IF PortB.1 = 1 line. Like this...
    Code:
    Tx:
    
    Include "modedefs.bas"
    b0 VAR BYTE
    
    Start:
        LET b0 = 0
        IF PortB.1 = 1 THEN LET b0 = 1
        SEROUT PortB.0, N2400, [b0]
        PAUSE 10
    GOTO Start
    And, add a short pause in the loop. Just to space things out a bit.

    Then on the receiving end...
    Code:
    Rx:
    
    Include "modedefs.bas"
    b0 VAR BYTE
    
    Start:
        SERIN PortB.0, N2400, [B0]
        IF b0 = 1 THEN 
            HIGH PortB.1
        ELSE
            LOW PortB.1
        ENDIF
    GOTO Start
    DT

  5. #5
    Join Date
    Sep 2003
    Location
    Vermont
    Posts
    373

    Default

    Start:
    LET b0 = 0
    IF PortB.1 = 1 THEN LET b0 = 1
    SEROUT PortB.0, N2400, [b0]
    PAUSE 10
    GOTO Start

    Don't forget the "ENDIF"

    Start:
    LET b0 = 0
    IF PortB.1 = 1 THEN LET b0 = 1
    * ENDIF*
    SEROUT PortB.0, N2400, [b0]
    PAUSE 10
    GOTO Start

  6. #6
    Join Date
    Feb 2006
    Posts
    20

    Question

    Hi, I tried Ron Marcus's "endif" and it did not compile. nothing seems to be working and I am beginning to thing that I should try the HSERIN and HSEROUT commands instead. My actual application will be a PIC 18F4320 PIC controlling 12 servos and monitoring 12 pushbuttons. The idea is to take the load of controlling servo motion routines of the master controller. I want to control the motion routines of the servos with the master controller via serial link. Does anyone have any ideas? I think that the hardware serial port would make the timing requirements easier to deal with but I do not fully understand the HSERIN HSEROUT commands and USAURT could someone please explain all this to me?

  7. #7
    Join Date
    Sep 2003
    Location
    Vermont
    Posts
    373

    Default

    O.K... Try this,

    Start:
    b0 = 0
    IF PortB.1 = 1 THEN
    b0 = 1
    ENDIF
    SEROUT PortB.0, N2400, [b0]
    PAUSE 10
    GOTO Start

    Now, it looks like you are using Stamp type conventions for labels. b0 is not very descriptive.

    button1 var portc.0

    This will alias the above port pin (can be any pin) so you can use a more descriptive term and it will be easier to follow the program in the future. Also, "Let" is unnecessary with PBP. It will still work,and if it is habit, then do it. Do you have the apropriate pullup(down) resistors for your switches?

  8. #8
    Join Date
    Feb 2006
    Posts
    20

    Exclamation

    HI, I tried the code that Ron Marcus posted and it did not work. I think the problem is in the receiver PIC software, (when I hook a speaker up to the serial line I can hear the data flowing). I have now tried several ways to make a serial connection but none worked. These attempts included DEBUG and DEBUGIN commands as well as HSERIN and HSEROUT commands. I would be extremely happy if someone could post some sample code and hardware information on getting a serial connection working between two PICs using any method ( HSERIN, DEBUG ect.). I appreciate any help you can offer on this problem.

  9. #9
    Join Date
    Oct 2013
    Posts
    9

    Default Re: Serial comm between two PICs

    I have a problem with comunication betwin two PIC-es
    PIC1-16F877A
    PIC2-16F628A

    PIC1 code:
    TRISC=%00000010
    PORTC=%00000000
    DEFINE BEBUG_REG PORTC
    DEFINE DEBUG_BIT 0
    DEFINE DEBUG_BAUD 2400
    DEFINE DEBUG_MODE 0
    DEFINE OSC 4
    DEFINE DEBUG_PACING 10
    BROJ VAR BYTE
    POCETAK:
    WHILE PORTC.1=0
    DEBUG BROJ
    WEND
    GOTO POCETAK
    END


    PIC2-code

    TRISA=%00000000
    TRISB=%00000010
    DEFINE OSC 4
    DEFINE DEBUG_REG PORTB
    DEFINE DEBUG_BIT 1
    DEFINE DEBUG_BAUD 2400
    DEFINE DEBUG_MODE 0
    DEFINE DEBUG_PACING 10
    BR VAR BYTE
    POC:
    DEBUGIN [BR]
    WRITE 00,BR
    GOTO POC
    END


    But something is wrong but what?

  10. #10
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,517

    Default Re: Serial comm between two PICs

    Hi,
    But something is wrong but what?
    Well, for starters, the line DEFINE BEBUG_REG PORTC is wrong.

    /Henrik.

  11. #11
    Join Date
    Oct 2013
    Posts
    9

    Default Re: Serial comm between two PICs

    Sorry, but it was wrong tiping the letter, but it is steel not working.

  12. #12
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,517

    Default Re: Serial comm between two PICs

    It's hard to guess since you haven't posted the actual code you're using, missing variable declarations etc but "it doesn't work" is THE worst problem description you can ever give - of course "it doesn't work"... You wouldn't even be posting here in the first place if it did, right?

    So, What exactly doesn't work? How do expect it TO work? What have you done, except posting here, to try finding the issue?

    Are the sending PIC actually running? Is it running at the correct speed (4MHz)? Is it sending anything at all? Is the receiving PIC actually running? Is IT running at the correct speed (4MHz)?

    One thing, which doesn't really prevent it from "working" but may be fooling you, is that on the sending side the variable BROJ never changes, it's always zero (or perhaps there's more code that you didn't show).

    /Henrik.

  13. #13
    Join Date
    Oct 2013
    Posts
    9

    Default Re: Serial comm between two PICs

    Here are the codes of pic1 and pic2.Pic1 have the quartc on 8 MHz but I gave it a DEFINE OSC 4
    becouse the pic2 works at 4 MHz.I am shure that everything works in PIC1 exept DEBUG becouse I checkedout.I am shure that PIC2 is running becouse it puts the port-out signals in the right order and it change the PIC2 addresses 00,01,02,03 to zero values (00,00,00,00).PIC2 have an internal OSC and it works at 4 MHz and I can't measure it, but PIC1 works in 8 MHz but I guess it devides the 8MHZ becouse I wrote DEFINE OSC 4.I am wondering if the BAUDRATE is to fast or is the code of DEBUG correct?
    Attached Files Attached Files

  14. #14
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,517

    Default Re: Serial comm between two PICs

    Hi,
    but I guess it devides the 8MHZ becouse I wrote DEFINE OSC 4
    Not at all. That's not what DEFINE OSC does, look it up in the manual.

    I am wondering if the BAUDRATE is to fast or is the code of DEBUG correct?
    Yes, the PIC is running at 8MHz but you're telling the compiler it's running at 4MHz. The compiler calculates the baudrate timings based on 4MHz when in reallity it's running twice as fast - the actual baudrate will be double what you tell it.

    /Henrik.

  15. #15
    Join Date
    Oct 2013
    Posts
    9

    Default Re: Serial comm between two PICs

    Ahaa I will change PIC1 code to DEFINE OSC 8 and then I will let you know what will happen.Thanks.

  16. #16
    Join Date
    Oct 2013
    Posts
    9

    Default Re: Serial comm between two PICs

    It is not working.I tried with SERIN and SEROUT instruction but it is the same thing.Is it the SERIN and SEROUT code wrong written?
    TRISC=%00000010
    PORTC=%00000000
    INCLUDE "Modedefs.bas"
    DEFINE OSC 8
    DEFINE DEBUG_PACING 100
    BROJ VAR BYTE
    BROJ=9
    POCETAK:
    WHILE PORTC.1=0
    SEROUT PORTC.0,T2400,[BROJ]
    WEND
    GOTO POCETAK
    END


    PIC2:
    PCON=%00001011
    TRISA=%00000000
    TRISB=%00000010
    DEFINE OSC 4
    INCLUDE "Modedefs.bas"
    BR VAR BYTE
    POC:
    SERIN PORTB.1,T2400,[BR]
    WRITE 00,BR
    GOTO POC
    END

  17. #17
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,517

    Default Re: Serial comm between two PICs

    Hi,
    Again, saying "it doesn't work" really does not make it easy trouble shooting someone elses code from a remote location. You really need to figure out WHAT isn't working. Where is the problem, is it the sending PIC or the receiving PIC? Perhaps the communication is working just fine and it's the fact that your sending the same value all the time that's making it LOOK like it's not working? How are you determining that it's not working? What does it do?

    One thing I did notice, in your previosuly posted code, is that you're using DEFINE DEBUG_XXXX on the receiving side as well. You need to use DEFINE DEBUGIN_XXXX.

    Another thing I'd do is implement some sort of delay, or even handshake, so that the sending PIC sends the value ONCE each time instead of sending it over and over again as fast as it possibly can.

    /Henrik.

  18. #18
    Join Date
    Dec 2010
    Posts
    409

    Default Re: Serial comm between two PICs

    I've also found it useful to stick a PC serial port in the middle during development. Set up your PC to run a terminal program, then start with making sure the transmitter and receiver work on their own before trying them together. Divide and conquer.

  19. #19
    Join Date
    Oct 2013
    Posts
    9

    Default Re: Serial comm between two PICs

    Quote Originally Posted by HenrikOlsson View Post
    your sending the same value all the time that's making it LOOK like it's not working?What does it do?
    /Henrik.
    I am shure that the PIC1 is sending the diferent value every time becouse I have LCD out view on PIC1.I am not shure that PIC2 is resiveing the data becouse the adress of PIC2: 00 is allwayes FF.So I must get a logic analiser and see what happens on resive PORTB.1.

  20. #20
    Join Date
    Jun 2009
    Location
    Sc*nthorpe, UK
    Posts
    333

    Default Re: Serial comm between two PICs

    Having read the manual the serin statement has this format.

    SERIN Pin,Mode,{Timeout,Label,}{[Qual...],}{Item...}

    As you can see square brackets are around the Qualifier.

    The statement in your code

    SERIN PORTB.1,T2400,[BR]

    Is therefore waiting for BR as a qualifier?

  21. #21
    Join Date
    Oct 2013
    Posts
    9

    Default Re: Serial comm between two PICs

    Quote Originally Posted by EarlyBird2 View Post

    SERIN Pin,Mode,{Timeout,Label,}{[Qual...],}{Item...}
    The statement in your code
    SERIN PORTB.1,T2400,[BR]
    Is therefore waiting for BR as a qualifier?
    I had never use SERIN or SEROUT or DEBUG or DEBUGIN instruction so I can't shurely tell you what' right. I need to send decimal value of GREJAC from PIC1 to BR on PIC2.Then a decimal value of BR I will compare in SELECT CASE rutine which I didn't wrote.

  22. #22
    Join Date
    Dec 2010
    Posts
    409

    Default Re: Serial comm between two PICs

    SERIN PORTB.1,T2400,[BR] should be SERIN PORTB.1, T2400, BR

  23. #23
    Join Date
    Jun 2009
    Location
    Sc*nthorpe, UK
    Posts
    333

    Default Re: Serial comm between two PICs

    Are you certain Charlie? I was not sure simply because all the way through this thread everyone has used the [BR] version of the serin instruction. Just goes to show that even experts can make mistakes

  24. #24
    Join Date
    Dec 2010
    Posts
    409

    Default Re: Serial comm between two PICs

    According to my manual, yes. (0609)
    I have not tried it. I like SEROUT for transmitting, but generally find using the hardware receiver (HSERIN) easier to get going.

  25. #25
    Join Date
    Oct 2013
    Posts
    9

    Default Re: Serial comm between two PICs

    Now I made a PIC2 as a transmiter and pic1 as a resiver becouse I have a LCD out on pic1 so I can see what is happening all the time.I took osciloscope and found that pic 2 is not transmiting anything at all.This is the code of the pic2 - transmiter:-16f628a

    INCLUDE "Modedefs.bas"
    DEFINE OSC 4
    PCON=%00001011
    CMCON=%00000111
    TRISB=%00000000
    TRISA=%00100000
    PORTA=0
    PORTB=0
    BR VAR BYTE
    BR=15
    pocetak:
    SEROUT PORTB.1,T2400,[BR]
    PAUSE 5000
    GOTO pocetak
    END

    Is the code wrong?I have 5V on the PORTB.1 and it is not changeing at the time.

Similar Threads

  1. Pic to pic interrupt ans serial comm
    By ronjodu in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 10th May 2008, 23:43
  2. Multiple Pics to One serial port
    By Rleonard in forum Serial
    Replies: 1
    Last Post: - 18th January 2007, 19:30
  3. Big characters on HD44780 4x20
    By erpalma in forum mel PIC BASIC Pro
    Replies: 23
    Last Post: - 7th January 2007, 03:21
  4. 16F877, DS18S20 and Serial Comm Problem
    By YellowTang in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 26th April 2004, 11:36
  5. serial comm from Pic to STAMP
    By d1camero in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 5th April 2004, 00:58

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