|
- /****************控制卡程序如果接手動接三根線和地****************/
- //#include<reg52.h>
- #include <STC12C5A60S2.H>
- #include<stdlib.h>
- #include <intrins.h>
- #include <stdio.h>
- #include <string.h>
- #define S2RI 0x01 //串口2接收中斷請求標(biāo)志位
- #define S2TI 0x02 //串口2發(fā)送中斷請求標(biāo)志位
- #define FOSC 11059200L //System frequency
- #define BAUD 9600 //UART baudrate
- #define uchar unsigned char
- #define uint unsigned int
- #define RELOAD_COUNT 0xfd //256-(11059200/9600/32/12+0.5)波特率設(shè)置,括號內(nèi)的數(shù)取整數(shù)(晶振頻率除波特率除32除12加取整數(shù))
- void Delay_ms(unsigned int n);
- void send_UART_one(unsigned char i);
- /****************定義IO口變量****************/
- sbit GREEN = P0^0;
- sbit RED = P0^1;
- sbit TING = P0^2;
- sbit KAI = P0^3;
- sbit GUAN = P0^4;
- sbit LED1 = P0^5;
- sbit LED2 = P0^6;
- sbit CHUN = P2^5;
- sbit XIA = P2^4;
- sbit QIU = P2^3;
- sbit DONG = P2^2;
- sbit QITA = P2^1;
- sbit KAIGUAN = P2^0;
- sbit YK_LUO = P1^0;
- sbit YK_SHENG = P1^1;
- sbit YK_TING= P1^5;
- sbit EN_TX_RX= P1^4;
- sbit WDT_WDI= P3^2;
- void IO_Init()
- {
- LED1 = 0;
- KAI = 1;
- GUAN=0;
- //TING=0;
- }
- /********************************/
- //sbit sf=P3^3;//485控制
- uchar a,week,yhy,sj,yuefen,shi,fen,miao,ri,nian; //添加
- uint yue,xiaoshi;
- uchar flag=1;
- void Show_Float(float fla);
- void xingqi();
- char xdata rev_buf[80]; //接收緩存
- uchar xdata rev_start = 0; //接收開始標(biāo)志
- uchar xdata rev_stop = 0; //接收停止標(biāo)志
- uchar xdata gps_flag = 0; //GPS處理標(biāo)志
- uchar xdata change_page = 0; //換頁顯示標(biāo)志
- uchar xdata num = 0; //
- void send_UART_two(unsigned char i);//串口二發(fā)送
- typedef struct{
- int year; //年
- int month; //月
- int day; //日
- int hour; //時
- int minute; //分
- int second; //秒
- }DATE_TIME;
- typedef xdata struct{
- double latitude; //經(jīng)度
- double longitude; //緯度
- int latitude_Degree; //度
- int latitude_Cent; //分
- int latitude_Second; //秒
- int longitude_Degree; //度
- int longitude_Cent; //分
- int longitude_Second; //秒
- float speed; //速度
- float direction; //航向
- float height_ground; //水平面高度
- float height_sea; //海拔高度
- int satellite;
- uchar NS;
- uchar EW;
- DATE_TIME D;
- }GPS_INFO;
- int GPS_RMC_Parse(char *line,GPS_INFO *GPS);
- #define DelayNOP(); {_nop_();_nop_();_nop_();_nop_();};
- static uchar GetComma(uchar num,char* str);
- static double Get_Double_Number(char *s);
- static float Get_Float_Number(char *s);
- static void UTC2BTC(DATE_TIME *GPS);
- //====================================================================//
- // 語法格式:int GPS_RMC_Parse(char *line, GPS_INFO *GPS)
- // 實現(xiàn)功能:把gps模塊的GPRMC信息解析為可識別的數(shù)據(jù)
- // 參 數(shù):存放原始信息字符數(shù)組、存儲可識別數(shù)據(jù)的結(jié)構(gòu)體
- // 返 回 值:
- // 1: 解析GPRMC完畢
- // 0: 沒有進行解析,或數(shù)據(jù)無效
- //====================================================================//
- int GPS_RMC_Parse(char *line,GPS_INFO *GPS)
- {
- uchar ch, status, tmp;
- float lati_cent_tmp, lati_second_tmp;
- float long_cent_tmp, long_second_tmp;
- float speed_tmp;
- char *buf = line;
- ch = buf[5];
- status = buf[GetComma(2, buf)];
- if (ch == 'C') //如果第五個字符是C,($GPRMC)
- {
- if (status == 'A') //如果數(shù)據(jù)有效,則分析
- {
- GPS -> NS = buf[GetComma(4, buf)];
- GPS -> EW = buf[GetComma(6, buf)];
- GPS->latitude = Get_Double_Number(&buf[GetComma(3, buf)]);
- GPS->longitude = Get_Double_Number(&buf[GetComma( 5, buf)]);
- GPS->latitude_Degree = (int)GPS->latitude / 100; //分離緯度
- lati_cent_tmp = (GPS->latitude - GPS->latitude_Degree * 100);
- GPS->latitude_Cent = (int)lati_cent_tmp;
- lati_second_tmp = (lati_cent_tmp - GPS->latitude_Cent) * 60;
- GPS->latitude_Second = (int)lati_second_tmp;
- GPS->longitude_Degree = (int)GPS->longitude / 100; //分離經(jīng)度
- long_cent_tmp = (GPS->longitude - GPS->longitude_Degree * 100);
- GPS->longitude_Cent = (int)long_cent_tmp;
- long_second_tmp = (long_cent_tmp - GPS->longitude_Cent) * 60;
- GPS->longitude_Second = (int)long_second_tmp;
- speed_tmp = Get_Float_Number(&buf[GetComma(7, buf)]); //速度(單位:海里/時)
- GPS->speed = speed_tmp * 1.85; //1海里=1.85公里
- GPS->direction = Get_Float_Number(&buf[GetComma(8, buf)]); //角度
- GPS->D.hour = (buf[7] - '0') * 10 + (buf[8] - '0'); //時間
- GPS->D.minute = (buf[9] - '0') * 10 + (buf[10] - '0');
- GPS->D.second = (buf[11] - '0') * 10 + (buf[12] - '0');
- tmp = GetComma(9, buf);
- GPS->D.day = (buf[tmp + 0] - '0') * 10 + (buf[tmp + 1] - '0'); //日期
- GPS->D.month = (buf[tmp + 2] - '0') * 10 + (buf[tmp + 3] - '0');
- GPS->D.year = (buf[tmp + 4] - '0') * 10 + (buf[tmp + 5] - '0');//+2000;
- UTC2BTC(&GPS->D);
- return 1;
- }
- }
- return 0;
- }
- //====================================================================//
- // 語法格式: static float Str_To_Float(char *buf)
- // 實現(xiàn)功能: 把一個字符串轉(zhuǎn)化成浮點數(shù)
- // 參 數(shù):字符串
- // 返 回 值:轉(zhuǎn)化后單精度值
- //====================================================================//
- static float Str_To_Float(char *buf)
- {
- float rev = 0;
- float dat;
- int integer = 1;
- char *str = buf;
- int i;
- while(*str != '\0')
- {
- switch(*str)
- {
- case '0':
- dat = 0;
- break;
- case '1':
- dat = 1;
- break;
- case '2':
- dat = 2;
- break;
- case '3':
- dat = 3;
- break;
- case '4':
- dat = 4;
- break;
- case '5':
- dat = 5;
- break;
- case '6':
- dat = 6;
- break;
- case '7':
- dat = 7;
- break;
- case '8':
- dat = 8;
- break;
- case '9':
- dat = 9;
- break;
- case '.':
- dat = '.';
- break;
- }
- if(dat == '.')
- {
- integer = 0;
- i = 1;
- str ++;
- continue;
- }
- if( integer == 1 )
- {
- rev = rev * 10 + dat;
- }
- else
- {
- rev = rev + dat / (10 * i);
- i = i * 10 ;
- }
- str ++;
- }
- return rev;
- }
-
- //====================================================================//
- // 語法格式: static float Get_Float_Number(char *s)
- // 實現(xiàn)功能: 把給定字符串第一個逗號之前的字符轉(zhuǎn)化成單精度型
- // 參 數(shù):字符串
- // 返 回 值:轉(zhuǎn)化后單精度值
- //====================================================================//
- static float Get_Float_Number(char *s)
- {
- char buf[10];
- uchar i;
- float rev;
- i=GetComma(1, s);
- i = i - 1;
- strncpy(buf, s, i);
- buf[i] = 0;
- rev=Str_To_Float(buf);
- return rev;
- }
- //====================================================================//
- // 語法格式: static double Str_To_Double(char *buf)
- // 實現(xiàn)功能: 把一個字符串轉(zhuǎn)化成浮點數(shù)
- // 參 數(shù):字符串
- // 返 回 值:轉(zhuǎn)化后雙精度值
- //====================================================================//
- static double Str_To_Double(char *buf)
- {
- double rev = 0;
- double dat;
- int integer = 1;
- char *str = buf;
- int i;
- while(*str != '\0')
- {
- switch(*str)
- {
- case '0':
- dat = 0;
- break;
- case '1':
- dat = 1;
- break;
- case '2':
- dat = 2;
- break;
- case '3':
- dat = 3;
- break;
- case '4':
- dat = 4;
- break;
- case '5':
- dat = 5;
- break;
- case '6':
- dat = 6;
- break;
- case '7':
- dat = 7;
- break;
- case '8':
- dat = 8;
- break;
- case '9':
- dat = 9;
- break;
- case '.':
- dat = '.';
- break;
- }
- if(dat == '.')
- {
- integer = 0;
- i = 1;
- str ++;
- continue;
- }
- if( integer == 1 )
- {
- rev = rev * 10 + dat;
- }
- else
- {
- rev = rev + dat / (10 * i);
- i = i * 10 ;
- }
- str ++;
- }
- return rev;
- }
-
- //====================================================================//
- // 語法格式: static double Get_Double_Number(char *s)
- // 實現(xiàn)功能:把給定字符串第一個逗號之前的字符轉(zhuǎn)化成雙精度型
- // 參 數(shù):字符串
- // 返 回 值:轉(zhuǎn)化后雙精度值
- //====================================================================//
- static double Get_Double_Number(char *s)
- {
- char buf[10];
- uchar i;
- double rev;
- i=GetComma(1, s);
- i = i - 1;
- strncpy(buf, s, i);
- buf[i] = 0;
- rev=Str_To_Double(buf);
- return rev;
- }
- //====================================================================//
- // 語法格式:static uchar GetComma(uchar num,char *str)
- // 實現(xiàn)功能:計算字符串中各個逗號的位置
- // 參 數(shù):查找的逗號是第幾個的個數(shù),需要查找的字符串
- // 返 回 值:0
- //====================================================================//
- static uchar GetComma(uchar num,char *str)
- {
- uchar i,j = 0;
- int len=strlen(str);
- for(i = 0;i < len;i ++)
- {
- if(str[i] == ',')
- j++;
- if(j == num)
- return i + 1;
- }
- return 0;
- }
- //====================================================================//
- // 語法格式:void UTC2BTC(DATE_TIME *GPS)
- // 實現(xiàn)功能:轉(zhuǎn)化時間為北京時區(qū)的時間
- // 參 數(shù):存放時間的結(jié)構(gòu)體
- // 返 回 值:無
- //====================================================================//
- static void UTC2BTC(DATE_TIME *GPS)
- {
- GPS->second ++;
- fen=((GPS->minute/10)<<4)+(GPS->minute%10); //添加
- miao=((GPS->second/10)<<4)+(GPS->second%10); //添加
- if(GPS->second > 59)
- {
- GPS->second = 0;
- GPS->minute ++;
- if(GPS->minute > 59)
- {
- GPS->minute = 0;
- GPS->hour ++;
- }
- }
- GPS->hour = GPS->hour + 8;
- xiaoshi=GPS->hour;
- shi=((GPS->hour/10)<<4)+(GPS->hour%10); //添加
- ri=((GPS->day/10)<<4)+(GPS->day%10); //添加
- if(GPS->hour > 23)
- {
- GPS->hour -= 24;
- GPS->day += 1;
- if(GPS->month == 2 ||GPS->month == 4 ||GPS->month == 6 ||GPS->month == 9 ||GPS->month == 11 )
- {
- if(GPS->day > 30)
- {
- GPS->day = 1;
- GPS->month++;
- }
- }
- else
- {
- if(GPS->day > 31)
- {
- GPS->day = 1;
- GPS->month ++;
- }
- }
- if(GPS->year % 4 == 0 )
- {
- if(GPS->day > 29 && GPS->month == 2)
- {
- GPS->day = 1;
- GPS->month ++;
- }
- }
- else
- {
- if(GPS->day > 28 &&GPS->month == 2)
- {
- GPS->day = 1;
- GPS->month ++;
- }
- }
- if(GPS->month > 12)
- {
- GPS->month -= 12;
- GPS->year ++;
- }
- }
- yue=GPS->month;
- yuefen=((GPS->month/10)<<4)+(GPS->month%10); //添加
- nian=((GPS->year/10)<<4)+(GPS->year%10); //添加
- // sj=0x14;//世紀(jì)
- if(GPS->month==0x01)
- {
- yue=0x0d;
- GPS->year--;
- }
- if(GPS->month==0x02)
- {
- yue=0x0e;
- GPS->year--;
- }
- // week=(70+GPS->year+(GPS->year/4)+((26*(yue+1))/10)+GPS->day+(sj/4)-1-(2*sj))%7;
- xingqi();
- week=(GPS->year+(GPS->year/4)+yhy+GPS->day+6)%7;
- if(GPS->month==0x01)
- {
- GPS->year++;
- }
- if(GPS->month==0x02)
- {
- GPS->year++;
- }
- if(week==0x00)
- {
- week=0x07;
- }
- send_UART_one(0xeb);
- send_UART_one(miao); //修改后
- send_UART_one(fen); //修改后
- send_UART_one(shi); //修改后
- send_UART_one(ri); //修改后
- send_UART_one(yuefen); //修改后
- send_UART_one(week);
- send_UART_one(nian); //修改后
- send_UART_one(0xec);
- }
- //EB 49 43 09 07 11 03 18 EC
- GPS_INFO GPS; //GPS信息結(jié)構(gòu)體
- void xingqi()
- {
- yhy=(26*(yue+1))/10;
- }
- /*void send_UART_two(unsigned char i)
- {
- unsigned char temp = 0;
- IE2=0x00; //關(guān)串口2中斷,es2=0
- S2CON=S2CON & 0xFD; //B'11111101,清零串口2發(fā)送完成中斷請求標(biāo)志
- S2BUF=i;
- do
- {
- temp=S2CON;
- temp=temp & 0x02;
- }while(temp==0);
- S2CON=S2CON & 0xFD; //B'11111101,清零串口2發(fā)送完成中斷請求標(biāo)志
- IE2=0x01; //允許串口2中斷,ES2=1
- } */
- void send_UART_one(unsigned char i) //外加+++
- {
- unsigned char temp = 0;
- ES=0x00; //關(guān)串口1中斷,es=0
- TI=0; //B'11111101,清零串口1發(fā)送完成中斷請求標(biāo)志
- SBUF=i;
- while(TI==0);
- TI=0; //B'11111101,清零串口1發(fā)送完成中斷請求標(biāo)志
- ES=0x01; //允許串口1中斷,ES=1
- }
- /****************************************
- 串口初始化
- /****************************************/
- void Uart_Init(void)
- {
- TMOD=0x21;
-
- TH0=0x3c;
- TL0=0xb0;
- ET0=1;//定時/計數(shù)器T0中斷允許位
- TR0=1;//啟動定時/計數(shù)器工
- EX1=0;//開外部中斷1 判斷程序運行需關(guān)閉外部中斷
- IT1=1;//跳變沿方式
-
- SCON=0x50; //0101,0000 8位可變波特率,無奇偶校驗位
- S2CON=0x50; //0101,0000 8位可變波特率,無奇偶校驗位,允許接收
- BRT=RELOAD_COUNT; //設(shè)置獨立波特率發(fā)生器的重載初值
- AUXR=0x11; // T0x12,T1x12,UART_M0x6,BRTR,S2SMOD,BRTx12,EXTRAM,S1BRS
- ES=1; //允許串口中斷
- IE2=0x01; //允許串口2中斷,ES2=1
-
- EA=1;//CPU中斷總允許
- //sf=1;
- }
- /*************道閘關(guān)閉函數(shù)******************/
- void hong_led()
- {
- if((RED==1)&&(GREEN==0)&&(flag==1))
- {
-
- KAI=0;
- GUAN=1;
- TING=1;
- Delay_ms(1000);
- WDT_WDI=~WDT_WDI;
- GUAN=0;
- KAI=0;
- flag=0;
- //TING=1;
- }
復(fù)制代碼
|
|