stm32f103c8t6串口2发送AT+LOCATION=1指令给EC01g,并解析GPS数据,stm32库函数版本的代码
时间: 2023-12-06 16:39:42 浏览: 66
下面是基于STM32库函数版本的代码示例,实现串口2发送AT指令给EC01g,并解析GPS数据:
```c
#include "stm32f10x.h"
#include <stdio.h>
#include <string.h>
#define GPS_BUFFER_SIZE 128
char gpsBuffer[GPS_BUFFER_SIZE];
uint8_t gpsIndex = 0;
uint8_t gpsReady = 0;
void USART2_Init(void);
void USART2_SendChar(char c);
void USART2_SendString(char *str);
void GPS_ParseData(void);
void GPS_ResetBuffer(void);
int main(void)
{
USART2_Init();
while (1)
{
if (gpsReady)
{
GPS_ParseData();
GPS_ResetBuffer();
gpsReady = 0;
}
}
}
void USART2_Init(void)
{
USART_InitTypeDef USART_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
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_3;
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(USART2, &USART_InitStructure);
USART_Cmd(USART2, ENABLE);
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);
}
void USART2_SendChar(char c)
{
while (!(USART2->SR & USART_SR_TXE))
;
USART2->DR = c;
while (!(USART2->SR & USART_SR_TC))
;
}
void USART2_SendString(char *str)
{
while (*str)
{
USART2_SendChar(*str);
str++;
}
}
void GPS_ParseData(void)
{
char *token;
uint8_t i = 0;
token = strtok(gpsBuffer, ",");
while (token != NULL)
{
if (i == 1)
{
printf("Latitude: %s\n", token);
}
else if (i == 3)
{
printf("Longitude: %s\n", token);
}
token = strtok(NULL, ",");
i++;
}
}
void GPS_ResetBuffer(void)
{
memset(gpsBuffer, 0, GPS_BUFFER_SIZE);
gpsIndex = 0;
}
void USART2_IRQHandler(void)
{
if (USART_GetITStatus(USART2, USART_IT_RXNE) != RESET)
{
char c = USART_ReceiveData(USART2);
if (c == '\n')
{
gpsReady = 1;
}
else if (c != '\r')
{
gpsBuffer[gpsIndex++] = c;
if (gpsIndex >= GPS_BUFFER_SIZE)
{
GPS_ResetBuffer();
}
}
}
}
```
在 main 函数中,我们初始化了串口2,并设置了一个死循环来监听 GPS 数据是否准备好(gpsReady 标志位被设置为1),如果准备好就调用 GPS_ParseData 函数解析数据,然后重置 GPS 缓冲区。
USART2_SendChar 和 USART2_SendString 函数用于向串口2发送数据。
GPS_ParseData 函数用于解析 GPS 数据,这里只是简单地将经度和纬度打印出来。实际应用中,可以根据需要解析更多的数据,如时间、速度等等。
GPS_ResetBuffer 函数用于清空 GPS 缓冲区和索引。
USART2_IRQHandler 函数是 USART2 的中断处理函数,当有数据从串口2接收到时,会触发 USART2_IRQn 中断,并在其中处理接收到的数据。当接收到 '\n' 字符时,将 gpsReady 标志位置为1,表示 GPS 数据已准备好。注意,如果缓冲区已满,需要调用 GPS_ResetBuffer 函数清空缓冲区,否则可能会导致数据被覆盖。
阅读全文