4
\$\begingroup\$

I would like to send and receive data between two PIC24F microcontrollers via RS485. Here i'm using SP3485 IC. I've set one microcontroller as transmitter and another one has receiver. My microcontroller has 4 uart. One of the uart(here i use UART4) is connected with RS485 IC(SP3485) for both. As usual data pins 'A' to 'A' and 'B' to 'B' are connected. Below is the image shows which PIC24F pins are connected with RS485 IC. I'm transmitting data from one pic to another pic via RS485. In receiver side i've connected UART1 with hyperterminal for seeing the received data.The problem is While i print the received data in the hyperterminal it shows junk data. I've checked the UART1 its working fine.

Here i just post only UART4 initialize code.

init_processor() { TRISB = 0x46E0;

/******** pps for uart4 **************/
RPINR27bits.U4RXR = 4;
RPOR3bits.RP6R=21; //RP(6)R---> 6th pin of pic(RP6R has RPOR3)

// Init UART4
U4MODE = 0x8000;
U4STA = 0x0000;     //Enable Transmission, Clear all flags
U4BRG = 25;
//IFS0bits.U1RXIF = 0;            // Clear the Receive Interrupt Flag
//IEC0bits.U1RXIE = 1;            // Enable Receive Interrupts
U4MODEbits.UARTEN = 1;            // And turn the peripheral on
U4STAbits.UTXEN = 1;

//UART4
void uartsend4( char in_c)
    {
        while(U4STAbits.UTXBF == 1);
        U4TXREG = in_c;
    }
void uart4str( char *s)
    {
        while(*s!='\0')
            {
                uartsend4(*s);
                s++;
            }
    }

char uartrec4()
    {
        while(U4STAbits.URXDA == 0); 
        if (U4STAbits.OERR)
            U4STAbits.OERR = 0; 
        return U4RXREG; 
    }
}

main.c transmitter:

int main()
{ 
    init_processor();
    PORTBbits.RB5=1; //RB5 is RE/DE
        while(1)
            {
                uart4str("Hello\r\n");
                Delayms(1000);
            }
    return 0;
}




main.c receiver:

int main()
[![{ 
    init_processor();
    PORTBbits.RB5=0; //RB5 is RE/DE][1]][1]
        while(1)
            {
                uart1str(uartrec4());  //printing received uart4 data in hyperterminal
                Delayms(1000);
            }
    return 0;
}

enter image description here

enter image description here

\$\endgroup\$
  • 2
    \$\begingroup\$ Please review your punctuation, the question is very hard to read. Also add a circuit diagram rather than all the descriptive and hard to follow text. \$\endgroup\$ – jippie Dec 29 '15 at 14:34
  • 1
    \$\begingroup\$ Terminate or get bad data. \$\endgroup\$ – Andy aka Dec 29 '15 at 14:34
  • \$\begingroup\$ See this answer for termination information: electronics.stackexchange.com/questions/208350/… \$\endgroup\$ – Andy aka Dec 29 '15 at 14:40
1
\$\begingroup\$

I guess you have triple-checked the baud rates between the microcontrollers, as well as between receive uart1 and the hyperterminal.

The receive code is not correct:

    while(1)
        {
            uart1str(uartrec4());  //printing received uart4 data in hyperterminal
            Delayms(1000);
        }

Problem here is that uartRec4 receives one character. What happens is:

  • sender sends out 7 bytes, waits one second
  • receiver receives the first byte, send it out, then ignores everything for 1 second.

Remove your delayms(1000) from the receiver side. Ensure that you're using the same bandwidth both between pics and also on hyperterminal.


If still not work, try to split into parts:

  • make a "receiver" side which does not receive anything just prints Hello to uart1 - this checks if you have everything correct towards the PC
  • send only one character, e.g. an A on the sender, wait 1000ms and repeat - this does not require any buffering and special timing consideration.

Note that uart1str(uartrec4()) can also be problematic if the hw uart does not have a buffer (PICs do have a 2 byte receiver buffer, afaik). (The problem is that you're waiting for one byte, then while sending it out to uart1, another byte may arrive which you supposed to check for.)

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.