|
#include <reg52.h>
#include<intrins.h>
#define uchar unsigned char
#define uint unsigned int
uint num;
uchar port;
sbit Hong_sen=P1^0;
sbit AD_eoc=P2^0;
sbit AD_clk=P2^1;
sbit AD_add=P2^2;
sbit AD_dat=P2^3;
sbit AD_cs=P2^4;
sbit buzzer=P2^5;
sbit led_alarm=P2^6;
sbit GSM_led=P2^7;
sbit DUAN=P1^6;
sbit WEI=P1^7;
uchar code led7[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
uchar sms_text1[] = "Warning: Gas Leakage !";
uchar sms_text2[] = "Warning: Illegal Intrusion !" ;
uchar num1[] = "ATD15236265397;\r";
void SerialInti()
{
TMOD=0x20;
TH1=0xf3;
TL1=0xf3;
TR1=1;
SM0=0;
SM1=1;
REN=1;
IP=0x10;
EA=1;
ES=1;
EX0=1;
IT0=0;
}
void Serial_interrupt() interrupt 4
{
RI=0;
}
void Uart1Send(uchar c)
{
SBUF=c;
while(!TI);
TI=0;
}
void Uart1Sends(uchar *str)
{
while(*str!='\0')
{
SBUF=*str;
while(!TI);
TI=0;
str++;
}
}
void Delay_S(uint sec)
{
uint i , j= 0;
for(i=0; i<sec; i++)
{
for(j=0; j<65535; j++);
}
}
void SMS_Send()
{
Uart1Sends("AT+CMGF=1\r");
Delay_S(3);
Uart1Sends("AT+CSCS=\"GSM\"\r");
Delay_S(3);
Uart1Sends("AT+CSMP=17,167,0,250\r");
// Uart1Sends("AT+CSMP=17,167,0,240\r");
Delay_S(3);
Uart1Sends("AT+CMGS=\"15236265397\"\r");
Delay_S(5);
Uart1Sends(sms_text1);
Uart1Send(0x1a);
GSM_led=0;
Delay_S(5);
}
void delay(uint z)
{
uint k;
for(z;z>0;z--)
for(k=110;k>0;k--);
}
void display()
{
uchar ge,shi,bai,qian;
qian=num/1000;
bai=num/100%10;
shi=num%100/10;
ge=num%100%10;
P0=0;
P0=led7[qian];
DUAN=1;
DUAN=0;
P0=0Xf7;
WEI=1;
WEI=0;
delay(5);
P0=0x00;
P0=led7[bai];
DUAN=1;
DUAN=0;
P0=0Xfb;
WEI=1;
WEI=0;
delay(5);
P0=0x00;
P0=led7[shi];
DUAN=1;
DUAN=0;
P0=0Xfd;
WEI=1;
WEI=0;
delay(5);
P0=0x00;
P0=led7[ge];
DUAN=1;
DUAN=0;
P0=0Xfe;
WEI=1;
WEI=0;
delay(5);
P0=0x00;
}
uint ADC(uchar chn1)
{
uchar i;
uchar addr8,m;
uint ADresult;
AD_eoc=1;
AD_cs=0;
_nop_();
addr8=chn1;
addr8<<=4;
for(i=0;i<4;i++)
{
AD_add=(bit)(addr8&0x80);
AD_clk=1;
AD_clk=0;
addr8<<=1;
}
for(i=0;i<6;i++)
{
AD_clk=1;
AD_clk=0;
}
AD_cs=1;
while(!AD_eoc);
_nop_();
ADresult=0;
AD_cs=0;
for(i=0;i<10;i++)
{
AD_clk=1;
ADresult<<=1;
m=AD_dat;
ADresult+=m;
AD_clk=0;
}
AD_cs=1;
return(ADresult);
}
void main()
{
GSM_led=1;
Hong_sen=1;
SerialInti();
while(1)
{
port=0x00;
num=ADC(port);
display();
if(num>200)
{
buzzer=0;
led_alarm=0;
Uart1Sends(num1);
GSM_led=0;
Delay_S(10);
Uart1Sends("ATH\r");
GSM_led=1;
Delay_S(3);
SMS_Send();
}
}
}
void ISR_Key(void) interrupt 0 using 1
{
buzzer=0;
led_alarm=0;
Uart1Sends(num1);
GSM_led=0;
Delay_S(10);
Uart1Sends("ATH\r");
GSM_led=1;
Delay_S(3);
Uart1Sends("AT+CMGF=1\r");
Delay_S(3);
Uart1Sends("AT+CSCS=\"GSM\"\r");
Delay_S(3);
Uart1Sends("AT+CSMP=17,167,0,250\r");
Uart1Sends("AT+CSMP=17,167,0,240\r");
Delay_S(3);
Uart1Sends("AT+CMGS=\"15236265397\"\r");
Delay_S(5);
Uart1Sends(sms_text2);
Uart1Send(0x1a);
GSM_led=0;
Delay_S(5);
}
|
|