Back to Electronics Cookbook Back to Smart&Small  

 

GPS GSM Tracker. Tracker utilizes the GPS satellites to determine precise location and GSM communications for connect to owner. Tracks objects worldwide via SMS.

  • Free PCAD2006 schematic and PCB files (open hardware project).
  • Free source code.
  • Tracker returns an SMS giving coordinates of it's location
  • Standby: up to 6 months (depends on the time of hibernation).
  • Small device, it can be hidden just about anywhere in a car or in equipment.
  • Tracker schematic diagram. Click to enlarge.



    PCAD2006 Schematic file. Click to download.

     
    PCB Top. Click to enlarge   PCB Bottom. Click to enlarge




    PCAD2006 PCB file. Click to download.



    Source code (IAR Embedded Workbench):

    ADC.h
    #ifndef _adc_h_
    #define _adc_h_
    
    #define ADC_VREF_TYPE 0x00 // VREF input
    
    void InitADC (void); // ADC init
    
    unsigned int ReadADC(unsigned char ADCInput); // ADC read
    
    #endif

    eeprom.h
    // Working with EEPROM
    #ifndef _eeprom_h_
    #define _eeprom_h_
    
    __eeprom __no_init unsigned int eeWaitState @ 0x01;
    
    void EEPROMInit (void);
    
    #endif

    GPSmod.h
    // Working with GPS
    #ifndef _gpsmod_h_
    #define _gpsmod_h_
    
    #include <stdio.h>
    #include <ctype.h>
    
    #include <string.h>
    #include "uart.h"
    
    extern char ReceiveString0[RXBUFLENGTH_1];
    
    void LIQ_On (void); // GPS On
    
    void LIQ_Off (void); // GPS Off
    
    char GPGGA_Extractor (void); 
    
    char GPGGA_NMEA_Parser (void);
    
    #endif

    GSMmod.h
    // Working with GSM module
    
    #ifndef _gsmmod_h_
    #define _gsmmod_h_
    
    #include <inavr.h>
    
    #include <iom128.h>
    #include <stdio.h>
    #include <ctype.h>
    #include <string.h>
    #include "sys.h"
    #include "portdefs.h"
    #include "uart.h"
    
    #define FIX_PASS_LENGTH 5 // Password length
    #define MAX_NUM_LENGTH 15 // Maximum number length
    
    extern char ReceiveString1[RXBUFLENGTH_1];
    
    #define CtrlZ 0x1A // Ctrl-Z
    
    void GR_On (void); // GSM On
    void GR_Off (void); // GSM Off
    
    char ReadAbRecord (void); 
    void SendMessage (char* MessageStr); 
    
    void GRInit(void); // Try to GSM On
    void SMS_Rec_SM(void); // Receive SMS from SM memory
    void SMS_Rec_ME(void); // Receive SMS from ME memory
    
    
    #endif
    

    main.h
    #ifndef _main_h_
    #define _main_h_
    
    #include <iom128.h>
    #include <inavr.h>
    
    #include <stdio.h>
    #include <ctype.h>
    #include <string.h>
    #include "sys.h" // System
    #include "portdefs.h" // Port definitions
    #include "timer.h" // Timers
    #include "uart.h" // UART
    #include "GPSmod.h" // GPS
    #include "GSMmod.h" // GSM
    #include "eeprom.h" // EEPROM
    #include "adc.h" // ADC
    
    #endif
    
    

    portdefs.h
    // Port definition
    
    #include <iom128.h> 
    
    #ifndef _portdefs_h_
    #define _portdefs_h_
    
    // Port A
    #define GPSON 3 // GPS power on
    
    // Port B
    #define PRI 4 // RI
    #define PDCD 5 // DCD
    #define PCTS 6 // CTS
    
    // Port D
    #define TRXPORT PORTD // UART2
    #define TXD1 3 // UART1 Transmitter
    #define TXD2 7 // UART2 Transmitter (Software UART)
    
    // Port E
    #define TXD0 1 // UART0 Transmitter
    #define GSMON 2 // GSM power on
    #define PRTS 3 // RTS
    #define PDTR 4 // DTR
    #define PON_OFF 5 // on/off GSM module
    
    // Port F
    #define VB 1 // V batt
    
    
    void PortInit(void);
    
    #endif
    
    
    

    sys.h
    #ifndef _sys_h_
    #define _sys_h_
    
    #define bit(n) (1 << (n))
    #define setbit(p,n) (p|=bit(n))
    #define clrbit(p,n) (p&=~bit(n))
    #define invbit(p,n) (p=p^bit(n))
    #define tstbit(p,n) (p&bit(n))
    
    #define MasterClock 4000000 // F clk
    
    #define delay_us(c) __delay_cycles(MasterClock/1000000*c) // wait 1 microsecond
    #define delay_ms(c) __delay_cycles(MasterClock/1000*c) // wait 1 millisecond
    
    
    unsigned int StrToULong (char* InputStr); // Convert string to unsigned long.
    
    #endif

    timer.h
    #ifndef _timer_h_
    #define _timer_h_
    
    #include <iom128.h>
    
    void TimerInit(void); 
    
    
    #pragma vector = TIMER0_OVF_vect // Timer 0 interrupt
    __interrupt void TIMER0_OVF_Interrupt (void);
    
    #endif

    uart.h
    // Working with UART
    
    #ifndef _uart_h_
    #define _uart_h_
    
    #include <inavr.h>
    
    #include <iom128.h>
    #include <stdio.h>
    #include "portdefs.h"
    #include "sys.h"
    
    #define TXDELAY 3 
    
    #define BR4800UC 208 // us/bit @ 4800 boud
    
    #define GSM_UART UART_1 // UART for GSM
    #define GPS_UART UART_0 // UART for GPS
    #define DEBUG_UART UART_2 // debug UART (software)
    
    // UART0
    #define FRAMING_ERROR_0 (1<<FE0)
    #define PARITY_ERROR_0 (1<<UPE0)
    #define DATA_OVERRUN_0 (1<<DOR0)
    #define DATA_REGISTER_EMPTY_0 (1<<UDRE0)
    #define RXBUFLENGTH_0 256 
    
    // UART1
    #define FRAMING_ERROR_1 (1<<FE1)
    #define PARITY_ERROR_1 (1<<UPE1)
    #define DATA_OVERRUN_1 (1<<DOR1)
    #define DATA_REGISTER_EMPTY_1 (1<<UDRE1)
    #define RXBUFLENGTH_1 256 
    
    
    typedef enum {UART_0, UART_1, UART_2} t_uarts;
    static t_uarts g_currentUsart;
    
    static t_uarts g_skipUsart;
    
    void UARTinit(void); 
    void SetActiveUart (t_uarts u); 
    
    int putchar( int data ); // for printf()
    void OutText (char const *text); // out text
    
    void OutDat (unsigned int val, unsigned char len, unsigned char Const); // out digits
    
    
    signed char ReadData0(void);
    void ClearRxBuffer0(void); 
    void StringBuilder0(void); 
    
    #pragma vector = USART0_RXC_vect
    __interrupt void USART0_RXC_Interrupt(void);
    
    signed char ReadData1(void);
    
    void ClearRxBuffer1(void); 
    void StringBuilder1(void); 
    #pragma vector = USART1_RXC_vect
    __interrupt void USART1_RXC_Interrupt(void);
    
    #endif
    

    ADC.c
    #include <iom128.h>
    #include "adc.h"
    
    
    void InitADC (void)
    {
    ADMUX = ADC_VREF_TYPE;
    ADCSRA = 0x87;
    
    SFIOR &= 0xEF;
    }//InitADC
    
    unsigned int ReadADC(unsigned char ADCInput)// Read ADC (number of channel)
    
    {
    ADMUX = ADCInput | ADC_VREF_TYPE;
    ADCSRA |= 0x40;
    
    while ((ADCSRA & 0x10) == 0);
    ADCSRA |= 0x10;
    
    return (unsigned int)(ADCL | (ADCH<<8));
    }//ReadADC

    eeprom.c
    #include "eeprom.h"
    
    void EEPROMInit (void)
    {
    
     if (eeWaitState == 0xFFFF) // This is first program load
     eeWaitState = 5;
    }

    GPSmod.c
    // Working with GPS
    
    #include "GPSmod.h"
    
    struct GPGGA_Struct 
    
    {
    char cUTCTime [sizeof("hhmmss.ss")] ;
    char cLatitude [sizeof("ddmm.mmmm")];
    
    char NS;
    char cLongitude[sizeof("dddmm.mmmm")];
    char EW;
    };
    
    struct GPGGA_Struct GPSFixData; 
    
    void LIQ_On (void) // GPS on
    {
    
     setbit(PORTA, GPSON);
    }
    void LIQ_Off (void) // GPS off
    
    {
     clrbit(PORTA, GPSON);
    }
    
    char GPGGA_Extractor (void) 
    {
    
     char Ret = 0; 
     unsigned int i = 0; // Counter
    
     unsigned int StartPos = 0; 
     unsigned int BucksPos = 0; // '$' counter
    
     
     while (!((ReceiveString0[StartPos] == '$')
     &&(ReceiveString0[StartPos+3] == 'G')
     &&(ReceiveString0[StartPos+4] == 'G')
     &&(ReceiveString0[StartPos+5] == 'A'))
     && StartPos < strlen(ReceiveString0) - sizeof("$GPGGA,,,,,,,,,,,,,*XX"))
    
     StartPos++;
    
     for (i = 0; ((i <= strlen(ReceiveString0) - StartPos) && (BucksPos < 2)); i++)
     {
    
     ReceiveString0[i] = ReceiveString0[i + StartPos];
     if (ReceiveString0[i + StartPos] == '$')
     {
    
     BucksPos++;
     if (BucksPos == 2) 
     {
     ReceiveString0[i+1] = '\0'; 
     Ret = 1; 
     }
     }
     }
    
     
     return Ret;
    }
    
    char GPGGA_NMEA_Parser (void) 
    {
     char Ret = 0; 
     unsigned int i = 0; // Counter
    
     unsigned int CommaPos = 0; // ',' counter
     unsigned int templen = 0; // 
    
     
     GPSFixData.cUTCTime[0] = '\0';
     GPSFixData.cLatitude[0] = '\0';
    
     GPSFixData.NS = '\0';
     GPSFixData.cLongitude[0] = '\0';
    
     GPSFixData.EW = '\0';
    
     for (i = sizeof("$GPGG"); i <= strlen(ReceiveString0); i++)
     {
    
     if (ReceiveString0[i] == ',')
     CommaPos++;
     else
     switch (CommaPos)
     {
    
     case 1: templen = strlen(GPSFixData.cUTCTime);
    
     GPSFixData.cUTCTime[templen] = ReceiveString0[i];
     GPSFixData.cUTCTime[templen + 1] = '\0'; // Close string
    
     break;
     case 2: templen = strlen(GPSFixData.cLatitude);
    
     GPSFixData.cLatitude[templen] = ReceiveString0[i];
     GPSFixData.cLatitude[templen + 1] = '\0'; // Close string
    
     break;
     case 3: GPSFixData.NS = ReceiveString0[i];
    
     break;
     case 4: templen = strlen(GPSFixData.cLongitude);
    
     GPSFixData.cLongitude[templen] = ReceiveString0[i];
     GPSFixData.cLongitude[templen + 1] = '\0'; // Close string
    
     break;
     case 5: GPSFixData.EW = ReceiveString0[i];
    
     Ret = 1;
     break;
     }
     }
     return Ret;
    }
    
    

    GSMmod.c
    // Working with GSM
    
    #include "GSMmod.h"
    
    unsigned int gsmtemp = 0; 
    
    unsigned int gsmtemp1 = 0;
    
    unsigned char AbRecReadComplete = 0; 
    
    unsigned char GSMStatus = 0; // GSM mmodule state: 0 - off ; 1 - on
    char NewPeriod[sizeof("4294967295")+1];
    
    unsigned char MessageDecoding = 0; 
    
    char AbPass [FIX_PASS_LENGTH+1]; // User password
    
    char AbNumber [MAX_NUM_LENGTH+1]; // User number
    
    char SendingPOWOFF = 0; 
    
    
    void GR_On(void) // GSM on
    {
     setbit(PORTE, GSMON); // Enable power
    
     
     clrbit(PORTE, PON_OFF); // ON signal
     delay_ms(4000);
     setbit(PORTE, PON_OFF);
    
     delay_ms(8000);
    }//GR_On
    
    void GR_Off(void) // GSM off
    {
    
     clrbit(PORTE, GSMON); // Disable power
     
     delay_ms(8000);
    }//GR_Off
    
    
    void GRInit(void)
    {
     if (GSMStatus == 0) 
     {
    
     SetActiveUart(GSM_UART); 
     printf("AT\n\r");
     delay_ms(1000);
    
     StringBuilder1();
    
     if (strstr(ReceiveString1, "OK") != NULL) 
     { 
     SetActiveUart(GSM_UART);
    
     printf("ATE=1\n\r"); // disable echo
     delay_ms(500);
     printf("AT*E2RS232=2\n\r"); // disable hardware control
    
     delay_ms(500);
     printf("AT+CLIP=1\n\r"); 
     delay_ms(500);
    
     printf("AT+CMEE=1\n\r"); 
     delay_ms(500);
     printf("AT+CSDH=1\n\r"); 
     delay_ms(500);
    
     printf("AT+CMGF=1\n\r"); 
     delay_ms(500);
     printf("AT*E2SSN\n\r");
    
     delay_ms(2000);
     
     StringBuilder1();
     if (strstr(ReceiveString1, "ERROR") == NULL) 
     GSMStatus = 1; 
     }//OK
    
     }//GSMStatus
    }//GRInit
    
    char ReadAbRecord(void) // User record read
    {
     char CharCount = 0; 
     char InNumCount = 0; 
     char InRecCount = 0; 
     char QuotesCount = 0; 
     char Ret = 0;
    
     if (GSMStatus == 0) Ret = 0; 
     else
    
     {
     SetActiveUart(GSM_UART);
     printf("AT+CPBR=1\n\r"); 
     delay_ms(2000);
    
     StringBuilder1();
     
     SetActiveUart(DEBUG_UART); 
     printf (">ReceiveString1 = %s\n\r", ReceiveString1); 
     
     if (strstr(ReceiveString1, "OK") != NULL) 
     {
    
     InRecCount = 0;
     AbPass [0] = '\0'; 
    
     for (CharCount = 0; CharCount < strlen(ReceiveString1); CharCount++)
     {
    
     if (QuotesCount == 1)
     {
     if (ReceiveString1[CharCount] != '"')
     {
    
     AbNumber[InNumCount] = ReceiveString1[CharCount]; 
     AbNumber[InNumCount+1] = 0;
     }
    
     InNumCount++;
     }//QuotesCount==1
     
     if (QuotesCount == 3)
     {
     if (ReceiveString1[CharCount] != '"')
     {
    
     AbPass[InRecCount] = ReceiveString1[CharCount]; 
     AbPass[InRecCount+1] = '\0';
    
     if (InRecCount == FIX_PASS_LENGTH - 1) Ret = 1;
     } 
     
     InRecCount++;
     }//QuotesCount==3
    
    
     if (ReceiveString1[CharCount] == '"') 
     QuotesCount++;
     }//for CharCount
     }//OK
    
     }//else (GSMStatus == 0)
     
     return Ret;
    }//ReadAbRecord
    
    void SendMessage (char* MessageStr) 
    {
    
     char ExtUserTemp = 0;
    
     SetActiveUart(DEBUG_UART); 
     printf (">AbNumber = %s\n\r", AbNumber); 
    
     SetActiveUart(GSM_UART);
    
     printf("AT+CMGS=\"%s\"\n\r", AbNumber);
     delay_ms(2000);
     printf("%s%c", MessageStr, CtrlZ);
    
     delay_ms(2000);
    
     StringBuilder1();
    
     SetActiveUart(DEBUG_UART); 
     printf (">ReceiveString1 = %s\n\r", ReceiveString1); 
    
     ExtUserTemp = 0;
    
     
     while ((strstr(ReceiveString1, "OK") == NULL) && (strstr(ReceiveString1, "ERROR") == NULL) && (ExtUserTemp < 20)) 
     {
    
     ExtUserTemp++;
     StringBuilder1();
     
     SetActiveUart(DEBUG_UART); 
     printf (">ReceiveString1 = %s\n\r", ReceiveString1); 
    
     delay_ms(1000);
     }//Ожидание ответа от GR-47
    
    }//SendMessage
    
    void SMS_Rec_SM(void) // Receiving SMS from SM
    {
     char CharCount = 0; 
     char StarCount = 0; 
     char InNumCount = 0; 
     char Erase = 0;
    
     MessageDecoding = 0;
    
     if (AbRecReadComplete) 
     { 
     SetActiveUart(GSM_UART);
    
     printf("AT+CPMS=\"SM\",\"SM\",\"SM\"\n\r"); 
     delay_ms(1000);
     StringBuilder1();
     
     SetActiveUart(DEBUG_UART); 
     printf (">ReceiveString1 = %s\n\r", ReceiveString1); 
     
     if (strstr(ReceiveString1, "+CPMS: 0") == NULL) 
     {
    
     SetActiveUart(GSM_UART);
     printf("AT+CMGL=\"REC UNREAD\"\n\r");
     delay_ms(5000);
    
     StringBuilder1();
     
     gsmtemp1 = 0;
     while ((strstr(ReceiveString1, "OK") == NULL) && (strstr(ReceiveString1, "ERROR") == NULL) && (gsmtemp1 < 10)) 
     {
    
     gsmtemp1++;
     StringBuilder1();
     delay_ms(1000);
     }//while
     
     SetActiveUart(DEBUG_UART); 
     printf (">ReceiveString1 = %s\n\r", ReceiveString1); 
    
     if (strstr(ReceiveString1, strcat(AbPass, "*")) != NULL) 
     {
    
     SetActiveUart(DEBUG_UART); 
     printf ("Пароль верен\n\r"); 
     
     for (CharCount = 0; CharCount < strlen(ReceiveString1); CharCount++)
     {
    
     if (StarCount == 1)
     {
     if (ReceiveString1[CharCount] != '*')
     {
    
     MessageDecoding = 1;
     NewPeriod[InNumCount] = ReceiveString1[CharCount]; 
     NewPeriod[InNumCount+1] = 0;
     }
    
     InNumCount++;
     }//StarCount==1
     
     if (ReceiveString1[CharCount] == '*') 
     StarCount++;
     }//for CharCount
    
     }
    
     SetActiveUart(GSM_UART);
     
     for (Erase = 0; Erase <= 30; Erase++)
     {
    
     printf("AT+CMGD = %d\n\r", Erase); 
     delay_ms(1000);
     }
     StringBuilder1(); 
     delay_ms(7000);
    
     StringBuilder1(); 
     }
     }//AbRecReadComplete
    }//SMS_Rec_SM
    
    void SMS_Rec_ME(void) // Receiving SMS from ME
    {
    
     char CharCount = 0; 
     char StarCount = 0; 
     char InNumCount = 0; 
     char Erase = 0;
    
     MessageDecoding = 0;
    
     if (AbRecReadComplete) 
     { 
     SetActiveUart(GSM_UART);
    
     printf("AT+CPMS=\"ME\",\"ME\",\"ME\"\n\r");
     delay_ms(1000);
     StringBuilder1();
     
     SetActiveUart(DEBUG_UART); 
     printf (">ReceiveString1 = %s\n\r", ReceiveString1); 
     
     if (strstr(ReceiveString1, "+CPMS: 0") == NULL) 
     {
    
     SetActiveUart(GSM_UART);
     printf("AT+CMGL=\"REC UNREAD\"\n\r");
     delay_ms(5000);
    
     StringBuilder1();
     
     gsmtemp1 = 0;
     while ((strstr(ReceiveString1, "OK") == NULL) && (strstr(ReceiveString1, "ERROR") == NULL) && (gsmtemp1 < 10)) 
     {
    
     gsmtemp1++;
     StringBuilder1();
     delay_ms(1000);
     }//while
     
     SetActiveUart(DEBUG_UART); 
     printf (">ReceiveString1 = %s\n\r", ReceiveString1); 
    
     if (strstr(ReceiveString1, strcat(AbPass, "*")) != NULL) 
     {
    
     SetActiveUart(DEBUG_UART); 
     printf ("Пароль верен\n\r"); 
     
     for (CharCount = 0; CharCount < strlen(ReceiveString1); CharCount++)
     {
    
     if (StarCount == 1)
     {
     if (ReceiveString1[CharCount] != '*')
     {
    
     MessageDecoding = 1;
     NewPeriod[InNumCount] = ReceiveString1[CharCount]; 
     NewPeriod[InNumCount+1] = 0;
     }
    
     InNumCount++;
     }//StarCount==1
     
     if (ReceiveString1[CharCount] == '*') 
     StarCount++;
     }//for CharCount
    
     }
    
     SetActiveUart(GSM_UART);
     
     for (Erase = 0; Erase <= 50; Erase++)
     {
    
     printf("AT+CMGD = %d\n\r", Erase); 
     delay_ms(1000);
     }
     StringBuilder1(); 
     delay_ms(7000);
    
     StringBuilder1(); 
     }
     }//AbRecReadComplete
    }//SMS_Rec_ME
    

    main.c
    #include "main.h"
    
    char temp = 0;
    
    char tempStr[2];
    
    struct GPGGA_Struct
    {
     char cUTCTime [sizeof("hhmmss.ss")]; 
     char cLatitude [sizeof("ddmm.mmmm")]; 
     char NS; 
     char cLongitude[sizeof("dddmm.mmmm")]; 
     char EW; 
    };
    
    extern struct GPGGA_Struct GPSFixData; 
    
    extern unsigned char AbRecReadComplete; 
    extern unsigned char GSMStatus; 
    
    
    extern char NewPeriod[sizeof("4294967295")+1]; 
    extern unsigned char MessageDecoding; 
    
    
    extern char AbPass [FIX_PASS_LENGTH+1]; 
    extern char AbNumber [MAX_NUM_LENGTH+1]; 
    
    
    unsigned int SleepSecondsCount = 0; 
    
    char ExtractSucc = 0; 
    
    char ParserSucc = 0; 
    
    #define STR_TO_SEND_LENGTH 250
    char StringToSend[STR_TO_SEND_LENGTH]; 
    
    
    unsigned int CurrentADCRead = 0; 
    float Vbat = 0; 
    
    char StrVbat[7] = ""; 
    #define BatChannel 1 // ADC channel for acc
    
    void main (void)
    {
    
     PortInit(); 
     UARTinit(); 
     EEPROMInit(); 
     TimerInit(); 
     InitADC(); 
     
     asm ("sei"); 
     
     SetActiveUart(DEBUG_UART); 
     printf ("\n\r>2G DEBUG START\n\r"); 
    
     AbRecReadComplete = 0; 
     
     while (1) 
     {
    
     GSMStatus = 0; 
     ExtractSucc = 0; 
     ParserSucc = 0; 
     StringToSend[0] = '\0'; 
     
     LIQ_On(); 
     
     while (GSMStatus == 0) 
     {
    
     GR_On(); 
     GRInit(); 
     }
    
     #define READATT 30 
     temp = 0;
    
     while ((temp <= READATT) && (AbRecReadComplete == 0)) 
     {
    
     AbRecReadComplete = ReadAbRecord(); 
     temp++;
     }
     
     SetActiveUart(DEBUG_UART); 
     printf (">90 sec wait"); 
     
     #define TIME_TO_COLD_START 90 
    
     for (SleepSecondsCount = 0; SleepSecondsCount <= TIME_TO_COLD_START; SleepSecondsCount++) 
     {
    
     delay_ms(1000); 
     SetActiveUart(DEBUG_UART); 
     printf ("."); 
     }
    
     
     SetActiveUart(DEBUG_UART); 
     printf ("\n\r>90 sec wait finished.\n\r"); 
    
     SMS_Rec_SM(); 
     if (MessageDecoding == 0)
    
     SMS_Rec_ME(); 
    
     AbRecReadComplete = ReadAbRecord(); 
    
     if (MessageDecoding == 1) 
     {
    
     eeWaitState = StrToULong(NewPeriod); 
     strcat (StringToSend, "New Period = ");
    
     strcat (StringToSend, NewPeriod);
     strcat (StringToSend, " minute(s). ");
     }
    
     SetActiveUart(DEBUG_UART); 
     printf(">eeWaitState = %d\n\r", eeWaitState); 
     
     #define READGPS 30 
     temp = 0;
    
     while ((temp <= READGPS) && ((ExtractSucc == 0) || (ParserSucc == 0))) 
     {
    
     delay_ms(1000);
     
     StringBuilder0();
     
     SetActiveUart(DEBUG_UART);
     printf(">ReceiveString0 = %s\n\r", ReceiveString0);
    
     ExtractSucc = GPGGA_Extractor();
     
     SetActiveUart(DEBUG_UART);
     printf(">ExtractSucc = %d\n\r", ExtractSucc);
    
     if (ExtractSucc == 1)
     {
     SetActiveUart(DEBUG_UART);
    
     printf(">Extract = %s\n\r", ReceiveString0);
     ParserSucc = GPGGA_NMEA_Parser();
    
     SetActiveUart(DEBUG_UART);
     printf(">ParserSucc = %d\n\r", ParserSucc);
    
     
     if (ParserSucc == 1)
     {
     SetActiveUart(DEBUG_UART);
    
     printf(">cUTCTime = %s\n\r",GPSFixData.cUTCTime);
     printf(">cLatitude = %s\n\r",GPSFixData.cLatitude);
    
     printf(">NS = %c\n\r",GPSFixData.NS);
     printf(">cLongitude = %s\n\r",GPSFixData.cLongitude);
    
     printf(">EW = %c\n\r",GPSFixData.EW);
     }
     }
     
     temp++;
     }
    
     if (ExtractSucc == 0)
    
     strcat(StringToSend, "Error: GPS module broken. ");
     else
     if (ParserSucc == 0)
    
     strcat(StringToSend, "Error: GPS satellites unavailable. ");
     else
     {
     strcat(StringToSend, "GPS Data: time ");
    
     strcat(StringToSend, GPSFixData.cUTCTime);
     strcat(StringToSend, ", latitude ");
    
     tempStr[0] = GPSFixData.NS;
     tempStr[1] = '\0';
    
     strcat(StringToSend, tempStr);
     strcat(StringToSend, GPSFixData.cLatitude);
    
     strcat(StringToSend, ", longitude ");
     tempStr[0] = GPSFixData.EW;
    
     tempStr[1] = '\0';
     strcat(StringToSend, tempStr);
    
     strcat(StringToSend, GPSFixData.cLongitude); 
     strcat(StringToSend, ".");
     }
    
     
     CurrentADCRead = ReadADC(BatChannel); 
     #define VCoeff 64.0 
     Vbat = CurrentADCRead / VCoeff; 
    
     sprintf(StrVbat, "%.3f", Vbat); 
     strcat(StringToSend, " Info: Vbat = "); 
     strcat(StringToSend, StrVbat);
    
     strcat(StringToSend, " V."); 
     
     SetActiveUart(DEBUG_UART); 
     printf(">Vbat = %.3f V\n\r", Vbat); 
    
     SendMessage (StringToSend); 
     
     SetActiveUart(DEBUG_UART); 
     printf(">SendMessage %s\n\r", StringToSend); 
    
     LIQ_On(); 
     GR_Off(); 
     
     for (SleepSecondsCount = 0; SleepSecondsCount <= eeWaitState*60; SleepSecondsCount++) 
     {
    
     delay_ms(1000); // !!! Заглушка на случай отсутствия кварцевого резонатора на ATC ATmega128
    // MCUCR = (1<<SM0) | (1<<SM1); // Power-save
    // setbit (MCUCR, SE); 
    // asm ("sleep"); 
     }
     
     SetActiveUart(DEBUG_UART); 
     printf (">eeWaitState minutes finished.\n\r");
     }//while (1)
    
    }//main
    

    portdefs.c
    #include "portdefs.h"
    
    void PortInit(void) 
    {
    
     // Port A
     DDRA = (1<<GPSON);
     PORTA = 0;
    
     // Port D
     DDRD = (1<<TXD1) | (1<<TXD2);
     PORTD = 0;
    
     // Port E
     DDRE = (1<<PDTR) | (1<<PON_OFF) | (1<<TXD0) | (1<<GSMON) | (1<<PRTS);
    
     PORTE = 0;
    }
    

    sys.c
    #include "sys.h"
    #include <stdio.h> 
    #include <ctype.h> 
    
    #include <string.h> 
    
    unsigned int StrToULong (char* InputStr) 
    {
     unsigned int ReturnValue = 0;
    
     unsigned char StrCount = 0;
    
     for (; StrCount <= strlen(InputStr); StrCount++)
    
     if (isdigit(InputStr[StrCount]))
     ReturnValue = 10*ReturnValue + (InputStr[StrCount] - 48);
    
     return ReturnValue;
    }//StrToULong
    

    timer.c
    #include "timer.h"
    #include "uart.h" 
    
    volatile unsigned long tmTicks; 
    
    volatile unsigned long tmSeconds; // Second counter
    volatile unsigned long tmMinutes; // Minute counter
    
    
    extern char SleepModeOn; // 0 - hybernate, 1 - work
    extern volatile unsigned int TimeToSleep_Minutes; // hybernate time 
    
    
    void TimerInit(void) 
    {
     tmTicks = 0; 
     tmSeconds = 0; // Second counter
    
     tmMinutes = 0; // Minute counter
    
     TCCR0 = (1<<CS00) | (1<<CS02); // divider = 128
    
     TIMSK = 1<<TOIE0; 
     ASSR = 1<<AS0; // 32768 Hz quartz
    
    }
    
    #pragma vector = TIMER0_OVF_vect 
    __interrupt void TIMER0_OVF_Interrupt (void) 
    {
     #define TM_SECOND 128 // 1 second
     #define TM_MINUTE 60 // 1 minute
    
    
     clrbit (MCUCR, SE); // hybernate off
    
     tmSeconds++; 
     SetActiveUart(DEBUG_UART); 
     printf ("+"); 
     
     if (tmSeconds % TM_MINUTE == 0) 
     {
    
     tmMinutes++;
     SetActiveUart(DEBUG_UART); 
     printf ("\n\r###\n\r"); 
     }
    }
    

    uart.c
    #include "uart.h"
    
    // UART0
    volatile char RxBuffer0[RXBUFLENGTH_0]; 
    
    volatile char RxBufWritePoint0; 
    char RxBufReadPoint0; 
    char ReceiveString0[RXBUFLENGTH_0]; 
    
    
    // UART1
    volatile char RxBuffer1[RXBUFLENGTH_1]; 
    volatile char RxBufWritePoint1; 
    
    char RxBufReadPoint1; 
    char ReceiveString1[RXBUFLENGTH_1]; 
    
    // UART2 (программный)
    int SwUartTXData;
    
    char SwUartTXBitCount;
    
    void UARTinit(void) 
    {
     // Init USART0: 4800 (for NMEA), 8 Data, 1 Stop, No Parity; RX interrupt ON
     UCSR0A = 0x00;
    
     UCSR0B = 0x98;
     UCSR0C = 0x06;
     UBRR0H = 0x00;
    
     UBRR0L = 0x33;
     
     // Init USART1: 9600, 8 Data, 1 Stop, No Parity; U2X = 1; RX interrupt ON
     UCSR1A = 0x00;
    
     UCSR1B = 0x98;
     UCSR1C = 0x06;
     UBRR1H = 0x00;
    
     UBRR1L = 0x19;
     
     RxBufWritePoint0 = 0;
     RxBufReadPoint0 = 0;
    
     ReceiveString0[0] = 0;
     ClearRxBuffer0();
    
     RxBufWritePoint1 = 0;
    
     RxBufReadPoint1 = 0;
     ReceiveString1[0] = 0;
    
     ClearRxBuffer1();
    }
    
    void SetActiveUart (t_uarts u) 
    {
     g_currentUsart = u;
    }//SetActiveUart
    
    
    int putchar( int data ) 
    {
     if (g_currentUsart == UART_0) 
     {
    
     while ( !( UCSR0A & DATA_REGISTER_EMPTY_0));
     UDR0 = data;
     }//UART0
    
    
     if (g_currentUsart == UART_1)
     {
     while ( !( UCSR1A & DATA_REGISTER_EMPTY_1));
    
     UDR1 = data;
     }//UART_1
    
     if (g_currentUsart == UART_2) 
     {
    
     SwUartTXData = data;
     SwUartTXBitCount = 0;
     
     clrbit(TRXPORT, TXD2); // start bit
    
     delay_us(BR4800UC);
     
     while(SwUartTXBitCount < 8)
     {
     if( SwUartTXData & 0x01 )
    
     setbit(TRXPORT, TXD2);
     else
     clrbit(TRXPORT, TXD2);
    
     delay_us(BR4800UC);
    
     SwUartTXData = SwUartTXData >> 1;
    
     SwUartTXBitCount++;
     }
     
     setbit(TRXPORT, TXD2); // stop bit 
     }//UART_2
    
    
     delay_ms(TXDELAY);
     return data;
    }//putchar
    
    void OutText (char const *text) 
    {
    
     while (*text)
     if (*text == '\n')
     {
    
     putchar('\n');
     putchar('\r');
     text++;
     }
     else putchar ( *text++ );
    }
    
    void OutDat (unsigned int val, unsigned char len, unsigned char Const) 
    {
    
     unsigned char Str[16];
     unsigned char k = 0;
    
     
     for(k = 0; k < len; k++)
     {
     *(Str + (len-k-1)) = (val%Const) + '0';
    
     
     if(*(Str+(len-k-1))>'9')
     *(Str+(len-k-1)) += 'A'-'0'-10;
    
     val /= Const;
     }
     
     Str[len] = 0;
    
     k = 0;
     while(Str[k] != 0)
    
     putchar(Str[k++]);
    }
    
    void ClearRxBuffer0(void) 
    {
     unsigned int ClearPoint = 0;
    
     for (; ClearPoint++ < RXBUFLENGTH_0; RxBuffer0[ClearPoint] = 0);
    
     RxBufReadPoint0 = 0;
     RxBufWritePoint0 = 0;
    }//ClearRxBuffer0
    
    
    void ClearRxBuffer1(void) 
    {
     unsigned int ClearPoint = 0;
    
     for (; ClearPoint++ < RXBUFLENGTH_1; RxBuffer1[ClearPoint] = 0);
    
     RxBufReadPoint1 = 0;
     RxBufWritePoint1 = 0;
    }//ClearRxBuffer0
    
    
    void StringBuilder0(void) 
    {
     signed char IncomChar = 0;
    
     int StringPoint = 0;
     ReceiveString0[0] = 0; 
    
     while ((IncomChar = ReadData0()) != -1) 
     {
    
     if (IncomChar > 20) 
     {
     ReceiveString0[StringPoint] = IncomChar;
    
     ReceiveString0[StringPoint+1] = 0; 
     StringPoint++;
     }
     }
    }//StringBuilder0
    
    void StringBuilder1(void) 
    {
    
     signed char IncomChar = 0;
     int StringPoint = 0;
    
     ReceiveString1[0] = 0;
     
     while ((IncomChar = ReadData1()) != -1) 
     {
    
     if (IncomChar > 20) 
     {
     ReceiveString1[StringPoint] = IncomChar;
    
     ReceiveString1[StringPoint+1] = 0; 
     StringPoint++;
     }
     }
    }//StringBuilder1
    
    signed char ReadData0(void)
    {
    
     char data;
    
     if (RxBufWritePoint0 == RxBufReadPoint0) return -1; 
    
     data = RxBuffer0[RxBufReadPoint0];
    
     if(RxBufReadPoint0 == RXBUFLENGTH_0-1) RxBufReadPoint0 = 0;
    
     else RxBufReadPoint0++;
     
     return data;
    }//ReadData0
    
    signed char ReadData1(void)
    {
    
     char data;
    
     if (RxBufWritePoint1 == RxBufReadPoint1) return -1; 
    
     data = RxBuffer1[RxBufReadPoint1];
    
     if(RxBufReadPoint1 == RXBUFLENGTH_1-1) RxBufReadPoint1 = 0;
    
     else RxBufReadPoint1++;
     
     return data;
    }//ReadData1
    
    #pragma vector = USART0_RXC_vect 
    __interrupt void USART0_RXC_Interrupt(void)
    {
    
     RxBuffer0[RxBufWritePoint0] = UDR0;
     if (RxBufWritePoint0 == RXBUFLENGTH_0-1)
    
     RxBufWritePoint0 = 0;
     else RxBufWritePoint0++;
    }//USART0_RXC_Interrupt
    
    #pragma vector = USART1_RXC_vect 
    __interrupt void USART1_RXC_Interrupt(void)
    {
    
     RxBuffer1[RxBufWritePoint1] = UDR1;
     if (RxBufWritePoint1 == RXBUFLENGTH_1-1)
    
     RxBufWritePoint1 = 0;
     else RxBufWritePoint1++;
    }//USART1_RXC_Interrupt
    
     

      Contact us: inbox@smasma.com © Smart&Small Webmaster: webmaster@smasma.com