Showing posts with label Microcontroller Based Project. Show all posts
Showing posts with label Microcontroller Based Project. Show all posts

Tuesday, July 14, 2015

A Digital Thermometer Using the AT89C2051 Microcontroller

Introduction:
The system presented in this application note implements a simple digital thermometer that includes a built-in LCD and RS-485 communicat ion por t . I t is designed around Atmel’s AT89C2051 processor, a DS1620 digital thermometer/ thermostat from Dallas Semiconductor, a small 8 X 2 LED backlit LCD, and an RS485 line interface. The system, shown in Figure 1, can be used as the basis for developing custom solutions for networked and stand alone data collection and control equipment. It can be centrally powered due to its low current requirement and its small size allows it tobe placed almost anywhere.

Software:
The LCD driver is written entirely in C and compiles under Micro-C (from Dunfield Development Systems) using the tiny memory model. Although a canonical stack-based implementation, Micro-C includes a number of special features that make it quite suitable for generating ROMable code for small systems. The overhead incurred performing stack manipulations is made up by the library functions that are all hand coded in an highly optimized assembler. As an added benefit, Micro-C comes with fully documented library source code so special modifications can be made as circumstances arise. 

The first few functions contained in the LCD driver module are conventional C library implementations. PutString displays a null terminated string by merely passing characters off to PutChar until a null byte is encountered. PutChar outputs a character at a time to the LCD and handles the newline character by advancing the cursor to the beginning of the next line. 

PositionLcd simply sets the cursor address to the value specified by the caller. The ClearLcd function is used to clear the entire LCD and home the cursor. 

The functions that follow concern themselves with actual physical communication to the LCD. Since there is not a direct correspondence between the LSI’s data RAM and the LCD’s physical mapping, it is necessary to keep watch for certain boundary conditions. When a boundary is encountered, the cursor must be repositioned in order to keep the output contiguous. Since all displayable data must pass through DataWr, it makes sense to contain the corrective actions here. To handle this problem, keep track of the logical cursor position and invoke a remedial maneuver whenever discontinuity may occur. 

There are two ways you can accomplish this: 
• Read the LCD’s status register (to get the cursor address) or
• Keep a local copy just for your reference.

Not wanting to waste a pin to control the LCD’s read/write line (it runs in write-only mode), the latter approach was adopted. Here, the global register (IRAM actually) variable Cursor is used for this purpose. Cursor is consulted prior to any data write operation. If a correction is necessary, a new cursor address is generated and dispatched to the LCD control register via CommandWr. 

Following this, DataWr splits the data byte into nibbles (remember the LCD operates using a 4-bit bus) and falls through to handle the actual physical transfer. Using Micro-C’s extended preprocessor allows bit manipulation macros that expand directly to 8051 SETB and CLR instructions. Here, clearing DRS selects the LCD’s data register and DEN is toggled to generate the data strobe. CommandWr operates similarly but does not have to deal with any cursor entanglements. It selects the command register as its destination by setting DRS high prior to clocking the nibbles across the interface. 

The initialization function InitLcd begins at a more rudimentary nibble oriented level since no assumption can be made as to the operational status of the LCD at this time. The first three sequences ensure that the transfer mode is set to operate over a 4-bit bus. Repeating the sequence three times ensures that the command will be recognized regardless of the operational mode of the LSI. (It is wise to make no assumptions when performing any low-level initialization.) Following this, the actual operating parameters are transferred to the LCD using the standard CommandWr function.

Digital Temperature:
Temperature acquisition is handled using the DS1620 thermometer/thermostat IC from Dallas Semiconductor. The DS1620 contains all temperature measurement and signal conditioning circuitry on-chip and presents the processor with a 3-wire digital interface composed of a bi-directional data line DQ, a reset input \RST, and a clock input CLK. The temperature reading is provided in a 9 bit, two’s complement format. The measurement range spans from -55°C to +125°C in .5°C increments.

Data transfers into and out of the DS1620 are initiated by driving \RST high. Once the DS1620’s reset is released, a series of clock pulses is emitted by the processor to actually transfer the data. For transmission to the DS1620, data must be valid during the rising edge of the clock pulse. Data bits received by the processor are output on the falling edge of the clock and remain valid through the rising edge. Taking the clock high results in DQ assuming a high impedance state. The sequence can be immediately terminated by pulling \RST low which forces DQ into a high impedance state and concludes the transfer. Temperature data is transmitted over the 3-wire bus in lsb first format. A total of nine bits are transmitted where the most significant bit is the sign bit. If all nine bits are not of interest, the transfer can be terminated at any time by asserting \RST. The DS1620 support routines are coded in assembly. 

The DS1620 also has nonvolatile EEPROM configuration registers that hold thermostatic and operational control information. TempConfig is hardcoded to set the mode for operation under CPU control and continuous temperature conversion. Once in continuous conversion mode, the actual conversion process is started by issuing the start conversion command through TempConvert. Now the DS1620 can be read at any time and the last temperature conversion that was performed will be returned. This is accomplished by calling TempRead. The result is returned in the 16 bit accumulator as defined by Micro-C consisting of the B (msb) and ACC (lsb) registers.


Step 1: Circuit Diagram

Step 2: Program (C Code)
Download Code

Step 3: Share Like and Follow us on Google+, Facebook






Simplest Frequency Meter

This is simplest frequency meter (counter) with Atmel AVR ATtiny2313. It allows to measure frequencies up to 10 MHz in 4 automatically selected ranges. Lowest range has a resolution of 1Hz. A 4-digit LED display is used to display the measured frequency. It is based on a Atmel AVR ATtiny2313A or ATTiny2313 microcontroller. The program to download you can find below. The microprocessor is clocked from 20MHz crystal (the maximum allowable frequency). The measurement accuracy is determined by the accuracy of this crystal. Minimum length of half period of the measured signal must be greater than the period of the crystal oscillator (restriction of the MCU architecture). At 50% duty cycle can therefore you can measure frequencies up to 10 MHz. The measured signal is input to pin 9 (T1). Counting is performed by a 16-bit timer/counter1, which is externally clocked. Overflow increments a 8-bit register, making in a 24-bit result. It is then transferred to the decadic form and displayed. The frequency is always displayed in kHz. Automatic range selection changes the position of the decimal point. The refresh rate is 1Hz. Cathodes of displays are connected to port B, the anodes to bits 0-3 of port D. Using the superbright display allows you to omit usual current amplifying transistors. The display is controlled in multiplex and connected the usual multiplex manner. Frequency of the multiplex is 156.25 Hz. You can use the display for example CA56-12SRWA. Resistors R1 to R8 determine the current into the display and thus its brightness. They are chosen so that the current does not exceed the maximum output current (40 mA) of pins. This frequency meter is powered from a supply of approximately 5V (+/- 10%). Consumption at 5V is about 15-35 mA, depending on the number of lit segments (most current consumption has the LED display). If the input of the frequency counter is "in free air", it can cause display meaningless values ​​because the input impedance is very high. You can prevent this by adding a resistor around 100k in parallel to the input. 

Step 1: Circuit Diagram
Add 1KOhm Resistors in series with display segments to limit the current.

ATmega Frequency Meter

Step 2: Code
Download Hex File

Step 3: Fuse Bits Settings
1. External Crystal
2. CLKDIV8 Unprogrammed(1)

Saturday, June 13, 2015

TV Based Oscilloscope

This single project turns your TV into a digital storage oscilloscope with a sampling rate 160 Ksps. As a bonus, five voltages on the five analog inputs can be monitored.

No additional video-controller and video-RAM are required. Main goal of this project is an implementation of a high-resolution (512x240 pixels) video generator inside the ARM microcontroller, LPC2138. This video generator operates in the background mode, with a low CPU loading that allows the other high-intensive tasks (such as the data collection and visualization) be run. 

Both of standards, American NTSC and Europe PAL are supported.

There are many solutions, as a sound card, as USB standalone devices based that turns you PC into digital oscilloscope. All of them have a significant disadvantage – when you use PC simultaneously for measurement and for other needs, you are compelled to click a mouse again and again to switch between applications. Of course, you may put a dedicated notebook, but the other disadvantages such as a continuous operating system loading and control by the mouse are remain.

Those devices require the PC with all peripherals, my device just the TV. It’s ready to be used immediately after the powering on. Note that the implementation of scope was not my main task, it was just a good way to demonstrate the operation of video-controller that has been implemented using the capabilities of the Philips’ chip LPC2138.



Step 1: Circuit Diagram and Connections
Step 2: Download Required Files
Download Code
Download Working Details



LPC2148 based water quality monitoring system

This project is based on LPC2148 ARM 7 Microcontroller, It measures pH, Flow, and temperature of water. It logs data on computer and shows real time readings on 16x2 LCD, It uses BlueBoard-LPC2148
BlueBoard-LPC214x






BlueBoard is a cost effective prototyping and solutions using the versatile LPC214x series of microcontrollers. It is a ready-to-run development platform with code snippets to demonstrate applications for every feature supported on the board. The BlueBoard offers ubiquitous interfaces making it the best board available for the offered price.
The LPC2148 microcontroller has 512KB of internal flash and 32+8K RAM, can be clocked up to 60Mhz. LPC2148 features include USB 2.0 device, 2xUARTs, RTC, 2x10bit ADCs each ADC has multiple channels, 1xDAC, 6XPWM, 2xI2C, 1xSPI, 1XSSP, 2x32-bit TIMERS, FAST I/0 support and WDT. LPC2148 also supports In System Programming (ISP)VB6 Software for Data logging

Step 1: Circuit Diagram and Connections
1. Flow sensor connections goes to EINT1 (Pin 45 of LPC2148).
2. Temperature Sensor to ADC Channel (AD0.3 Pin 15 of LPC2148).
3. pH sensor RX, TX lines are connected to USART1 (TTL Rx Tx).
4. UART0 is used for sending data to PC for data logging.

Step 2: Download Required Files
In this project we are directly connecting these sensor to blueboard so no need of separate PCB or Circuit Diagram
1. Circuit Diagram of Blueboard
2. pH Sensor Protocol and Details

Step 3: Download Source Code
1. Download VB6 Software for DataLogging
2. Download LPC2148 Project Code File

Kiel ARM  Code (main.c):


/* blog.circuits4you.com */

#include <stdio.h>
#include <LPC214x.H>                       /* LPC214x definitions */
#include "lcd.h"
#include "adc.h"
#include "uart.h"
#include "irq.h"

extern char pH[5];
int volatile EINT2   =   0;
void ExtInt_Serve2(void)__irq;
void ExtInt_Init2(void);
int Flow;

/*******************************************************************
  Function Name : wait()
  Description :This function suspends the tasks for specified ticks. 
  Input :  ticks:no of ticks in multiple of 1 usec
            task: task to be suspended
*******************************************************************/

void wait(int count)
{
  int j=0,i=0;

  for(j=0;j<count;j++)
  {
    /* At 60Mhz, the below loop introduces
    delay of 10 us */
    for(i=0;i<35;i++);
  }
}

/******************************************************************
 Function Name : process_adc()
 Description :
Reads ADC data LM35, then displays it on LCD
******************************************************************/
void process_adc(void)
{
 unsigned short adc_value = 0;
   unsigned char buf[16] = {0};


  lcd_putstring(LINE1,"pH:");

  lcd_putchar(pH[0]);lcd_putchar(pH[1]);  
  lcd_putchar(pH[2]);lcd_putchar(pH[3]);lcd_putchar(pH[4]);

  adc_value = adc_read(ADC0, CHANNEL_3); 
  adc_value=adc_value/3.1;
  sprintf((char *)buf, " T:%02d ", adc_value);
  //lcd_putstring(LINE2, (char *)buf);  
  lcd_putchar(buf[0]);
  lcd_putchar(buf[1]);
  lcd_putchar(buf[2]);
  lcd_putchar(buf[3]);
  lcd_putchar(buf[4]);
  lcd_putchar(0xDF);
  lcd_putchar('C');
 sprintf((char *)buf, "Flow:%d ", Flow); 
 lcd_putstring(LINE2, (char *)buf);
 lcd_putchar('L');lcd_putchar('/');lcd_putchar('H');lcd_putchar('r');

//Send data to PC through uart0
  uart0_putc('(');
  uart0_putc(pH[0]);uart0_putc(pH[1]);  
  uart0_putc(pH[2]);uart0_putc(pH[3]);uart0_putc(pH[4]);
  uart0_putc(',');

  sprintf((char *)buf, "%04d", Flow);  
  uart0_puts((char *)buf);
  uart0_putc(',');
  sprintf((char *)buf, "%02d",adc_value);
  uart0_puts((char *)buf);
  uart0_putc(')');
}

/*******************************************************
 Function Name : main()
*********************************************************/
int main (void) 
{
  init_adc0();      // Initialize ADC
  init_lcd();      // Initialize LCD
  wait(100000);

 init_VIC();      //Interrupt enable
 ExtInt_Init2();     //Enable Flow Sensor Intterupt 
    UARTInit1(38400);    //UART1 init
 UARTInit(38400);    //UART0 init

 wait(100000);
 
 uart1_puts("C");    //pH Sensor Command "C+CR" to continuously send pH data 
 uart1_putc(13);     //at baud rate of 38400
 

  lcd_clear();      // clear display
  while(1)
  {
    process_adc();     // Raed ADC value and display it on first line of LCD
    wait(50000);
 //Measure Flow
 EINT2 = 0;                      //Set NbTops to 0 ready for calculations
  VICIntEnable |= 1<<16;           //Enables interrupts
  wait(100000);                    //Wait 1 second
  VICIntEnable &= ~(1<<16);           //Disable interrupts
  Flow = ((EINT2 * 60) / 7.5);       //(Pulse frequency x 60) / 7.5Q = flow rate in L/hour 
  }
}

void ExtInt_Serve2(void)__irq 
{    
 EINT2++;    
 EXTINT |= 4;    
 VICVectAddr = 0; 
}

void ExtInt_Init2(void) 
{      
 EXTMODE |= 4;         //Edge sensitive mode on EINT2    
 EXTPOLAR = 0;           //Falling Edge Sensitive    
 PINSEL0 |= 0x80000000; //Enable EINT2 on P0.15    
 VICVectCntl1 = 0x20 | 16; // 16 is index of EINT2    
 VICVectAddr1 = (unsigned int) ExtInt_Serve2;    
// VICIntEnable |= 1<<16;   //Enable EINT2   
} 


Step 4: Testing and Debuging
1. Give power supply to respective sensors from BlueBoard, LPC2148 is +5V Compatible.
2. Flow Sensor is common hall effect type sensor, gives pulses as output
3. Temperature Sensor is LM35
4. Follow us on Google+, Facebook
5. Comment if you find any difficulty.








