#include
#include
# define Buf_Max 60//cache the length 200
250//# define Delay_Time delay length
# define uint unsigned int
# define uchar unsigned char
Sbit zhendong=P3 ^ 4;
Sbit lvled=P0 ^ 1;
Sbit huangled=P0 ^ 0;
Sbit hongled=P0 ^ 2;
Sbit beer=P3 ^ 7;
Sbit chefangbtn P1=^ 1;
Sbit bufangbtn P1=^ 0;
Sbit baojingbtn=P3 ^ 3;
Uchar kongzhi=0;//open the alarm
Unsigned char I=0;//define the buffer pointer
Unsigned char Rec_Buf [Buf_Max];//define the cache array
Void Serial_Init (void);//declare a serial port initialization function
Void Send_ASCII (unsigned char * b);//declare send character (ASCII) function
Void Delay_ms (unsigned int time);//declare delay function
Void CLR_Buf (void);//declare clear the cache content
Void Send_Hex (unsigned char b);//declare send characters (hexadecimal) function
Bit Hand (unsigned char * a);//declare the function to determine whether a cache containing the specified string
//delay function is about 1 s clock, but delay large no...
Void DelaySec (int SEC)
{
Unsigned int I, j=0;
for(i=0; i
for(j=0; J<65535; J++)
{
}
}
}
Void Uart1Send (unsigned char c)
{
SBUF=c;
while(! TI);//waiting to send complete signal appears (TI=1)
TI=0;
}
//serial port for sending char array, termination date/0 will stop
Void Uart1Sends (unsigned char * STR)
{
While (* STR!='\ 0')
{
SBUF=* STR;
while(! TI);//waiting to send complete signal appears (TI=1)
TI=0;
Str++;
}
}
Void main ()
{
Unsigned int t=0;
Unsigned char time_temp=0;
//led1=0;
Serial_Init ();//initialize serial port
CLR_Buf ();//clear the cache content
Send_ASCII (" ats ");//send "AT" command
while(! Hand (" OK "))//if the right connection
{
Delay_ms (100);//delay
Time_temp + +;
If (time_temp & gt; 10)
{
Time_temp=0;
Send_ASCII (" ats ");//send "AT" command
}
}
Time_temp=0;
CLR_Buf ();//clear the cache content
Send_ASCII (" AT + CMGF=1 ");//send "AT + CMGF=1" instruction
while(! Hand (" OK "))//determine whether set to text format
{
Delay_ms (100);
Time_temp + +;
If (time_temp & gt; 10)
{
Time_temp=0;
Send_ASCII (" AT + CMGF=1 ");//send "AT + CMGF=1" instruction
}
}
Time_temp=0;
CLR_Buf ();
Send_ASCII (" AT + CMGD=1, 4 ");//send "AT + CMGD=1, 4" instruction
while(! Hand (" OK "))//determine whether delete the SIM card memory short message
{
Delay_ms (100);
Time_temp + +;
If (time_temp & gt; 10)
{
Time_temp=0;
Send_ASCII (" AT + CMGD=1, 4 ");//send "AT + CMGD=1, 4" instruction
}
}
Time_temp=0;
CLR_Buf ();
T=0;
//led_test=0;
//led2=0;
While (1)
{
If (bufangbtn==1)
{
Kongzhi=1;
Lvled=0;
}
If (chefangbtn==1)
{
Kongzhi=0;
}
If (baojingbtn==1)
{
Kongzhi=2;
}
//kongzhi==0 cases for the removal of 1 case for protection for 2 cases is an emergency alarm
If (zhendong==1)
{
Beer=0;
Hongled=0;
Lvled=1;
Huangled=1;
Uart1Sends (" AT + CMGF=1 \ r \ n ");
DelaySec (1);//delay of 1 second
Uart1Sends (" AT + CSCS=\ "GSM \ \ r \ n");
DelaySec (1);//delay of 1 second
Uart1Sends (AT + CMGS=\ "" + 8617513235451 " \ r \ n ");//here is modified to the other side of the phone number
DelaySec (1);//delay of 1 second
Uart1Sends (" zhen dong bao jing ");//modify text content
Uart1Send (0 x1a);
DelaySec (2);//delay 2 seconds,
Kongzhi=0;
}
If (kongzhi==2)//emergency alarm
{
Lvled=0;
Hongled=0;
Huangled=0;
Beer=0;
}
If (kongzhi==0)
{
Huangled=0;
Lvled=1;
Hongled=1;
Beer=1;
}
If (STRSTR (Rec_Buf, "+ CMTI")!=NULL)//from the cache if the string contains "+ CMTI" to determine whether there is a new short message
{
CLR_Buf ();
Send_ASCII (" AT + CMGR=1 ");//send "AT + CMGR=1" instruction
while(! Hand (" OK "))//read the short message determine whether success
{
Delay_ms (100);
Time_temp + +;
If (time_temp & gt; 10)
{
Time_temp=0;
Send_ASCII (" AT + CMGR=1 ");//send "AT + CMGR=1" instruction
}
}
If (STRSTR (Rec_Buf, "bufang")!=NULL)//judge whether there is "kai" information content
{
Kongzhi=1;//protection
//led_test=0;
}
If (STRSTR (Rec_Buf, chefang ")!=NULL)//judge whether there is "guan" information content
{
Kongzhi=0;//removal
}
If (STRSTR (Rec_Buf, "baojing")!=NULL)//judge whether there is "guan" information content
{
Kongzhi=2;//removal
}
//CLR_Buf ();
Send_ASCII (" AT + CMGD=1, 4 ");//send "AT + CMGD=1, 4" instruction
while(! Hand (" OK "))//determine whether delete the SIM card memory short message
{
Delay_ms (100);
Time_temp + +;
If (time_temp & gt; 10)
{
Time_temp=0;
Send_ASCII (" AT + CMGD=1, 4 ");//send "AT + CMGD=1, 4" instruction
}
CLR_Buf ();
T=0;
}
t++;
If (t/2500)
{
T=0;
//led1=~ led1;//flash system running, waiting for the instruction input
}
}
//}
}
}
Void Serial_Init (void)//serial port initialization function
{
SCON=0 x50;//serial port: mode 1, allows the sending and receiving
TMOD=0 x20; 1://timer mode 2, 8 automatic reloading pattern, used to produce baud rate
TH1=0 XFD;//11.0592 MHZ crystal vibration, baud rate to 9600
TL1=0 XFD;
TR1=1;//open the timer 1
ES=1;//open the serial port interrupt
EA=1;//open global interrupt
}
Void Serial_Int () interrupt 4//serial port interrupt handlers
{
ES=0;//close a serial port interrupt, prevent the interrupt nesting
If (RI)//if it is a transportation interruption, the processing
{
RI=0;//remove receiving interrupt flag bit
Rec_Buf [I]=SBUF;//to save receives the string as a cache
i++;//buffer pointer backward
If (i> nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull