stm32f103c8t6串口2发送AT+LOCATION=1指令给EC01g,得到GPS,解析GPS数据,stm32库函数版本的代码
时间: 2023-12-06 10:39:35 浏览: 147
以下是使用STM32库函数版本的代码,实现串口2发送AT指令给EC01g模块并解析GPS数据。
首先需要定义串口发送和接收的缓冲区大小:
```c
#define RX_BUFFER_SIZE 256
#define TX_BUFFER_SIZE 256
```
然后定义串口发送和接收的缓冲区:
```c
uint8_t rx_buffer[RX_BUFFER_SIZE];
uint8_t tx_buffer[TX_BUFFER_SIZE];
```
接下来需要初始化串口2:
```c
void init_uart2(uint32_t baud_rate)
{
// Enable GPIOA clock
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
// Enable USART2 clock
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
// Configure USART2 Tx (PA2) as alternate function push-pull
GPIO_InitTypeDef gpio_init;
gpio_init.GPIO_Pin = GPIO_Pin_2;
gpio_init.GPIO_Mode = GPIO_Mode_AF_PP;
gpio_init.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &gpio_init);
// Configure USART2 Rx (PA3) as input floating
gpio_init.GPIO_Pin = GPIO_Pin_3;
gpio_init.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &gpio_init);
// Configure USART2
USART_InitTypeDef usart_init;
usart_init.USART_BaudRate = baud_rate;
usart_init.USART_WordLength = USART_WordLength_8b;
usart_init.USART_StopBits = USART_StopBits_1;
usart_init.USART_Parity = USART_Parity_No;
usart_init.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
usart_init.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
USART_Init(USART2, &usart_init);
// Enable USART2
USART_Cmd(USART2, ENABLE);
}
```
然后需要定义串口发送和接收的中断处理函数:
```c
void USART2_IRQHandler(void)
{
// Check if receive buffer not empty
if (USART_GetITStatus(USART2, USART_IT_RXNE) != RESET)
{
uint8_t data = USART_ReceiveData(USART2);
rx_buffer[rx_index++] = data;
// Check if receive buffer overflow
if (rx_index >= RX_BUFFER_SIZE)
{
rx_index = 0;
rx_overflow = true;
}
}
// Check if transmission completed
if (USART_GetITStatus(USART2, USART_IT_TC) != RESET)
{
USART_ClearITPendingBit(USART2, USART_IT_TC);
tx_busy = false;
}
}
```
接下来定义发送AT指令的函数:
```c
void send_at_command(const char* command)
{
// Wait for previous transmission to complete
while (tx_busy)
{
}
// Clear receive buffer and overflow flag
rx_index = 0;
rx_overflow = false;
// Copy command to transmit buffer
uint32_t len = strlen(command);
memcpy(tx_buffer, command, len);
// Send command
USART_ITConfig(USART2, USART_IT_TC, DISABLE);
USART_DMACmd(USART2, USART_DMAReq_Tx, DISABLE);
USART_ClearITPendingBit(USART2, USART_IT_TC);
DMA_Cmd(DMA1_Channel7, DISABLE);
DMA1_Channel7->CNDTR = len;
DMA1_Channel7->CPAR = (uint32_t)&USART2->DR;
DMA1_Channel7->CMAR = (uint32_t)tx_buffer;
DMA_Cmd(DMA1_Channel7, ENABLE);
USART_DMACmd(USART2, USART_DMAReq_Tx, ENABLE);
USART_ITConfig(USART2, USART_IT_TC, ENABLE);
tx_busy = true;
// Wait for response
uint32_t timeout = 10000;
while (timeout--)
{
if (rx_index > 0 && rx_buffer[rx_index - 1] == '\n')
{
break;
}
delay_ms(1);
}
}
```
接下来定义解析GPS数据的函数:
```c
void parse_gps_data(const char* data)
{
char* token = strtok((char*)data, ",");
int i = 0;
while (token != NULL)
{
i++;
if (i == 3)
{
// Latitude
float lat_degrees = atof(token) / 100;
float lat_minutes = fmod(atof(token), 100);
float lat_decimal = lat_degrees + (lat_minutes / 60);
printf("Latitude: %f\n", lat_decimal);
}
else if (i == 4)
{
// North or South
if (strcmp(token, "N") == 0)
{
printf("Direction: North\n");
}
else if (strcmp(token, "S") == 0)
{
printf("Direction: South\n");
}
}
else if (i == 5)
{
// Longitude
float lon_degrees = atof(token) / 100;
float lon_minutes = fmod(atof(token), 100);
float lon_decimal = lon_degrees + (lon_minutes / 60);
printf("Longitude: %f\n", lon_decimal);
}
else if (i == 6)
{
// East or West
if (strcmp(token, "E") == 0)
{
printf("Direction: East\n");
}
else if (strcmp(token, "W") == 0)
{
printf("Direction: West\n");
}
}
token = strtok(NULL, ",");
}
}
```
最后,在main函数中调用上述函数:
```c
int main()
{
// Initialize UART2
init_uart2(9600);
// Send AT command to EC01g module
send_at_command("AT+LOCATION=1\r\n");
// Parse GPS data
if (!rx_overflow)
{
parse_gps_data((const char*)rx_buffer);
}
while (1)
{
}
}
```
阅读全文