[PIC] PIC32 RS485 using MAX14783E

nihanth

Newbie
Joined
Nov 18, 2024
Messages
2
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
41
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.


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:

What does "not working" mean? Please be more specific in what you can see working, what doesn't and what you expect that is not happening.
For example, can you see the \RE\ and DE signals as expected? Can you see a signal on the DI line when you are sending? Do the A and B signals occur as expected?
For receiving, can you see a signal on the A and B lines? What about the RO signal? Is the ISR being called?
Also what is your development environment - that might help us with making suggestions so we don't suggest something that you can't do.
Susan

PS - also just seen that line 49 uses a capital 'I' where the loop variable is a lower case 'i'. Did this actually compile????
 

Hi,

there are ready to use libraries for RS485.
Also I recommend to use a standard protocol .. maybe MODBUS is suitable.

Klaus
 

Hi,

@Aussie Susan Sorry for not being specific. The code compiles and that capital must have been when I copied it and edited. The issue is that I cannot get any data on receiver. The master send the data(according to debugger) but I can't see any on the receiver. I'm using mplabx ide and icd3. I'm not sure about the signal on particular lines as I would need an oscilloscope to be sure but I don't have one right now. I have done a similar implementation for Rs232 using uart and it works fine but I'm having issues with this.
@KlausST I have searched for libraries but couldn't find any. Can you point me to any site that I can explore?

Thanks
 
Last edited:

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…