'--------------------------------------------------------------
'                 (c) Taptronics
'--------------------------------------------------------------
'  file: recgps.BAS
'  Reading GPS NMEA -0183 data and save it to Eeprom
'--------------------------------------------------------------
'To use another baudrate and crystalfrequency use the
'metastatements $BAUD =  and $CRYSTAL =

$regfile = "89S8252.DAT"
$crystal = 11059000
$baud = 9600
$timeout
$large
'---------------------------------------------------------------
'   When you need that the program times out on waiting for a character
'   you need to use the TIMEOUT option.
'   When the charcter is not received within the specified time ERR will be set to 1
'   otherwise ERR will be 0.
'   IMPORTANT : the TIMEOUT variable will use 4 bytes of internal memory
'---------------------------------------------------------------
Dim A As Byte
Dim V As Byte , Dim B As Integer
Dim Count As Integer , Gt As Byte
Dim S As String * 32
Dim T As String * 32
Dim U As String * 15
Dim X As String * 15
Dim O As String * 15
Dim I As Integer
Dim L1 As Bit
Dim L3 As Bit
Dim L2 As Bit


Config Timer0 = Timer , Gate = Internal , Mode = 2
'Timer0 = Counter : Timer0 Operates As A Counter
'Gate = Internal : No External Gate Control
'Mode = 2 : 8 -bit Auto Reload(default)

  On Timer0 Timer_0_int
Load Timer0 , 255                                             'when the timer reaches 100 an interrupt will occur
Enable Interrupts                                             'enable the use of interrupts
Enable Timer0                                                 'enable the timer




If P2.1 = 0 Then
  Print "=======Programming Phone number======="
'Saving data to EEPROM"
 Input "Enter phone number to receive GPS data =" , O
 Writeeeprom O , 50
 Print "========================================"
 Input "Enter phone number to activate GPS tracking box =" , O
 Writeeeprom O , 10
 Print "You have stored those numbers,Please connect the GSM modem"
 Print "back to Tracking box and reset the box"
 Print "do NOT forget to switch back to RUN mode "
 Goto Awal

 Else

   End If

 'P0.2 = 0
While A = 0
  Print "ATE0"
A = Inkey()
 Wait 3
 Wend
   Print "ATE0"
Waitms 200
 Print "AT+CPBS=";
   Print Chr(34);
    Print "MC";
    Print Chr(34)
     Wait 1
 P0.2 = 0


 U = ""
 O = ""
 X = ""


S = ""                                                        'assign string
T = ""
Reset L1
 Reset L2
  Reset L3


B = 0

'Print "ATE0"                                                  'Print " receiving SMS "

Do

   Gosub Remote

  If P2.2 = 1 Then

  Reset L3
    Else

 Rem Setting Of Priority
'Priority Set Timer0                                           'highest priority
                                                   'reset counteR
    Start Timer0
   Gosub Getgps

 Reset L1
   Reset L2

      Gosub Sendsms

  End If
Loop



''==================GET GPS data ====================
'===Only data From $GRMC format will be transmitted
 Getgps:



  Count = 0

 'Open "com3.1:4800" For Output As #1                           'p3.1 is normally used for tx so testing is easy
Open "com3.4:4800" For Input As #2                            'p3.0 is normally used for RX so testing is easy
 S = ""
 T = ""
 B = 0

 Count = 0
 Do

 Get #2 , A                                                   'get character from comport
   Stop Timer0
   If A = "$" Then                                            'check First Nmea Sign

  Set L1
 Else
'    End If
 If L1 = 1 Then
 If A = "C" Then                                              'detecting C of "GPRMC"
   Set L2
  Else
    End If
 If L2 = 1 Then
  Incr B
 If B < 32 Then
  S = S + Chr(a)                                              'put data input on RAM segment1
  Else
  T = T + Chr(a)
  End If                                                      'put input data on RAM segment 2
 'If B > 64 Then Goto Selesai
 If A = 13 Then Goto Selesai
    Else
       End If
    Else
       End If

      End If

       Loop



       Selesai:
Close #2

 B = 0
 Return




 ''====================Sending SMS ============
 Sendsms:

 While L3 = 0
 Set L3

Print "AT+CMGF=1"

 Waitms 200

'=========Send SMS to phone number saved on Eeprom
 Print "AT+CMGS=";
 Print Chr(34);
Readeeprom O , 50
Print O;
 '  Print "14694713613";

 Print Chr(34);
 Print Chr(13);
 Waitms 100

 A = Waitkey()
'Print Chr(a)
' Waitms 200
 P0.0 = 0
Print "$GPRM";
  Print S;
   Print T;

Waitms 200
   Print Chr(26);

 A = Waitkey()
 Print "AT"
 P0.0 = 1
 Set P2.2


Wend
Return

'=======================Remote activation
 Remote:

A = Inkey()
 '=========Checking first letter of of RING signal
 If A = "R" Then

 While V < 4
  A = Waitkey()
 'ASCII  82   ->"R"
 ' 73  -> "I"

 X = X + Chr(a)
' Print X
 V = Len(x)
 Wend



  O = Mid(x , 1 , 3)
  ' Print O
   X = ""
   V = 0
 '============Checking rest of the letters of  RING signal
    If O = "ING" Then


    Print "AT+CPBR=1,1"
    Waitms 100

  '  Waitms 200
 'P0.0 = 0


 If A <> 34 Then
 ' P0.0 = 0
 Do
 A = Waitkey()
 ' A = Inkey()
    Loop Until A = 34
  Do
 ' P0.0 = 0
  ' A = Waitkey()
 A = Inkey()
     X = X + Chr(a)
 Loop Until A = 34


    U = Mid(x , 1 , 10)

 'Save missed call number if P2.3 is activated
  If P2.3 = 0 Then
  Writeeeprom U , 10
  Writeeeprom U , 50
  Else
    End If

  Readeeprom O , 10
' P0.0 = 0
  If U = O Then

 ' If U = "4694713613" Then
 '======== If missed call number matched number in EEPROM, turn P0.0 on
 P0.0 = 0

  Reset P2.2
   U = ""
   X = ""

 Else
 P0.0 = 1
  End If
  Else
  End If
Else
    End If
Else
  End If
    Return

 Rem The Interrupt HandlEr For The Timer0 Interrupt
Timer_0_int:
   Inc Count
   If Count = 255 Then
      Count = 0
      P0.1 = 0
      Wait 2
      P0.1 = 1
      Print "Invalid data or No GPS device found !"

  ' S = " Invalid,NO GPS device found"
  ' T = ""
  '   Gosub Sendsms
   End If
Return


 Awal:
 End