单片机源程序如下:
- #include "sys.h"
- #include "delay.h"
- #include "usart.h"
- #include "led.h"
- #include "lcd.h"
- #include "usmart.h"
- #include "usart3.h"
- #include "key.h"
- #include "string.h"
- #include "gps.h"
- #include "ff.h"
- #include "exfuns.h"
- //ALIENTEK 探索者STM32F407开发板 扩展实验2
- //ATK-NEO-6M GPS模块测试实验 -库函数版本
- u8 USART1_TX_BUF[USART3_MAX_RECV_LEN]; //串口1,发送缓存区
- nmea_msg gpsx; //GPS信息
- __align(4) u8 dtbuf[50]; //打印缓存器
- const u8*fixmode_tbl[4]={"Fail","Fail"," 2D "," 3D "}; //fix mode字符串
- //显示GPS定位信息
- void Gps_Msg_Show(void)
- {
- float tp;
- POINT_COLOR=BLUE;
- tp=gpsx.longitude;
- sprintf((char *)dtbuf,"Longitude:%.7f %1c ",tp/=100000,gpsx.ewhemi); //得到经度字符串
- LCD_ShowString(30,130,200,16,16,dtbuf);
- tp=gpsx.latitude;
- sprintf((char *)dtbuf,"Latitude:%.7f %1c ",tp/=100000,gpsx.nshemi); //得到纬度字符串
- LCD_ShowString(30,150,200,16,16,dtbuf);
- tp=gpsx.altitude;
- sprintf((char *)dtbuf,"Altitude:%.1fm ",tp/=10); //得到高度字符串
- LCD_ShowString(30,170,200,16,16,dtbuf);
- tp=gpsx.speed;
- sprintf((char *)dtbuf,"Speed:%.3fkm/h ",tp/=1000); //得到速度字符串
- LCD_ShowString(30,190,200,16,16,dtbuf);
- if(gpsx.fixmode<=3) //定位状态
- {
- sprintf((char *)dtbuf,"Fix Mode:%s",fixmode_tbl[gpsx.fixmode]);
- LCD_ShowString(30,210,200,16,16,dtbuf);
- }
- sprintf((char *)dtbuf,"Valid satellite:%02d",gpsx.posslnum); //用于定位的卫星数
- LCD_ShowString(30,230,200,16,16,dtbuf);
- sprintf((char *)dtbuf,"Visible satellite:%02d",gpsx.svnum%100); //可见卫星数
- LCD_ShowString(30,250,200,16,16,dtbuf);
- sprintf((char *)dtbuf,"UTC Date:%04d/%02d/%02d ",gpsx.utc.year,gpsx.utc.month,gpsx.utc.date); //显示UTC日期
- //printf("year2:%drn",gpsx.utc.year);
- LCD_ShowString(30,270,200,16,16,dtbuf);
- sprintf((char *)dtbuf,"UTC Time:%02d:%02d:%02d ",gpsx.utc.hour,gpsx.utc.min,gpsx.utc.sec); //显示UTC时间
- LCD_ShowString(30,290,200,16,16,dtbuf);
- }
- int main(void)
- {
- u8 record=0;
- u16 i,rxlen,j=0;
- u16 r=0;
- u16 lenx;
- u8 key=0XFF;
- u8 upload=0;
- u8 temp[24];
- u32 total,free;
- NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);//设置系统中断优先级分组2
- delay_init(168); //初始化延时函数
- uart_init(115200); //初始化串口波特率为115200
- usart3_init(9600); //初始化串口3波特率为9600
- usmart_dev.init(84); //初始化USMART
- LED_Init(); //初始化LED
- KEY_Init(); //初始化按键
- LCD_Init(); //初始化LCD
- usmart_dev.init(72); //初始化USMART
- exfuns_init(); //为fatfs相关变量申请内存
- f_mount(fs[0],"0:",1); //挂载SD卡
- while(SD_Init()) //初始化SD卡失败
- {
- LCD_ShowString(60,150,240,16,16,"SD Init Error!");
- delay_ms(200);
- }
- //如果之前有记录,重新上电先算出之前记录的条数,非常重要
- f_open(file, "gps_log.gtxt", FA_READ );
- r=(*file).fsize/64;
- f_close(file); //关闭文件
- POINT_COLOR=RED;
- LCD_ShowString(30,20,200,16,16,"ALIENTEK STM32F4 ^_^");
- LCD_ShowString(30,40,200,16,16,"NE0-6M GPS TEST");
- LCD_ShowString(30,60,200,16,16,"ATOM@ALIENTEK");
- LCD_ShowString(30,80,200,16,16,"KEY0:Upload NMEA Data SW");
- LCD_ShowString(30,100,200,16,16,"NMEA Data Upload:OFF");
- if(Ublox_Cfg_Rate(1000,1)!=0) //设置定位信息更新速度为1000ms,顺便判断GPS模块是否在位.
- {
- LCD_ShowString(30,120,200,16,16,"NEO-6M Setting...");
- while((Ublox_Cfg_Rate(1000,1)!=0)&&key) //持续判断,直到可以检查到NEO-6M,且数据保存成功
- {
- usart3_init(9600); //初始化串口3波特率为9600(EEPROM没有保存数据的时候,波特率为9600.)
- Ublox_Cfg_Prt(38400); //重新设置模块的波特率为38400
- Ublox_Cfg_Tp(1000000,100000,1); //设置PPS为1秒钟输出1次,脉冲宽度为100ms
- key=Ublox_Cfg_Cfg_Save(); //保存配置
- }
- LCD_ShowString(30,120,200,16,16,"NEO-6M Set Done!!");
- delay_ms(500);
- LCD_Fill(30,120,30+200,120+16,WHITE);//清除显示
- }
- while(1)
- {
- delay_ms(1);
- if(USART3_RX_STA&0X8000) //接收到一次数据了
- {
- rxlen=USART3_RX_STA&0X7FFF; //得到数据长度
- for(i=0;i=USART3_RX_BUF;
- USART3_RX_STA=0; //启动下一次接收
- USART1_TX_BUF=0; //自动添加结束符
- GPS_Analysis(&gpsx,(u8*)USART1_TX_BUF);//分析字符串
- Gps_Msg_Show(); //显示信息
- //SD卡信息显示
- exf_getfree("0",&total,&free); //得到SD卡总容量和剩余容量
- LCD_ShowString(30, 330,200,16,16,"SD Card:");
- sprintf(temp,"%d/%dMB",free>>10,total>>10);
- LCD_ShowString(30+80,330,200,16,16,temp);
- if(upload)printf("rn%srn",USART1_TX_BUF);//发送接收到的数据到串口1
- }
- if(j>=1000) //大约1s记录一次
- {
- j=0;
- if(record)
- {
- r++;
- f_lseek(file,(*file).fsize); //移动指针
- f_printf(file,"记录%03d 自动 ",r); //写序号
- sprintf(temp,"%04d-%02d-%02d ",gpsx.utc.year,gpsx.utc.month,gpsx.utc.date); //日期
- f_write(file,temp,11,&br); //日期
- sprintf(temp,"%02d:%02d:%02d ",gpsx.utc.hour,gpsx.utc.min,gpsx.utc.sec); //时间
- f_write(file,temp,9,&br); //时间
- sprintf(temp,"(%03.7f,",gpsx.longitude/100000+gpsx.longitude%100000/100000.0); //得到经度字符串
- f_write(file,temp,13,&br); //经度
- sprintf(temp,"%02.7f)",gpsx.latitude/100000+gpsx.latitude%100000/100000.0); //得到纬度字符串
- f_write(file,temp,12,&br); //纬度
- f_printf(file,"rn"); //回车换行
- }
- }
- j++;
- key=KEY_Scan(0);
- if(key==KEY0_PRES)
- {
- upload=!upload;
- POINT_COLOR=RED;
- if(upload)LCD_ShowString(30,100,200,16,16,"NMEA Data Upload:ON ");
- else LCD_ShowString(30,100,200,16,16,"NMEA Data Upload:OFF");
- ……………………
- …………限于本文篇幅 余下代码请从电子发烧友下载附件…………
所有资料51hei提供下载:
扩展实验2 ATK-NEO-6M GPS模块实验(文件记录).zip
|