stm32f103c8t6连接ec01g,at指令得到GPS位置,解析GPS数据代码
时间: 2023-08-21 14:02:33 浏览: 122
以下是一个简单的示例代码,用于连接STM32F103C8T6和EC01G,并使用AT指令获取GPS位置并解析数据:
```c
#include "stm32f10x.h"
#include <stdio.h>
#include <string.h>
#define GPS_BUFFER_LEN 256
char GPS_Buffer[GPS_BUFFER_LEN];
uint16_t GPS_Buffer_Index = 0;
void USART1_IRQHandler(void)
{
if (USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)
{
char ch = USART_ReceiveData(USART1);
if (GPS_Buffer_Index < GPS_BUFFER_LEN - 1)
{
GPS_Buffer[GPS_Buffer_Index++] = ch;
}
if (ch == '\n')
{
GPS_Buffer[GPS_Buffer_Index] = '\0';
// 解析GPS数据
if (strstr(GPS_Buffer, "$GPGGA"))
{
char* p = strtok(GPS_Buffer, ",");
int i = 0;
while (p != NULL)
{
switch (i++)
{
case 2: // 纬度
{
float lat = atof(p + 2) / 60.0f + atof(p) * 100.0f / 60.0f;
printf("Latitude: %.6f\r\n", lat);
break;
}
case 3: // 纬度半球
{
printf("Latitude direction: %c\r\n", *p);
break;
}
case 4: // 经度
{
float lon = atof(p + 3) / 60.0f + atof(p) * 100.0f / 60.0f;
printf("Longitude: %.6f\r\n", lon);
break;
}
case 5: // 经度半球
{
printf("Longitude direction: %c\r\n", *p);
break;
}
case 6: // GPS状态
{
printf("GPS status: %c\r\n", *p);
break;
}
}
p = strtok(NULL, ",");
}
}
GPS_Buffer_Index = 0;
}
}
}
void USART1_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
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_InitStructure.USART_BaudRate = 9600;
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);
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
USART_Cmd(USART1, ENABLE);
}
void EC01G_Send_AT_Command(char* command)
{
while (*command)
{
USART_SendData(USART1, *command++);
while (USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
}
USART_SendData(USART1, '\r');
while (USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
USART_SendData(USART1, '\n');
while (USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
}
void EC01G_Init(void)
{
EC01G_Send_AT_Command("AT+CGNSPWR=1");
EC01G_Send_AT_Command("AT+CGNSSEQ=\"RMC\"");
EC01G_Send_AT_Command("AT+CGNSTST=1");
}
int main(void)
{
USART1_Init();
EC01G_Init();
while (1)
{
// 定期发送AT指令获取GPS位置
EC01G_Send_AT_Command("AT+CGNSINF");
delay_ms(1000);
}
}
```
注意,这只是一个示例代码,可能需要根据你的具体硬件和需求进行修改。
阅读全文