PIC to PIC UART

Thread Starter

eckcotech

Joined Jan 17, 2012
28
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?
 

ErnieM

Joined Apr 24, 2011
8,196
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.
 

Thread Starter

eckcotech

Joined Jan 17, 2012
28
Are you using a level converter? the UART's on the PIC's are bit-banged or hardware? a schematic would be nice!!
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.
 

Thread Starter

eckcotech

Joined Jan 17, 2012
28
BTW,

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"
 

MrChips

Joined Oct 2, 2009
24,618
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?
 

Felo

Joined Feb 20, 2012
91
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.
 

Thread Starter

eckcotech

Joined Jan 17, 2012
28
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?
Thanks MrChips, I will start testing this with a scope as described right now and get back to you when I have more info.
 

hgmjr

Joined Jan 28, 2005
9,029
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?
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.

hgmjr
 

Thread Starter

eckcotech

Joined Jan 17, 2012
28
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.

example....

Board1
Rich (BB code):
if(UART1_Data_Ready()){
       uart_rd = UART1_Read();
       UART2_Write(uart_rd);
}
 

Thread Starter

eckcotech

Joined Jan 17, 2012
28
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....

Rich (BB code):
void Init_Main(){
    //Setup ADC Pins
    ADCON1 = 0x0E;         // AN0 Pin Set To Analog 00001110
    ADCON2 = 0x80;         // 10000000
    
    //Setup I/O Ports
    PORTA = 0x8C;    // configure PortA input output pins 10001100
    TRISA = 0x8C;
    TRISB=0x00;
    PORTB=0x00;         // configure PortB input output pins 00000000
    PORTC=0x80;      // configure PortC input output pins 10000000
    TRISC = 0x80;
    PORTD=0x81;      // configure PortD input output pins 10000001
    TRISD=0x81;
    PORTE=0xC0;      // configure PortE input output pins 11000000
    TRISE=0xC0;
    PORTG=0x05;      // configure PortG input output pins 00000101
    TRISG=0x05;
    
    //Setup Comparators
    CMCON = 0x07;     // Turn off comparators

    
    // Setup UARTs
    UART1_Init(9600);  // Initialize UART1 module at 4800 bps
    Delay_ms(10);     // Wait for UART 1module to stabilize
    UART2_Init(9600);  // Initialize UART2 module at 4800 bps
    Delay_ms(10);     // Wait for UART 2module to stabilize
    
    Lcd_Init();                        // Initialize LCD
    Delay_ms(10);
    Lcd_Cmd(_LCD_CLEAR);               // Clear display
    Lcd_Cmd(_LCD_CURSOR_OFF);          // Cursor off
    
    ADC_Init();        // Initiate ADC Conversion
    Delay_ms(10);
}

void main() {
    Init_Main();
    
    while(1){

         if (UART1_Data_Ready()) {     // If data is received from UART1,
            uart1_rd = UART1_Read();   // read the received data,
            Delay_ms(10);
            UART2_Write(uart1_rd);     // and send data via UART2 to other board
            Lcd_Chr(1,1,uart1_rd);     //Display recieved data on line1 of LCD
         }
         

         if (UART2_Data_Ready()) {     // If data is received from other board,
            uart2_rd = UART1_Read();   // read the received data,
            Lcd_Chr(2,1,uart2_rd);      //Display responce on line2 of LCD
         }
}
Any Ideas of what I am doing wrong?

Thanks
 

Thread Starter

eckcotech

Joined Jan 17, 2012
28
Solved!

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.
 
Top