Multiple PICs on usart network


Closed Thread
Results 1 to 40 of 42

Hybrid View

  1. #1
    Join Date
    Jan 2005
    Location
    Montreal, Quebec, Canada
    Posts
    2,597

    Question Multiple PICs on usart network

    Hi,

    This is what my circuit looks like:

    Name:  USART network.JPG
Views: 12534
Size:  26.0 KB


    - The 18F4550 sends 1 byte to the slaves on Master TX line.
    - Both slaves receive the byte properly.
    - 1st slave replies successfully with same byte on Master RX line.

    Problem: communication is blocked if 2nd slave is added on Master RX line.

    Note: 2nd slave does not reply to master yet, extra address byte will be added later so each slave will reply only when spoken to.


    Master:
    Code:
    ' 184550 external oscillator 20MHz
    ' 4K7 pull-up on RX
    
    @   __CONFIG    _CONFIG1L, _PLLDIV_5_1L & _CPUDIV_OSC1_PLL2_1L & _USBDIV_2_1L
    @   __CONFIG    _CONFIG1H, _FOSC_HSPLL_HS_1H & _FCMEN_OFF_1H & _IESO_OFF_1H
    @   __CONFIG    _CONFIG2L, _PWRT_OFF_2L & _BOR_ON_2L & _BORV_1_2L & _VREGEN_ON_2L
    @   __CONFIG    _CONFIG2H, _WDT_OFF_2H
    @   __CONFIG    _CONFIG3H, _CCP2MX_OFF_3H & _PBADEN_OFF_3H & _LPT1OSC_OFF_3H & _MCLRE_ON_3H
    @   __CONFIG    _CONFIG4L, _STVREN_ON_4L & _LVP_OFF_4L & _ICPRT_OFF_4L & _XINST_OFF_4L & _DEBUG_OFF_4L
    DEFINE OSC 48
    ADCON1  = %00001111
    INTCON2 = %10000000
    
    TRISA = %00000000
    TRISB = %00000000
    TRISC = %11000000
    TRISD = %00000000
    TRISE = %00000000
    
    DEFINE HSER_RCSTA 90h               ' Enable serial port & continuous receive
    DEFINE HSER_TXSTA 20h               ' Enable transmit, BRGH = 0
    DEFINE HSER_CLROERR 1               ' Clear overflow automatically
    DEFINE HSER_SPBRG 25                ' 115200 Baud @ 48MHz, 0,16%
    SPBRGH = 0
    BAUDCON.3 = 1                       ' Enable 16 bit baudrate generator
    
    DEFINE  LCD_DREG      PORTD         ' Set LCD data port
    DEFINE  LCD_DBIT      0             ' Set starting data bit
    DEFINE  LCD_BITS      8             ' Set LCD bus size
    DEFINE  LCD_LINES     2             ' Set number of lines on LCD
    DEFINE  LCD_RSREG     PORTC         ' Set LCD register select port
    DEFINE  LCD_RSBIT     1             ' Set LCD register select bit
    DEFINE  LCD_EREG      PORTC         ' Set LCD enable port
    DEFINE  LCD_EBIT      0             ' Set LCD enable bit
    define  LCD_COMMANDUS 1500          ' Set command delay time in microseconds
    DEFINE  LCD_DATAUS    50            ' Set data delay time in microseconds
    
    DataIn      VAR byte
    DataOut     VAR byte
    
    low PORTC.2
    
    Start:
      Pause 2000
    
    Main:
      DataOut = PortC.2
      LCDOUT  $FE,$80,"This is the data out:  ", dec1 DataOut
      hserout [dec1 DataOut]
    
      hSERIN [dec1 DataIn]
      LCDOUT  $FE,$C0,"This is the data in:   ", dec1 DataIn
    
      pause 1000
      toggle PortC.2
      GOTO Main
    end

    1st slave:
    Code:
    ' 18F44K22 internal oscillator 64MHz
    ' 4K7 pull-up on RX
    
    @   __CONFIG    _CONFIG1H, _FOSC_INTIO67_1H & _PLLCFG_ON_1H & _PRICLKEN_OFF_1H & _FCMEN_OFF_1H & _IESO_OFF_1H
    @   __CONFIG    _CONFIG2L, _PWRTEN_ON_2L & _BOREN_SBORDIS_2L & _BORV_285_2L
    @   __CONFIG    _CONFIG2H, _WDTEN_OFF_2H
    @   __CONFIG    _CONFIG3H, _PBADEN_OFF_3H & _HFOFST_OFF_3H & _MCLRE_EXTMCLR_3H
    @   __CONFIG    _CONFIG4L, _STVREN_ON_4L & _LVP_OFF_4L & _XINST_OFF_4L & _DEBUG_OFF_4L
    DEFINE OSC 64
    OSCCON  = %01110000
    OSCCON2 = %10000000
    OSCTUNE = %11000000
    INTCON2 = %10000000
    
    ANSELA = 0
    ANSELB = 0
    ANSELC = 0
    ANSELD = 0
    ANSELE = 0
    TRISA = %00001111                           ' Set port A pins 0-3 to input, others output
    TRISB = %00000000                           ' Set port B pins to output
    TRISC = %11111111                           ' Set port C pins to input
    TRISD = %00111111                           ' Set port D pins 0-5 to input, others output
    TRISE = %00000000                           ' Set port E pins to output
    
    DEFINE HSER_RCSTA 90h                       ' Enable serial port & continuous receive
    DEFINE HSER_TXSTA 24h                       ' Enable transmit, BRGH = 1
    DEFINE HSER_CLROERR 1                       ' Clear overflow automatically
    DEFINE HSER_SPBRG 138                       ' 115200 Baud @ 64MHz, -0,08%
    SPBRGH = 0
    BAUDCON.3 = 1                               ' Enable 16 bit baudrate generator
    
    INCLUDE "DT_INTS-18.bas"                    ; Base Interrupt System
    INCLUDE "ReEnterPBP-18.bas"                 ; PBP Re-entry for external interrupt
    INCLUDE "Elapsed_INT-18.bas"                ; Elapsed Timer Routines
    ASM
    INT_LIST  macro    ; IntSource,        Label,           Type,   ResetFlag?
            INT_Handler    RX1_INT,    _ReceiveInterrupt,    PBP,    no
        endm
        INT_CREATE                              ; Creates the interrupt processor
    ENDASM
    
    DataIn         VAR byte
    DataOut        VAR byte
    
    RS232Flag      VAR byte
    
    goto Start
    
    ReceiveInterrupt:
      hSERIN [dec1 DataIn]
      RS232Flag = 1
    @ INT_RETURN
    
    Start:
      Pause 1500
    @   INT_ENABLE   RX1_INT
    
    mainloop:
      if RS232Flag = 1 then
        RS232Flag = 0
        PortA.4 = DataIn
        DataOut = DataIn
        hserout [dec1 DataOut]
      endif
    
      Goto mainloop
    end

    2nd slave:
    Code:
    ' 18F26K22 internal oscillator 64MHz
    ' 4K7 pull-up on RX
    
    @   __CONFIG    _CONFIG1H, _FOSC_INTIO67_1H & _PLLCFG_ON_1H & _PRICLKEN_OFF_1H & _FCMEN_OFF_1H & _IESO_OFF_1H
    @   __CONFIG    _CONFIG2L, _PWRTEN_ON_2L & _BOREN_SBORDIS_2L & _BORV_285_2L
    @   __CONFIG    _CONFIG2H, _WDTEN_OFF_2H
    @   __CONFIG    _CONFIG3H, _PBADEN_OFF_3H & _HFOFST_OFF_3H & _MCLRE_EXTMCLR_3H
    @   __CONFIG    _CONFIG4L, _STVREN_ON_4L & _LVP_OFF_4L & _XINST_OFF_4L & _DEBUG_OFF_4L
    DEFINE OSC 64
    OSCCON  = %01110000
    OSCCON2 = %10000000
    OSCTUNE = %11000000
    INTCON2 = %10000000
    
    ANSELA = 0                          ' Digital I/O
    ANSELB = 0
    ANSELC = 0
    TRISA = %00111111                   ' Set port A pins 0-5 to input
    TRISB = %00000000                   ' Set port B pins to output
    TRISC = %11000000                   ' Set port C pins 6-7 to input, others output
    
    DEFINE HSER_RCSTA 90h               ' Enable serial port & continuous receive
    DEFINE HSER_TXSTA 24h               ' Enable transmit, BRGH = 1
    DEFINE HSER_CLROERR 1               ' Clear overflow automatically
    DEFINE HSER_SPBRG 138               ' 115200 Baud @ 64MHz, -0,08%
    SPBRGH = 0
    BAUDCON.3 = 1                       ' Enable 16 bit baudrate generator
    
    INCLUDE "DT_INTS-18.bas"        ; Base Interrupt System
    INCLUDE "ReEnterPBP-18.bas"     ; PBP Re-entry for external interrupt
    INCLUDE "Elapsed_INT-18.bas"    ; Elapsed Timer Routines
    ASM
    INT_LIST  macro    ; IntSource,        Label,           Type,   ResetFlag?
            INT_Handler    RX1_INT,    _ReceiveInterrupt,    PBP,    no
        endm
        INT_CREATE                  ; Creates the interrupt processor
    ENDASM
    
    DataIn         VAR byte
    RS232Flag      VAR byte
    
      RS232Flag  = 0
    
    goto Start                          ' Jump over sub-routines
    
    ReceiveInterrupt:                   ' [USART RX - interrupt handler]
      hSERIN 50,No232,[dec1 DataIn]
      RS232Flag = 1
    No232:
    @ INT_RETURN
    
    Start:
      Pause 1500
    @   INT_ENABLE   RX1_INT             ; Enable USART Receive interrupts 
    
    mainloop:
      if RS232Flag = 1 then
        RS232Flag = 0
        PortA.7 = DataIn
      endif
    
      Goto mainloop
    end
    This has been driving me nuts for several days. I've trimmed my programs down to this bare essential and I still can't see what I'm doing wrong.

    Everything works beautifully as long as I don't add that 2nd slave on the reply line.

    Robert
    Last edited by Demon; - 22nd October 2012 at 02:16. Reason: PBP 2.60C, MPASM v5.46

  2. #2
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,521

    Default Re: Multiple PICs on usart network

    Hi Robert,
    I think the two TX-pins are fighting each other. When neither are trying to send they are both idle at high (or low, I can't remember). Then all of a sudden one is trying to send a bit pulling the pin low (or high) which effectively shorts the output thru the other slaves TX-pin. It doesn't matter that the second slave isn't actually sending anything yet, its TX-line is still pulled to the idle state of the line so when the other slave is trying to send its output gets shorted.

    What you could try is to "isolate" each slave from the masters RX-line with a 1k resistor. I'd probably remove the pullup on the masters RX-line as well otherwise it'll act as a voltage divider when the slave pulls the line low thru its "isolating resistor".

    /Henrik.

  3. #3
    Join Date
    May 2007
    Posts
    604

    Default Re: Multiple PICs on usart network

    RS-485

    RS-485, is a standard defining the electrical characteristics of drivers and receivers for use in balanced digital multipoint systems. The standard is published by the Telecommunications Industry Association/Electronic Industries Alliance (TIA/EIA). Digital communications networks implementing the EIA-485 standard can be used effectively over long distances and in electrically noisy environments. Multiple receivers may be connected to such a network in a linear, multi-drop configuration. These characteristics make such networks useful in industrial environments and similar applications.
    Why pay for overpriced toys when you can have
    professional grade tools for FREE!!!

  4. #4
    Join Date
    Dec 2005
    Posts
    1,073

    Default Re: Multiple PICs on usart network


  5. #5
    Join Date
    Feb 2011
    Location
    Michigan, USA
    Posts
    33

    Default Re: Multiple PICs on usart network

    why not use open mode. in the manual under serout2 command says:

    Bit 15 selects whether the data pin is always driven (bit 15 = 0), or is open in one of
    the states (bit 15 = 1). The open mode can be used to chain several devices
    together on the same serial bus.

    of course this is no good if you still want to use the hardware uart.

  6. #6
    Join Date
    Jan 2005
    Location
    Montreal, Quebec, Canada
    Posts
    2,597

    Default Re: Multiple PICs on usart network

    Thank you.

    Your replies sent me on search for "rs232 multidrop" and that gave important note.

    "One Master PIC can talk to multiple slaves, but only 1 slave can connect back to Master RX."

    I had missed that fact if it was mentionned in the PBP manual, or anywhere else.

    I've since switched to an alternate solution.

    Thanks!

    Robert

  7. #7

    Default Re: Multiple PICs on usart network

    Name:  image.jpeg
Views: 5471
Size:  563.7 KB

    here you have

  8. #8

    Default Re: Multiple PICs on usart network


  9. #9
    Join Date
    Jan 2006
    Location
    Istanbul
    Posts
    1,185

    Default Re: Multiple PICs on usart network

    Post #8, "MAX3322E & MAX3323E are designed for this..."

    Pretty much the same thing, but you need an extra I/O pin.
    "If the Earth were a single state, Istanbul would be its capital." Napoleon Bonaparte

  10. #10
    Wilton55's Avatar
    Wilton55 Guest

    Default Re: Multiple PICs on usart network

    Just another quick question, would using two MAX232 devices (one on each chip) and a common ground not also work?

  11. #11
    Join Date
    Jan 2005
    Location
    Montreal, Quebec, Canada
    Posts
    2,597

    Default Re: Multiple PICs on usart network

    Quote Originally Posted by Wilton55 View Post
    Just another quick question, would using two MAX232 devices (one on each chip) and a common ground not also work?

    Sorry for the delayed reply to Wilton.

    Yes, a MAX232 chip on each PIC will work. It will also improve reliability over long distances (can't remember if this was from a Microchip datasheet of Maxim application note).

    Robert

  12. #12
    Join Date
    May 2013
    Location
    australia
    Posts
    2,389

    Default Re: Multiple PICs on usart network

    for a half duplex scheme to work the slave must not talk unless asked to by the master .
    in this spi example the slave loops constantly looking for a change in the rotary coder position.
    when a change is detected the new position is calculated and the "mint pin" is set to 0, this signals the master that the slave has something to say. the slave now waits
    at some stage the master clocks the data out of the slave , when done it generates a sspif interrupt which terminates the slaves waiting , rinse repeat


    the half duplex (simplex) principle remains the same no mater what the transmission medium/method is, for a multi master scheme its no less complex a solution than Ethernet or any other collision sensing multiple access network and not for the faint hearted


    pos var byte
    old_pos var byte
    mint var portc.0
    tmp var byte

    DEFINE OSC 20

    adcon1=6
    trisc=%11011110
    option_reg.7=0
    mint=1
    SSPSTAT.6=1
    sspcon.5=1
    sspcon.4=0
    sspcon.2=1
    pir1.3=0
    PIE1.3=1
    INTCON=$c0
    gosub getp
    old_pos=pos
    pause 2000

    MAIN ; loop here till re moves
    gosub getp
    if (pos != old_pos )&& (mint) then ; if we have a new move and we are not waiting then
    old_pos=pos
    sspbuf=pos; ; prep the data to send
    mint=0; ;set the pin low to signal master
    endif
    GOTO MAIN


    GETP:
    tmp= (porta&$f)|((portb&$f)<<4)
    Lookdown tmp,[$7F,$3F,$3E,$3A,$38,$B8,$98,$18,$08,$48,$49,$4D,$4 F,$0F,$2F,$AF,$BF,$9F,$1F,$1D,$1C,$5C,$4C,$0C,$04, $24,$A4,$A6,$A7,$87,$97,$D7,$DF],Pos
    return


    spo: ; the master has read the data locks released
    mint=1;
    pir1.3=0
    @ INT_RETURN

  13. #13
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,521

    Default Re: Multiple PICs on usart network

    Andy,
    Just like Richard says the key is to make sure that the slaves never ever starts to speak without being spoken to. If you have one master and multiple slaves you need to come up with a scheme where the master interrogates the slaves, one by one, and the slaves only speak when asked to speak.

    Most of my experience with this type of thing comes from writing code for MODBUS. With MODBUS there's one master and one or more slaves (or servers as they're sometimes called). The master sends out messages on the bus containing a slave adress, a command and some data like "Slave 2, give me the value of your register number 5 and a checksum). All the slaves gets the message, verifies the chcksum and then determines if the message was for them or not. Only the slave with the adress matching that of the message is allowes to speak and must do so within a certain amount of time or a timeout error occurs. A slave can never start talking unless asked to by the master.

    There are of course other alternatives, like each slave has a "slave select line". The master asserts the slave select line of one slave at the time and the slave sends whatever it needs to send.

    If you need to have multimaster capabillity then perhaps CAN is an alternative.

    /Henrik.

  14. #14
    Join Date
    Oct 2004
    Posts
    448

    Default Re: Multiple PICs on usart network

    http://www.sampson-jeff.com/article/.../int.htm#async

    Please scroll down to figure 3b;

    Looks interesting; Tx, Rx, and perhaps power too, all on 1 line.

    Comments, folks?

  15. #15
    Join Date
    Oct 2004
    Posts
    448

    Default Re: Multiple PICs on usart network

    Also, from the forum, http://www.picbasic.co.uk/forum/showthread.php?t=4292

    Bob talks of a separate line for flow control, but that could be avoided by querying each slave, I guess.

  16. #16
    Join Date
    Oct 2004
    Posts
    448

    Default Re: Multiple PICs on usart network

    Recently, I too needed to connect a couple of nodes to a common controller. I wanted a bus on which I could go on adding slaves "in parallel".

    Also, half duplex was acceptable in my application. This is the outcome: works like a charm along a 20 meter cable run at 9600 baud. I'm sure will run much longer at slower speeds..

    And, comms happen over a single line, so you also save a pin on the pic.

    Hope somebody finds it useful!

    Name:  TxRx multiplex.jpg
Views: 2794
Size:  106.9 KB

Similar Threads

  1. PICs in a RS-485 network?
    By atomski in forum mel PIC BASIC Pro
    Replies: 21
    Last Post: - 12th November 2011, 09:52
  2. Multiple PICS from Same Crystal?
    By WOZZY-2010 in forum General
    Replies: 2
    Last Post: - 6th February 2010, 15:18
  3. Problems controlling multiple pics
    By gandora in forum mel PIC BASIC Pro
    Replies: 11
    Last Post: - 29th May 2007, 08:59
  4. Multiple Pics to One serial port
    By Rleonard in forum Serial
    Replies: 1
    Last Post: - 18th January 2007, 18:30
  5. Multiple Data on to USART RX pin
    By Squibcakes in forum Serial
    Replies: 2
    Last Post: - 20th July 2006, 00:37

Members who have read this thread : 2

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