将北斗模块连接到单片机系统板,将经纬度和海拔数据解析出来,并在液晶显示出来。请给出代码
时间: 2023-12-29 18:03:45 浏览: 201
以下是将北斗模块连接到单片机系统板,解析经纬度和海拔数据并在液晶显示出来的示例代码。我们假设使用的是STM32系列单片机,液晶屏幕为16x2字符型液晶。
```c
#include <stdio.h>
#include "stm32f10x.h"
#include "lcd.h"
#define BAUDRATE 9600
void USART1_Init(void)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA, ENABLE);
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
USART_InitTypeDef USART_InitStructure;
USART_InitStructure.USART_BaudRate = BAUDRATE;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_Parity = USART_Parity_No;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
USART_Init(USART1, &USART_InitStructure);
USART_Cmd(USART1, ENABLE);
}
void USART1_SendByte(uint8_t byte)
{
while (USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
USART_SendData(USART1, byte);
}
uint8_t USART1_ReceiveByte(void)
{
while (USART_GetFlagStatus(USART1, USART_FLAG_RXNE) == RESET);
return USART_ReceiveData(USART1);
}
void USART1_SendString(const char* str)
{
while (*str)
USART1_SendByte(*str++);
}
void USART1_SendNumber(uint32_t num)
{
char buffer[10];
sprintf(buffer, "%d", num);
USART1_SendString(buffer);
}
void USART1_SendFloat(float num)
{
char buffer[16];
sprintf(buffer, "%.2f", num);
USART1_SendString(buffer);
}
void NMEA_Parse(char* sentence)
{
// Check if it is a GGA sentence
if (sentence[3] == 'G' && sentence[4] == 'G' && sentence[5] == 'A')
{
// Parse latitude and longitude
char* token = strtok(sentence, ",");
for (int i = 0; i < 2; i++)
token = strtok(NULL, ",");
float latitude = atof(token) / 100;
token = strtok(NULL, ",");
if (token[0] == 'S')
latitude = -latitude;
token = strtok(NULL, ",");
float longitude = atof(token) / 100;
token = strtok(NULL, ",");
if (token[0] == 'W')
longitude = -longitude;
// Parse altitude
token = strtok(NULL, ",");
float altitude = atof(token);
// Display data on LCD
LCD_Clear();
LCD_SetCursor(0, 0);
LCD_Print("Latitude:");
LCD_SetCursor(10, 0);
USART1_SendFloat(latitude);
LCD_SetCursor(0, 1);
LCD_Print("Longitude:");
LCD_SetCursor(11, 1);
USART1_SendFloat(longitude);
LCD_SetCursor(0, 2);
LCD_Print("Altitude:");
LCD_SetCursor(9, 2);
USART1_SendFloat(altitude);
}
}
int main(void)
{
LCD_Init();
USART1_Init();
char buffer[128];
int buffer_index = 0;
while (1)
{
char byte = USART1_ReceiveByte();
if (byte == '$')
{
buffer_index = 0;
buffer[buffer_index++] = byte;
}
else if (byte == '\r')
{
buffer[buffer_index++] = '\0';
NMEA_Parse(buffer);
}
else
{
buffer[buffer_index++] = byte;
}
}
}
```
上述代码中,我们使用了USART1串口与北斗模块通讯,并使用了液晶屏幕进行显示。在接收到北斗模块传来的数据后,我们使用了NMEA协议解析经纬度和海拔数据,并通过串口和液晶屏幕进行显示。
需要注意的是,上述代码中的解析方式仅适用于GGA类型的NMEA语句,如果北斗模块传来的语句类型不是GGA,则需要根据具体情况进行修改。另外,上述代码中的串口波特率为9600,如果北斗模块的波特率不同,则需要调整串口波特率。
阅读全文