4

我正在写 和 之间的Atmega162通信PC

在我的PCB我有接口RS485(从转换),它通过RS422收发器进入。MAX485ADAM-4520COM port

我一直在终端中测试我的程序,这对我来说似乎很奇怪,从MCU工作正常发送字符,但是从接收的字符PC被更改(我无法弄清楚这种转换的任何方案)。

例如,这些 ASCII 字符是这样解释的:

0   => 0
1   => 64
2   => 32
3   => 32
4   => 16
5   => 65
6   => 16
7   => 16
8   => 8
'1' => 204
'2' => 102
'3' => 70
'4' => 51
'5' => 141
'6' => 35
'7' => 51
'8' => 6
'9' => 142

我一直在测试它的几个传输参数,但它似乎没有帮助。源代码在这里:

void USART_init()
{   
    UCSR0B |= (1<<RXEN0) | (1<<TXEN0) | (1<<RXCIE0);    
    UCSR0C |= (1<<UCSZ10)|(1<<UCSZ00)|(1<<USBS0)|(1<<UPM10);        
    UBRR0H = 0;
    UBRR0L = 12;

    DDRD |= 1<<PD1;

    _delay_ms(1);
}

void USART_Transmit( unsigned char data )
{   
    PORTD |= 1<<PD4;
    while ( !( UCSR0A & (1<<UDRE0)) );  
    UDR0 = data;    
    while (!(UCSR0A & (1 << TXC0)))
    PORTD &= ~(1<<PD4);
}

ISR(USART0_RXC_vect)
{   
    unsigned char a;
    while ( !(UCSR0A & (1<<RXC0)) );

    a = UDR0; 

    speed_1 = a;
}

PD4在发送和接收之间切换。

4

0 回答 0