基于stm32f10x系列单片机demo程序修改
配置串口
void USART1_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
/* 使能串口时钟*/
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA, ENABLE);
/* USART1的IO脚配置*/
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); //³õʼ»¯GPIOA
/* USART1 工作模式配置*/
USART_InitStructure.USART_BaudRate = 115200; //波特率
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);// USART1使能
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE); // 使能串口中断
NVIC_InitStructure.NVIC_IRQChannel=USART1_IRQn; //中断配置
NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority=1;
NVIC_Init(&NVIC_InitStructure);
}
NVIC_InitTypeDef 函数接口需要的头文件 #include "misc.h"
串口数据发送(发送到串口助手的数据,我们一般在单片机接收到串口数据后不知道数据是否正确,需要将接收到的数据显示出来可以用这个函数来实现)
void UART1SendByte(USART_TypeDef* USARTx,unsigned char SendData)
{
USART_SendData(USARTx,SendData);
while(USART_GetFlagStatus(USARTx, USART_FLAG_TXE) == RESET);
}
串口中断方式进行数据的接收
void USART1_IRQHandler(void)
{
uint8_t rec = 0;
char str[15];
char str1[15];
char str2[5];
uint16_t x=0;
uint16_t x1 = 0;
if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)
{
USART_ClearITPendingBit(USART1,USART_IT_RXNE);
rec = USART_ReceiveData(USART1);
//头判断
if(rec == '$')
{
gps_count =0;
count = 1;
for(i=0;i<RECEIVE_BUF_LEN;i++)
{
ReceiveBuff[i] = 0;
sendBuff[i] = 0;
}
ReceiveBuff[0] = rec;
gps_state = f_start;
}
else if(gps_state == f_start)
{
//判断接收的数据个数
if(rec == ',')
{
sendBuff[gps_count++] = count;
}
if(rec == '*' && gps_count > 15)
{
gps_state = f_end;
}
ReceiveBuff[count++] = rec;
}
else if(gps_state == f_end)
{
gps_state = f_finished;
//UART1SendByte(2);
for(i=0; i< count; i++)
{
UART1SendByte(USART1, ReceiveBuff[i]);
}
// ½âÎö
for( i =sendBuff[8]+1; i < sendBuff[9]; i++)
{
str[x++] = ReceiveBuff[i];
}
for( i =sendBuff[9]+1; i < sendBuff[10]; i++)
{
str1[x1++] = ReceiveBuff[i];
}
if(second_get == 1)
{
x_latitude = atof(str)*10;
y_longtitude = atof(str1)*10;
second_get = 2;
}
else if(second_get == 2)
{
x1_latitude = atof(str)*10;
y1_longtitude = atof(str1)*10;
three_get = 1;
}
if(three_get == 1)
{
float result1 = x_latitude - x1_latitude;
float result2 = y_longtitude - y1_longtitude;
if((abs(result1) > 5.0)||(abs(result2) > 5.0))
{
LED1( ON );
}
else if((abs(result1) < 5.0)&&(abs(result2) < 5.0))
{
//UART1SendByte(USART1, 2);
LED1( OFF );
}
x_latitude = x1_latitude;
y_longtitude = y1_longtitude;
second_get = 2;
}
}
}
if(USART_GetFlagStatus(USART1,USART_FLAG_ORE) == SET)
{
USART_ClearFlag(USART1,USART_FLAG_TC);
USART_ReceiveData(USART1);
}
}
中断函数中主要实现逻辑,接收串口发送的字节rec,对rec进行判断是否为符合要求的数据头,如果符合,将接收串口数据保存到ReceiveBuff中,这里没有对校验位进行判断,通过对‘"," 和结束符来判定数据是否接收完成,接收完成后在if(gps_state == f_end)中对最终的数据进行解析,
for(i=0; i< count; i++)
{
UART1SendByte(USART1, ReceiveBuff[i]);
}
将接收的完整数据打印出来
for( i =sendBuff[8]+1; i < sendBuff[9]; i++)
{
str[x++] = ReceiveBuff[i];
}
sendBuff中存储的是数据中的每一个", "的位置,通过这个我们能找到需要的字符串数据保存到str数组中
if(second_get == 1)
{
x_latitude = atof(str)*10;
y_longtitude = atof(str1)*10; //字符转float
second_get = 2;
}
else if(second_get == 2)
{
x1_latitude = atof(str)*10;
y1_longtitude = atof(str1)*10;
three_get = 1;
}
if(three_get == 1)
{
float result1 = x_latitude - x1_latitude;
float result2 = y_longtitude - y1_longtitude;
if((abs(result1) > 5.0)||(abs(result2) > 5.0))
{
LED1( ON );
}
else if((abs(result1) < 5.0)&&(abs(result2) < 5.0))
{
//UART1SendByte(USART1, 2);
LED1( OFF );
}
x_latitude = x1_latitude;
y_longtitude = y1_longtitude;
second_get = 2;
}
这个里面的作用是取接收到的相邻的两组数据进行比较判断来确定我们当前是否需要进行其他的操作
atof函数使用的头文件#include <stdlib.h>
sqrt函数使用的头文件 #include <math.h>
附 :使用的GPS模块的通信协议
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)