Friday, June 12, 2015

Token number display system using microcontroller

Bank token number display project is build using ATmega8 Microcontroller and ULN2003 for driving large LED display, PCB layout, Circuit diagram are self explanatory. It is capable to display three digits, its simple project using microcontroller.

Token issue systems are ideal for banks, airports, public dealing offices, hospitals,doctor’s clinics, restaurants and other such places where people have to wait in line for their turn. These systems allow customers to wait without having to stand in line, oncetheir number is displayed then only will they have to get in line for their turn. No need tomake your customers stand in long queues in sunlight or rain and wasting time. Justdistribute tokens on first come first serve basis and as soon as any counter is ready to provide services the person there just has to press one push switch to show the tokennumber in displays. All models are easy to install, operate & maintain. Any ordinaryelectrician can do the installation quickly.“A digital token is issued by cashier in bank to display the token number (etched on thetokens) which is issued to the customers against cheques for facilitating cash withdrawnfrom the bank. The cashier operates the display to show the token number against whichhe is ready to make the payment. The customer having the displayed token number canreport to the cashier’s counter to receive the payment. In this way payment is madesystematically to all desire customers without letting chaos to be created at the paymentwindow. In this way the device serve the purpose of improving the performance of the banking system.



Token Number Display Circuit Diagram

Step 1: Components Required
1. Atmega 8 Microcontroller
2. LM7805
3. 1000uf/16V Capacitor
4. LEDs
5. BC558
6. 100,10K Ohm Resistors
7. Tectile Switches

Step 2: Circuit Design and PCB Manufacturing
Download Requires Files
1. Try Simulation Click Here to Download Proteus Simulation File
2. Download pdf PCB Layout


PCB Layout
3. Assemble components on PCB
Step 3: Programming the controller
Download Hex File

AVR Studio C Code

/***************************************************************/
/*                     Token Number Display                    */
/*                     blog.circuits4you.com                   */
/***************************************************************/
#include <avr/io.h>
#include <string.h>
#include <avr/interrupt.h>

//Segment Connection
#define D0   PC1
#define D1   PB1
#define D2   PB2
#define D3   PB3
#define D4   PB4
#define D5   PC0
#define D6   PB5


//Scan Line Connection
#define S1  PC3
#define S2  PC4
#define S3  PC5

//Keypad Connections
#define W0  PD3
#define W1  PD6
#define W2  PD4
#define W3  PD2

#define W4  PB0
#define W5  PB7

#define W6  PD1
#define W7  PD7

#define W8  PB6

#define W9  PD0

//Declaration
void Display(char f);
void delay();
char dat[3];

/***************************************************************/
/*                     Main                                    */
/***************************************************************/
int main(void)
{

 DDRB = 0b00111110; 
 DDRD = 0b00000000;
 DDRC = 0b00111011;

 PORTB = 0b00000000; 
 PORTC = 0b00111000;
 
 dat[0]=0x00;
 dat[1]=0x00;
 dat[2]=0x00;

 while(1)
 {

  if((PIND & 0x08) == 0x08)
  {
   delay();
   if((PIND & 0x08)==0x08)
   {
    dat[0]=dat[1];
    dat[1]=dat[2];
    dat[2]=0x00;
    while((PIND & 0x08)==0x08); 
   }
  }
  if((PIND & 0x40) == 0x40)
  {
   delay();
   if((PIND & 0x40)==0x40)
   {
    dat[0]=dat[1];
    dat[1]=dat[2];
    dat[2]=0x01;
    while((PIND & 0x40)==0x40); 
   }
  }

  if((PIND & 0x10) == 0x10)
  {
   delay();
   if((PIND & 0x10)==0x10)
   {
    dat[0]=dat[1];
    dat[1]=dat[2];
    dat[2]=0x02;
    while((PIND & 0x10)==0x10); 
   }
  }

  if((PIND & 0x04) == 0x04)
  {
   delay();
   if((PIND & 0x04)==0x04)
   {
    dat[0]=dat[1];
    dat[1]=dat[2];
    dat[2]=0x03;
    while((PIND & 0x04)==0x04); 
   }
  }

  if((PINB & 0x01) == 0x01)
  {
   delay();
   if((PINB & 0x01)==0x01)
   {
    dat[0]=dat[1];
    dat[1]=dat[2];
    dat[2]=0x04;
    while((PINB & 0x01)==0x01); 
   }
  }

  if((PINB & 0x80) == 0x80)
  {
   delay();
   if((PINB & 0x80)==0x80)
   {
    dat[0]=dat[1];
    dat[1]=dat[2];
    dat[2]=0x05;
    while((PINB & 0x80)==0x80); 
   }
  }
  if((PIND & 0x02) == 0x02)
  {
   delay();
   if((PIND & 0x02)==0x02)
   {
    dat[0]=dat[1];
    dat[1]=dat[2];
    dat[2]=0x06;
    while((PIND & 0x02)==0x02); 
   }
  }

  if((PIND & 0x80) == 0x80)
  {
   delay();
   if((PIND & 0x80)==0x80)
   {
    dat[0]=dat[1];
    dat[1]=dat[2];
    dat[2]=0x07;
    while((PIND & 0x80)==0x80); 
   }
  }

  if((PINB & 0x40) == 0x40)
  {
   delay();
   if((PINB & 0x40)==0x40)
   {
    dat[0]=dat[1];
    dat[1]=dat[2];
    dat[2]=0x08;
    while((PINB & 0x40)==0x40); 
   }
  }
  if((PIND & 0x01) == 0x01)
  {
   delay();
   if((PIND & 0x01)==0x01)
   {
    dat[0]=dat[1];
    dat[1]=dat[2];
    dat[2]=0x09;
    while((PIND & 0x01)==0x01); 
   }
  }

  PORTC &=~(1<<S1);
  PORTC |=(1<<S2);
  PORTC |=(1<<S3);
  Display(dat[0]);
  delay();

  PORTC |=(1<<S1);
  PORTC &=~(1<<S2);
  PORTC |=(1<<S3);
  Display(dat[1]);
  delay();

  PORTC |=(1<<S1);
  PORTC |=(1<<S2);
  PORTC &=~(1<<S3);
  Display(dat[2]);
  delay();
 }
}
/***************************************************************/
/*                     7-Segment Display Decoding Logic        */
/***************************************************************/
void Display(char f)
{
 if(f==0x00)
 {
  PORTC |=(0x03);
  PORTB |=(0x3E);
  PORTB &=~(1<<D6);
 }

 if(f==0x01)
 {
  PORTC &=~(0x03);
  PORTB &=~(0x3E);
  PORTB |=(1<<D1);
  PORTB |=(1<<D2);
 }

 if(f==0x02)
 {
  PORTC |=(0x03);
  PORTB |=(0x3E);
  PORTC &=~(1<<D5);
  PORTB &=~(1<<D2);
 }

 if(f==0x03)
 {
  PORTC |=(0x03);
  PORTB |=(0x3E);
  PORTC &=~(1<<D5);
  PORTB &=~(1<<D4);
 }

 if(f==0x04)
 {
  PORTC |=(0x03);
  PORTB |=(0x3E);

  PORTC &=~(1<<D0);
  PORTB &=~(1<<D3);
  PORTB &=~(1<<D4);
 }

 if(f==0x05)
 {
  PORTC |=(0x03);
  PORTB |=(0x3E);

  PORTB &=~(1<<D1);
  PORTB &=~(1<<D4);
 }

 if(f==0x06)
 {
  PORTC |=(0x03);
  PORTB |=(0x3E);

  PORTB &=~(1<<D1);
 }

 if(f==0x07)
 {
  PORTC &=~(0x03);
  PORTB &=~(0x3E);
  PORTC |=(1<<D0);
  PORTB |=(1<<D2);
  PORTB |=(1<<D1);
 }

 if(f==0x08)
 {
  PORTC |=(0x03);
  PORTB |=(0x3E);
 }

 if(f==0x09)
 {
  PORTC |=(0x03);
  PORTB |=(0x3E);

  PORTB &=~(1<<D4);
 }


}

/***************************************************************/
/*                     Display Refresh delay                   */
/***************************************************************/
void delay()
{
int i;
for (i=1;i<450;i++)
{}
}

Download TokenNumber.C

Step 4: Test the code and Hardware
1. Press the keys and observe display changes
2. Follow us on Google+
3. You Did it Yourself
4. Refer Tutorials from this site for more understanding of code and Circuits


Tuesday, June 9, 2015

Black box for car project

Black box for bike/car project is used for vehicle tracking and accident monitoring. This project uses 128x64 Graphical LCD, GSM module, GPS module to track the vehicle and displays real time data on LCD also keeps log of the data. It is having VB6 Software to to display log data.

All required data to develop this project is provided here.

Features:
   1. Keeps log of various activities such as break, indicator, fuel status.
   2. Displays Fuel Level, Time, Position, Speed, Temperature.
   3. Uses Large Graphical Display.
   4. Isolated Inputs.


Microcontroller based vehicle tracking and accident detection system circuit


Step 1: Major Components Required

1. Atmega32 Microcontroller
2. Relays, Switches
3. LM35 Temperature Sensor
4. LM7805
5. PC817
6. 128x64 GLCD Display
7. Buy Components from our Store www.circuits4you.com

Step 2: Circuit Design and PCB Manufacturing
Download Requires Files
1. Download pdf Circuit Diagram
2. Download pdf PCB Layout
Microcontroller based vehicle tracking and accident detection system PCB Layout
3. Assemble components using above files.

Step 3: Programming the controller
Download Complete Project Code .zip
At some places in code You have to enter your mobile number

AVR Studio Code
/****************************************************************
blog.circuits4you.com
www.circuits4you.com
Black Box For Bike-2012
*****************************************************************/

#include <avr/io.h>
#include <string.h>
#include <avr/pgmspace.h> 
#include <avr/interrupt.h>
#include <avr/eeprom.h>
#include "GraphicLCD.h"

//Switch Inputs
//#define Key1         PB2 
//#define Key2        PB4

void Delay100ms();
void Delay();
void Wait();
void USART_Init();
void ProcessFifo();
void USART_Transmit( unsigned char data );
void senddata(char string[16]);

void Command(unsigned char y);
void GPS();
void GSM();
void com();
void SendSMS();
void SendSMS2();

void delay1(unsigned int de);

void DigitOne(char one,char x);
void DisplaySpeed(char speed);

void DisplayAll();
void StoreLog();
void SendLog();

uint32_t WordOfData; 
char MenuCnt,MyStr[8],mystr[3];
unsigned char Fifo[405],RollOut,RMCcnt,RMCflg,Time[8],LAT[10],LON[14],SPEED[6],VTGflg,VTGcnt;

unsigned int Baud,FifoCnt,FiCnt;
unsigned char GraphX, py, pGraphX, u8_data, AsciiX, Value1[65];//Value1[20],Value2[45];
char AsciiZ,once,HexmodeZ,once1, DecimalZ, once2;
unsigned char AsciiY, HexmodeX, HexmodeY, DecimalX,DecimalY;
/*********************************************************************************/
/*                                                                        MAIN PROGRAM                                 */
/*********************************************************************************/
int main(void)
{
//        unsigned char i,j;
         DDRA = 0x30; //PA4 and PA5 as output for selection of rs232
         DDRC = 0b00010000;
        DDRB = 0xFF;
        DDRD = 0b01111110;

        PORTA=0x00;
        PORTA &=~(1<<PA4);   //GPS selection
                delay1(100);
        PORTA &=~(1<<PA5);   //GSM selection



        Delay();

        FifoCnt=0;
        FiCnt=0;
        RollOut=0x00;

        RMCcnt=0;
        RMCflg=0;

        VTGcnt=0;
        VTGflg=0;

        USART_Init();
        SREG=0x80;

        Wait();
        Wait();
        Wait();
        
        com();
        senddata("This is test");

        InitLCD();        
        LCD_Clear();        
        MenuCnt=0;        
        FifoCnt=0;

        GraphX=0;
        AsciiX=0;
        AsciiY=0;
        AsciiZ=0;
        HexmodeX=0;
        HexmodeY=0;
        HexmodeZ=0;
        DecimalX=0;
        DecimalY=0;
        DecimalZ=0;
        once=0;
        once1=0;
        once2=0;

        GPS();
        while(1)
        {
                ProcessFifo();
        }

}

/*********************************************************************************/
/*                                                100 mSec Delay                                                                                   */
/*********************************************************************************/
void DisplayAll()
{
//--------------PICK THIS BLOCK AND PUT IT IN SUBROUTINE------------
                        
                                //USE SERIAL PORT FOR VERIFICATION OF GPS MODULE DATA
unsigned char q;
char j,HH,MM;
        DisplayText(63,19,"Km/Hr");
        DisplayText(1,30,"FUEL:");

        rect(32,30,92,37,1);

        ADMUX=0xE3;
        ADCSRA=0xC7;
        while (!(ADCSRA & (1<<ADIF)));
        q=ADCH;
        if(q>240)
        {q=240;}
        ADCSRA |= 1<<4;
        rect(33,32,91,35,0);        //60= fuel quantity
        rect(33,33,91,34,0);

        rect(32,32,(q/4)+32,35,1);        //60= fuel quantity
        rect(32,33,(q/4)+32,34,1);



        DisplayText(1,45,"LAT N:");//0000000000");
        DisplayText(37,45,LAT);
        DisplayText(1,56,"LON E:");//0000000000");
        DisplayText(37,56,LON);

        if((PINC & 0x08) == 0x08)
        {
                DisplayText(96,21,"     ");
        }
        else
        {
                DisplayText(96,21,"STAND");
        }

        ADMUX=0xE2;
        ADCSRA=0xC7;
        while (!(ADCSRA & (1<<ADIF)));
        q=0;
        q=ADCH;        
        ADCSRA |= 1<<4;
        sprintf(mystr, "%03d", q);

        DisplayText(96,31,mystr);
        DisplayText(118,31,"C");
        rect(114,31,116,33,1);

        
        HH=((Time[0] & 0x0F) *10) + (Time[1] & 0x0F);
        MM=((Time[2] & 0x0F) *10) + (Time[3] & 0x0F);
        
        HH=HH+5;
        MM=MM+30;
        if(MM>59)
        {
                MM=MM-60;
                HH=HH+1;
        }
        sprintf(mystr, "%02d", HH);
        DisplayChar(91,5,mystr[0]);
        DisplayChar(97,5,mystr[1]);
        DisplayChar(103,5,':');
        sprintf(mystr, "%02d", MM);
        DisplayChar(109,5,mystr[0]);
        DisplayChar(115,5,mystr[1]);

//        DisplayText(85,5,"12:00");

        if((PINC & 0x04)==0x04)
        {
                line(100,55,109,48,0);        //Left35 15
                line(100,55,109,61,0);
                line(109,48,109,61,0);
        }
        else
        {
                line(100,55,109,48,1);        //Left35 15
                line(100,55,109,61,1);
                line(109,48,109,61,1);
                StoreLog();
        }

        if((PINC & 0x02)==0x02)
        {
                line(113,48,122,55,0);        //Right   
                line(113,61,122,55,0);                  
                line(113,48,113,61,0);        
        }
        else
        {
                line(113,48,122,55,1);        //Right   
                line(113,61,122,55,1);                  
                line(113,48,113,61,1);                
                StoreLog();
        }

        if((PINC & 0x01) == 0x00) //Break
        {
                StoreLog();
                SendSMS2();
        }
        if((PIND & 0x80)==0x80)        
        {
                //Send SMS Accident
                SREG=0x00;
                GSM();
                        SendSMS();
                GPS();
                SREG=0x80;
        }


                j=0;
                j=(SPEED[2] & 0x0F) | (((SPEED[1] & 0x0F)<<4) & 0xF0);
                DisplaySpeed(j);
}

/*********************************************************************************/
/*                                                100 mSec Delay                                                                                   */
/*********************************************************************************/
void Delay100ms()
{
int k;
        for(k=-29000;k<29000;k++)
        {
                asm("nop");
        }
}
/*********************************************************************************/
/*                                                                        DELAY 100mSec                                */
/*********************************************************************************/
void Delay()
{
        int t;
                for(t=-22500;t<22500;t++)
                {
                        asm("nop");
                }
}
/****************************************************************************************/
/*                                                                          WAIT                                              */
/****************************************************************************************/
void Wait()
{
                Delay100ms();
                Delay100ms();
                Delay100ms();
                Delay100ms();
                Delay100ms();
                Delay100ms();
                Delay100ms();
                Delay100ms();
                Delay100ms();
                Delay100ms();
                Delay100ms();
}

/****************************************************************************************/
/*                                                                          USART INIT                                        */
/****************************************************************************************/
void USART_Init()
{
/* Set baud rate */
//                                            UBRRH=0x00;
//                                                UBRRL=207;        //4800 @ 8MHz
                                            UBRRH=0x00;
                                            UBRRL=103;        //9600 @ 8MHz

//Set double speed
         UCSRA |= (1<<U2X);
/* Enable receiver and transmitter */
        UCSRB = (1<<RXEN) | (1<<RXCIE) | (1<<UCSZ2) | (1<<TXEN);
/* Set frame format: 8data, 1stop bit 1 Parity */
        UCSRC = (1<<URSEL) | (1<<UCSZ0) | (1<<UCSZ1);


//        UCSRC = (1<<URSEL)|(1<<USBS)|(3<<UCSZ0);
//Set interrupt on RX
         UCSRB |= (1<<RXCIE);
}
/****************************************************************************************/
/*                                                                          USART Send data                                   */
/****************************************************************************************/
void senddata(char string[16])
{
  int len,count;
  len = strlen(string);

          for (count=0;count<len;count++)
         {
           USART_Transmit(string[count]);
        }
}
/****************************************************************************************/
/*                                                                          USART Transmit                                    */
/****************************************************************************************/

void USART_Transmit( unsigned char data )
{
/* Wait for empty transmit buffer */
while ( !( UCSRA & (1<<UDRE)) )
;
/* Put data into buffer, sends the data */
UDR = data;
}
/****************************************************************************************/
/*                                                                          USART ISR                                         */
/****************************************************************************************/
SIGNAL(USART_RXC_vect)
{
        u8_data=UDR;

//        USART_Transmit(u8_data);

        Fifo[FifoCnt]=u8_data;
        FifoCnt++;
        if(FifoCnt==400)
        {FifoCnt=0;
         RollOut=0x05;}

        return;        
}
//===================================================================================================
//                                                                           Seial Port 1 - GPS Module
//===================================================================================================
void GPS()
{
/* Set baud rate */
//        UBRRH = 0x00;  //03
//        UBRRL = 207;  //baud rate 4800 at 8MHz 
        UBRRH = 0x00;  //03
        UBRRL = 103;  //baud rate 9600 at 8MHz 

        PORTA &=~(1<<PA5);
        PORTA |=(1<<PA4);
}

//===================================================================================================
//                                                                           Seial Port 2 - GSM Module
//===================================================================================================
void GSM()
{
/* Set baud rate */
        UBRRH = 0x00;  //03
        UBRRL = 103;  //baud rate 9600 at 8MHz 
        
        PORTA &=~(1<<PA4);
        PORTA |=(1<<PA5);
}
//===================================================================================================
//                                                                           Seial Port 2 - GSM Module
//===================================================================================================
void com()
{
/* Set baud rate */
        UBRRH = 0x00;  //03
        UBRRL = 103;  //baud rate 9600 at 8MHz 
        
        PORTA &=~(1<<PA4);
        PORTA &=~(1<<PA5);
}
/****************************************************************************************/
/*                                                                           Fifo process                                     */
/****************************************************************************************/
void ProcessFifo()
{
        //Use circular buffer with fill zero after read to prevent recommand

        if((FifoCnt>FiCnt) || (RollOut==0x05))
        {
                                        Command(Fifo[FiCnt]);
                                        Fifo[FiCnt]=0x00;                                        
                                        FiCnt++;
                                        if(FiCnt==400)
                                        {FiCnt=0;
                                         RollOut=0x00;}
                        
        }
        else
        {
                DisplayAll();
           //************************* MAIN PROCESS COMES HERE *************************************************************
           //***************************************************************************************************************
           //***************************************************************************************************************
        }
}
//===================================================================================================
//                                                                           Process Serial Commands
//===================================================================================================
void Command(unsigned char y)
{
//        int i;
        Value1[43]=Value1[42];
        Value1[42]=Value1[41];
        Value1[41]=Value1[40];
        Value1[40]=Value1[39];
        Value1[39]=Value1[38];
        Value1[38]=Value1[37];
        Value1[37]=Value1[36];
        Value1[36]=Value1[35];
        Value1[35]=Value1[34];
        Value1[34]=Value1[33];
        Value1[33]=Value1[32];
        Value1[32]=Value1[31];
        Value1[31]=Value1[30];
        Value1[30]=Value1[29];
        Value1[29]=Value1[28];
        Value1[28]=Value1[27];
        Value1[27]=Value1[26];
        Value1[26]=Value1[25];
        Value1[25]=Value1[24];
        Value1[24]=Value1[23];
        Value1[23]=Value1[22];
        Value1[22]=Value1[21];
        Value1[21]=Value1[20];
        Value1[20]=Value1[19];
        Value1[19]=Value1[18];
        Value1[18]=Value1[17];
        Value1[17]=Value1[16];

        Value1[16]=Value1[15];
        Value1[15]=Value1[14];
        Value1[14]=Value1[13];
        Value1[13]=Value1[12];
        Value1[12]=Value1[11];

        Value1[11]=Value1[10];
        Value1[10]=Value1[9];
        Value1[9]=Value1[8];
        Value1[8]=Value1[7];
        Value1[7]=Value1[6];
        Value1[6]=Value1[5];
        Value1[5]=Value1[4];
        Value1[4]=Value1[3];
        Value1[3]=Value1[2];
        Value1[2]=Value1[1];
        Value1[1]=Value1[0];
        Value1[0]=y;

//============================================================================================
//                                  GPS Response Decoder
//============================================================================================
//GPS Recived

//$GPRMC,031701.360,A,1950.7141,N,07521.1769,E,000.0,305.2,061210,,,A*69
//       ~~~~~~~      ~~~~~~~~~   ~~~~~~~~~~
//       HHMMSS         LAT          LONG
//     UTC time

        if(Value1[43]=='$' && Value1[42]=='G' && Value1[40]=='R' && Value1[39]=='M' && Value1[38]=='C')
        {
                        Time[0]=Value1[36];  //HH
                        Time[1]=Value1[35];

                        Time[2]=Value1[34];  //MM
                        Time[3]=Value1[33];

                        Time[4]=Value1[32];  //SS
                        Time[5]=Value1[31];

                        LAT[0]=Value1[23];
                        LAT[1]=Value1[22];
                        LAT[2]=Value1[21];
                        LAT[3]=Value1[20];
                        LAT[4]=Value1[19];
                        LAT[5]=Value1[18];
                        LAT[6]=Value1[17];
                        LAT[7]=Value1[16];
                        LAT[8]=Value1[15];

                        LON[0]=Value1[11];
                        LON[1]=Value1[10];
                        LON[2]=Value1[9];
                        LON[3]=Value1[8];
                        LON[4]=Value1[7];
                        LON[5]=Value1[6];
                        LON[6]=Value1[5];
                        LON[7]=Value1[4];
                        LON[8]=Value1[3];
                        LON[9]=Value1[2];

        }

//Get Speed from GPS

//$GPVTG,305.2,T,,M,000.0,N,000.0,K,A*09
//                         ----- speed in km/hr
//$GPVTG,89.68,T,,M,0.00,N,0.0,K,A*5F

        if(Value1[31]=='$' && Value1[30]=='G' && Value1[28]=='V' && Value1[27]=='T' && Value1[26]=='G')
        {
                SPEED[0]=Value1[5];
                SPEED[1]=Value1[4];
                SPEED[2]=Value1[3];
                SPEED[3]=Value1[2];
                SPEED[4]=Value1[1];
        }

// (Get)
        if(Value1[0]==')' && Value1[1]=='t' && Value1[2]=='e' && Value1[3]=='G' && Value1[4]=='(')
        {
                SREG=0x00;
                SendLog();
                SREG=0x80;
        }
}

