基于STM32实现的的短信实时传送位置----GPS+GSM

该博客介绍了如何使用STM32微控制器接收GPS数据,解码经纬度信息,并通过GSM模块将位置信息以短信形式发送到指定手机。主要涉及STM32串口通信、GPS解码及GSM短信发送功能,代码包括main.c、串口驱动文件和GSM模块配置等。每5秒发送一次位置更新。
摘要由CSDN通过智能技术生成

 

总的来说就是实现了一个GPS数据通过串口发送给STM32,

STM32进行解码,

在通过串口把解码提取出的经纬度发送给GSM,

GSM根据给定的手机号发短信过去。

 

main函数里的最后一个while循环是每隔5s发一个位置出去

延时函数写在sim900a.c里,可以自行调节时间间隔。

就是这么任性。

 

 

 

 

main.c

/*
  ******************************************************************************
  * @attention
  *
  * 实验平台:野火 ISO-STM32 开发板

  ******************************************************************************
    */
#include "stm32f10x.h"
#include "bsp_usart1.h"
#include "gps_config.h"
#include <string.h>
#include "gsm.h"
#include <stdio.h>

        

extern void nmea_decode_test(void);

/*
 * 测试GPS模块功能
 * 
 */
 
 
double  zaishichua;
double  zaishichub;
int main(void)
{
    char zaiship1[14];
    char zaiship2[14];
    /* 配置USART1 用于向电脑printf调试信息*/
   USART1_Config();                          
    /* 初始化GPS模块使用的接口 */
    GPS_Config();
    printf("\r\nGPS模块测试例程\r\n");

    printf("\r\nGPS模块测试例程\r\n");      /
         while(1)
         {
    /* GPS解码测试 */
    nmea_decode_test();
    printf("\r\n纬度:%f,经度%f\r\n",zaishichua,zaishichub);
     printf("\r\n纬度:%f,经度%f\r\n",zaishichua,zaishichub);
   

      sprintf(zaiship1,"%013.6f",zaishichua);
      sprintf(zaiship2,"%013.6f",zaishichub);
       gsm(zaiship1,zaiship2);
         }

  
  
}

 

 

bsp_usart1.c

/**
  ******************************************************************************
  * @file    bsp_usart1.c
  * @author  fire
  * @version V1.0
  * @date    2013-xx-xx
  * @brief   重现c库printf函数到usart端口
  ******************************************************************************
  * @attention
  *
  * 实验平台:野火 iSO STM32 开发板
  *
  ******************************************************************************
  */

#include "bsp_usart1.h"


/**
 * @brief  USART1 GPIO 配置,工作模式配置。115200 8-N-1
 * @param  无
 * @retval 无
 */
void USART1_Config(void)
{
    GPIO_InitTypeDef GPIO_InitStructure;
    USART_InitTypeDef USART_InitStructure;

    /* config USART1 clock */
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA, ENABLE);

    /* USART1 GPIO config */
    /* Configure USART1 Tx (PA.09) as alternate function push-pull */
    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);

    /* Configure USART1 Rx (PA.10) as input floating */
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
    GPIO_Init(GPIOA, &GPIO_InitStructure);

    /* USART1 mode config */
    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);
}

///重定向c库函数printf到USART1
int fputc(int ch, FILE *f)
{
    /* 发送一个字节数据到USART1 */
    USART_SendData(USART1, (uint8_t) ch);

    /* 等待发送完毕 */
    while (USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);

    return (ch);
}

///重定向c库函数scanf到USART1
int fgetc(FILE *f)
{
    /* 等待串口1输入数据 */
    while (USART_GetFlagStatus(USART1, USART_FLAG_RXNE) == RESET);

    return (int)USART_ReceiveData(USART1);
}



/*********************************************END OF FILE**********************/

 

bsp_usart2.c

 

/******************** (C) COPYRIGHT 2012 WildFire Team **************************
 * 文件名  :usart2.c
 * 描述    :将printf函数重定向到USART2。这样就可以用printf函数将单片机的数据
 *           打印到PC上的超级终端或串口调试助手。         
 * 实验平台:野火STM32开发板
 * 库版本  :ST3.5.0
 *
 * 作者    :wildfire team 
**********************************************************************************/
#include "bsp_usart2.h"
#include <stdarg.h>


/// 配置USART2接收中断
static void NVIC_Configuration(void)
{
    NVIC_InitTypeDef NVIC_InitStructure;
    /* Configure the NVIC Preemption Priority Bits */
    NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);

    /* Enable the USARTy Interrupt */
    NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority =0;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
    NVIC_Init(&NVIC_InitStructure);
}

/*
 * 函数名:USART2_Config
 * 描述  :USART2 GPIO 配置,工作模式配置
 * 输入  :无
 * 输出  : 无
 * 调用  :外部调用
 */
void USART2_Config(void)
{
    GPIO_InitTypeDef GPIO_InitStructure;
    USART_InitTypeDef USART_InitStructure;

    /* config USART2 clock */
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);

    /* USART2 GPIO config */
   /* Configure USART2 Tx (PA.02) as alternate function push-pull */
    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);
        
  /* Configure USART2 Rx (PA.03) as input floating */
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  GPIO_Init(GPIOA, &GPIO_InitStructure);
      
    /* USART2 mode config */
    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(USART2, &USART_InitStructure); 
    
    /*    配置中断优先级 */
    NVIC_Configuration();
    /* 使能串口2接收中断 */
    USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);

    
    USART_Cmd(USART2, ENABLE);
}

/*
 * 函数名:fputc
 * 描述  :重定向c库函数printf到USART2
 * 输入  :无
 * 输出  :无
 * 调用  :由printf调用
 */
//int fputc(int ch, FILE *f)
  • 3
    点赞
  • 65
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值