Discussion in 'General Electronics Chat' started by eckcotech, Feb 20, 2012.

  1. eckcotech

    Thread Starter New Member

    Jan 17, 2012
    Hey all,

    I am trying to interface two PIC Micro controllers via the UART Tx/Rx Pins and I seem to be running into a bit of trouble.

    I have done this before with both PICs on one board and it seemed to work just fine, I am now trying to interface two PICS in the same way but on different physical boards about 3ft apart. For some reason this doesn't seem to work.

    I have them connected with a standard CAT-5E cable like this.....

    TX -------- RX
    RX -------- TX
    Grd ------- Grd

    Any idea why this would not work?
  2. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    That's the correct connection when not using any flow control. 3 feet should be OK (especial for slow speeds) without a true RS232 level converter. That can be checked with a 'scope if you have one.

    Otherwise, make sure the baud rates are the same.
  3. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    What happens when you loop one back to itself?
  4. Felo


    Feb 20, 2012
    Are you using a level converter? the UART's on the PIC's are bit-banged or hardware? a schematic would be nice!!
  5. eckcotech

    Thread Starter New Member

    Jan 17, 2012
    Seems to work ok that way.

    How would one check this with a scope? I do have one here.
  6. eckcotech

    Thread Starter New Member

    Jan 17, 2012
    No level converter or funny wiring. Just a straight through UART to UART connection with hardware UART on PIC.

    Both boards (PICS) share a common power and ground as well.
  7. eckcotech

    Thread Starter New Member

    Jan 17, 2012

    One board has an LCD so it's very easy to display what it is sending or receiving.

    The other board has no real outputs other than the UART and I am unsure as to how to check this one.

    I did put a routine to display anything the first board sent/received on separate lines of the LCD.

    If I send a letter to the second board, it should echo it back but instead it looks something like this....

    Board1 - Send "A"
    Board2 - Reply "BhBhBhBh"
  8. MrChips


    Oct 2, 2009
    If you do have a scope that would make life sooo much easier.

    Check one chip at a time. Write a program that sends out a known bit pattern repeatedly. I use 'U' or 0x55. You will have to insert a delay of at least two or more character width in the loop to space the characters.

    Check the TX signal with the scope and confirm that the bit width is correct.
    Are your MCUs using a crystal or are you using the internal RC oscillator?
    What is your xtal or oscillator frequency? What is the baud rate you selected?

    The internal RC should work but check that both MCU are generating the same baud rate.

    This checks out the TX on both chips.

    Testing the RX comes next. We'll do one thing at a time.

    What is the part number of your PICs?
  9. Felo


    Feb 20, 2012
    If it's not a wiring issue, perhaps a good look at the code would help.... anyway getting garbage out is always a good sign, the problem is when they go silent.
  10. eckcotech

    Thread Starter New Member

    Jan 17, 2012
    Thanks MrChips, I will start testing this with a scope as described right now and get back to you when I have more info.
  11. hgmjr

    Retired Moderator

    Jan 28, 2005
    I strongly suspect that your problem is due to the fact that the cat 5e cable is twisted pair and RS-232 is a single ended transmission scheme. You are probably doing one or both of the following:
    1. Connecting TX and RX signals in one of the twisted pairs.
    2. Transmitting at a very high bit rate.

    You can try a couple of things to see if it helps.

    1. Send TX down one conductor and make sure that RX is not using the same twisted pair.
    2. Try operating at a lower bit rate.

  12. eckcotech

    Thread Starter New Member

    Jan 17, 2012
    Actually after some testing, I think my problem may indeed be in the code.

    The last time I did PIC to PIC via UART it was always on a PIC with only 1 hardware UART. This PIC (18F6723) has two hardware UART.

    That said, I am certain after double and triple checking that my connections are all good, but it seems I am not getting data out at all on the second UART.

    Do I need to set one active at a time or can I use both hardware UARTs at the same time.


    Code ( (Unknown Language)):
    2. if(UART1_Data_Ready()){
    3.        uart_rd = UART1_Read();
    4.        UART2_Write(uart_rd);
    5. }
  13. eckcotech

    Thread Starter New Member

    Jan 17, 2012
    Still having the same issue, It seems that UART2 is just plain unresponsive. It doesn't send OR receive Here is the code I am using (MikroC) to set them up....

    Code ( (Unknown Language)):
    2. void Init_Main(){
    3.     //Setup ADC Pins
    4.     ADCON1 = 0x0E;         // AN0 Pin Set To Analog 00001110
    5.     ADCON2 = 0x80;         // 10000000
    7.     //Setup I/O Ports
    8.     PORTA = 0x8C;    // configure PortA input output pins 10001100
    9.     TRISA = 0x8C;
    10.     TRISB=0x00;
    11.     PORTB=0x00;         // configure PortB input output pins 00000000
    12.     PORTC=0x80;      // configure PortC input output pins 10000000
    13.     TRISC = 0x80;
    14.     PORTD=0x81;      // configure PortD input output pins 10000001
    15.     TRISD=0x81;
    16.     PORTE=0xC0;      // configure PortE input output pins 11000000
    17.     TRISE=0xC0;
    18.     PORTG=0x05;      // configure PortG input output pins 00000101
    19.     TRISG=0x05;
    21.     //Setup Comparators
    22.     CMCON = 0x07;     // Turn off comparators
    25.     // Setup UARTs
    26.     UART1_Init(9600);  // Initialize UART1 module at 4800 bps
    27.     Delay_ms(10);     // Wait for UART 1module to stabilize
    28.     UART2_Init(9600);  // Initialize UART2 module at 4800 bps
    29.     Delay_ms(10);     // Wait for UART 2module to stabilize
    31.     Lcd_Init();                        // Initialize LCD
    32.     Delay_ms(10);
    33.     Lcd_Cmd(_LCD_CLEAR);               // Clear display
    34.     Lcd_Cmd(_LCD_CURSOR_OFF);          // Cursor off
    36.     ADC_Init();        // Initiate ADC Conversion
    37.     Delay_ms(10);
    38. }
    40. void main() {
    41.     Init_Main();
    43.     while(1){
    45.          if (UART1_Data_Ready()) {     // If data is received from UART1,
    46.             uart1_rd = UART1_Read();   // read the received data,
    47.             Delay_ms(10);
    48.             UART2_Write(uart1_rd);     // and send data via UART2 to other board
    49.             Lcd_Chr(1,1,uart1_rd);     //Display recieved data on line1 of LCD
    50.          }
    53.          if (UART2_Data_Ready()) {     // If data is received from other board,
    54.             uart2_rd = UART1_Read();   // read the received data,
    55.             Lcd_Chr(2,1,uart2_rd);      //Display responce on line2 of LCD
    56.          }
    57. }
    Any Ideas of what I am doing wrong?

  14. eckcotech

    Thread Starter New Member

    Jan 17, 2012

    It seems I had a bad port on one of the PICs. I made up a new board with a new PIC and it started working right away.

    Thanks to all that tried to help.