/****************************************************************************************/
/*                                                                           Display Speed                                    */
/****************************************************************************************/
void DisplaySpeed(char speed)
{
        DigitOne(0,0);
        DigitOne(((speed>>4) & 0x0F),22);
        DigitOne((speed & 0x0F),44);
}

void DigitOne(char one,char x)
{

switch (one){
          case 1:
                line(0+x, 2, 0+x, 11,0);        //Segment F
                line(1+x, 3, 1+x, 10,0);
                line(0+x,14, 0+x, 23,0);        //Segment E
                line(1+x,15, 1+x, 22,0);
                line(1+x,25,15+x, 25,0);        //Segment D
                line(2+x,24,14+x, 24,0);
                line(16+x,2,16+x, 11,1);        //Segment B
                line(15+x,3,15+x, 10,1);
                line(16+x,13,16+x,23,1);        //Segment C
                line(15+x,14,15+x,22,1);
                line(1+x,0, 15+x, 0,0);                //Segment A
                line(2+x,1, 14+x, 1,0);
                line(2+x,12, 14+x, 12,0);                //Segment G
                line(1+x,13, 13+x, 13,0);
          break;
          case 2:
                line(0+x, 2, 0+x, 11,0);        //Segment F
                line(1+x, 3, 1+x, 10,0);
                line(0+x,14, 0+x, 23,1);        //Segment E
                line(1+x,15, 1+x, 22,1);
                line(1+x,25,15+x, 25,1);        //Segment D
                line(2+x,24,14+x, 24,1);
                line(16+x,2,16+x, 11,1);        //Segment B
                line(15+x,3,15+x, 10,1);
                line(16+x,13,16+x,23,0);        //Segment C
                line(15+x,14,15+x,22,0);
                line(1+x,0, 15+x, 0,1);                //Segment A
                line(2+x,1, 14+x, 1,1);
                line(2+x,12, 14+x, 12,1);                //Segment G
                line(1+x,13, 13+x, 13,1);
          break;
          case 3:
                line(0+x, 2, 0+x, 11,0);        //Segment F
                line(1+x, 3, 1+x, 10,0);
                line(0+x,14, 0+x, 23,0);        //Segment E
                line(1+x,15, 1+x, 22,0);
                line(1+x,25,15+x, 25,1);        //Segment D
                line(2+x,24,14+x, 24,1);
                line(16+x,2,16+x, 11,1);        //Segment B
                line(15+x,3,15+x, 10,1);
                line(16+x,13,16+x,23,1);        //Segment C
                line(15+x,14,15+x,22,1);
                line(1+x,0, 15+x, 0,1);                //Segment A
                line(2+x,1, 14+x, 1,1);
                line(2+x,12, 14+x, 12,1);                //Segment G
                line(1+x,13, 13+x, 13,1);
          break;
          case 4:
                line(0+x, 2, 0+x, 11,1);        //Segment F
                line(1+x, 3, 1+x, 10,1);
                line(0+x,14, 0+x, 23,0);        //Segment E
                line(1+x,15, 1+x, 22,0);
                line(1+x,25,15+x, 25,0);        //Segment D
                line(2+x,24,14+x, 24,0);
                line(16+x,2,16+x, 11,1);        //Segment B
                line(15+x,3,15+x, 10,1);
                line(16+x,13,16+x,23,1);        //Segment C
                line(15+x,14,15+x,22,1);
                line(1+x,0, 15+x, 0,0);                //Segment A
                line(2+x,1, 14+x, 1,0);
                line(2+x,12, 14+x, 12,1);                //Segment G
                line(1+x,13, 13+x, 13,1);
          break;
          case 5:
                line(0+x, 2, 0+x, 11,1);        //Segment F
                line(1+x, 3, 1+x, 10,1);
                line(0+x,14, 0+x, 23,0);        //Segment E
                line(1+x,15, 1+x, 22,0);
                line(1+x,25,15+x, 25,1);        //Segment D
                line(2+x,24,14+x, 24,1);
                line(16+x,2,16+x, 11,0);        //Segment B
                line(15+x,3,15+x, 10,0);
                line(16+x,13,16+x,23,1);        //Segment C
                line(15+x,14,15+x,22,1);
                line(1+x,0, 15+x, 0,1);                //Segment A
                line(2+x,1, 14+x, 1,1);
                line(2+x,12, 14+x, 12,1);                //Segment G
                line(1+x,13, 13+x, 13,1);
          break;
          case 6:
                line(0+x, 2, 0+x, 11,1);        //Segment F
                line(1+x, 3, 1+x, 10,1);
                line(0+x,14, 0+x, 23,1);        //Segment E
                line(1+x,15, 1+x, 22,1);
                line(1+x,25,15+x, 25,1);        //Segment D
                line(2+x,24,14+x, 24,1);
                line(16+x,2,16+x, 11,0);        //Segment B
                line(15+x,3,15+x, 10,0);
                line(16+x,13,16+x,23,1);        //Segment C
                line(15+x,14,15+x,22,1);
                line(1+x,0, 15+x, 0,1);                //Segment A
                line(2+x,1, 14+x, 1,1);
                line(2+x,12, 14+x, 12,1);                //Segment G
                line(1+x,13, 13+x, 13,1);
          break;
          case 7:
                line(0+x, 2, 0+x, 11,0);        //Segment F
                line(1+x, 3, 1+x, 10,0);
                line(0+x,14, 0+x, 23,0);        //Segment E
                line(1+x,15, 1+x, 22,0);
                line(1+x,25,15+x, 25,0);        //Segment D
                line(2+x,24,14+x, 24,0);
                line(16+x,2,16+x, 11,1);        //Segment B
                line(15+x,3,15+x, 10,1);
                line(16+x,13,16+x,23,1);        //Segment C
                line(15+x,14,15+x,22,1);
                line(1+x,0, 15+x, 0,1);                //Segment A
                line(2+x,1, 14+x, 1,1);
                line(2+x,12, 14+x, 12,0);                //Segment G
                line(1+x,13, 13+x, 13,0);
          break;
          case 8:
                line(0+x, 2, 0+x, 11,1);        //Segment F
                line(1+x, 3, 1+x, 10,1);
                line(0+x,14, 0+x, 23,1);        //Segment E
                line(1+x,15, 1+x, 22,1);
                line(1+x,25,15+x, 25,1);        //Segment D
                line(2+x,24,14+x, 24,1);
                line(16+x,2,16+x, 11,1);        //Segment B
                line(15+x,3,15+x, 10,1);
                line(16+x,13,16+x,23,1);        //Segment C
                line(15+x,14,15+x,22,1);
                line(1+x,0, 15+x, 0,1);                //Segment A
                line(2+x,1, 14+x, 1,1);
                line(2+x,12, 14+x, 12,1);                //Segment G
                line(1+x,13, 13+x, 13,1);
          break;
          case 9:
                line(0+x, 2, 0+x, 11,1);        //Segment F
                line(1+x, 3, 1+x, 10,1);
                line(0+x,14, 0+x, 23,0);        //Segment E
                line(1+x,15, 1+x, 22,0);
                line(1+x,25,15+x, 25,1);        //Segment D
                line(2+x,24,14+x, 24,1);
                line(16+x,2,16+x, 11,1);        //Segment B
                line(15+x,3,15+x, 10,1);
                line(16+x,13,16+x,23,1);        //Segment C
                line(15+x,14,15+x,22,1);
                line(1+x,0, 15+x, 0,1);                //Segment A
                line(2+x,1, 14+x, 1,1);
                line(2+x,12, 14+x, 12,1);                //Segment G
                line(1+x,13, 13+x, 13,1);
          break;
          default:
                line(0+x, 2, 0+x, 11,1);        //Segment F
                line(1+x, 3, 1+x, 10,1);
                line(0+x,14, 0+x, 23,1);        //Segment E
                line(1+x,15, 1+x, 22,1);
                line(1+x,25,15+x, 25,1);        //Segment D
                line(2+x,24,14+x, 24,1);
                line(16+x,2,16+x, 11,1);        //Segment B
                line(15+x,3,15+x, 10,1);
                line(16+x,13,16+x,23,1);        //Segment C
                line(15+x,14,15+x,22,1);
                line(1+x,0, 15+x, 0,1);                //Segment A
                line(2+x,1, 14+x, 1,1);
                line(2+x,12, 14+x, 12,0);                //Segment G
                line(1+x,13, 13+x, 13,0);
        }

}
/****************************************************************************************/
/*                                                                           Send SMS                                                    */
/****************************************************************************************/
void SendSMS()
{
                                        USART_Transmit(13);
                                        USART_Transmit(10);
                                        senddata("AT+CMGD=1");
                                        USART_Transmit(13);
                                        USART_Transmit(10);
                                        delay1(100);
                                                                                
                                        senddata("AT+CMGF=1");
                                        USART_Transmit(13);
                                        USART_Transmit(10);
                                        delay1(100);
                                        
                                        senddata("AT+CMGW=");
                                        USART_Transmit(34);
                                        
                                        senddata("+910000000000");  //Enter Your Mobile Number Here
                        
                                        USART_Transmit(34);
                                        USART_Transmit(13);
                                        USART_Transmit(10);
                                        delay1(100);
                                        
                                        senddata("Accident ");
                                        USART_Transmit(13);
                                        USART_Transmit(10);
                                        senddata("Location:");
                                        USART_Transmit(13);
                                        USART_Transmit(10);

                                        senddata("LAT N:");
                                        senddata(LAT);

                                        USART_Transmit(13);
                                        USART_Transmit(10);


                                        senddata("LON E:");
                                        senddata(LON);

                                        USART_Transmit(13);
                                        USART_Transmit(10);


                                        delay1(50);
                                        USART_Transmit(26); //Cntrl+Z
                                        delay1(3000);
                                        delay1(300);

                                        senddata("AT+CMSS=1");
                                        USART_Transmit(13);
                                        USART_Transmit(10);
                                        delay1(5000);
}

