找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 4700|回复: 4
打印 上一主题 下一主题
收起左侧

A6_A7 GPS模块的51单片机例程源码分享

[复制链接]
跳转到指定楼层
楼主

1.准备一个STC89C52最小系统板
   
2.烧录代码(先烧录代码后接线,防止接线后下载不了代码)
3.给模块供电,给模块开机
4.接线:
    STC89C52        A6&A7
    GND        ->        GND
    TXD/P3.1->        U_RXD
    RXD/P3.0->        U_TXD

单片机源程序如下:
  1. /*********************************************************************
  2.                  作者:神秘藏宝室

  3.         本例程仅适用于在本店购买模块的用户使用,鄙视其它店铺的盗用行为
  4.         版权所有,盗版必究!!!
  5.         A6模块链接:

  6.         A7模块                STC15W最小系统
  7.         GND                 -->                GND
  8.         U_TXD        -->                P0.0/RXD3
  9.         U_RXD        <--                 P0.1/TXD3

  10.         GPS_TXD        -->                P1.0/RXD2
  11. *********************************************************************/
  12. #include "main.h"
  13. #include "delay.h"
  14. #include "uart.h"

  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. #include <string.h>


  18. /*************        功能说明        **************



  19. ******************************************/

  20. /*************        本地常量声明        **************/
  21. #define Success 1U
  22. #define Failure 0U

  23. /*************        本地变量声明        **************/
  24. unsigned long  Time_Cont = 0;       //定时器计数器
  25. bit ledState = LED_OFF;

  26. unsigned int gprsBufferCount = 0;



  27. /*************        本地函数声明        **************/



  28. /*************  外部函数和变量声明 *****************/



  29. /******************** IO配置函数 **************************/
  30. void        GPIO_config(void)
  31. {
  32.         P5M0 = 0;                 //设置准双向口
  33.         P5M1 = 0;
  34. }

  35. /******************** 主函数 **************************/
  36. void main(void)
  37. {
  38.         GPIO_config();
  39.         UartInit();

  40.         SendString("ILoveMCU.taobao.com");
  41.         delay_ms(200);
  42.         clrStruct();

  43.         initGprs();
  44.         while(1)
  45.         {
  46.                 parseGpsBuffer();
  47.                 printGpsBuffer();       
  48.         }
  49. }

  50. void initGprs()
  51. {
  52.         if (sendCommand("AT\r\n", "OK", 3000, 10) == Success);
  53.         else errorLog(1);


  54. //      如果输入 AT+CREG? <CR>则返回+CREG: <mode>, <stat> [ ,<lac>,<ci> ]
  55. // 注: <mode>的值共有三个选项,分别是 0 or 1 or 2,  其中0 代表关闭网络注册结果
  56. //            码, 1 代表当网络注册状态改变时激活网络注册结果码, 2 代表激活网
  57. // 络注册结果码同时显示区域和小区信息.
  58. //    <stat>的返回值共有三个,分别是 0, 1, 2,3,4,5 ,  其中 0 代表没有注册网络同时
  59. //   模块没有找到运营商, 1代注册到了本地网络, 2 代表找到运营商但没
  60. // 有注册网络, 3 代表注册被拒绝, 4 代表未知的数据, 5代表注册在漫游
  61. // 状态.
  62. //    <lac>表示所属网络区域代码,十六进制格式显示,如: “ 279C”
  63. //    <ci>表示所属网络的小区 ID,十六进制格式显示,如: “ 0EB2”  Tech-Link T&E
  64.         if (sendCommand("AT+CPIN?\r\n", "READY", 1000, 10) == Success);
  65.         else errorLog(2);
  66.         delay_ms(10);

  67.         if (sendCommand("AT+CREG?\r\n", "CREG: 1", 1000, 10) == Success);
  68.         else errorLog(3);
  69.         delay_ms(10);

  70.         if (sendCommand("AT+GPS=1\r\n", "OK\r\n", 1000, 10) == Success);
  71.         else errorLog(4);
  72.         delay_ms(10);
  73. }

  74. unsigned int sendCommand(char *Command, char *Response, unsigned long Timeout, unsigned char Retry)
  75. {
  76.         unsigned char n;
  77.         Uart3CLR_Buf();
  78.         for (n = 0; n < Retry; n++)
  79.         {
  80.                 SendString("\r\n---------send AT Command:---------\r\n");
  81.                 SendString(Command);

  82.                 Uart3SendString(Command);                 //发送GPRS指令

  83.                 Time_Cont = 0;
  84.                 while (Time_Cont < Timeout)
  85.                 {
  86.                         delay_ms(100);
  87.                         Time_Cont += 100;
  88.                         if (strstr(Uart3_Rec_Buf, Response) != NULL)
  89.                         {
  90.                                 SendString("\r\n==========receive AT Command:==========\r\n");
  91.                                 SendString(Uart3_Rec_Buf); //输出接收到的信息
  92.                                 Uart3CLR_Buf();
  93.                                 return Success;
  94.                         }
  95.                        
  96.                 }
  97.                 Time_Cont = 0;
  98.         }
  99.         SendString("\r\n==========receive AT Command:==========\r\n");
  100.         SendString(Uart3_Rec_Buf);//输出接收到的信息
  101.         Uart3CLR_Buf();
  102.         return Failure;
  103. }


  104. void soft_reset(void)         //制造重启命令
  105. {
  106.    ((void (code *) (void)) 0x0000) ();
  107. }

  108. void errorLog(int num)
  109. {
  110.         SendString("ERROR");
  111.         SendData(num+0x30);
  112.         SendString("\r\n");
  113.         while (1)
  114.         {
  115.                   if (sendCommand("AT\r\n", "OK", 100, 10) == Success)
  116.                 {
  117.                         SendString("\r\nRESET!!!!!!\r\n");
  118.                         soft_reset();
  119.                 }
  120.                 delay_ms(200);
  121.         }
  122. }


  123. void parseGpsBuffer()
  124. {
  125.         char *subString;
  126.         char *subStringNext;
  127.         char i = 0;
  128.         if (Save_Data.isGetData)
  129.         {
  130.                 Save_Data.isGetData = false;
  131.                 SendString("**************\r\n");
  132.                 SendString(Save_Data.GPS_Buffer);

  133.                
  134.                 for (i = 0 ; i <= 6 ; i++)
  135.                 {
  136.                         if (i == 0)
  137.                         {
  138.                                 if ((subString = strstr(Save_Data.GPS_Buffer, ",")) == NULL)
  139.                                         errorLog(1);        //解析错误
  140.                         }
  141.                         else
  142.                         {
  143.                                 subString++;
  144.                                 if ((subStringNext = strstr(subString, ",")) != NULL)
  145.                                 {
  146.                                         char usefullBuffer[2];
  147.                                         switch(i)
  148.                                         {
  149.                                                 case 1:memcpy(Save_Data.UTCTime, subString, subStringNext - subString);break;        //获取UTC时间
  150.                                                 case 2:memcpy(usefullBuffer, subString, subStringNext - subString);break;        //获取UTC时间
  151.                                                 case 3:memcpy(Save_Data.latitude, subString, subStringNext - subString);break;        //获取纬度信息
  152.                                                 case 4:memcpy(Save_Data.N_S, subString, subStringNext - subString);break;        //获取N/S
  153.                                                 case 5:memcpy(Save_Data.longitude, subString, subStringNext - subString);break;        //获取经度信息
  154.                                                 case 6:memcpy(Save_Data.E_W, subString, subStringNext - subString);break;        //获取E/W

  155.                                                 default:break;
  156.                                         }

  157.                                         subString = subStringNext;
  158.                                         Save_Data.isParseData = true;
  159.                                         if(usefullBuffer[0] == 'A')
  160.                                                 Save_Data.isUsefull = true;
  161.                                         else if(usefullBuffer[0] == 'V')
  162.                                                 Save_Data.isUsefull = false;

  163.                                 }
  164.                                 else
  165.                                 {
  166.                                         errorLog(2);        //解析错误
  167.                                 }
  168.                         }


  169.                 }
  170.         }
  171. }

  172. void printGpsBuffer()
  173. {
  174.         if (Save_Data.isParseData)
  175.         {
  176.                 Save_Data.isParseData = false;
  177.                
  178.                 SendString("Save_Data.UTCTime = ");
  179.                 SendString(Save_Data.UTCTime);
  180.                 SendString("\r\n");

  181.                 if(Save_Data.isUsefull)
  182.                 {
  183.                         Save_Data.isUsefull = false;
  184.                         SendString("Save_Data.latitude = ");
  185.                         SendString(Save_Data.latitude);
  186.                         SendString("\r\n");


  187.                         SendString("Save_Data.N_S = ");
  188.                         SendString(Save_Data.N_S);
  189.                         SendString("\r\n");

  190.                         SendString("Save_Data.longitude = ");
  191.                         SendString(Save_Data.longitude);
  192.                         SendString("\r\n");

  193.                         SendString("Save_Data.E_W = ");
  194. ……………………

  195. …………限于本文篇幅 余下代码请从51黑下载附件…………
复制代码

所有资料51hei提供下载:
51单片机例程_A6_A7.zip (638.21 KB, 下载次数: 47)




分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏2 分享淘帖 顶 踩1
回复

使用道具 举报

沙发
ID:562672 发表于 2019-6-14 17:03 | 只看该作者
楼主厉害
回复

使用道具 举报

板凳
ID:562672 发表于 2019-6-15 09:37 | 只看该作者
向楼主学习
回复

使用道具 举报

地板
ID:585455 发表于 2019-7-22 15:36 | 只看该作者
感謝作品的分享
回复

使用道具 举报

5#
ID:448965 发表于 2019-10-17 23:37 | 只看该作者
okok 楼主厉害 楼主厉害
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表