STM32
直播中

jfsky

16年用户 508经验值
私信 关注
[问答]

怎样去修改基于stm32f10x系列单片机的demo程序呢

怎样去修改基于STM32f10x系列单片机的demo程序呢?

使用GPS模块的通信协议有何功能?

回帖(1)

白旭蕊

2021-12-9 14:15:38
基于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                         {
                                ReceiveBuff = 0;
                                sendBuff = 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);
                        }
                        // ½âÎö
                        for( i =sendBuff[8]+1; i < sendBuff[9]; i++)
                        {
                                str[x++] = ReceiveBuff;
                        }
                        for( i =sendBuff[9]+1; i < sendBuff[10]; i++)
                        {
                                str1[x1++] = ReceiveBuff;
                        }

                                       
                        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);
}
将接收的完整数据打印出来


for( i =sendBuff[8]+1; i < sendBuff[9]; i++)
{
        str[x++] = ReceiveBuff;
}
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

  sqrt函数使用的头文件 #include

  附 :使用的GPS模块的通信协议
  
  

  

   
   
   
举报

更多回帖

发帖
×
20
完善资料,
赚取积分