|
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ī)源程序如下:
- /*********************************************************************
- 作者:神秘藏寶室
- 本例程僅適用于在本店購(gòu)買模塊的用戶使用,鄙視其它店鋪的盜用行為
- 版權(quán)所有,盜版必究。!
- A6模塊鏈接:
- A7模塊 STC15W最小系統(tǒng)
- GND --> GND
- U_TXD --> P0.0/RXD3
- U_RXD <-- P0.1/TXD3
- GPS_TXD --> P1.0/RXD2
- *********************************************************************/
- #include "main.h"
- #include "delay.h"
- #include "uart.h"
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- /************* 功能說明 **************
- ******************************************/
- /************* 本地常量聲明 **************/
- #define Success 1U
- #define Failure 0U
- /************* 本地變量聲明 **************/
- unsigned long Time_Cont = 0; //定時(shí)器計(jì)數(shù)器
- bit ledState = LED_OFF;
- unsigned int gprsBufferCount = 0;
- /************* 本地函數(shù)聲明 **************/
- /************* 外部函數(shù)和變量聲明 *****************/
- /******************** IO配置函數(shù) **************************/
- void GPIO_config(void)
- {
- P5M0 = 0; //設(shè)置準(zhǔn)雙向口
- P5M1 = 0;
- }
- /******************** 主函數(shù) **************************/
- void main(void)
- {
- GPIO_config();
- UartInit();
- SendString("ILoveMCU.taobao.com");
- delay_ms(200);
- clrStruct();
- initGprs();
- while(1)
- {
- parseGpsBuffer();
- printGpsBuffer();
- }
- }
- void initGprs()
- {
- if (sendCommand("AT\r\n", "OK", 3000, 10) == Success);
- else errorLog(1);
- // 如果輸入 AT+CREG? <CR>則返回+CREG: <mode>, <stat> [ ,<lac>,<ci> ]
- // 注: <mode>的值共有三個(gè)選項(xiàng),分別是 0 or 1 or 2, 其中0 代表關(guān)閉網(wǎng)絡(luò)注冊(cè)結(jié)果
- // 碼, 1 代表當(dāng)網(wǎng)絡(luò)注冊(cè)狀態(tài)改變時(shí)激活網(wǎng)絡(luò)注冊(cè)結(jié)果碼, 2 代表激活網(wǎng)
- // 絡(luò)注冊(cè)結(jié)果碼同時(shí)顯示區(qū)域和小區(qū)信息.
- // <stat>的返回值共有三個(gè),分別是 0, 1, 2,3,4,5 , 其中 0 代表沒有注冊(cè)網(wǎng)絡(luò)同時(shí)
- // 模塊沒有找到運(yùn)營(yíng)商, 1代注冊(cè)到了本地網(wǎng)絡(luò), 2 代表找到運(yùn)營(yíng)商但沒
- // 有注冊(cè)網(wǎng)絡(luò), 3 代表注冊(cè)被拒絕, 4 代表未知的數(shù)據(jù), 5代表注冊(cè)在漫游
- // 狀態(tài).
- // <lac>表示所屬網(wǎng)絡(luò)區(qū)域代碼,十六進(jìn)制格式顯示,如: “ 279C”
- // <ci>表示所屬網(wǎng)絡(luò)的小區(qū) ID,十六進(jìn)制格式顯示,如: “ 0EB2” Tech-Link T&E
- if (sendCommand("AT+CPIN?\r\n", "READY", 1000, 10) == Success);
- else errorLog(2);
- delay_ms(10);
- if (sendCommand("AT+CREG?\r\n", "CREG: 1", 1000, 10) == Success);
- else errorLog(3);
- delay_ms(10);
- if (sendCommand("AT+GPS=1\r\n", "OK\r\n", 1000, 10) == Success);
- else errorLog(4);
- delay_ms(10);
- }
- unsigned int sendCommand(char *Command, char *Response, unsigned long Timeout, unsigned char Retry)
- {
- unsigned char n;
- Uart3CLR_Buf();
- for (n = 0; n < Retry; n++)
- {
- SendString("\r\n---------send AT Command:---------\r\n");
- SendString(Command);
- Uart3SendString(Command); //發(fā)送GPRS指令
- Time_Cont = 0;
- while (Time_Cont < Timeout)
- {
- delay_ms(100);
- Time_Cont += 100;
- if (strstr(Uart3_Rec_Buf, Response) != NULL)
- {
- SendString("\r\n==========receive AT Command:==========\r\n");
- SendString(Uart3_Rec_Buf); //輸出接收到的信息
- Uart3CLR_Buf();
- return Success;
- }
-
- }
- Time_Cont = 0;
- }
- SendString("\r\n==========receive AT Command:==========\r\n");
- SendString(Uart3_Rec_Buf);//輸出接收到的信息
- Uart3CLR_Buf();
- return Failure;
- }
- void soft_reset(void) //制造重啟命令
- {
- ((void (code *) (void)) 0x0000) ();
- }
- void errorLog(int num)
- {
- SendString("ERROR");
- SendData(num+0x30);
- SendString("\r\n");
- while (1)
- {
- if (sendCommand("AT\r\n", "OK", 100, 10) == Success)
- {
- SendString("\r\nRESET!!!!!!\r\n");
- soft_reset();
- }
- delay_ms(200);
- }
- }
- void parseGpsBuffer()
- {
- char *subString;
- char *subStringNext;
- char i = 0;
- if (Save_Data.isGetData)
- {
- Save_Data.isGetData = false;
- SendString("**************\r\n");
- SendString(Save_Data.GPS_Buffer);
-
- for (i = 0 ; i <= 6 ; i++)
- {
- if (i == 0)
- {
- if ((subString = strstr(Save_Data.GPS_Buffer, ",")) == NULL)
- errorLog(1); //解析錯(cuò)誤
- }
- else
- {
- subString++;
- if ((subStringNext = strstr(subString, ",")) != NULL)
- {
- char usefullBuffer[2];
- switch(i)
- {
- case 1:memcpy(Save_Data.UTCTime, subString, subStringNext - subString);break; //獲取UTC時(shí)間
- case 2:memcpy(usefullBuffer, subString, subStringNext - subString);break; //獲取UTC時(shí)間
- case 3:memcpy(Save_Data.latitude, subString, subStringNext - subString);break; //獲取緯度信息
- case 4:memcpy(Save_Data.N_S, subString, subStringNext - subString);break; //獲取N/S
- case 5:memcpy(Save_Data.longitude, subString, subStringNext - subString);break; //獲取經(jīng)度信息
- case 6:memcpy(Save_Data.E_W, subString, subStringNext - subString);break; //獲取E/W
- default:break;
- }
- subString = subStringNext;
- Save_Data.isParseData = true;
- if(usefullBuffer[0] == 'A')
- Save_Data.isUsefull = true;
- else if(usefullBuffer[0] == 'V')
- Save_Data.isUsefull = false;
- }
- else
- {
- errorLog(2); //解析錯(cuò)誤
- }
- }
- }
- }
- }
- void printGpsBuffer()
- {
- if (Save_Data.isParseData)
- {
- Save_Data.isParseData = false;
-
- SendString("Save_Data.UTCTime = ");
- SendString(Save_Data.UTCTime);
- SendString("\r\n");
- if(Save_Data.isUsefull)
- {
- Save_Data.isUsefull = false;
- SendString("Save_Data.latitude = ");
- SendString(Save_Data.latitude);
- SendString("\r\n");
- SendString("Save_Data.N_S = ");
- SendString(Save_Data.N_S);
- SendString("\r\n");
- SendString("Save_Data.longitude = ");
- SendString(Save_Data.longitude);
- SendString("\r\n");
- SendString("Save_Data.E_W = ");
- ……………………
- …………限于本文篇幅 余下代碼請(qǐng)從51黑下載附件…………
復(fù)制代碼
所有資料51hei提供下載:
51單片機(jī)例程_A6_A7.zip
(638.21 KB, 下載次數(shù): 49)
2018-8-11 11:04 上傳
點(diǎn)擊文件名下載附件
|
|