//
***************************************************************************************
// **** PROGRAM = PROSTY ODBIORNIK GPS (C) 2010 LUCIDSCIENCE.COM
// **** PLATFORMA = ATMEGA88P
// **** PRDKO ZEGARA = 14,7456 MHZ, KRYSZTA
//
***************************************************************************************
#include <mega324.h>
#include <delay.h>
//
***************************************************************************************
// **** ZMIENNE GLOBALNE NA DANE NMEA
//
***************************************************************************************
char fix[16];
char sats[16];
char height[16];
char utctime[16];
char altitude[16];
char latitude[16];
char longitude[16];
char horizontal[16];
//
***************************************************************************************
// **** WYSYANIE DANYCH PRZEZ PORT USART0 DO LCD Z PRDKOCI 9600 B/S
//
***************************************************************************************
void LCDWRITE(char lcd) {
while(!(UCSR0A & (1<<5)));
UDR0 = lcd;
}
//
***************************************************************************************
// **** ODCZYT DANYCH NMEA Z PORTU USART1 Z PRDKOCI 38400 B/S I ZAPIS DO ACUCHA
//
****************************************
void GPSREAD() {
  char cntr;
  char data;
  // $GPGGA, 223611.000, 4821.9234,N, 08916.4091,W, 1, 8, 1.17, 190.1,M, -35.0,M, ,*6F
  // $GPGGA, CZAS UTC, SZER. GEORG.,D. GEOGR., STAN, SATELITY,PO. POZ.,WYS.,PO. PION., SUMA KONTR.
  // SKASOWANIE OSTATNICH DANYCH W CIGU ZNAKW
  for (cntr = 0; cntr < 16; cntr++) {
    fix[cntr] = 32;
    sats[cntr] = 32;
    height[cntr] = 32;
    utctime[cntr] = 32;
    altitude[cntr] = 32;
    latitude[cntr] = 32;
    longitude[cntr] = 32;
    horizontal[cntr] = 32;
  }
  // OCZEKIWANIE NA ZNACZNIK CIGU ZNAKW ($GPGGA)
  while (cntr != 6) {
    cntr = 0;
    while (!(UCSR1A & (1<<7)));
    data = UDR1;
    if (data == '$') cntr++;
    while (!(UCSR1A & (1<<7)));
    data = UDR1;
    if (data == 'G') cntr++;
    while (!(UCSR1A & (1<<7)));
    data = UDR1;
    if (data == 'P') cntr++;
    while (!(UCSR1A & (1<<7)));
    data = UDR1;
    if (data == 'G') cntr++;
    while (!(UCSR1A & (1<<7)));
    data = UDR1;
    if (data == 'G') cntr++;
    while (!(UCSR1A & (1<<7)));
    data = UDR1;
    if (data == 'A') cntr++;
  }
  while (data != ',') {
    while (!(UCSR1A & (1<<7)));
    data = UDR1;
  }
  // ODCZYT CIGU ZNAKW Z CZASEM UTC
  data = 0;
  cntr = 0;
  while (data != ',') {
    while (!(UCSR1A & (1<<7)));
    data = UDR1;
    utctime[cntr] = data;
    cntr++;
  }
  utctime[cntr-1] = 32;
  // ODCZYT CIGU ZNAKW Z SZEROKOCI GEOGRAFICZN
  data = 0;
  cntr = 0;
  while (data != ',') {
    while (!(UCSR1A & (1<<7)));
    data = UDR1;
    latitude[cntr] = data;
    cntr++;
  }
  data = 0;
  while (data != ',') {
    while (!(UCSR1A & (1<<7)));
    data = UDR1;
    latitude[cntr] = data;
    cntr++;
  }
  latitude[cntr-1] = 32;
  // ODCZYT CIGU ZNAKW Z DUGOCI GEOGRAFICZN
  data = 0;
  cntr = 0;
  while (data != ',') {
    while (!(UCSR1A & (1<<7)));
    data = UDR1;
    longitude[cntr] = data;
    cntr++;
  }
  data = 0;
  while (data != ',') {
    while (!(UCSR1A & (1<<7)));
    data = UDR1;
    longitude[cntr] = data;
    cntr++;
  }
  longitude[cntr-1] = 32;
  // ODCZYT ZNACZNIKA SATELITY
  data = 0;
  cntr = 0;
  while (data != ',') {
    while (!(UCSR1A & (1<<7)));
    data = UDR1;
    fix[cntr] = data;
    cntr++;
  }
  fix[cntr-1] = 32;
  // ODCZYT LICZBY SATELITW
  data = 0;
  cntr = 0;
  while (data != ',') {
    while (!(UCSR1A & (1<<7)));
    data = UDR1;
    sats[cntr] = data;
    cntr++;
  }
  sats[cntr-1] = 32;
  // ODCZYT CIGU ZNAKW Z POOENIEM POZIOMYM
  data = 0;
  cntr = 0;
  while (data != ',') {
    while (!(UCSR1A & (1<<7)));
    data = UDR1;
    horizontal[cntr] = data;
    cntr++;
  }
  horizontal[cntr-1] = 32;
  // ODCZYT CIGU ZNAKW Z POOENIEM PIONOWYM
  data = 0;
  cntr = 0;
  while (data != ',') {
    while (!(UCSR1A & (1<<7)));
    data = UDR1;
    altitude[cntr] = data;
    cntr++;
  }
  data = 0;
  while (data != ',') {
    while (!(UCSR1A & (1<<7)));
    data = UDR1;
    altitude[cntr] = data;
    cntr++;
  }
  altitude[cntr-1] = 32;
  // ODCZYT CIGU ZNAKW Z WYSOKOCI
  data = 0;
  cntr = 0;
  while (data != ',') {
    while (!(UCSR1A & (1<<7)));
    data = UDR1;
    height[cntr] = data;
    cntr++;
  }
  data = 0;
  while (data != ',') {
    while (!(UCSR1A & (1<<7)));
    data = UDR1;
    height[cntr] = data;
    cntr++;
  }
  height[cntr-1] = 32;
}
// ***********************************************************************************
// **** ZMIENNE PROGRAMU I KONFIGURACJA PORTU
// ***********************************************************************************
void main(void) {
// GWNE ZMIENNE PROGRAMU
char temp;
char ct;
// USTAWIENIE PRDKOCI PORTU USART0 NA 9600,8,N,1
UCSR0A=0x00;
UCSR0B=0x08;
UCSR0C=0x06;
UBRR0H=0x00;
UBRR0L=0x5F;
// USTAWIENIE PRDKOCI PORTU USART0 NA 38400,8,N,1
UCSR1A=0x00;
UCSR1B=0x10;
UCSR1C=0x06;
UBRR1H=0x00;
UBRR1L=0x17;
// ***********************************************************************************
// **** INICJALIZACJA PROGRAMU
// ***********************************************************************************
// SKASOWANIE EKRANU LCD
LCDWRITE(12);
delay_ms(500);
// WYCZENIE KURSORA
LCDWRITE(22);
delay_ms(500);
// WCZENIE PODWIETLENIA
LCDWRITE(17);
delay_ms(500);
// KOMUNIKAT STARTOWY
LCDWRITE('S');
LCDWRITE('T');
LCDWRITE('A');
LCDWRITE('R');
LCDWRITE('T');
LCDWRITE(32);
LCDWRITE('G');
LCDWRITE('P');
LCDWRITE('S');
LCDWRITE('.');
LCDWRITE('.');
LCDWRITE('.');
ct = 0;
// ***********************************************************************************
// **** GWNA PTLA PROGRAMU
// ***********************************************************************************
while (1) {
  // ODCZYT DANYCH GPGGA Z GPS
  GPSREAD();
  // SKASOWANIE DANYCH LCD
  LCDWRITE(128);
  ct++;
  // WYWIETLENIE DANYCH I STANU
  if (ct == 1) {
    LCDWRITE('S');
    LCDWRITE('T');
    LCDWRITE('A');
    LCDWRITE('N');
    LCDWRITE('=');
    for (temp = 0; temp < 14; temp++) {
      VVLCDWRITE(fix[temp]);
    }
  }
  // WYWIETLENIE DANYCH I LICZBY SATELITW
  if (ct == 2) {
    LCDWRITE('S');
    LCDWRITE('A');
    LCDWRITE('T');
    LCDWRITE('=');
    for (temp = 0; temp < 14; temp++) {
      LCDWRITE(sats[temp]);
    }
  }
  // WYWIETLENIE DANYCH I CZASU
  if (ct == 3) {
    LCDWRITE('C');
    LCDWRITE('Z');
    LCDWRITE('A');
    LCDWRITE('S');
    LCDWRITE('=');
    for (temp = 0; temp < 14; temp++) {
      LCDWRITE(utctime[temp]);
    }
  }
  // WYWIETLENIE DANYCH I SZEROKOCI GEOGRAFICZNEJ
  if (ct == 4) {
    LCDWRITE('D');
    LCDWRITE('L');
    LCDWRITE('U');
    LCDWRITE('G');
    LCDWRITE('=');
    for (temp = 0; temp < 14; temp++) {
      LCDWRITE(latitude[temp]);
    }
  }
  // WYWIETLENIE DANYCH I DUGOCI GEOGRAFICZNEJ
  if (ct == 5) {
    LCDWRITE('S');
    LCDWRITE('Z');
    LCDWRITE('E');
    LCDWRITE('R');
    LCDWRITE('=');
    for (temp = 0; temp < 14; temp++) {
      LCDWRITE(longitude[temp]);
    }
  }
  // WYWIETLENIE DANYCH I POOENIA POZIOMEGO
  if (ct == 6) {
    LCDWRITE('P');
    LCDWRITE('O');
    LCDWRITE('Z');
    LCDWRITE('=');
    for (temp = 0; temp < 14; temp++) {
      LCDWRITE(horizontal[temp]);
    }
  }
  // WYWIETLENIE DANYCH I WYSOKOCI
  if (ct == 7) {
    LCDWRITE('W');
    LCDWRITE('Y');
    LCDWRITE('S');
    LCDWRITE('=');
    for (temp = 0; temp < 14; temp++) {
      LCDWRITE(altitude[temp]);
    }
  }
  // WYWIETLENIE DANYCH I POOENIA PIONOWEGO
  if (ct == 8) {
    LCDWRITE('P');
    LCDWRITE('I');
    LCDWRITE('O');
    LCDWRITE('N');
    LCDWRITE('=');
    for (temp = 0; temp < 14; temp++) {
      LCDWRITE(height[temp]);
    }
    ct = 0;
  }
  // OPӏNIENIE 1 SEKUNDA
  delay_ms(1000);
  // KONIEC GWNEJ PTLI
  }
}
