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.

UART communication between two PIC32MX120F032B

Status
Not open for further replies.

In2Dusk

Junior Member level 3
Joined
Jul 9, 2011
Messages
31
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,286
Activity points
1,496
I have two PIC32MX120F032B and i need to communicate between them (preferably using uart but it doent really matter), i have made the connection between their TXs and RXs and tryed simple example code, but it didnt work, i checked the code and hardware connections, but i cant find any problem...
Any help would be appreciated.
 

I assume you have set up the same baud rate, data bits, etc on the two devices
have you set up the peripheral pin select for the UART Rx and Tx ?
have you looked at the signals using an oscilloscope?
 
You'll need to post your code using CODE or SYNTAX tags for both devices, a schematic might be helpful as well.

Have you ran a simple program to ensure each devices is operating as expected, blink an LED, etc?

BigDog
 

Transmitter's Code:

Code:
int main(void) {
	PPSUnLock;
	PPSInput(3, U1RX, RPB6);
	PPSOutput(1, RPB7, U1TX);
	PPSLock; 

	PORTB = 0;
	TRISB = 0;

	U1BRG = 51; 	// Set Baud rate (9600 bps)
	U1MODE = 0x8000; // Enable UART for 8-bit data, no parity, 1 Stop bit
	U1STA = 0x1400;	

	U1TXREG = 'u';
	while(!U1STAbits.TRMT);

	PORTBbits.RB4 = 1;

	while(1);
	return 1;
}

Receivers's code:

Code:
int main(void) {
	PPSUnLock;
	PPSInput(3, U1RX, RPB6);
	PPSOutput(1, RPB7, U1TX);
	PPSLock; 

	PORTB = 0;
	TRISB = 0;

	U1BRG = 51; 	// Set Baud rate (9600 bps)
	U1MODE = 0x8000; // Enable UART for 8-bit data, no parity, 1 Stop bit
	U1STA = 0x1400;	

	PORTBbits.RB4 = 1;

	while(!U1STAbits.URXDA);

	PORTBbits.RB4 = 0;

	while(1);
	return 1;
}
 

For a PIC32, I suggest using the <plib.h> file in your include section and then run off the UART functions. Documentation for this should be found in the microchip folder of your program files, then under this file branch or whatever it is called:
\Microchip\xc32\v1.10\docs\Microchip-PIC32MX-Peripheral-Library.chm
Obviously if you are using c32 or a different version those numbers will be changed.
This insures all of the bits needed set are set and set correctly, as well as making your code very readable.
 

have a look at
Code:
#include <plib.h>					// Peripheral Library
#include "stdio.h" 
// External oscillator frequency 
#define SYSCLK  80000000UL		//  (8MHz Crystal/ FPLLIDIV * FPLLMUL / FPLLODIV)

#define	GetSystemClock() 			(SYSCLK)
#define	GetPeripheralClock()		(GetSystemClock()/(1 << OSCCONbits.PBDIV))

//  initialize UART
//   Input: baud rate
//   Output: 1 for OK, 0 if baud rate error > 3%
int UART2Init(const long int BAUDRATE)
{
    int error;
	// This initialization assumes 36MHz Fpb clock. If it changes,
	// you will have to modify baud rate initializer.
    UARTConfigure(UART2, UART_ENABLE_PINS_TX_RX_ONLY);
    UARTSetFifoMode(UART2, UART_INTERRUPT_ON_TX_NOT_FULL | UART_INTERRUPT_ON_RX_NOT_EMPTY);
    UARTSetLineControl(UART2, UART_DATA_SIZE_8_BITS | UART_PARITY_NONE | UART_STOP_BITS_1);
    UARTSetDataRate(UART2, GetPeripheralClock(), BAUDRATE);
    UARTEnable(UART2, UART_ENABLE_FLAGS(UART_PERIPHERAL | UART_RX | UART_TX));
    error = (int) (100 *( (long int) UARTGetDataRate(UART2, GetPeripheralClock())- BAUDRATE)/BAUDRATE);
    if (error > 2) 
       { printf("\n\n" UARTNAME " baud rate shold be %ld is %lu error %d\n", BAUDRATE, UARTGetDataRate(UART2, GetPeripheralClock()), error);  return -1; }
    printf(""UARTNAME " baud rate %ld set up\n", BAUDRATE);
    return 0;
}

//Function:UART GetChar : waits for a byte to be received.  It then returns that byte.
char UART2GetChar()
{
     return UARTGetDataByte(UART2);						// read character into buffer
}

// Function:UART PutChar - This routine writes a character to the transmit FIFO
void UART2PutChar(const char ch )
{
    while(!UARTTransmitterIsReady(UART2));
    UARTSendDataByte(UART2, ch);				// transmit character
}
 
I have solved it. The problem was that i have configured all chip's pins as outputs (TRISB = 0;), but the RX pin have to be configured as input...

Thank you all.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top