Вы находитесь на странице: 1из 6

#include<reg51.

h>
#include<intrins.h>
#include<string.h>
sbit relay=P1^0;
void data_send(unsigned char *);//send AT commands to gsm module
void delay1();//to provide delay
void compare();
void data_send1(unsigned char *);
void command_mode(unsigned char );
void busy();
void data_res();
void lcd_init();
void gsm_receive();
void gsm_send(unsigned char *);
void delay();
void data_mode(unsigned char *);
void data1_mode(unsigned char);
unsigned char data_receive();
void intialisation();
sbit rs=P0^1;
sbit rw=P0^2;
sbit en=P0^3;
sbit en1=P2^7;
unsigned char *ch="AT\r\n";
unsigned char *ch1="AT+CMGF=1\r\n";
unsigned char *ch2="AT+CMGS=";
//unsigned char *ch3="Hi world\r\n";
unsigned char *ch4="AT+CNMI=2,2,0,0,0\r\n";
unsigned char *ch5="AT+CMGR=1\r\n";
unsigned char *ch6="AT+CMGD=1\r\n";
unsigned char *ph1="8892600919";
unsigned char *msg1="MOTOR ON";
unsigned char *msg2="MOTOR OFF";
unsigned char *ch7="ATEO\r\n";
unsigned char *ch8="AT&W\r\n";
unsigned char *ch9="+918892600xxx";
unsigned char a[2],cha,ph[14],msg[10],count=0,count1=0;
int main()
{
unsigned char j=0,cha;
P1=0x00;//initially P1 is 1,so to make it 0
P2=0x00;//initially P2 is 1,so to make it 0
rs=0;//for lcd command
rw=0;
en=0;
en1=1;
SCON=0x50;//for serial communication
TMOD=0x20;
TH1=0xfd;
TR1=1;
a[0]='"';
a[1]='\0';
//gsm_send();
lcd_init();
gsm_receive();//used to set the gsm in receiving mode
while(1)
{
command_mode(0x80);
//force cursor to beginning to 1st line
cha=data_receive();
//value of cha will be the character entered by user

//command_mode(0x01);
while(cha!='+')
{
cha=data_receive();
}
//command_mode(0x01);
while(cha!='+')
{
cha=data_receive();
}
while(cha!='9')
{
cha=data_receive();
}
while(cha!='1')
{
cha=data_receive();
}
command_mode(0x01);
for(j=0;j<10;j++)
{
cha=data_receive();
ph[j]=cha;
}
ph[j]='\0';
while(cha!='\n')
{
cha=data_receive();
}
j=0;
cha=data_receive();
while(cha!='\n')
{
msg[j]=cha;
cha=data_receive();
j++;
}
msg[j-1]='\0';
compare();
}
return 0;
}
void data_send(unsigned char *p)
modem
{
while(*p!='\0')
{
SBUF=*p;
while(TI==0);
TI=0;
p++;
}
}
unsigned char data_receive()
{
unsigned char i=0,j=0,ch;
RI=0;
while(RI==0);

//microcontroller sends AT commands to gsm


//checking the last of a string

//in order to send data,TI must be 0

ch=SBUF;
return ch;
}
void data_res()
{
unsigned char i=0;
while(i<4)
{
unsigned char ch;
RI=0;//in order to receive data
while(RI==0);
ch=SBUF;
i++;
}
}
void compare()
{
unsigned char j=0;
if(strcmp(ph,ph1)==0&&strcmp(msg,msg1)==0)
{
data_mode("Valid msg & no");
command_mode(0xc0);
//force cursor to beginning to 2nd line
data_mode(msg);
relay=1;
count1=0;
ch9=ph;
if(count>0)
{
gsm_send("MOTOR ALREADY ON");
}
else
{
gsm_send("MOTOR ON");
}
count++;
}
else if(strcmp(ph,ph1)==0&&strcmp(msg,msg2)==0)
{
data_mode("Valid msg & no");
command_mode(0xc0);
data_mode(msg);
relay=0;
count=0;
ch9=ph;
if(count1>0)
{
gsm_send("MOTOR ALREADY OFF");
count=0;
}
else
{
gsm_send("MOTOR OFF");
}
count1++;
}
else if(strcmp(ph,ph1)!=0&&strcmp(msg,msg1)!=0)
{

ch9=ph;
data_mode("Invalid no & msg");
gsm_send("you r not authorised");
}
else if(strcmp(ph,ph1)!=0||strcmp(msg,msg1)!=0)
{
if(strcmp(ph,ph1)!=0)
{
ch9=ph;
data_mode("Invalid no");
command_mode(0xc0);
data_mode(ph);
gsm_send("you r not authorised");
}
else{
ch9=ph;
data_mode("Invalid msg");
command_mode(0xc0);
data_mode(msg);
gsm_send("Invalid msg");
}
}
}
void command_mode(unsigned char a)//lcd command mode
{
busy();
rs=0;
rw=0;
P2=a;
en=1;
_nop_();//delay
_nop_();
en=0;
}
void busy( )//if gsm is busy
{
en1=1;
rs=0;
rw=1;
while(en1)
{
en=0;
_nop_();
_nop_();
en=1;
}
}
void data_mode(unsigned char *p)
{
busy();
rs=1;
rw=0;
while(*p!='\0')
{
P2=*p;
intialisation();
p++;
}

}
/*void data1_mode(unsigned char p)
{
busy();
rs=1;
rw=0;
P2=p;
intialisation();
}*/
void intialisation()
{
en=1;
_nop_();
_nop_();
en=0;
}
void gsm_send(unsigned char *ch3)
{
data_send(ch);
data_res();
data_send(ch1);
data_res();
data_send(ch2);
data_res();
data_send(a);
data_send(ch9);
data_send(a);
data_send("\r\n");
data_send(ch3);
data_res();
SBUF=0x1a;
while(TI==0);
TI=0;
}
void gsm_receive()
{
data_send(ch7);//"ATEO\r\n"(cancel echo from the command)
data_res();
data_send(ch8);//AT&W(store the current user profile)
data_res();
data_send(ch1);//AT+CMGF=1(sets the gsm modem in SMS text mode)
/ /delay();
data_res();
data_send(ch4);//AT+CNMI(used to get a notification when SMS arrives)
//delay();
data_res();
}
void lcd_init()
{
command_mode(0x01);//clear display screen
command_mode(0x38);//2 lines and 5*7 matrix(8 bit data)
command_mode(0x06);//Increment cursor
command_mode(0x80);//force cursor to beginning to 1st line
command_mode(0x0f);//display on,cursor not blinking
}

/*void delay1()
{
TMOD=0x01;
TH0=0x00;
TL0=0x00;
TR0=1;
while(TF0==0);
TF0=0;
TR0=0;
}*/
/*void delay()//10ms delay
{
TMOD=0x01;
TH0=0xdb;
TL0=0xff;
TR0=1;
while(TF0==0);
TF0=0;
TR0=0;
}*/

Вам также может понравиться