简体   繁体   English

2 Arduino Uno 之间的串行通信仅使用寄存器

[英]Serial Communication between 2 Arduino Uno using just Registers

I need to transfer a string between 2 Arduino Uno, using serial communication without functions(just manipulating registers, such as UDR0).我需要在 2 Arduino Uno 之间传输一个字符串,使用没有功能的串行通信(只是操作寄存器,例如 UDR0)。

I am able to send a string using我可以使用发送字符串

#define BAUD 9600
#define MYUBRR F_CPU/16/BAUD-1


void USART_Init(unsigned int ubrr)
{
  UBRR0H=(unsigned char)(ubrr>>8);
  UBRR0L=(unsigned char)ubrr;
  UCSR0B=(1<<RXEN0)|(1<<TXEN0);
  UCSR0C=(1<<USBS0)|(3<<UCSZ00);
}

void USART_Transmit(unsigned char data)
{
  while(!(UCSR0A&(1<<UDRE0)));
  UDR0=data;
}

void SendString(char *StringPtr)
{
  while(*StringPtr !=0x00)
  {
    USART_Transmit(*StringPtr);
    StringPtr++;
  }
}

void setup()
{
    USART_Init(MYUBRR);
}

void loop()
{
 SendString("123456");
  delay(1000);
}

but I have no idea how to copy the content of the UDR0 register at the other end.但我不知道如何复制另一端的UDR0寄存器的内容。 The function should be something like: function 应该是这样的:

unsigned char USART_Receive(void)
{
  while(!(UCSR0A&(1<<RXC0)))

  return UDR0;
}

But I don't know how to actually use it;但我不知道如何实际使用它; I need to save all those chars from the serial to a string, then show the string on a LCD connected to the second Arduino Uno, but every time when I attempt to receive more that one char it's all working very bad(blank spaces, a lot of zeros, invalid characters).我需要将所有这些字符从串行保存到一个字符串,然后在连接到第二个 Arduino Uno 的 LCD 上显示该字符串,但是每次我尝试接收更多的一个字符时,它都工作得非常糟糕(空格,一个很多零,无效字符)。

I read that if I have an LCD connected to the second arduino, it's much better to not use the USART_Receive() function, and implement it as an interrupt, but I don't know how to do that either.我读到如果我有一个 LCD 连接到第二个 arduino,最好不要使用 USART_Receive() function,并将其实现为中断,但我也不知道该怎么做。

Do you have any idea how to transfer the string to the other Arduino?您知道如何将字符串传输到其他 Arduino 吗?

Perhaps try to put all the chars in an big array and then send that array to your LCD.也许尝试将所有字符放入一个大数组中,然后将该数组发送到您的 LCD。 That way, the time between reading one char and the next one is minimized.这样,读取一个字符和下一个字符之间的时间就最小化了。 (UART is asynchronous, so if you're too slow, you miss the requested data). (UART是异步的,所以如果你太慢,你会错过请求的数据)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM