Interfacing of GSM with AVR:
#include <avr/io.h>
#include <util/delay.h>
#include <string.h>
#define rs 0
#define rw 1
#define en 2
unsigned char data;
void lcd_init();
void serial_init();
void serial_send_char(unsigned char x);
void serial_send_string(unsigned char *x);
char serial_receive();
void lcd_cmd(unsigned char);
void lcd_string(unsigned char*);
//void receive_sms();
//void response();
/*void receive_sms()
{
serial_send_string("ATE0\r\n");
_delay_ms(5000);
response();
serial_send_string("AT\r\n");
_delay_ms(5000);
response();
serial_send_string("AT+CMGF=1\r\n");
_delay_ms(5000);
response();
serial_send_string("AT+CNMI=2,2,0,0,0\r\n");
_delay_ms(5000);
response();
}
void response()
{
int i = 0;
char data[10];
while(serial_receive()>0)
{
data[i] = serial_receive();
i++;
}
if(strcmp(data ,"OK")==0)
{
lcd_cmd(0x01);
lcd_string("OK");
}
else if(strcmp(data ,"ERROR")==0)
{
lcd_cmd(0x01);
lcd_string("Error");
}
}*/
void display_msg()
{
unsigned int i;
unsigned char msg[32],ch;
lcd_cmd(0xc0);
lcd_string("Reading MSG...");
serial_send_string("AT+CMGR=1\r\n");
//delay_sec(1);
for(i=0;ch!='*';i++)
{
ch=serial_receive();
}
for(i=0;ch!='$';i++)
{
ch=serial_receive();
msg[i]=ch;
// lcd_display(msg[i]);
}
lcd_cmd(0x01);
lcd_cmd(0x80);
for(i=0;((i<16)&&(msg[i]!='$'));i++)
lcd_char(msg[i]);
lcd_cmd(0x01);
lcd_cmd(0xc0);
for(i=16;((i<32)&&(msg[i]!='$'));i++)
lcd_char(msg[i]);
serial_send_string("AT+CMGD=1\r\n");
_delay_ms(100);
}
int main(void)
{
unsigned char msg_chk[9];
int i;
DDRB = 0xFF;
serial_init();
_delay_ms(100);
lcd_init();
_delay_ms(100);
lcd_cmd(0x01);
lcd_cmd(0x80);
lcd_string("WELCOME TO GSM module");
_delay_ms(500);
serial_send_string("ATE0\r\n");
_delay_ms(500);
serial_send_string("AT\r\n");
_delay_ms(200);
serial_send_string("ATE0\r\n");
_delay_ms(200);
serial_send_string("AT&W\r\n");
_delay_ms(200);
serial_send_string("AT+CMGF=1\r\n");
_delay_ms(200);
serial_send_string("AT+CMGS=\"+919999999999\"\r\n");
_delay_ms(200);
serial_send_string("Ready\r\n");
_delay_ms(200);
serial_send_string(0x1A);
_delay_ms(200);
/* for(i=0;i<strlen(arr);i++)
{
serial_send_char(arr[i]);
_delay_ms(2);
}*/
lcd_gotoxy(0,0);
lcd_string("Message Received");
_delay_ms(1000);
lcd_cmd(0xc0);
while(1)
{
del_msg();
for(i=0;i<9;i++)
{
msg_chk[i]=serial_receive();
}
if(strncmp(msg_chk+3,"+CMTI:",6)==0)
{
lcd_gotoxy(0,0);
lcd_string(" 1 Message Receive");
display_msg();
}
else if(strncmp(msg_chk+3,"+CNMI:",6)==0)
{
lcd_gotoxy(0,0);
lcd_string(" 1 Message Received");
display_msg();
}
}
/* while (1)
{
if(serial_receive()=='+')
{
if(serial_receive()=='C')
{
if(serial_receive()=='M')
{
if(serial_receive()=='T')
{
if(serial_receive()=='I')
{
while(serial_receive()!=',');
a =serial_receive();
_delay_ms(50);
serial_send_char(0x0D);
serial_send_char(0x0A);
serial_send_string("AT+CMGF=1");
serial_send_char(0x0D);
serial_send_char(0x0A);
serial_send_string("AT+CMGR=");
serial_send_char(a);
serial_send_char(0x0D);
serial_send_char(0x0A);
while(serial_receive()!=0x0A);
while(serial_receive()!=0x0A);
while(serial_receive()!=0x0A);
for(i=0;i<15;i++)
{
read[i]=serial_receive();
}
lcd_string(read);
_delay_ms(5000);
}
}
}
}
}
}*/
// while(1)
// {
// data = serial_receive();
// _delay_ms(5);
// lcd_char(data);
// _delay_ms(5);
// }
return 0;
}
void lcd_cmd(unsigned char x)
{
PORTB = x & 0xF0;
PORTB &= ~(1<<rs);
PORTB &= ~(1<<rw);
PORTB |= (1<<en);
_delay_ms(50);
PORTB &= ~(1<<en);
_delay_ms(50);
PORTB = (x<<4)&0xF0;
PORTB &= ~(1<<rs);
PORTB &= ~(1<<rw);
PORTB |= (1<<en);
_delay_ms(5);
PORTB &= ~(1<<en);
_delay_ms(5);
}
void lcd_char(unsigned char x)
{
PORTB = x & 0xF0;
PORTB |= (1<<rs);
PORTB &= ~(1<<rw);
PORTB |= (1<<en);
_delay_ms(5);
PORTB &= ~(1<<en);
_delay_ms(5);
PORTB = (x<<4)&0xF0;
PORTB |= (1<<rs);
PORTB &= ~(1<<rw);
PORTB |= (1<<en);
_delay_ms(5);
PORTB &= ~(1<<en);
_delay_ms(5);
}
void lcd_string(unsigned char *ch)
{
while(*ch!='\0')
{
lcd_char(*ch);
_delay_ms(10);
ch++;
}
}
void lcd_init()
{
lcd_cmd(0x02);//cursor at home
lcd_cmd(0x28);//lcd in 4 bit mode
lcd_cmd(0x0E);//display on
lcd_cmd(0x01);//clear the display
lcd_cmd(0x06);//display and shift cursor to the right
}
void serial_init()
{
UCSRB = (1<<TXEN)|(1<<RXEN);
UCSRC = (1<<URSEL)|(1<<UCSZ0)|(1<<UCSZ1);
UBRRL = 51;
_delay_ms(10);
}
void serial_send_char(unsigned char x)
{
while(!(UCSRA & (1<<UDRE)));
UDR = x;
_delay_ms(10);
}
void serial_send_string(unsigned char *x)
{
while(*x!='\0')
{
serial_send_char(*x);
x++;
}
}
char serial_receive()
{
while(!(UCSRA &(1<<RXC)));
_delay_ms(5);
return UDR;
}
void lcd_gotoxy(unsigned char row,unsigned char column) //move cursor to the specified position
{
if(row==0)
{
lcd_cmd(0x80+column);
}
else if(row==1)
{
lcd_cmd(0xC0+column);
}
}
0 comments:
Post a Comment
if you have any doubt please let me know