void delay1(unsigned int de)
{
unsigned int rr,rr1;
   for (rr=0;rr<de;rr++)
   {
                 
                for(rr1=0;rr1<395;rr1++)   //395
                {
                        asm("nop");
                }
                        
   }
}
/****************************************************************************************/
/*                                                                           Store Log                                            */
/****************************************************************************************/
void StoreLog()
{
int Address,counter;
unsigned char HH,MM,Tm,q;
//1. Define Data counter
//2. If Data counter = 30 then counter = 0        
//3. Address = 27 * counter
//4. Write all 27 bytes
//5. counter++

        //eeprom_write_byte((uint8_t*)2,0xFF);

        counter=eeprom_read_byte((uint8_t*)2);
        if(counter>30)
        {
                counter=1;
                eeprom_write_byte((uint8_t*)2,0x01);
        }

        Address=counter * 30;

//                Data format 27 Bytes
//                1. Time        (4 Byte)
//                2. LAN,LOT (20 Bytes)
//                3. Break,Indicator,Stand, Accident (1 Byte)
//                4. Temprature (1 Byte)
//                5. Fuel (1 Byte)

        HH=((Time[0] & 0x0F) *10) + (Time[1] & 0x0F);
        MM=((Time[2] & 0x0F) *10) + (Time[3] & 0x0F);
        
        HH=HH+6;
        MM=MM+30;
        if(MM>59)
        {
                MM=MM-60;
                HH=HH+1;
        }
//Time
        sprintf(mystr, "%02d", HH);
        eeprom_write_byte((uint8_t*)Address,mystr[0]);
        eeprom_write_byte((uint8_t*)(Address+1),mystr[1]);
        
        sprintf(mystr, "%02d", MM);
        eeprom_write_byte((uint8_t*)(Address+2),mystr[0]);
        eeprom_write_byte((uint8_t*)(Address+3),mystr[1]);

//LAT        
        eeprom_write_byte((uint8_t*)(Address+4),LAT[0]);
        eeprom_write_byte((uint8_t*)(Address+5),LAT[1]);
        eeprom_write_byte((uint8_t*)(Address+6),LAT[2]);
        eeprom_write_byte((uint8_t*)(Address+7),LAT[3]);
        eeprom_write_byte((uint8_t*)(Address+8),LAT[4]);
        eeprom_write_byte((uint8_t*)(Address+9),LAT[5]);
        eeprom_write_byte((uint8_t*)(Address+10),LAT[6]);
        eeprom_write_byte((uint8_t*)(Address+11),LAT[7]);
        eeprom_write_byte((uint8_t*)(Address+12),LAT[8]);

//LON
        eeprom_write_byte((uint8_t*)(Address+13),LON[0]);
        eeprom_write_byte((uint8_t*)(Address+14),LON[1]);
        eeprom_write_byte((uint8_t*)(Address+15),LON[2]);
        eeprom_write_byte((uint8_t*)(Address+16),LON[3]);
        eeprom_write_byte((uint8_t*)(Address+17),LON[4]);
        eeprom_write_byte((uint8_t*)(Address+18),LON[5]);
        eeprom_write_byte((uint8_t*)(Address+19),LON[6]);
        eeprom_write_byte((uint8_t*)(Address+20),LON[7]);
        eeprom_write_byte((uint8_t*)(Address+21),LON[8]);
        eeprom_write_byte((uint8_t*)(Address+22),LON[9]);

// Break,Indicator,Stand, Accident (1 Byte)
        Tm=0x00;

        if((PINC & 0x04)==0x00) //Left
        { Tm |= 0x01;        }

        if((PINC & 0x02)==0x00) //Right
        { Tm |= 0x02;        }


        if((PIND & 0x80)==0x80)         //Accident
        { Tm |= 0x04;        }

        if((PINC & 0x08) == 0x00) //Stand
        {Tm |= 0x08;        }
        
        if((PINC & 0x01) == 0x00) //Break
        {Tm |= 0x10;        }


        eeprom_write_byte((uint8_t*)(Address+23),Tm);

//Fuel
        ADMUX=0xE3;
        ADCSRA=0xC7;
        while (!(ADCSRA & (1<<ADIF)));
        q=ADCH;
        if(q>240)
        {q=240;}
        ADCSRA |= 1<<4;
        eeprom_write_byte((uint8_t*)(Address+24),q);

//Temprature
        ADMUX=0xE2;
        ADCSRA=0xC7;
        while (!(ADCSRA & (1<<ADIF)));
        q=ADCH;        
        eeprom_write_byte((uint8_t*)(Address+25),q);
        ADCSRA |= 1<<4;
//Speed

        eeprom_write_byte((uint8_t*)(Address+26),SPEED[0]);
        eeprom_write_byte((uint8_t*)(Address+27),SPEED[1]);
        eeprom_write_byte((uint8_t*)(Address+28),SPEED[2]);
        eeprom_write_byte((uint8_t*)(Address+29),SPEED[3]);
        eeprom_write_byte((uint8_t*)(Address+30),SPEED[4]);

        counter=counter+1;
        eeprom_write_byte((uint8_t*)2,counter);
}

