'GPS loger

$regfile = "m128def.dat"
$crystal = 8000000
$baud = 9600
$hwstack = 128
$swstack = 128
$framesize = 128
Ddra.= 1                                                  'Configuring the port for output
Ddra.= 1                                                  '---------//--------
Ddra.= 1                                                  '---------//--------
Ddrd.= 0                                                  'Configuring the port for input
Ddrc.= 0                                                  ' ---------//--------
Ddrg.= 0                                                  '---------//--------



     Porta.= 1
      Wait 1
     Porta.= 0
      Wait 1
$include "Config_MMC.bas"                                   'include mmc

Dim B As Byte
Dim V As Byte



  V = Gbdriveerror
  ' If V <> 0 Then Print "EROR"


$include "Config_AVR-DOS.BAS"
= Initfilesystem(1)

If B <> 0 Then                                              ' check initialization flash

 Porta.= 1                                                ' if B <> 0 Initialization Failed, light red lights, stop the program
 Stop

End If

                                                       ' Variables
Dim P As Byte
Dim Uts As String * 10
Dim Av As String * 2
Dim Lat As String * 12
Dim Ns As String * 2
Dim Lon As String * 13
Dim Ew As String * 2
Dim Spd As String * 6
Dim Crs As String * 6
Dim Leng As Byte
Dim Uts1 As String * 2
Dim Uts2 As String * 2
Dim Uts3 As Byte
Dim Uts4 As String * 6
Dim Da As String * 8
Dim Da0 As String * 5
Dim Da1 As String * 5
Dim Da2 As String * 12
Dim Da3 As String * 12
Dim Da4 As String * 8
 Dim Spd2 As Single
 Dim Spd3 As Single
 Dim Road As Single
   Dim Td1 As String * 10
   Dim Tj1 As String * 10
   Dim Gd1 As String * 10
   Dim Gj1 As String * 10

   Dim Td As Single
   Dim Tj As Single
   Dim Tm As Single

   Dim Gd As Single
   Dim Gj As Single
   Dim Gm As Single
   Dim Yy As Single
   Dim Xx As Single
Road = 0
Da = "Gps1"
Da0 = ".TXT"
Da1 = ".KML"


      Porta.= 1
      Wait 1
      Porta.= 0

      Wait 10

      Porta.= 1
      Waitms 250
      Porta.= 0
      Waitms 250
      Porta.= 1
      Waitms 250
      Porta.= 0
       Porta.= 1
      Waitms 250
      Porta.= 0
      Wait 1
 M4:
  Da2 = Da + Da1                                            'The formation of the file name
  Da3 = Da + Da0                                            'The formation of the file name

  Porta.= 1                                               'flashing all LEDs
  Porta.= 1
  Porta.= 1
  Waitms 150
  Porta.= 0
  Porta.= 0
  Porta.= 0
                                                          'creates a flash file to googl named Da2 and write in his cap
 Open Da2 For Output As #9

Print #, "<?xml version=";
Write #, "1.0"
Print #, "encoding=";
Write #, "Utf-8"
Print #, "?>"
Print #, "<kml xmlns=";
Write #, "http://earth.google.com/kml/2.1"
Print #, ">"
Print #, "<Folder> "
Print #, "<name>" ; Da ; "</name> "
Print #, "<open>0</open> "
Print #, "<Style> "
Print #, "<ListStyle> "
Print #, "<listItemType>checkHideChildren</listItemType> "
Print #, "<bgColor>00ffffff</bgColor> "
Print #, "</ListStyle> "
Print #, "</Style> "
Print #, "<Placemark> "
Print #, "<name>Part 1 - GPS V 1.1</name> "
Print #, "<Style> "
Print #, "<LineStyle> "
Print #, "<color>ffffffff</color>    "
Print #, "<width>4</width> "
Print #, "</LineStyle> "
Print #, "</Style> "
Print #, "<LineString> "
Print #, "<tessellate>1</tessellate> "
Print #, "<altitudeMode>clampToGround</altitudeMode>"
Print #, "<coordinates>"

 Close #9
                                                         'creates a flash file named Da3 and write in his position, time, speed, distance path
 Open Da3 For Output As #1
  Print #, "GPS loger V1.3 21/06/2008     email egor1812@ngs.ru"
  Print #, "Lat            Lon             Time           Spid            road"
 Close #1



Declare Sub Wait_for_string(byval S As String)
Declare Function Read_string() As String


Config Serialin = Buffered , Size = 100                     ' Konfig. der ser. Eingabe
Enable Interrupts
' $GPRMC,hhmmss.ss,A,llll.ll,a,yyyyy.yy,a,x.x,x.x,ddmmyy,x.x,a*hh
M3:
Do                                                          ' main loop
  P = Pind.7                                                'check the button to stop, when you open a file Da2 and add a final line
