nihanth
Newbie
Missing CODE or SYNTAX Tags
Hi Everyone,
I new here and not very familiar with all info that i need to give but ill do my best to explain.
I am using pic32mz2048efm144 controller for my project and want to implement communication with another pic(same model). For this I have decided to use Rs485 communication as this will in future expand to communicate with multiple slaves over relatively long distance. I have MAX14783E attached to UART6 on both controllers. I have implemented a similar UART communication with rs232 to connect it to pc terminal for communication and it works fine. I have used a similar approach along with controlling directions for rs485 according to datasheet but i couldnt figure out what the issue is but its not working.
Any suggestions would be great. I wrote a code for testing the communication which is below. In the main- i am using code under master(comment) while commenting the code under slave(comment) for master controller and vice-versa.
I new here and not very familiar with all info that i need to give but ill do my best to explain.
I am using pic32mz2048efm144 controller for my project and want to implement communication with another pic(same model). For this I have decided to use Rs485 communication as this will in future expand to communicate with multiple slaves over relatively long distance. I have MAX14783E attached to UART6 on both controllers. I have implemented a similar UART communication with rs232 to connect it to pc terminal for communication and it works fine. I have used a similar approach along with controlling directions for rs485 according to datasheet but i couldnt figure out what the issue is but its not working.
Any suggestions would be great. I wrote a code for testing the communication which is below. In the main- i am using code under master(comment) while commenting the code under slave(comment) for master controller and vice-versa.
Code C - [expand] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 #define UART_BAUDRATE 9600 #define PBCLK 100000000 // Adjust according to your peripheral clock frequency #define BUFFER_SIZE 200 #define DE_PIN LATJbits.LATJ2 // Driver Enable (DE) #define RE_PIN LATJbits.LATJ3 // Receiver Enable (RE) char txBuffer[BUFFER_SIZE]; char rxBuffer[BUFFER_SIZE]; volatile int rxIndex = 0; volatile bool messageReceived = false; void UART6_Init() { // UART6 Pin Configuration U6RXRbits.U6RXR = 0b0110; // Map U6RX to RD5 RPD4Rbits.RPD4R = 0b0100; // Map U6TX to RD4 // UART6 Configuration U6MODE = 0; // Reset UART U6BRG = ((PBCLK / (16 * UART_BAUDRATE)) - 1)/2; // Set baud rate U6STA = 0; // Clear status U6STAbits.UTXEN = 1; // Enable TX U6STAbits.URXEN = 1; // Enable RX U6MODEbits.ON = 1; // Enable UART6 // Enable UART RX Interrupt IPC47bits.U6RXIP = 6; // Set priority IPC47bits.U6RXIS = 1; // Set subpriority IEC5bits.U6RXIE = 1; // Enable RX interrupt } void __ISR(_UART6_RX_VECTOR, IPL6SOFT) UART6_RX_ISR(void) { char received = U6RXREG; // Read received byte MZ_LED1 = !MZ_LED1; rxBuffer[rxIndex++] = received; if (received == '\n') { // End of message messageReceived = true; rxBuffer[rxIndex] = '\0'; // Null-terminate the string rxIndex = 0; } IFS5CLR = _IFS5_U6RXIF_MASK; // Clear RX interrupt flag } void sendMessage(const char* message) { DE_PIN = 1; // Enable transmitter RE_PIN = 1; // Disable receiver delay_ms(1); // Allow DE/RE signal to settle for (int i = 0; i < strlen(message); i++) { while (U6STAbits.UTXBF); // Wait for TX buffer to be empty U6TXREG = message[I]; } while (!U6STAbits.TRMT); // Wait for transmission to complete delay_ms(1); // Allow bus to stabilize DE_PIN = 0; // Disable transmitter RE_PIN = 0; // Enable receiver } void main() { TRISJbits.TRISJ2 = 0; // Set J2 as output (DE) TRISJbits.TRISJ3 = 0; // Set J3 as output (RE) DE_PIN = 0; // Start in receive mode RE_PIN = 0; // Start in receive mode UART6_Init(); while(1){ //testing 485 communication //master sendMessage("0x01\n"); delay_ms(1000); // Adjust delay as needed if (messageReceived) { messageReceived = false; debug_print("message received/n"); } //slave if (messageReceived) { messageReceived = false; // Echo the received message back to the master debug_print("received\n"); sendMessage("Message Received: "); sendMessage(rxBuffer); } } }
Last edited by a moderator: