找回密碼
 立即注冊(cè)

QQ登錄

只需一步,快速開始

搜索
查看: 5166|回復(fù): 4
收起左側(cè)

A6_A7 GPS模塊的51單片機(jī)例程源碼分享

[復(fù)制鏈接]
ID:339027 發(fā)表于 2018-8-11 11:05 | 顯示全部樓層 |閱讀模式
QQ截圖20180811110255.png
1.準(zhǔn)備一個(gè)STC89C52最小系統(tǒng)板
   
2.燒錄代碼(先燒錄代碼后接線,防止接線后下載不了代碼)
3.給模塊供電,給模塊開機(jī)
4.接線:
    STC89C52        A6&A7
    GND        ->        GND
    TXD/P3.1->        U_RXD
    RXD/P3.0->        U_TXD

單片機(jī)源程序如下:
  1. /*********************************************************************
  2.                  作者:神秘藏寶室

  3.         本例程僅適用于在本店購(gòu)買模塊的用戶使用,鄙視其它店鋪的盜用行為
  4.         版權(quán)所有,盜版必究。!
  5.         A6模塊鏈接:

  6.         A7模塊                STC15W最小系統(tǒng)
  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;       //定時(shí)器計(jì)數(shù)器
  25. bit ledState = LED_OFF;

  26. unsigned int gprsBufferCount = 0;



  27. /*************        本地函數(shù)聲明        **************/



  28. /*************  外部函數(shù)和變量聲明 *****************/



  29. /******************** IO配置函數(shù) **************************/
  30. void        GPIO_config(void)
  31. {
  32.         P5M0 = 0;                 //設(shè)置準(zhǔn)雙向口
  33.         P5M1 = 0;
  34. }

  35. /******************** 主函數(shù) **************************/
  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>的值共有三個(gè)選項(xiàng),分別是 0 or 1 or 2,  其中0 代表關(guān)閉網(wǎng)絡(luò)注冊(cè)結(jié)果
  56. //            碼, 1 代表當(dāng)網(wǎng)絡(luò)注冊(cè)狀態(tài)改變時(shí)激活網(wǎng)絡(luò)注冊(cè)結(jié)果碼, 2 代表激活網(wǎng)
  57. // 絡(luò)注冊(cè)結(jié)果碼同時(shí)顯示區(qū)域和小區(qū)信息.
  58. //    <stat>的返回值共有三個(gè),分別是 0, 1, 2,3,4,5 ,  其中 0 代表沒有注冊(cè)網(wǎng)絡(luò)同時(shí)
  59. //   模塊沒有找到運(yùn)營(yíng)商, 1代注冊(cè)到了本地網(wǎng)絡(luò), 2 代表找到運(yùn)營(yíng)商但沒
  60. // 有注冊(cè)網(wǎng)絡(luò), 3 代表注冊(cè)被拒絕, 4 代表未知的數(shù)據(jù), 5代表注冊(cè)在漫游
  61. // 狀態(tài).
  62. //    <lac>表示所屬網(wǎng)絡(luò)區(qū)域代碼,十六進(jìn)制格式顯示,如: “ 279C”
  63. //    <ci>表示所屬網(wǎng)絡(luò)的小區(qū) ID,十六進(jìn)制格式顯示,如: “ 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);                 //發(fā)送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);        //解析錯(cuò)誤
  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時(shí)間
  150.                                                 case 2:memcpy(usefullBuffer, subString, subStringNext - subString);break;        //獲取UTC時(shí)間
  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;        //獲取經(jīng)度信息
  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);        //解析錯(cuò)誤
  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. …………限于本文篇幅 余下代碼請(qǐng)從51黑下載附件…………
復(fù)制代碼

所有資料51hei提供下載:
51單片機(jī)例程_A6_A7.zip (638.21 KB, 下載次數(shù): 49)




回復(fù)

使用道具 舉報(bào)

ID:562672 發(fā)表于 2019-6-14 17:03 | 顯示全部樓層
樓主厲害
回復(fù)

使用道具 舉報(bào)

ID:562672 發(fā)表于 2019-6-15 09:37 | 顯示全部樓層
向樓主學(xué)習(xí)
回復(fù)

使用道具 舉報(bào)

ID:585455 發(fā)表于 2019-7-22 15:36 | 顯示全部樓層
感謝作品的分享
回復(fù)

使用道具 舉報(bào)

ID:448965 發(fā)表于 2019-10-17 23:37 | 顯示全部樓層
okok 樓主厲害 樓主厲害
回復(fù)

使用道具 舉報(bào)

本版積分規(guī)則

手機(jī)版|小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機(jī)教程網(wǎng)

快速回復(fù) 返回頂部 返回列表