【问题标题】:AVR USART doesn't work as expectedAVR USART 无法按预期工作
【发布时间】:2016-04-08 11:23:11
【问题描述】:

我正在使用带有 GPS 模块的 ATMega32 在 LCD 显示屏上显示一些数据(经度和纬度)。 GPS 模块每秒以 9600 bps 发送一串数据。 该字符串是一个 NMEA 语句,以 $ 符号开头,我使用该字符来同步接收器 (AVR UART)。

这是我使用的代码:

// GPS.h

void GPS_read(char *sentence)           
{
  while ((*sentence = USART_receive()) != '$')
    ;
  USART_receive_string(++sentence);
}



// USART.h

unsigned char USART_receive(void)
{
  while (!(UCSRA & (1<<RXC)))
    ;
  return UDR;
}

void USART_receive_string(char *string)
{
  do
  {
    *string = USART_receive();
  } while (*string++ != '\n');                             // NMEA sentences are <CR><LF> terminated
  *string = '\0';   
}

我将一个 char 数组传递给 GPS_read,然后在 LCD 上显示该字符串。 根据我选择显示数据的时间,我会得到一些由 $G 和 \n 字符组成的垃圾数据。

我在这里犯了一些错误,但是已经两天了,我无法弄清楚我做错了什么(我是一个新手嵌入式程序员:))

请帮忙! 谢谢 卢卡

【问题讨论】:

    标签: gps avr uart atmega


    【解决方案1】:

    您是否检查过您的 TX 和 RX 的波特率是否正确?还要检查帧错误。

    【讨论】:

      【解决方案2】:

      你的代码有一些错误,试试这个:

      您尚未包含您的 char 数组声明,但我建议您使用索引器来跟踪您正在读取和/或写入数组中的哪个元素。

      unsigned char Sentence[*Insert array size here*];
      unsigned char Indexer = 0;
      

      至于你的函数,我想说你的 USART_receive() 函数很好,但是试试...

      void GPS_read(char *sentence)
      {
          unsigned char Data = USART_receive();   // Read initial value
          while (Data != '$')                     // while Data is not a '$' ...
              Data = USART_receive();             // ... Read the USART again until it is
      
          sentence[Indexer++] = Data;
          USART_receive_string(sentence);        
      }
      
      void USART_receive_string(char *string)
      {
          unsigned char Data = USART_receive();
      
          while (Data != '\n')
          {
              *string[Indexer++] = Data;
              Data = USART_receive();
          }
          *string[Indexer] = '\n';
      }
      

      【讨论】:

      • *sentence[Indexer++]? - 我不这么认为。
      • “您的代码有一些错误” - 这些错误是什么?
      • 为什么? *sentence 指向 char 数组的地址,Index 被用作索引器,一旦 Data 的值存储到数组的特定元素,将 Indexer 递增 1 以“指向”数组中的下一个元素对于 USART_receive_string() 函数。我错过了什么?
      • 不,sentence 指向某事,*sentencechar
      • 啊,看不到什么在盯着我 - 我已经编辑了答案。
      猜你喜欢
      • 1970-01-01
      • 2013-12-23
      • 2014-12-09
      • 2016-01-13
      • 2020-09-21
      • 2011-08-17
      • 2012-04-29
      • 2021-08-12
      • 2019-02-04
      相关资源
      最近更新 更多