|
我的程序也是参考、抄袭论坛上的程序基础上写的,现在有几个问题,请高手帮忙改改!
1.日期显示错误,2000/00/00,时间可以正常显示,
2.速度显示为海里,努力多次没有转换成公里,汗!
3.方位角量示正确,但想能够实现方位提示:N NW W WS S SE E EN,
我在邮购部买的370C 发送的数据如下:
$GPGGA,111634.000,29**.**32,N,111**.**08,E,1,05,1.7,22.0,M,,,18.8,0000*22
$GPGSA,A,3,02,04,10,12,17,,,,,,,,3.5,1.7,3.0*33
$GPGSV,3,1,11,04,61,338,38,17,57,084,31,05,48,277,,10,48,197,33*78
$GPGSV,3,2,11,02,38,281,45,07,32,316,,28,19,164,29,12,16,318,41*78
$GPGSV,3,3,11,23,15,075,26,20,05,040,23,09,02,242,*47
$GPRMC,111634.000,A,29**.**32,N,111**.**08,E,0.00,,090910,,*1D
经纬度处理过!请原谅!
原程序如下:
1.main.c
#include <ioM16v.h>
#include <macros.h>
#include "delay.h"
#include "1602.h"
#include "uart.h"
#include "GPS.h"
/*******************系统初始化********************/
void INIT_devices(void)
{
CLI(); //禁止所有中断
MCUCR = 0x00;
MCUCSR = 0x80;//禁止JTAG
GICR = 0x00;
LCD_init();//1602初始
LCD_clear();//清屏
Uart_init(9600);//串口初始
Logo_show();//显示欢迎界面
SEI();//开全局中断
}
/*******************显示欢迎界面********************/
void Logo_show(void)//显示欢迎界面
{
unsigned int show_num;
LCD_clear();//1602清屏
LCD_write_string(4,0,"Welcome!");
LCD_write_string(0,1,"V:2010-09-10.3");
delay_nms(1200);
LCD_clear();//1602清屏
LCD_write_string(0,0,"Initializing...");
for(show_num=0;show_num<16;show_num++)
{
LCD_write_char(show_num,1,0xFF);
delay_nms(100);
}
LCD_clear();//1602清屏
}
/*******************主函数********************/
void main(void)
{
INIT_devices();
while(1)
{
Gps_display();
}
}
GPS.h
#define TIME_AREA 8 //时区
//GPS数据存储数组
unsigned char speed_km[7]; //速度km/h
unsigned char JD[11]; //经度
unsigned char JD_a; //经度方向
unsigned char WD[10]; //纬度
unsigned char WD_a; //纬度方向
unsigned char time[6]={"000000"}; //时间
unsigned char date[6]={"000000"}; //日期
unsigned char speed[7]; //速度 1海里=1.852M/H
unsigned char high[6]; //高度
unsigned char DD[3]; //二维三维定位标志
unsigned char angle[6]; //方位角
unsigned char use_sat[3]; //使用的卫星数
unsigned char total_sat[3]; //天空中总卫星数
unsigned char DW[3]; //定位标志 V未定位 A已定位
//串口中断需要的变量
unsigned char seg_count; //逗号计数器
unsigned char dot_count; //小数点计数器
unsigned char byte_count; //位数计数器
unsigned char cmd_number; //命令类型
unsigned char mode; //0:结束模式,1:命令模式,2:数据模式
volatile unsigned char buf_full;//:整句接收完成,相应数据有效。0:缓存数据无效。
unsigned char cmd[5]; //命令类型存储数组
//显示需要的变量
unsigned char dsp_count; //刷新次数计数器
unsigned char time_count;
volatile unsigned char high_num,a_num,s_num,d_num,t_num;
unsigned char i;
unsigned char Bhour=0,Bday=0,Bmonth=0;
unsigned int Byear=0;
unsigned char week(unsigned char year, unsigned char mon, unsigned char day);
void GPS_expan(void)//串口接收中断
{
unsigned char tmp;
while(!(UCSRA & (1<<RXC)));
tmp=UDR;
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;
case 4:
buf_full|=0x08;
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;
high_num=0;
}
}
else if(cmd[3]=='S'){
if(cmd[4]=='V'){
cmd_number=2;
mode=2;
seg_count=0;
byte_count=0;
}
if(cmd[4]=='A'){
cmd_number=3;
mode=2;
seg_count=0;
byte_count=0;
}
}
}
else if(cmd[2]=='R'){
if(cmd[3]=='M'){
if(cmd[4]=='C'){
cmd_number=4;
mode=2;
seg_count=0;
byte_count=0;
a_num=0;
s_num=0;
d_num=0;
}
}
}
}
}
}
}
else if(mode==2){
//接收数据处理
switch (cmd_number){
/******************GPGGA*****************/
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;
high_num++;
}
break;
}
break;
/******************GPGSV*****************/
case 2: //类型2数据接收。GPGSV
switch(seg_count){
case 3: //天空中的卫星总数
if(byte_count<2){
total_sat[byte_count]=tmp;
}
break;
}
break;
/******************GPGSA*****************/
case 3: //类型数据接收。GPGSA
switch(seg_count){
case 2:
if(byte_count<1){ //二维三维定位标志处理
DD[byte_count]=tmp;
}
break;
}
break;
/******************GPRMC*****************/
case 4: //类型3数据接收。GPRMC
switch(seg_count){
case 1 : //时间
if(byte_count<6){
time[byte_count]=tmp;
}
break;
case 2 : //定位标志
if(byte_count<1){
DW[byte_count]=tmp;
}
break;
case 7: //速度处理,单位节,1节=1852M/H
if(byte_count<5){
speed[byte_count]=tmp;
//s_num++;
}
break;
case 8: //方位角处理
if(byte_count<5){
angle[byte_count]=tmp;
//a_num++;
}
break;
case 9: //日期处理
if(byte_count<6){
data[byte_count]=tmp;
//d_num++;
}
break;
}
break;
}
}
byte_count++; //接收数位加1
break;
}
}
void Gps_display()//GGA GSV GPZDA GPGSA GPVTG GPRMC
{
if(buf_full==0)//无GPS信号时
{
dsp_count++;
delay_nms(10);
if(dsp_count>=200){
LCD_clear();//清屏 */
LCD_write_string(0,0,"No GPS connect..");
while(buf_full==0);
LCD_clear();//清屏 */
dsp_count=0;
}
}
else
{ //有GPS信号时
if(buf_full&0x01)
{ //GGA语句
//LCD_write_string(0, 0,"E");
//LCD_write_string(0, 0,JD);
// LCD_write_string(0, 1,"N");
//LCD_write_string(0, 1,WD);//显示纬度
LCD_write_string(14, 0,use_sat);//显示接收卫星数
LCD_write_string(9,1,"H");
if(high_num<6) //高度显示
{
for(i=0;i<(6-high_num);i++)
{
LCD_write_string(10+i,1," ");
LCD_write_string(11+i, 1,high);
}
}
else
{
LCD_write_string(10, 1,high); //高度显示
}
buf_full&=~0x01;
dsp_count=0;
if(buf_full&0x02)
{ //GSV语句
//LCD_write_string(14,0,total_sat);//天空卫星总数
buf_full&=~0x02;
dsp_count=0;
}
if(buf_full&0x04)
{ //$GPGSA
/*LCD_write_string(96, 48,DD); //二维三维定位标志*/
buf_full&=~0x16;
dsp_count=0;
}
if(buf_full&0x08)
{ //GPRMC
/* if(Bhour!=((time[0]-0x30)*10+time[1]-0x30)+TIME_AREA){
Bhour=((time[0]-0x30)*10+time[1]-0x30)+TIME_AREA; //北京时间转换
Bday=(date[0]-0x30)*10+date[1]-0x30;
Bmonth=(date[2]-0x30)*10+date[3]-0x30;
Byear=(date[4]-0x30)*10+date[5]-0x30+2000;
if(Bhour>=24){ //如果小时数大于24
Bhour-=24; //小时数减24
Bday++; //日期数加1
switch(Bday){ //判断日期
case 29: //普通年的2月份
if((!((Byear%400==0)||((Byear%4==0)&&(Byear%100!=0)))&&(Bmonth==2))){
Bday=1;
Bmonth++;
}
break;
case 30: //如果是闰年的2月
if(((Byear%400==0)||((Byear%4==0)&&(Byear%100!=0)))&&(Bmonth==2)){
Bday=1;
Bmonth++;
}
break;
case 31:
if((Bmonth==4)||(Bmonth==6)||(Bmonth==9)||(Bmonth==11)){
Bday=1;
Bmonth++;
}
break;
case 32:
Bday=1;
Bmonth++;
if(Bmonth>=13){
Byear++;
Bmonth=1;
}
break;
}
}
}
LCD_write_string(0,0,"20");
LCD_write_char(2,0,(Byear%100)/10+0x30);
LCD_write_char(3,0,Byear%10+0x30);
LCD_write_string(4,0,"/");
LCD_write_char(5,0,Bmonth/10+0x30);
LCD_write_char(6,0,Bmonth%10+0x30);
LCD_write_string(7,0,"/");
LCD_write_char(8,0,Bday/10+0x30);
LCD_write_char(9,0,Bday%10+0x30);
if(Bhour/10!=0)
{
LCD_write_char(0,1,Bhour/10+0x30);
}
else
{
LCD_write_string(0,1,"0");
}
LCD_write_char(1,1,Bhour%10+0x30);
LCD_write_string(2,1,":");
LCD_write_char(3,1,time[2]);
LCD_write_char(4,1,time[3]);
LCD_write_string(5,1,":");
LCD_write_char(6,1,time[4]);
LCD_write_char(7,1,time[5]);*/
LCD_write_string(12,0,DW);//定位标志
LCD_write_string(0,1,"AG");
LCD_write_string(2,1,angle);//方位角显示
//LCD_write_string(0,0,"SP");
LCD_write_string(2,0,speed);//速度显示(海里)
buf_full&=~0x04;
dsp_count=0;
}
}
}
}
请高手帮忙改下,就算带带新手,好让更多人买阿莫的GPS 来玩,拜托了! |
|