我使用带有GPS模块的ATMega32在液晶屏上显示一些数据(经度和纬度)。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';
}我将一个字符数组传递给GPS_read,然后在LCD上显示该字符串。根据我选择的显示数据的时间,我会得到一些由$G和\n字符组成的垃圾数据。
我在这里犯了一些错误,但是已经两天了,我不知道我做错了什么(我是一个新手嵌入式程序员:)
请帮帮我!谢谢卢卡
发布于 2016-04-15 19:24:13
你检查过你的TX和RX波特率是正确的吗?同时检查是否有帧错误。
发布于 2016-04-27 01:50:43
您的代码中有一些错误,请尝试如下所示:
您没有包含您的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';
}https://stackoverflow.com/questions/36498371
复制相似问题