Advertisement
dashko

GPS

Nov 6th, 2014
324
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 8.85 KB | None | 0 0
  1. /**
  2.  *  Keil project for GPS
  3.  *
  4.  *  Before you start, select your target, on the right of the "Load" button
  5.  *
  6.  *  @author     Tilen Majerle
  7.  *  @email      tilen@majerle.eu
  8.  *  @website    http://stm32f4-discovery.com
  9.  *  @ide        Keil uVision 5
  10.  *  @packs      STM32F4xx Keil packs version 2.2.0 or greater required
  11.  *  @stdperiph  STM32F4xx Standard peripheral drivers version 1.4.0 or greater required
  12.  *
  13.  *  Make sure you have correct baudrate set for your GPS
  14.  */
  15. #include "defines.h"
  16. #include "stm32f4xx.h"
  17. #include "tm_stm32f4_gps.h"
  18. #include "tm_stm32f4_delay.h"
  19. #include "tm_stm32f4_rtc.h"
  20. #include "tm_stm32f4_low_power.h"
  21. #include <stdio.h>
  22.  
  23.     char buf[50];
  24.     TM_RTC_Time_t datatime;
  25.  
  26. int main(void) {
  27.     /* Variables used */
  28.     TM_GPS_Data_t GPS_Data;
  29.     TM_GPS_Result_t result, current;
  30.     TM_GPS_Float_t GPS_Float;
  31.     TM_GPS_Distance_t GPS_Distance;
  32.     char buffer[40];
  33.     uint8_t i;
  34.     float temp;
  35.    
  36.  
  37.  
  38.    
  39.     /* Initialize system */
  40.     SystemInit();
  41.    
  42.     /* Delay init */
  43.     TM_DELAY_Init();
  44.    
  45.    
  46.     // RTC test
  47.         if (!TM_RTC_Init(TM_RTC_ClockSource_Internal)) {
  48.         /* RTC was first time initialized */
  49.         /* Do your stuff here */
  50.         /* eg. set default time */
  51.             datatime.hours = 0;
  52.             datatime.minutes = 59;
  53.             datatime.seconds = 55;
  54.             datatime.year = 14;
  55.             datatime.month = 6;
  56.             datatime.date = 30;
  57.             datatime.day = 6;
  58.             /* Set new time */
  59.             TM_RTC_SetDateTime(&datatime, TM_RTC_Format_BIN);
  60.     }
  61.     TM_RTC_Interrupts(TM_RTC_Int_10s);
  62.    
  63. // end of RTC test declarations
  64.    
  65.    
  66.     //TM_LOWPOWER_SleepUntilInterrupt(1);
  67.     /* Initialize GPS on 115200 baudrate */
  68.     TM_GPS_Init(&GPS_Data, 9600);
  69.    
  70.     /* Initialize USART3 for debug */
  71.     /* TX = PB10 */
  72.     TM_USART_Init(USART3, TM_USART_PinsPack_1, 115200);
  73.    
  74.     /* Version 1.1 added */
  75.     /* Set two test coordinates */
  76.     GPS_Distance.Latitude1 = 48.300215;
  77.     GPS_Distance.Longitude1 = -122.285903;
  78.     GPS_Distance.Latitude2 = 45.907813;
  79.     GPS_Distance.Longitude2 = 56.659407;
  80.    
  81.     /* Calculate distance and bearing between 2 pointes */
  82.     TM_GPS_DistanceBetween(&GPS_Distance);
  83.     /* Convert float number */
  84.     TM_GPS_ConvertFloat(GPS_Distance.Distance, &GPS_Float, 6);
  85.     sprintf(buffer, "Distance is: %d.%06d meters\n\r", GPS_Float.Integer, GPS_Float.Decimal);
  86.     TM_USART_Puts(USART3, buffer);
  87.     TM_GPS_ConvertFloat(GPS_Distance.Bearing, &GPS_Float, 6);
  88.     sprintf(buffer, "Bearing is: %d.%06d degrees\n\r", GPS_Float.Integer, GPS_Float.Decimal);
  89.     TM_USART_Puts(USART3, buffer);
  90.    
  91.  
  92.     /* Reset counter */
  93.     TM_DELAY_SetTime(0);
  94.  
  95.  
  96.  
  97.    
  98.     while (1) {
  99.         /* Update GPR data */
  100.         /* Call this as faster as possible */
  101.        
  102.         result = TM_GPS_Update(&GPS_Data);
  103.         /* If we didn't receive any useful data in the start */
  104.         if (result == TM_GPS_Result_FirstDataWaiting && TM_DELAY_Time() > 3000000) {
  105.             /* If we didn't receive nothing within 3 seconds */
  106.             TM_DELAY_SetTime(0);
  107.             /* Display data on USART */
  108.             TM_USART_Puts(USART3, "\nNothing received after 3 seconds. Is your GPS connected and baudrate set correct?\n\r");
  109.             TM_USART_Puts(USART3, "Most GPS receivers has by default 9600 baudrate and 1Hz refresh rate. Check your settings!\n\r");
  110.         }
  111.         /* If we have any unread data */
  112.         if (result == TM_GPS_Result_NewData) {
  113.             /* We received new packet of useful data from GPS */
  114.             current = TM_GPS_Result_NewData;
  115.            
  116.             /* Is GPS signal valid? */
  117.             if (GPS_Data.Validity) {
  118.                 /* If you want to make a GPS tracker, now is the time to save your data on SD card */
  119.                
  120.                 /* We have valid GPS signal */
  121.                 TM_USART_Puts(USART3, "New received data have valid GPS signal\n\r");
  122.                 TM_USART_Puts(USART3, "---------------------------------------\n\r");
  123. #ifndef GPS_DISABLE_GPGGA
  124.                 /* GPGGA data */
  125.                 TM_USART_Puts(USART3, "GPGGA statement:\n\r");
  126.                
  127.                 /* Latitude */
  128.                 /* Convert float to integer and decimal part, with 6 decimal places */
  129.                 TM_GPS_ConvertFloat(GPS_Data.Latitude, &GPS_Float, 6);
  130.                 sprintf(buffer, " - Latitude: %d.%d\n\r", GPS_Float.Integer, GPS_Float.Decimal);
  131.                 TM_USART_Puts(USART3, buffer);
  132.                
  133.                 /* Longitude */
  134.                 /* Convert float to integer and decimal part, with 6 decimal places */
  135.                 TM_GPS_ConvertFloat(GPS_Data.Longitude, &GPS_Float, 6);
  136.                 sprintf(buffer, " - Longitude: %d.%d\n\r", GPS_Float.Integer, GPS_Float.Decimal);
  137.                 TM_USART_Puts(USART3, buffer);
  138.                
  139.                 /* Satellites in use */
  140.                 sprintf(buffer, " - Sats in use: %02d\n\r", GPS_Data.Satellites);
  141.                 TM_USART_Puts(USART3, buffer); 
  142.                
  143.                 /* Current time */
  144.                 sprintf(buffer, " - UTC Time: %02d.%02d.%02d:%02d\n\r", GPS_Data.Time.Hours, GPS_Data.Time.Minutes, GPS_Data.Time.Seconds, GPS_Data.Time.Hundredths);
  145.                 TM_USART_Puts(USART3, buffer);
  146.                
  147.                 /* Fix: 0 = invalid, 1 = GPS, 2 = DGPS */
  148.                 sprintf(buffer, " - Fix: %d\n\r", GPS_Data.Fix);
  149.                 TM_USART_Puts(USART3, buffer);             
  150.                
  151.                 /* Altitude */
  152.                 /* Convert float to integer and decimal part, with 6 decimal places */
  153.                 TM_GPS_ConvertFloat(GPS_Data.Altitude, &GPS_Float, 6);
  154.                 sprintf(buffer, " - Altitude: %3d.%06d\n\r", GPS_Float.Integer, GPS_Float.Decimal);
  155.                 TM_USART_Puts(USART3, buffer);             
  156. #endif
  157. #ifndef GPS_DISABLE_GPRMC
  158.                 /* GPRMC data */
  159.                 TM_USART_Puts(USART3, "GPRMC statement:\n\r");
  160.                
  161.                 /* Current date */
  162.                 sprintf(buffer, " - Date: %02d.%02d.%04d\n\r", GPS_Data.Date.Date, GPS_Data.Date.Month, GPS_Data.Date.Year + 2000);
  163.                 TM_USART_Puts(USART3, buffer);
  164.                
  165.                 /* Current speed in knots */
  166.                 TM_GPS_ConvertFloat(GPS_Data.Speed, &GPS_Float, 6);
  167.                 sprintf(buffer, " - Speed in knots: %d.%06d\n\r", GPS_Float.Integer, GPS_Float.Decimal);
  168.                 TM_USART_Puts(USART3, buffer);
  169.                
  170.                 /* Current speed in km/h */
  171.                 temp = TM_GPS_ConvertSpeed(GPS_Data.Speed, TM_GPS_Speed_KilometerPerHour);
  172.                 TM_GPS_ConvertFloat(temp, &GPS_Float, 6);
  173.                 sprintf(buffer, " - Speed in km/h: %d.%06d\n\r", GPS_Float.Integer, GPS_Float.Decimal);
  174.                 TM_USART_Puts(USART3, buffer);
  175.                
  176.                 TM_GPS_ConvertFloat(GPS_Data.Direction, &GPS_Float, 3);
  177.                 sprintf(buffer, " - Direction: %3d.%03d\n\r", GPS_Float.Integer, GPS_Float.Decimal);
  178.                 TM_USART_Puts(USART3, buffer);
  179. #endif
  180. #ifndef GPS_DISABLE_GPGSA
  181.                 /* GPGSA data */
  182.                 TM_USART_Puts(USART3, "GPGSA statement:\n\r");
  183.                
  184.                 /* Horizontal dilution of precision */
  185.                 TM_GPS_ConvertFloat(GPS_Data.HDOP, &GPS_Float, 2);
  186.                 sprintf(buffer, " - HDOP: %2d.%02d\n\r", GPS_Float.Integer, GPS_Float.Decimal);
  187.                 TM_USART_Puts(USART3, buffer);
  188.                
  189.                 /* Vertical dilution of precision */
  190.                 TM_GPS_ConvertFloat(GPS_Data.VDOP, &GPS_Float, 2);
  191.                 sprintf(buffer, " - VDOP: %2d.%02d\r", GPS_Float.Integer, GPS_Float.Decimal);
  192.                 TM_USART_Puts(USART3, buffer);
  193.                
  194.                 /* Position dilution of precision */
  195.                 TM_GPS_ConvertFloat(GPS_Data.PDOP, &GPS_Float, 2);
  196.                 sprintf(buffer, " - PDOP: %2d.%02d\n\r", GPS_Float.Integer, GPS_Float.Decimal);
  197.                 TM_USART_Puts(USART3, buffer); 
  198.                
  199.                 /* Current fix mode in use */
  200.                 sprintf(buffer, " - Fix mode: %d\n\r", GPS_Data.FixMode);
  201.                 TM_USART_Puts(USART3, buffer);
  202.                
  203.                 /* Display IDs of satellites in use */
  204.                 TM_USART_Puts(USART3, "- ID's of used satellites: ");
  205.                 for (i = 0; i < GPS_Data.Satellites; i++) {
  206.                     if (i < (GPS_Data.Satellites - 1)) {
  207.                         sprintf(buffer, "%d,", GPS_Data.SatelliteIDs[i]);
  208.                     } else {
  209.                         sprintf(buffer, "%d\n\r", GPS_Data.SatelliteIDs[i]);
  210.                     }
  211.                     TM_USART_Puts(USART3, buffer);
  212.                 }
  213.                
  214. #endif
  215. #ifndef GPS_DISABLE_GPGSV
  216.                 /* GPGSV data */
  217.                 TM_USART_Puts(USART3, "GPGSV statement:\n\r");
  218.                
  219.                 /* Satellites in view */
  220.                 sprintf(buffer, " - Satellites in view: %d\r", GPS_Data.SatellitesInView);
  221.                 TM_USART_Puts(USART3, buffer); 
  222. #endif
  223.                 TM_USART_Puts(USART3, "---------------------------------------\n\r");
  224.  
  225.             } else {
  226.                 /* GPS signal is not valid */
  227.                 TM_USART_Puts(USART3, "\nNew received data haven't valid GPS signal!\n\r");
  228.                 TM_USART_Puts(USART3, "Actual RTC time: ");
  229.                 TM_USART_Puts(USART3, buf);
  230.                 TM_USART_Puts(USART3, "\n\r");
  231.                 TM_LOWPOWER_SleepUntilInterrupt(1);
  232.                 //TM_USART_Puts(USART3, "New received data haven't valid GPS signal test!\n\r");
  233.             }
  234.         } else if (result == TM_GPS_Result_FirstDataWaiting && current != TM_GPS_Result_FirstDataWaiting) {
  235.             current = TM_GPS_Result_FirstDataWaiting;
  236.             TM_USART_Puts(USART3, "Waiting first data from GPS!\n\r");
  237.         } else if (result == TM_GPS_Result_OldData && current != TM_GPS_Result_OldData) {
  238.             current = TM_GPS_Result_OldData;
  239.             /* We already read data, nothing new was received from GPS */
  240.         }
  241.     }
  242. }
  243.  
  244.  
  245.     void TM_RTC_RequestHandler() {
  246.     /* Get time */
  247.     TM_RTC_GetDateTime(&datatime, TM_RTC_Format_BIN);
  248.    
  249.     /* Format time */
  250.   sprintf(buf, "%02d.%02d.%04d %02d:%02d:%02d  Unix: %u",
  251.                 datatime.date,
  252.             datatime.month,
  253.                 datatime.year + 2000,
  254.                 datatime.hours,
  255.         datatime.minutes,
  256.             datatime.seconds,
  257.                 datatime.unix
  258.     );
  259.    
  260.     //TM_USART_Putc(USART3, 0x1b);
  261.     //TM_USART_Puts(USART3, "[H");
  262.     //TM_USART_Puts(USART3, buf);
  263.    
  264. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement