ashutosh121
Newbie level 3
can anyone plz explain the interfacing of GPS and GSM with ATMEGA16
Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
ashutosh121,plz can u explain me the code...
/*
* Project name:
GSM_Test
* Copyright:
(c) MikroElektronika, 2005-2010.
* Revision History:
20090617:
- initial release;
* Description:
This is a simple project which demonstrates the use of SmartGM862-GPS Board.
Upon programming the MCU, connect your SmartGM862-GPS Board to PORTC of your EasyAVR6 development board.
The goal of this simple example is to display some basic functions of the GSM module like
making and answering a call and hanging up. Note that you'll have to enter the number you wish
to dial to in the atc3 constant.
* Test configuration:
MCU: ATmega16
[url]http://www.atmel.com/dyn/resources/prod_documents/doc2466.pdf[/url]
Dev.Board: EasyAVR6
[url=http://www.mikroe.com/eng/products/view/321/easyavr6-development-system/]EasyAVR6 Development System - AVR Development Board[/url]
Oscillator: External, 8.0000 MHz
Ext. Modules: SmartGM862-GPS Board extra board
[url=http://www.mikroe.com/eng/products/view/469/smartgm862-gps-board/]SmartGM862-GPS Board - Telit GM862-GPS GSM/GPRS development tool - mikroElektronika[/url]
ac:SmartGM862-GPS
SW: mikroC PRO for AVR
[url=http://www.mikroe.com/eng/products/view/228/mikroc-pro-for-avr/]MikroElektronika - mikroC PRO for AVR - C compiler for AVR microcontroller device development[/url]
* NOTES:
- Put the pull-down resistors on the PORTA
- Pull-up resistors on the PD0(Rx) and PD1(Tx) pins, pull-down PB0(RTS)
- In order to connect SmartGM862-GPS to EasyAVR6 dev. board you need to use SmartAdapt2 board:
[url=http://www.mikroe.com/eng/products/view/158/smartadapt2-board/]SmartADAPT2 Board - 16-pin configuration adapter with voltage selection - mikroElektronika[/url]
- SmartGM862-GPS extra board: SW1.3 and SW1.7 ON
*/
// Lcd module connections
sbit LCD_RS at PORTD2_bit;
sbit LCD_EN at PORTD3_bit;
sbit LCD_D4 at PORTD4_bit;
sbit LCD_D5 at PORTD5_bit;
sbit LCD_D6 at PORTD6_bit;
sbit LCD_D7 at PORTD7_bit;
sbit LCD_RS_Direction at DDD2_bit;
sbit LCD_EN_Direction at DDD3_bit;
sbit LCD_D4_Direction at DDD4_bit;
sbit LCD_D5_Direction at DDD5_bit;
sbit LCD_D6_Direction at DDD6_bit;
sbit LCD_D7_Direction at DDD7_bit;
// End Lcd module connections
// set of AT commands
const char atc1[] = "ATE0"; // disable command echo
const char atc2[] = "AT# CAP= 1"; // enables handsfree external mic/ear audio path
const char atc3[] = "ATD123456789;"; // place a call to phone number 123456789
// instead of 123456789 insert your phone number
const char atc4[] = "ATH"; // hang up
const char atc5[] = "ATA"; // answer a call
const char atc6[] = "AT#HFMICG=4"; // handsfree microphone gain
const char atc7[] = "AT#SHFEC=1"; // handsfree echo canceller
const char atc8[] = "AT+CLVL=12"; // loudspeaker volume level
const char atc9[] = "AT#SRS= 3,0"; // select ringer sound
const char atc10[] = "ATS0=0"; // number of rings to auto answer (auto answer disabled)
// lcd interface messages
const LCD_MESSAGE_LENGTH = 16;
const char lcd1[] = "Initializing...";
const char lcd2[] = "Power-Up GSM!!!";
const char lcd3[] = "RA3-Continue";
const char lcd4[] = "Ready! RA0-Dial";
const char lcd5[] = "RA1-HNG RA2-ANSW";
const char lcd6[] = "Calling...";
const char lcd7[] = "Hanging Up!";
const char lcd8[] = "Answering!";
const char lcd9[] = "RING!!!";
// responses to parse
const GSM_OK = 0;
const GSM_RING = 1;
char gsm_state = 0;
char response_rcvd = 0;
short responseID = -1, response = -1;
char msg[LCD_MESSAGE_LENGTH+1];
// copy const to ram string
char * CopyConst2Ram(char * dest, const char * src){
char * d ;
d = dest;
for(;*dest++ = *src++;)
;
return d;
}
// uart rx interrupt handler
void interrupt() org IVT_ADDR_USART_RXC {
char tmp;
tmp = UART1_Read(); // get received byte
// process reception through state machine
// we are parsing only "OK" and "RING" responses
switch (gsm_state) {
case 0: {
response = -1; // clear response
if (tmp == 'O') // we have 'O', it could be "OK"
gsm_state = 1; // expecting 'K'
if (tmp == 'R') // we have 'R', it could be "RING"
gsm_state = 10; // expecting 'I'
break;
}
case 1: {
if (tmp == 'K') { // we have 'K' ->
response = GSM_OK; // we have "OK" response
gsm_state = 50; // expecting CR+LF
}
else
gsm_state = 0; // reset state machine
break;
}
case 10: {
if (tmp == 'I') // we have 'I', it could be "RING"
gsm_state = 11; // expecting 'N'
else
gsm_state = 0; // reset state machine
break;
}
case 11: {
if (tmp == 'N') // we have 'N', it could be "RING"
gsm_state = 12; // expecting 'G'
else
gsm_state = 0; // reset state machine
break;
}
case 12: {
if (tmp == 'G') { // we have 'G' ->
response = GSM_RING; // we have "RING" response
gsm_state = 50; // expecting CR+LF
}
else
gsm_state = 0; // reset state machine
break;
}
case 50: {
if (tmp == 13) // we have 13, it could be CR+LF
gsm_state = 51; // expecting LF
else
gsm_state = 0; // reset state machine
break;
}
case 51: {
if (tmp == 10) { // we have LF, response is complete
response_rcvd = 1; // set reception flag
responseID = response; // set response ID
}
gsm_state = 0; // reset state machine
break;
}
default: { // unwanted character
gsm_state = 0; // reset state machine
break;
}
}
}
// send ATC command
void send_atc(const char *s)
{
// send command string
while(*s) {
UART1_Write(*s++);
}
// terminate command with CR
UART1_Write(0x0D);
}
// get GSM response, if there is any
short get_response() {
if (response_rcvd) {
response_rcvd = 0;
return responseID;
}
else
return -1;
}
// wait for GSM response
void wait_response(char rspns) {
while (get_response() != rspns)
;
}
// pause
void wait() {
Delay_ms(1000);
}
void main() {
// set PORTA inputs
DDA0_bit = 0; // place a call input
DDA1_bit = 0; // hang up input
DDA2_bit = 0; // answer a call input
DDA3_bit = 0; // continue with PIC program
DDB0_bit = 1; // RTS pin
UART1_Init(9600); // initialize USART module
Delay_ms(500);
SREG_I_bit = 1; // enable global interrupt
RXCIE_bit = 1; // enable interrupt on UART receive
// set RTS pin to zero, we will use only RX i TX
PORTB0_bit = 0;
// setup lcd module
Lcd_Init();
Lcd_Cmd(_LCD_CURSOR_OFF);
// LCD_Out: power-up gsm
LCD_Out(1,1,CopyConst2Ram(msg,lcd2));
LCD_Out(2,1,CopyConst2Ram(msg,lcd3));
// wait for start
while(!PINA3_bit)
;
Lcd_Cmd(_LCD_CLEAR);
// LCD_Out: Initializing...
LCD_Out(1,1,CopyConst2Ram(msg,lcd1));
Delay_ms(5000); // wait for the GSM module to initialize it self
// negotiate baud rate
while(1) {
send_atc("AT"); // send "AT" string until gsm sets up its baud rade
Delay_ms(100); // and gets it correctly
if (get_response() == GSM_OK) // if gsm says "OK" on our baud rate we got it
break;
}
// disable command echo
send_atc(atc1);
wait_response(GSM_OK);
// change audio path (enables handsfree external mic/ear audio path)
send_atc(atc2);
wait_response(GSM_OK);
// handsfree microphone gain
send_atc(atc6);
wait_response(GSM_OK);
// handsfree echo canceller
send_atc(atc7);
wait_response(GSM_OK);
// loudspeaker volume level
send_atc(atc8);
wait_response(GSM_OK);
// select ringer sound
send_atc(atc9);
wait_response(GSM_OK);
// number of rings to auto answer (auto answer disabled)
send_atc(atc10);
wait_response(GSM_OK);
while(1) {
// LCD_Out: Ready! RA0-Dial
LCD_Out(1,1,CopyConst2Ram(msg,lcd4));
// LCD_Out: RA1-HNG RA2-ASW
LCD_Out(2,1,CopyConst2Ram(msg,lcd5));
if(Button(&PINA, 0, 10, 1)) {
// LCD_Out: Calling...
Lcd_Cmd(_LCD_CLEAR);
LCD_Out(1,1,CopyConst2Ram(msg,lcd6));
// mobile originated call to specified number
send_atc(atc3);
wait_response(GSM_OK);
}
if(Button(&PINA, 1, 10, 1)) {
// disconnect existing connection
send_atc(atc4);
wait_response(GSM_OK);
Lcd_Cmd(_LCD_CLEAR);
// LCD_Out: Hanging Up!
LCD_Out(1,1,CopyConst2Ram(msg,lcd7));
wait();
}
if(Button(&PINA, 2, 10, 1)) {
// answer a call
send_atc(atc5);
wait_response(GSM_OK);
Lcd_Cmd(_LCD_CLEAR);
// LCD_Out: Answering!
LCD_Out(1,1,CopyConst2Ram(msg,lcd8));
wait();
}
// process gsm response
if (response_rcvd) {
response_rcvd = 0; // clear response received flag
switch (responseID) {
case GSM_OK : break; // do nothing
case GSM_RING : {
// LCD_Out:RING!!!
LCD_Out(1,1,CopyConst2Ram(msg,lcd9));
wait();
}
default : {
// process illegal responses
}
}
}
}
}