本文介绍了dsPic33E:RS485通信问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在通过RS485端口进行串行通信时,我遇到了问题.

I am facing issue while communicating serially over RS485 port.

我正在将dsPic33E微控制器与Max485配合使用.

I am using dsPic33E microcontroller with Max485.

我使用断点分析说,每当我向控制器发送"0x00" 时,都会收到"0xFF" .然后我发送了"0x01" ,在控制器上收到了"0xFD" ,依此类推.

using breakpoint i analyzed that Whenever I send "0x00" to controller, "0xFF" is received. Then I sent "0x01", "0xFD" is received on controller.and so on.

我也尝试使用环回逻辑,这意味着发送回接收到的字符,但是每次收到任何发送的值时,"0x00" .

Also i tried to use loop-back logic, means sending back the received character, but every time I receive "0x00" for any value sent.

我无法解决问题.下面是我正在使用的代码的快照:

I am unable to get the issue. below is the snapshot of the code I am using :

// RS485
TRISBbits.TRISB6    = INPUT_PIN;        // RX - RB6/RP38 PIN<42>
TRISBbits.TRISB7    = OUTPUT_PIN;       // TX - RB7/RP39 PIN<43>
TRISBbits.TRISB8    = OUTPUT_PIN;       // !RE/DE Control Pin RB8/RP40 PIN<44>

// RS485 Config
#define RS485_TX    PORTBbits.RB6           // RS485 Transmitter
#define RS485_RX    LATBbits.LATB7          // RS485 Reciever
#define RS485_CTRL  LATBbits.LATB8          // RS485 Control Pin

void __attribute__((interrupt, no_auto_psv)) _U4RXInterrupt(void)
{
    rs485Char = U4RXREG;
    RS485_CTRL = 1;         // Enable driver
    U4TXREG = rs485Char;
    RS485_CTRL = 0;         // disable driver RE/DO
}

void InitClock( void )
{
    PLLFBD  =   63;                      // M=65
    CLKDIVbits.PLLPOST  =   0;           // N2=2
    CLKDIVbits.PLLPRE   =   0;           // N1=2

    // Initiate Clock Switch to FRC oscillator with PLL (NOSC=0b001)
    __builtin_write_OSCCONH(0x01);
    __builtin_write_OSCCONL(OSCCON | 0x01);
    // Wait for Clock switch to occur
    while (OSCCONbits.COSC!= 0b001);
    // Wait for PLL to lock
    while (OSCCONbits.LOCK!= 1);
}

void InitRs485(void){
            // configure U1MODE
U4MODEbits.UARTEN = 0;      // Bit15 TX, RX DISABLED, ENABLE at end of func

U4MODEbits.URXINV = 1;      // 1:URXINV Idle state is '0' ; 0=UxRX Idle state is '1';
U4MODEbits.ABAUD = 0;       // Bit5 No Auto baud (would require sending '55')
U4MODEbits.BRGH  = 0;       // Bit3 16 clocks per bit period
U4MODEbits.PDSEL = 0;       // 0 : 8 bit,no parity; 1 : 8 bit,even parity; 2 : 8 bit,odd parity; 3 : 9 bit,no Parity
U4MODEbits.STSEL = 1;       // 1 : 2 Stop bits; 0 : 1 Stop bits

// Load a value into Baud Rate Generator.
U4BRG = BRGVAL_RS485;             // 60Mhz osc, 9600 Baud

// Load all values in for U1STA SFR
U4STAbits.UTXISEL1 = 0;     // Bit15 Int when Char is transferred (1/2 config!)
U4STAbits.UTXISEL0 = 0;     // Bit13 Other half of Bit15
U4STAbits.UTXINV = 1;       // 1:UxTX Idle state is '0' ; 0=UxTX Idle state is '1';

U4STAbits.UTXBRK = 0;       // Bit11 Disabled
U4STAbits.UTXEN = 0;        // Bit10 TX pins controlled by peripheral
U4STAbits.URXISEL = 0;      // Bits6,7 Int. on character received

IPC22bits.U4RXIP = 7;
IPC22bits.U4TXIP = 7;

IFS5bits.U4TXIF = 0;        // Clear the Transmit Interrupt Flag
IEC5bits.U4TXIE = 1;        // Enable Transmit Interrupts
IFS5bits.U4RXIF = 0;        // Clear the Receive Interrupt Flag
IEC5bits.U4RXIE = 1;        // Enable Receive Interrupts

RPOR2bits.RP39R = 0x1D;     // dsPic33EP512GM604 => RP39 as U4TX PIN<43>
_U4RXR = 38;                // dsPic33EP512GM604 => RP38 as U4RX PIN<42>

U4MODEbits.UARTEN = 1;      // And turn the peripheral on
U4STAbits.UTXEN = 1;

// Hardware control bits
RS485_CTRL = 0;                     // disable driver
IEC5bits.U4RXIE = 1;

}

int main(int argc, char** argv) {

InitClock();    // This is the PLL settings
InitPorts();    // Configure all Input/Output Ports
InitUarts();    // Initialize UART1 for 9600,8,N,1 TX/RX

Rs485Initialise( 0x10, 0x10);

while( 1 )
{

}

return (EXIT_SUCCESS);
}

这是测试代码.实际通信将使用Modbus协议.请帮助我纠正问题.

this is a test code. Actual communication will using Modbus Protocol.Please help me in rectifying the issue.

推荐答案

这似乎表明您的极性相反.请参见数据表或系列参考手册中的U1MODEbits.URXINV和U1STAbits.UTXINV位( http://ww1.microchip.com/downloads/zh-CN/DeviceDoc/70000582e.pdf ).

This seems to indicate that your polarity is reversed. See the U1MODEbits.URXINV and U1STAbits.UTXINV bits in your datasheet or the family reference manual (http://ww1.microchip.com/downloads/en/DeviceDoc/70000582e.pdf).

这篇关于dsPic33E:RS485通信问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 15:20