AtMega2560 的 UART0 到 UART2 网关(某种)

发布于 2024-12-04 10:46:46 字数 2226 浏览 2 评论 0原文

我将一个设备连接到 AtMega2560 的 UART0。我想将UART0数据传输到UART2以在终端(PC)上查看它。

当我使用 UART 转串行设备 (FTDI) 将设备直接连接到 PC 时,它可以很好地发送数据。

当我出于上述目的将 UART2 放在中间时,它只发送第一行,具体来说:

Ver V2DAPV142 On-Line: 然后忘记了。有时它也不发送第一行。

代码:

#define UART0_BUFFER_SIZE 40 
#define RX_WAIT 65000 
volatile unsigned char UART0_rx_ArrUC85[UART0_BUFFER_SIZE]; 
volatile  unsigned char UART0_rx_ArrLength = 0, UART0_rx_ArrIndex = 0; 

void uart0_init( unsigned int baudrate ) 
{ 

    UBRR0H = (unsigned char) (baudrate>>8); 
    UBRR0L = (unsigned char) baudrate; 

    UCSR0B = ( 1 << RXEN0 ) | ( 1 << TXEN0 ) | (1<<RXCIE0); 
    UCSR0C = ( 1 << USBS0 ) | ( 1 << UCSZ01 ) | ( 1 << UCSZ00 );  // 8N1 

} 

void USART2Init(UINT16 ubrr_value) 
{ 
   UBRR2L = ubrr_value; 
   UBRR2H = (ubrr_value>>8); 

   UCSR2C|=(3<<UCSZ20); 

   UCSR2B = (1<<RXEN2) | (1<<TXEN2);  

} 
ISR(USART0_RX_vect) 
{ 
   unsigned char recChar = UDR0; 
   if (UART0_BUFFER_SIZE > UART0_rx_ArrLength) 
   {    
      UART0_rx_ArrUC85[UART0_rx_ArrIndex++] = recChar; 
      UART0_rx_ArrLength = UART0_rx_ArrIndex; 
   } 

} 

void uart2_putchar(UINT8 data) 
{ 
    //Local variables 
    unsigned int i; 
    for( i = 0; !( UCSR2A & ( 1 << UDRE2 ) ); i++ ) // Wait for empty transmit buffer 
    { 
        if( i > RX_WAIT )                           // How long one should wait 
        { 
            return ;                              // Give feedback to function caller 
        } 
    } 
    UDR2 = data;                                    // Start transmitting     
    //return (int)data;                               // Cast and return int value 
} 


void uart2_puts(unsigned char *str) 
{ 
   UINT8 dat; 
   for( ;*str != '\0'; ) 
   { 
      dat= *str++ ; 
      uart2_putchar(dat); 
   } 

} 
int main() 
{ 
   USART2Init(8); 
   uart0_init(103); 
   sei(); 
   while(1) 
   {       
      if(UART0_rx_ArrLength>0) 
      { 
         uart2_puts((unsigned char *) UART0_rx_ArrUC85);       
         UART0_rx_ArrLength = UART0_rx_ArrIndex = 0;                      
      } 
   } 
}

可能是什么问题。 我也用相同和不同的波特率检查了 UART0 和 UART2。

I connected a device to the UART0 of the AtMega2560. I want to transfer the UART0 data to the UART2 to view it on the Terminal(PC).

When I connect the device directly to the PC using an UART to serial device (FTDI) It sends the data nicely.

When I put the UART2 in the middle for said purpose, then It only sends the first line, specifically:

Ver V2DAPV142 On-Line: And then forgets. Sometimes it doesn't send the first line too.

Code:

#define UART0_BUFFER_SIZE 40 
#define RX_WAIT 65000 
volatile unsigned char UART0_rx_ArrUC85[UART0_BUFFER_SIZE]; 
volatile  unsigned char UART0_rx_ArrLength = 0, UART0_rx_ArrIndex = 0; 

void uart0_init( unsigned int baudrate ) 
{ 

    UBRR0H = (unsigned char) (baudrate>>8); 
    UBRR0L = (unsigned char) baudrate; 

    UCSR0B = ( 1 << RXEN0 ) | ( 1 << TXEN0 ) | (1<<RXCIE0); 
    UCSR0C = ( 1 << USBS0 ) | ( 1 << UCSZ01 ) | ( 1 << UCSZ00 );  // 8N1 

} 

void USART2Init(UINT16 ubrr_value) 
{ 
   UBRR2L = ubrr_value; 
   UBRR2H = (ubrr_value>>8); 

   UCSR2C|=(3<<UCSZ20); 

   UCSR2B = (1<<RXEN2) | (1<<TXEN2);  

} 
ISR(USART0_RX_vect) 
{ 
   unsigned char recChar = UDR0; 
   if (UART0_BUFFER_SIZE > UART0_rx_ArrLength) 
   {    
      UART0_rx_ArrUC85[UART0_rx_ArrIndex++] = recChar; 
      UART0_rx_ArrLength = UART0_rx_ArrIndex; 
   } 

} 

void uart2_putchar(UINT8 data) 
{ 
    //Local variables 
    unsigned int i; 
    for( i = 0; !( UCSR2A & ( 1 << UDRE2 ) ); i++ ) // Wait for empty transmit buffer 
    { 
        if( i > RX_WAIT )                           // How long one should wait 
        { 
            return ;                              // Give feedback to function caller 
        } 
    } 
    UDR2 = data;                                    // Start transmitting     
    //return (int)data;                               // Cast and return int value 
} 


void uart2_puts(unsigned char *str) 
{ 
   UINT8 dat; 
   for( ;*str != '\0'; ) 
   { 
      dat= *str++ ; 
      uart2_putchar(dat); 
   } 

} 
int main() 
{ 
   USART2Init(8); 
   uart0_init(103); 
   sei(); 
   while(1) 
   {       
      if(UART0_rx_ArrLength>0) 
      { 
         uart2_puts((unsigned char *) UART0_rx_ArrUC85);       
         UART0_rx_ArrLength = UART0_rx_ArrIndex = 0;                      
      } 
   } 
}

What could be the issue.
I checked it with same and different baud rates too for UART0 and UART2.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

输什么也不输骨气 2024-12-11 10:46:46

问题在于电路功率水平。 Pen-Drive ctrlr 的电源不足,并且调节器无法为其通信功率级别提供电源。因此它有时不起作用。此外,我们对其进行了测试并得出结论,在使用另一个电源调节器为 Pen-Drive ctrlr 提供足够的电源后,上述通信顺利进行。我希望这可以帮助人们引起对可能的电路问题的注意。

The issue was circuitry power level. The power supply was not sufficient for the Pen-Drive ctrlr and the regulator was not able to source for its communication power level. Hence it was not working sometimes. Further we have tested it and drew a conclusion that after giving sufficient power to the Pen-Drive ctrlr using another power regulator, the above said communication takes nicely place. I hope this can help ppl to draw attention towards the possible circuitry issues.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文