Sign In

TROUBLE IN AVR AND GSM INTERFACING FOR HOME AUTOMATION CODE IN C

RESHMA reshmabhadani5@gmail.com India

Hardware: AtMEGA ON 16mhZ Software: C PROJECT USING ATMEL

WE ARE DO PROJECT ON GSM BASED HOME AUTOMATION USING SMS
IN WHICH WAY WE DO ALL PROGRAMMING IN 'C' I POST OUR PROGRAMME SO PLZ,
GIVE WHICH PROBLEM AND ALSO HARDWARE CONNECTION

Images

Program

#define F_CPU 16000000UL
#include <avr/io.h>
#include <util/delay.h>
#include <stdlib.h>
#include <string.h>
#include "GSM.h"
#include "lcd.h"
void gsm_read()
{
int k;
clrscr();
LCD_write_string("System Activated");
gotoxy(1,16);
UART_Transmit_string("AT+CMGR=1r");
gsm_waitfor('r');
gsm_waitfor('n');
if(UART_Receive()=='+')
{
gsm_waitfor('M');
if(UART_Receive()=='G')
{
gsm_waitfor('A');
gsm_waitfor(',');
gsm_waitfor('"');
for(k=0;k<13;k++)
number[k] = UART_Receive();
gsm_waitfor(',');
gsm_waitfor(',');
gsm_waitfor('+');
gsm_waitfor('n');
for(k=0;k<9;k++)
msg[k]=UART_Receive();
gsm_waitfor('K');
gsm_waitfor('n');
_delay_ms(300);
clrscr();
LCD_write_string("Ph:");
LCD_write_string(number);
gotoxy(1,0);
LCD_write_string("Msg:");
LCD_write_string(msg);
_delay_ms(2000);
if(!strcmp(msg,"Motor on "))
{
PORTB |= (1<<PB2);
}
if(!strcmp(msg,"Motor off"))
{
PORTB &= ~(1<<PB2);
}
gsm_delete();
}
}
_delay_ms(1000);
}
int main(void)
{
DDRB  |= (1<<PB2);
lcd_init();
LCD_write_string("Initializing... ");
gsm_init();
gsm_delete();
while(1)
{
gsm_read();
}
return 0;
}
#ifndef GSM
#define GSM
#include <avr/io.h>
#include <util/delay.h>
#include <string.h>
#include <avr/wdt.h>
#include <avr/interrupt.h>
#include "lcd.h"
char msg[10];
char number[14];
int i,j;
void UART_Init( unsigned int baud );
void UART_Transmit_char( unsigned char data );
unsigned char UART_Receive( void );
void UART_Transmit_string( char *string );
void UART_Init( unsigned int baud )
{
  /* Set baud rate */
  UBRRH = (unsigned char)(baud>>8);
  UBRRL = (unsigned char)baud;
  /* Enable receiver and transmitter */
  UCSRB = (1<<RXEN)|(1<<TXEN);
  /* Set frame format: 8data, 1stop bit */
  UCSRC = (1<<URSEL)|(0<<USBS)|(3<<UCSZ0);
}
void UART_Transmit_char( unsigned char data )
{
  /* Wait for empty transmit buffer */
  while ( !( UCSRA & (1<<UDRE)) )
  ;
  /* Put data into buffer, sends the data */
  UDR = data;
}
unsigned char UART_Receive( void )
{
  /* Wait for data to be received */
   while ( !(UCSRA & (1<<RXC)) )
   ;
   /* Get and return received data from buffer */
   return UDR;
}
void UART_Transmit_string( char string[] )
{
  int i=0;
  while ( string[i] > 0)
  UART_Transmit_char(string[i++]);
}
void gsm_init(void);
void gsm_read(void);
void gsm_send(char *number,char *string);
void gsm_delete(void);
void gsm_waitfor(char c);
void gsm_waitfor(char c)
{
//enabling watchdogtimer with a time of 2.1secs
wdt_enable(7);
//waiting for the byte to be received
while(UART_Receive()!= c);
//resetting watchdogtimer and turning off the watchdogtimer
wdt_reset();
wdt_disable();
}
void gsm_init()
{
UART_Init(103); // baudrate=9600
gotoxy(1,0);
LCD_write_string(" Testing Modem  ");
_delay_ms(500);
UART_Transmit_string("ATr");
gsm_waitfor('O');
gsm_waitfor('K');
gotoxy(1,0);
LCD_write_string("   Modem : OK   ");
_delay_ms(1000);
INS:
gotoxy(1,0);
LCD_write_string(" Checking SIM   ");
_delay_ms(500);
UART_Transmit_string("AT+CSMINS?r");
gsm_waitfor( 'n');
gsm_waitfor(',');
if(UART_Receive() == '2')
{ 
gotoxy(1,0);
LCD_write_string("  SIM NOTFOUND  ");
_delay_ms(1000);
goto INS;
}
}
else if(UART_Receive() == '1');
gsm_waitfor( 'K');
gsm_waitfor( 'n');
gotoxy(1,0);
LCD_write_string("   SIM FOUND    ");
_delay_ms(1000);
REG:
gotoxy(1,0);
LCD_write_string(" Network Status ");
_delay_ms(500);
UART_Transmit_string("AT+CREG?r");
gsm_waitfor( 'n');
gsm_waitfor(',');
if(UART_Receive() == '2')
{
gotoxy(1,0);
LCD_write_string("Network NotFound");
_delay_ms(1000);
goto REG;
}
else if(UART_Receive() == '1');
gsm_waitfor( 'K');
gsm_waitfor( 'n');
gotoxy(1,0);
LCD_write_string(" Network Found  ");
_delay_ms(1000);
UART_Transmit_string("AT+CMGF=1r");
gotoxy(1,0);
LCD_write_string("Setting Textmode");
gsm_waitfor('O');
gsm_waitfor('K');
gotoxy(1,0);
LCD_write_string("  Textmode set  ");
_delay_ms(1000);
}
void gsm_delete()
{
UART_Transmit_string("AT+CMGD=1r");
gsm_waitfor('K');
gsm_waitfor('n');
_delay_ms(500);
}
#endif
#include<avr/io.h>
#include<util/delay.h>
#include<inttypes.h>
#include "lcd.h"
void LCD_write_string(const char *str) //store address value of t                                        he string in pointer *str
{
int i=0;
while(str[i]!='') // loop will go on till the NULL character in                           the string
{
if (str[i]=='*')
{
i++;
int8_t cc=str[i]-'0';
if(cc>=0 && cc<=7)
{
dis_data(cc);
}
else
{
dis_data('%');
dis_data(str[i]);
}
}
else dis_data(str[i]); // sending data on LCD byte by byte
i++;
}
return;
}
void lcd_init() // function for initialize
{
DDRA=0xFF;
dis_cmd(0x02); // to initialize LCD in 4-bit mode.
dis_cmd(0x28); //to initialize LCD in 2 lines, 5X7 dots and 4bit                     mode.
dis_cmd(0x0C);
dis_cmd(0x06);
dis_cmd(0x0E);
custom_char();
gotoxy(0,0);
}
void dis_cmd(char cmd_value)
{
char cmd_value1;
cmd_value1 = ((cmd_value>>4) & 0x0F); //shift 4-bit and mask
lcdcmd(cmd_value1); // send to LCD
cmd_value1 = cmd_value & 0x0F; //mask lower nibble because PA4-PA                                 7 pins are used.
lcdcmd(cmd_value1); // send to LCD
}
void dis_data(char data_value)
{
char data_value1;
data_value1=((data_value>>4)&0x0F);
lcddata(data_value1);
data_value1=data_value&0x0F;
lcddata(data_value1);
}
void lcdcmd(char cmdout)
{
PORTA=cmdout;
PORTA&=~(1<<rs);
PORTA&=~(1<<rw);
PORTA|=(1<<en);
_delay_ms(1);
PORTA&=~(1<<en);
}
void lcddata(char dataout)
{
PORTA=dataout;
PORTA|=(1<<rs);
PORTA&=~(1<<rw);
PORTA|=(1<<en);
_delay_ms(1);
PORTA&=~(1<<en);
}
void clrscr()
{
_delay_ms(10);
dis_cmd(0x01);
_delay_ms(100);
}
void gotoxy(char a,char b)
{
if(a==0)  a=0b10000000;
else if(a==1) a=0b11000000;
else if(a==2) a=0b10010100;
else if(a==3) a=0b11010100;
dis_cmd(a+b); 
}
void custom_char()
{
unsigned char c[]={0x04, 0x0E, 0x0E, 0x1F, 0x1F, 0x0E, 0x0E, 0x04, //Char0
0x1F, 0x11, 0x0A, 0x04, 0x04, 0x04, 0x04, 0x04,  //Char1
0x01, 0x01, 0x01, 0x05, 0x05, 0x15, 0x15, 0x15, //Char2
0x00, 0x00, 0x00, 0x04, 0x04, 0x14, 0x14, 0x14, //Char3
0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x10, 0x10, //Char4
0x00, 0x00, 0x0A, 0x00, 0x04, 0x1F, 0x0A, 0x04, //Char5
0x00, 0x04, 0x0A, 0x11, 0x1F, 0x00, 0x00, 0x00, //Char6
0x00, 0x00, 0x00, 0x1F, 0x11, 0x0A, 0x04, 0x00, //Char7
};
uint8_t a[]={72,80,88,96,104,112,120,64};
uint8_t i,j;
for(i=0;i<sizeof(a);i++)
{
dis_cmd(a[i]);
for(j=0;j<sizeof(c);j++)
{
dis_data(c[j]);
}
}

}

Post a reply to RESHMA

Think you can help RESHMA ? Then post your thoughts that might help RESHMA. You will earn a lot of reputation in the technical community.

If your program is in multiple files, then ZIP the entire folder and attach below

Images and Screenshots

These helps other better understand your suggestion.