基于51單片機的GPS系統,顯示經緯度,由于gps是無法仿真的硬件 所以只能用串口來模擬
仿真原理圖如下(proteus仿真工程文件可到本帖附件中下載)
51hei.gif (65.06 KB, 下載次數: 69)
下載附件
2022-4-27 19:55 上傳
單片機源程序如下:
- /*************************************
- GPS解碼顯示程序,
- ***************************************/
- #include<reg52.h>
- #include "1602.h"
- //#include"math.h"
- //#include <stdlib.h>
- sbit GPS_SPD=P1^1; //GPS模塊速率設置
- sbit KEY1=P1^0; //顯示內容分屏切換,(T0,T1引腳 的第二功能為計數器。)
- char code TIME_AREA= 8; //時區
- /***************************************
- 這是做的部分更改
- ************************************/
- unsigned long maxspeed,b;
- unsigned int count=0;
- unsigned int a[5];
- unsigned char hspeed[5];
- unsigned int dot_count; //小數點計數器
- //unsigned char x;
- //GPS數據存儲數組
- unsigned char JD[10]; //經度
- unsigned char JD_a; //經度方向
- unsigned char WD[9]; //緯度
- unsigned char WD_a; //緯度方向
- unsigned char date[6]; //日期
- unsigned char time[6]; //時間
- unsigned char speed[5]={'0','0','0','.','0'}; //速度
- unsigned char high[6]; //高度
- unsigned char angle[5]={'0','0','0','0','0'}; //方位角
- unsigned char use_sat[2]; //使用的衛星數
- unsigned char total_sat[2]; //天空中總衛星數
- unsigned char lock; //定位狀態
- //串口中斷需要的變量
- unsigned char seg_count; //逗號計數器
- unsigned char byte_count; //位數計數器
- unsigned char cmd_number; //命令類型
- unsigned char mode; //0:結束模式,1:命令模式,2:數據模式
- unsigned char buf_full; //1:整句接收完成,相應數據有效。0:緩存數據無效。
- unsigned char cmd[5]; //命令類型存儲數組
- //顯示需要的變量
- unsigned int dsp_count; //刷新次數計數器
- //unsigned char time_count;
- bit page;
- void sys_init(void);
- bit chk_key(void);
- main()
- {
- unsigned char i;
- char Bhour;
- sys_init();
- lock=1;
- use_sat[0]='0';
- use_sat[1]='0';
- total_sat[0]='0';
- total_sat[1]='0';
- while(1){
- if(buf_full==0) //無GPS信號時
- {
- dsp_count++;
- if(dsp_count>=65000){
- LCD_cls(); //清屏
- LCD_write_string(0,0,"No GPS connect..");
- LCD_write_string(0,1,"Please Check..");
- while(buf_full==0);
- LCD_cls();
- dsp_count=0;
- }
- }
- else{ //有GPS信號時
- /*************************************
- 最大速度處理
- *************************************/
- dot_count=0;
- b=0;
- for(i=0;i<5;i++)
- {
- if(speed[i]!='.')
- dot_count++;
- else
- break ;
- }
- switch(dot_count)
- {
-
- case 1:
- b=((speed[0]-'0')*10+(speed[2]-'0'))*1.852;
- break;
- case 2:
- b=((speed[0]-'0')*100+(speed[1]-'0')*10+(speed[4]-'0'))*1.852;
- break;
- case 3:
- b=((speed[0]-'0')*1000+(speed[1]-'0')*100+(speed[2]-'0')*10+(speed[4]-'0'))*1.852;
- break;
-
- }
- if(b>maxspeed)
- {
- maxspeed=b;
- }
- /*************************************
- 最大速度處理
- *************************************/
-
- if(chk_key()){ //檢測到按鍵切換顯示
- page=!page;
- LCD_cls();
- }
- if(!page){ //頁面1
-
- if(buf_full|0x01){ //GGA語句
- if(lock==0){ //如果未定位
- LCD_write_string(0,0,"*---.--.---- ");
- LCD_write_string(0,1,"* --.--.---- ");
- }else{ //如果已定位
-
- LCD_write_char(0,0,JD_a); //顯示經度
- for(i=0;i<3;i++)
- {
- LCD_write_char(i+1,0,JD[i]);
- }
- LCD_write_char(4,0,'.');
- for(i=3;i<10;i++)
- {
- LCD_write_char(i+2,0,JD[i]);
- }
- LCD_write_char(0,1,WD_a); //顯示緯度
- LCD_write_char(1,1,' ');
- for(i=0;i<2;i++)
- {
- LCD_write_char(i+2,1,WD[i]);
- }
- LCD_write_char(4,1,'.');
- for(i=2;i<9;i++)
- {
- LCD_write_char(i+3,1,WD[i]);
- }
-
- }
- LCD_write_char(14,1,use_sat[0]); //顯示接收衛星數
- LCD_write_char(15,1,use_sat[1]);
- buf_full&=~0x01;
- dsp_count=0;
- }
- if(buf_full|0x02){ //GSV語句
- LCD_write_char(14,1,total_sat[0]);
- LCD_write_char(15,1,total_sat[1]);
- buf_full&=~0x02;
- dsp_count=0;
- }
- if(buf_full|0x04){
- if(lock==0){ //如果未定位
- LCD_write_string(0,0,"*---.--.---- ");
- LCD_write_string(0,1,"* --.--.---- ");
- }else{ //如果已定位
- LCD_write_char(0,0,JD_a); //顯示經度
- for(i=0;i<3;i++)
- {
- LCD_write_char(i+1,0,JD[i]);
- }
- LCD_write_char(4,0,'.');
- for(i=3;i<10;i++)
- {
- LCD_write_char(i+2,0,JD[i]);
- }
- LCD_write_char(0,1,WD_a); //顯示緯度
-
- LCD_write_char(1,1,' ');
- for(i=0;i<2;i++)
- {
- LCD_write_char(i+2,1,WD[i]);
- }
- LCD_write_char(4,1,'.');
- for(i=2;i<9;i++)
- {
- LCD_write_char(i+3,1,WD[i]);
- }
- }
- LCD_write_char(14,0,use_sat[0]); //顯示接收衛星數
- LCD_write_char(15,0,use_sat[1]);
- buf_full&=~0x04;
- dsp_count=0;
- }
- }
- else{ //頁面2
- if(buf_full|0x01){ //GGA語句
- buf_full&=~0x01;
- dsp_count=0;
- }
- if(buf_full|0x02){
- buf_full&=~0x02;
- dsp_count=0;
- }
- if(buf_full|0x04){ //RMC語句
- Bhour=((time[0]-0x30)*10+time[1]-0x30)+TIME_AREA;
- if(Bhour>=24){
- Bhour-=24;
- }else if(Bhour<0){
- Bhour+=24;
- }
- LCD_write_char(0,1,date[4]);
- LCD_write_char(1,1,date[5]);
- LCD_write_char(2,1,date[2]);
- LCD_write_char(3,1,date[3]);
- LCD_write_char(4,1,date[0]);
- LCD_write_char(5,1,date[1]);
- LCD_write_char(8,1,Bhour/10+0x30);
- LCD_write_char(9,1,Bhour%10+0x30);
- LCD_write_char(10,1,':');
- LCD_write_char(11,1,time[2]);
- LCD_write_char(12,1,time[3]);
- LCD_write_char(13,1,':');
- LCD_write_char(14,1,time[4]);
- LCD_write_char(15,1,time[5]);
- LCD_write_string(5,0,"knot A");
- if(lock=='0'){ //如果未定位
- LCD_write_string(0,0,"---.-");
- LCD_write_string(11,0,"---.-");
- }else{ //已經定位,在此處做的改動。
- /*******************************************************************************/
-
-
- if(count<10)
- {
-
- for(i=0;i<5;i++)
- {
- LCD_write_char(i,0,speed[i]);//knot顯示
- }
- count++;
- }
- else
- {
- if(count>15)
- {
- count=0;
- }
- hspeed[0]=maxspeed/1000+0x30; //把小數轉成字符數組
- hspeed[1]=(maxspeed/100)%10+0x30;
- hspeed[2]=(maxspeed/10)%10+0x30;
- hspeed[3]='.';
- hspeed[4]= maxspeed%10+0x30;
-
- count++;
- LCD_write_string(5,0,"Km/h A");
- LCD_write_char(0,0,hspeed[0]);
- LCD_write_char(1,0,hspeed[1]);
- LCD_write_char(2,0,hspeed[2]);
- LCD_write_char(3,0,hspeed[3]);
- LCD_write_char(4,0,hspeed[4]); //最大速度顯 */
- }
-
- /*******************************************************************************/
- for(i=0;i<5;i++){
- LCD_write_char(11+i,0,angle[i]);
- }
- }
- buf_full&=~0x04;
- dsp_count=0;
- }
- }
- }
- }
- }
- bit chk_key(void)
- {
- if(!KEY1){
- delayms(10);
- if(!KEY1){
- while(!KEY1);
- delayms(10);
- return(1);
- }
- }
- LCD_cls(); //清屏
- return(0);
- }
- //系統初始化
- void sys_init() {
- unsigned char i;
- SCON = 0x50; /* SCON: mode 1, 8-bit UART, enable rcvr */
- TMOD = 0x21; /* TMOD: timer 1, mode 2, 8-bit reload */
- if(GPS_SPD){
- TH1 = 0xfa; /* TH1: reload value for 9600 baud @ 11.059MHz */
- }else{
- TH1 = 0xfd; /* TH1: reload value for 4800 baud @ 11.059MHz */
- }
- TR1 = 1; /* TR1: timer 1 run */
- LCD_init(8); //初始化LCD
- LCD_write_string(0,0," GPS SIRF II 2 ");
- LCD_write_string(0,1," 11-11-23 1342 ");
-
- for(i=1;i<4;i++){
- delayms(250);
- }
- //LCD_cls();
- IE=0x90; //開總中斷、串口中斷
- }
- //串口接收中斷
- void uart(void) interrupt 4
- {
- unsigned char tmp;
- if(RI){
- tmp=SBUF;
- switch(tmp){
- case '
- cmd_number=0; //命令類型清空
- mode=1; //接收命令模式
- byte_count=0; //接收位數清空
- break;
- case ',':
- seg_count++; //逗號計數加1
- byte_count=0;
- break;
- case '*':
- switch(cmd_number){
- case 1:
- buf_full|=0x01;
- break;
- case 2:
- buf_full|=0x02;
- break;
- case 3:
- buf_full|=0x04;
- break;
- }
- mode=0;
- break;
- default:
- if(mode==1){
- //命令種類判斷
- cmd[byte_count]=tmp; //接收字符放入類型緩存
- if(byte_count>=4){ //如果類型數據接收完畢,判斷類型
- if(cmd[0]=='G'){
- if(cmd[1]=='P'){
- if(cmd[2]=='G'){
- if(cmd[3]=='G'){
- if(cmd[4]=='A'){
- cmd_number=1;
- mode=2;
- seg_count=0;
- byte_count=0;
- }
- }
- else if(cmd[3]=='S'){
- if(cmd[4]=='V'){
- cmd_number=2;
- mode=2;
- seg_count=0;
- byte_count=0;
- }
- }
- }
- else if(cmd[2]=='R'){
- if(cmd[3]=='M'){
- if(cmd[4]=='C'){
- cmd_number=3;
- mode=2;
- seg_count=0;
- byte_count=0;
- }
- }
- }
- }
- }
- }
- }
- else if(mode==2){
- //接收數據處理
- switch (cmd_number){
- case 1: //類型1數據接收。GPGGA
- switch(seg_count){
- case 2: //緯度處理
- if(byte_count<9){
- WD[byte_count]=tmp;
- }
- break;
- case 3: //緯度方向處理
- if(byte_count<1){
- WD_a=tmp;
- }
- break;
- case 4: //經度處理
- if(byte_count<10){
- JD[byte_count]=tmp;
- }
- break;
- case 5: //經度方向處理
- if(byte_count<1){
- JD_a=tmp;
- }
- break;
- case 6: //定位判斷
- if(byte_count<1){
- lock=tmp;
- }
- break;
- case 7: //定位使用的衛星數
- if(byte_count<2){
- use_sat[byte_count]=tmp;
- }
- break;
- case 9: //高度處理
- if(byte_count<6){
- high[byte_count]=tmp;
- }
- break;
- }
- break;
- case 2: //類型2數據接收。GPGSV
- switch(seg_count){
- case 3: //天空中的衛星總數
- if(byte_count<2){
- total_sat[byte_count]=tmp;
- }
- break;
- }
- break;
- case 3: //類型3數據接收。GPRMC
- switch(seg_count){
- case 1: //時間處理
- if(byte_count<6){
- time[byte_count]=tmp;
- }
- break;
- case 2: //定位判斷
- if(byte_count<1){
- if (tmp=='A') {lock=1;}
- else{
- lock=0;}
- }
- break;
- case 3: //緯度處理
- if(byte_count<9){
- WD[byte_count]=tmp;
- }
- break;
- case 4: //緯度方向處理
- if(byte_count<1){
- WD_a=tmp;
- }
- break;
- case 5: //經度處理
- if(byte_count<10){
- JD[byte_count]=tmp;
- }
- break;
- case 6: //經度方向處理
- if(byte_count<1){
- JD_a=tmp;
- }
- break;
- case 7: //速度處理
- if(byte_count<5){
- speed[byte_count]=tmp;
- }
-
- break;
- case 8: //方位角處理
- if(byte_count<5){
- angle[byte_count]=tmp;
- }
- break;
- case 9: //方位角處理
- if(byte_count<6){
- date[byte_count]=tmp;
- }
- break;
- }
- break;
- }
- }
- byte_count++; //接收數位加1
- break;
- }
- }
- RI=0;
- }
復制代碼 由于gps是無法仿真的硬件 所以只能用串口來模擬
Keil代碼與Proteus仿真下載:
基于51單片機的GPS系統.zip
(102.6 KB, 下載次數: 175)
2022-4-27 16:49 上傳
點擊文件名下載附件
下載積分: 黑幣 -5
|