/****************************************************************************************/
/*                                                                           Send Log                                                    */
/****************************************************************************************/
void SendLog()
{
int Address,counter;

        //eeprom_write_byte((uint8_t*)2,0xFF);

for(counter=1;counter<31;counter++)
 {
        Address=counter * 30;

        USART_Transmit('(');

//Time
        USART_Transmit(eeprom_read_byte((uint8_t*)Address));
        USART_Transmit(eeprom_read_byte((uint8_t*)(Address+1)));
        
        USART_Transmit(eeprom_read_byte((uint8_t*)(Address+2)));
        USART_Transmit(eeprom_read_byte((uint8_t*)(Address+3)));

//LAT        
        USART_Transmit(eeprom_read_byte((uint8_t*)(Address+4)));                //6
        USART_Transmit(eeprom_read_byte((uint8_t*)(Address+5)));
        USART_Transmit(eeprom_read_byte((uint8_t*)(Address+6)));
        USART_Transmit(eeprom_read_byte((uint8_t*)(Address+7)));
        USART_Transmit(eeprom_read_byte((uint8_t*)(Address+8)));
        USART_Transmit(eeprom_read_byte((uint8_t*)(Address+9)));
        USART_Transmit(eeprom_read_byte((uint8_t*)(Address+10)));
        USART_Transmit(eeprom_read_byte((uint8_t*)(Address+11)));
        USART_Transmit(eeprom_read_byte((uint8_t*)(Address+12)));

//LON                        
        USART_Transmit(eeprom_read_byte((uint8_t*)(Address+13)));                //15
        USART_Transmit(eeprom_read_byte((uint8_t*)(Address+14)));
        USART_Transmit(eeprom_read_byte((uint8_t*)(Address+15)));
        USART_Transmit(eeprom_read_byte((uint8_t*)(Address+16)));
        USART_Transmit(eeprom_read_byte((uint8_t*)(Address+17)));
        USART_Transmit(eeprom_read_byte((uint8_t*)(Address+18)));
        USART_Transmit(eeprom_read_byte((uint8_t*)(Address+19)));
        USART_Transmit(eeprom_read_byte((uint8_t*)(Address+20)));
        USART_Transmit(eeprom_read_byte((uint8_t*)(Address+21)));
        USART_Transmit(eeprom_read_byte((uint8_t*)(Address+22)));

// Break,Indicator,Stand, Accident (1 Byte)

        USART_Transmit(eeprom_read_byte((uint8_t*)(Address+23)));                //25

//Fuel
        USART_Transmit(eeprom_read_byte((uint8_t*)(Address+24)));                //26

//Temprature
        USART_Transmit(eeprom_read_byte((uint8_t*)(Address+25)));                //27

//Speed

        USART_Transmit(eeprom_read_byte((uint8_t*)(Address+26)));                //28
        USART_Transmit(eeprom_read_byte((uint8_t*)(Address+27)));
        USART_Transmit(eeprom_read_byte((uint8_t*)(Address+28)));
        USART_Transmit(eeprom_read_byte((uint8_t*)(Address+29)));
        USART_Transmit(eeprom_read_byte((uint8_t*)(Address+30))); 
        USART_Transmit(')');
        delay1(100);
  }
}
/****************************************************************************************/
/*                                                                           Send Position SMS                                        */
/****************************************************************************************/
void SendSMS2()
{
                                        USART_Transmit(13);
                                        USART_Transmit(10);
                                        senddata("AT+CMGD=1");
                                        USART_Transmit(13);
                                        USART_Transmit(10);
                                        delay1(100);
                                                                                
                                        senddata("AT+CMGF=1");
                                        USART_Transmit(13);
                                        USART_Transmit(10);
                                        delay1(100);
                                        
                                        senddata("AT+CMGW=");
                                        USART_Transmit(34);
                                        
                                        senddata("+910000000000");  //Enter Your Mobile Number Here
                        
                                        USART_Transmit(34);
                                        USART_Transmit(13);
                                        USART_Transmit(10);
                                        delay1(100);
                                        
                                        senddata("Location:");
                                        USART_Transmit(13);
                                        USART_Transmit(10);

                                        senddata("LAT N:");
                                        senddata(LAT);

                                        USART_Transmit(13);
                                        USART_Transmit(10);


                                        senddata("LON E:");
                                        senddata(LON);

                                        USART_Transmit(13);
                                        USART_Transmit(10);


                                        delay1(50);
                                        USART_Transmit(26); //Cntrl+Z
                                        delay1(3000);
                                        delay1(300);

                                        senddata("AT+CMSS=1");
                                        USART_Transmit(13);
                                        USART_Transmit(10);
                                        delay1(5000);
}

Step 5: Download VB6 Software for taking logged data 

Download VB6 PC Code


Step 6: Test the code and Hardware
1. Follow us on Google+
2. You Did it Yourself
3. Refer Tutorials from this site for more understanding of code and Circuits
4. Comment if you find any difficulty.