Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Communicating two microcontroller using RS485. Receiving junk data in receiver side..

Status
Not open for further replies.

maheshece28

Junior Member level 2
Joined
Jun 13, 2013
Messages
24
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
249
Hi,
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. I've checked the UART4 configuration looks fine. What is the problem?
Screenshot_123015_020714_PM.jpg

Screenshot_123015_020822_PM.jpg
Here i just post only UART4 initialize code.

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:
Code:
	int main()
    { 
		init_processor();
		PORTBbits.RB5=1; //RB5 is RE/DE
			while(1)
				{
					uart4str("Hello\r\n");
					Delayms(1000);
				}
		return 0;
    }

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

Thanks...
 

I don't understand the code.
Code:
uart1str(uartrec4());

uartrec() is returning a character, uartstr() is expecting a string (pointer to null terminated character array) argument.

Apart from non-matching types, how is the code expected to receive a line of text?
 

I don't understand the code.
Code:
uart1str(uartrec4());

uartrec() is returning a character, uartstr() is expecting a string (pointer to null terminated character array) argument.

Apart from non-matching types, how is the code expected to receive a line of text?

Hi. Thanks for the reply. That's UART1 send string also. Pls check my UART1 code

Code:
void uartsend(char in_c)
{
    while(U1STAbits.UTXBF == 1);
    U1TXREG = in_c;
}

void uart1str( char *s)
{
    while(*s!='\0')
	{
		uartsend(*s);
		s++;
	}
}
 

Unfortunately you didn't refer to the problem addressed in my post.

What makes you think that uart1str(uartrec4()); does anything useful?
 

Unfortunately you didn't refer to the problem addressed in my post.

What makes you think that uart1str(uartrec4()); does anything useful?

I changed the code as

Code:
uartsend(uartrec4());

Now im not receiving junk data, instead i receive nothing in my hyperterminal.
 

Hi,

You connected A to A and B to B, but did you connect Gnd to Gnd also?

Klaus
 

Hi,

You connected A to A and B to B, but did you connect Gnd to Gnd also?

Klaus


Each PIC board has one common ground. Is that have to connect/short those ground?
 

An isolated RS-485 with common mode termination resistors is often used without common ground (e.g. in PROFIBUS standard), non-isolated should better have a ground wire. But missing ground is not necessarily the reason for failure.

The "uartsend(uartrec4());" construct must be looped without any delay, otherwise you'll get receiver overrun. Useful debugguíng steps could be:
- checking the RX signal with an oscilloscope
- tracing code execution with a debugger adapter
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top