If P = 0 Then

   Open Da2 For Append As #9
     Print #, "</coordinates>"
     Print #, "</LineString>"
     Print #, "</Placemark>"
     Print #, "</Folder></kml>"
   Close #9
      Porta.= 1                                           ' LED light, and stop program. You can retrieve flesh
   Stop
End If


   Call Wait_for_string( "$GPRMC,")                         ' podprogram call waiting line that starts with $GPRMC,
                                                             'string $ GPRMC
   Uts = Read_string()
   Av = Read_string()

   Lat = Read_string()
   Leng = Len(lat) - 1
   Lat = Left(lat , Leng)

   Ns = Read_string()
   Leng = Len(ns) - 1
   Ns = Left(ns , Leng)

   Lon = Read_string()
   Leng = Len(lon) - 1
   Lon = Left(lon , Leng)

   Ew = Read_string()
   Leng = Len(ew) - 1
   Ew = Left(ew , Leng)

   Spd = Read_string()
   Leng = Len(spd) - 1
   Spd = Left(spd , Leng)

   Crs = Read_string()
   Leng = Len(crs) - 1
   Crs = Left(crs , Leng)

   Da4 = Read_string()
   Da4 = Left(da4 , 6)


   Spd2 = Val(spd)                                          'rate of miles in km / h
   Spd2 = Spd2 * 1.852
   Spd3 = Spd2 / 3600                                       ' the path traversed for 1 second
   Road = Road + Spd3                                       'Total passed way

   Uts1 = Left(uts , 2)                                     'korektirovka time to time zone (7 hours, in this case)
   Uts2 = Mid(uts , 3 , 2)
   Uts3 = Val(uts1)
   Uts3 = Uts3 + 7



   Td1 = Mid(lat , 1 , 2)                                   ' recalculation of the coordinates format
   Tj1 = Mid(lat , 3 , 8)
   Gd1 = Mid(lon , 2 , 2)
   Gj1 = Mid(lon , 4 , 8)


   Td = Val(td1)
   Tj = Val(tj1)
   Gd = Val(gd1)
   Gj = Val(gj1)




   Tj = Tj / 60
   Yy = Td + Tj

   Gj = Gj / 60
   Xx = Gd + Gj

   If Yy = 0 Then                                           ' in the case of zero coordinates (satellites are not captured), the transition tag on M3
     Porta.= 1
     Waitms 100
     Porta.= 0
     Goto M3
   End If

   If Da4 <> Da Then                                        ' in case of change of the day, closing the file with a daily route

   Open Da2 For Append As #9
     Print #, "</coordinates>"
     Print #, "</LineString>"
     Print #, "</Placemark>"
     Print #, "</Folder></kml>"
   Close #9
                                                            'Changing the current file name
   Da = Da4                                                 ' Care to open a file with a change of name.
   Goto M4
   End If
                                                          ' check the current speed, and if = 0 blinking two LEDs and care to read the new coordinates
   If Spd2 = 0 Then                                         ' ,without their record fleshku (why duplicate some tezhe coordinates and increasing file size when parked)
    Porta.= 1
    Porta.= 1
     Waitms 100
    Porta.= 0
    Porta.= 0
    Goto M3
   End If
                                                             'Record coordinates flesh
   Open Da3 For Append As #1
    Print #, Lat;
    Print #, " ";
    Print #, Lon;
    Print #, " ";
    Print #, Uts3 ; ":" ; Uts2;
    Print #, " ";
    Print #, Spd2;
    Print #, " ";
    Print #, Road

  Close #1

  Open Da2 For Append As #9                                 'Record coordinates flesh

   Print #, Xx;
   Print #, ",";
   Print #, Yy


  Close #9

      Porta.= 1                                           ' confirmation of the recording coordinates flesh flashing LEDs
      Waitms 100
      Porta.= 0
Loop
End
 Goto M3
'------------------------------------------------------
Sub Wait_for_string(byval S As String) As String            'subprogramme waiting lines $ GPRMC data GPS Receiver
Local Ii As Byte
Local Cc As Byte
Ii = 1
M1:
   Cc = Waitkey()
   If Cc <> Mid(, Ii , 1) Then
      Goto M1
   Else
      Incr Ii
      If Ii > Len(s) Then Goto M2
      Goto M1
   End If
M2:
End Sub

'------------------------------------------------------         'subprogramme "indiscriminately" at the line of $ GPRMC
Function Read_string() As String
Local Cc As Byte
Local Ss As String * 14

Ss = ""
Do
   Cc = Waitkey()                                           'expects symbol
      Ss = Ss + Chr(cc)

Loop Until Cc = ","
Read_string = Left(ss , 12)
End Function
End