/**************http://tpytongxin.taobao.com/****************/
#include <reg52.h>
#include <intrins.h>
#include <string.h>
#include <stdio.h>
#define uint unsigned int
#define uchar unsigned char
sbit Send_ENGLISH_key=P1^0; //发英文TEXT短信按键
sbit Send_CALLING_key=P1^1; //打电话按键
sbit Send_DS18B20_key=P1^2; //发温度按键
sbit Send_SMS_PDU_key=P1^3; //发中文PDU短信按键
#define MAXCHAR 81
uchar aa[MAXCHAR];
code uchar ATE0[]="ATE0\r\n";
code uchar CREG_CMD[]="AT+CREG?\r\n";
code uchar SMS_send[]="AT+CMGS=18\r\n";
code uchar ATCN[]="AT+CNMI=2,1\r\n";
code uchar CMGF0[]="AT+CMGF=0\r\n";
code uchar CMGF1[]="AT+CMGF=1\r\n";
code uchar CMGR[12]="AT+CMGR=1\r\n";
code uchar CMGD[12]="AT+CMGD=1\r\n";
#define CALL_YOU_TELNUMBER "ATD18945917868;\r\n" //填入你手上的手机号码 不是板子的
#define SEND_MESSA_TO_YOUR "at+cmgs=\"18945917868\"\r\n" //填入你手上的手机号码 不是板子的
#define SEND_MESSA_CONTENT "warning"
sbit JIDIANQI=P2^0;
sbit FENGMINGQI=P2^1;
uchar a,j=0,flag=0;
int g_timeout=0;
void delay(uint ms)// 延时子程序
{
uchar i;
while(ms--)
{
for(i=0;i<120;i++);
}
}
/***********************************************************
函数名称:Print_Char
函数功能:发送单个字符
入口参数:ch 出口参数:无
***********************************************************/
Print_Char(uchar ch)//发送单个字符
{
SBUF=ch; //送入缓冲区
while(TI!=1); //等待发送完毕
TI=0; //软件清零
}
/***********************************************************
函数名称:Print_Str
函数功能:发送字符串
入口参数:*str 出口参数:无
***********************************************************/
Print_Str(uchar *str)//发送字符串
{
while(*str!='\0')
{
Print_Char(*str);
delay(2);
str++;
}
}
/***********************************************************
函数名称:Ini_UART
函数功能:串口初始化、定时器初始化
入口参数:无 出口参数:无
***********************************************************/
Ini_UART(void)//串口初始化、定时器初始化
{
SCON = 0x50 ; //SCON: serail mode 1, 8-bit UART, enable ucvr
//UART为模式1,8位数据,允许接收
TMOD |= 0x20 ; //TMOD: timer 1, mode 2, 8-bit reload
//定时器1为模式2,8位自动重装
PCON |= 0x80 ; //SMOD=1;
TH1 = 0xFd ; //Baud:19200 fosc="11".0592MHz
TL1=0xFd;
IE |= 0x90 ; //Enable Serial Interrupt
TR1 = 1 ; // timer 1 run
TI=1;
ES=1;
}
void clearBuff(void)
{
for(j=0;j<MAXCHAR;j++)
{
aa[j]=0x00;
}
j=0;
}
void led(int i)
{
P0 |= i;
delay(20);
P0 &= ~i;
delay(20);
P0 |= i;
delay(20);
P0 &= ~i;
}
int Hand()
{
delay(10);
if(strstr(aa,"OK")!=NULL) //检测单片机和模块的连接
{
g_timeout=0;
return 1;
}
if(g_timeout>50)
{
g_timeout=0;
return -1;
}
g_timeout++;
return 0;
}
void AT(void)
{
clearBuff();
Print_Str(ATE0);
delay(50);
while(strstr(aa,"OK")==NULL)
{
delay(50);
led(0x01);
clearBuff();
Print_Str(ATE0);
}
led(0x02);
Print_Str(ATCN);
delay(50);
while(1)
{
clearBuff();
Print_Str(CREG_CMD);
delay(50);
if(((aa[9]=='0')&&(aa[11]=='1'))||((aa[9]=='0')&&(aa[11]=='5')))
{
clearBuff();
led(0x08);
break;
}
else
{
clearBuff();
led(0x04);
delay(50);
}
}
}
void send_english()
{
P2=0XFF;
clearBuff();
Print_Str(CMGF1);
delay(100);
led(0x01);
Print_Str(SEND_MESSA_TO_YOUR);
delay(300);
led(0x02);
Print_Str(SEND_MESSA_CONTENT); //发短信内容
delay(10);
Print_Char(0x1A); //发送结束符号
delay(10);
led(0x04);
clearBuff();
delay(3000);
delay(3000);
}
void send_call()
{
clearBuff();
delay(100);
Print_Str(CALL_YOU_TELNUMBER); //打电话
clearBuff();
delay(3000);
delay(3000);
}
void main()
{
Ini_UART(); //初始化串口
AT(); //初始化模块
delay(10);
Send_ENGLISH_key=1; //发英文TEXT短信按键
Send_CALLING_key=1; //打电话按键
Send_DS18B20_key=1; //发温度按键
Send_SMS_PDU_key=1; //发中文PDU短信按键
clearBuff();
Print_Str(CMGD);
delay(50);
while(Hand()==0);
clearBuff();
Print_Str(CMGF1);
delay(30);
Print_Str(ATCN);
delay(30);
send_english();
while(1)
{
////////////////////////////////////////////////////////////////////////
if(Send_ENGLISH_key==0) //发英文TEXT短信按键
{
send_english();
}
else if(Send_CALLING_key==0) //打电话按键
{
send_call();
}
else if(Send_SMS_PDU_key==0)
{
}
if(strstr(aa,"RING")!=NULL)
{
FENGMINGQI=0;
delay(2);
clearBuff();
}
else
{
FENGMINGQI=1;
}
if(strstr(aa,"+CMTI")!=NULL) //缓存中有+CMTI字符
{
clearBuff();
Print_Str(CMGF1);
delay(50);
while(Hand()==0);
clearBuff();
Print_Str(CMGR); //读取1号位置的短信
delay(100);
if(strstr(aa,"k11")!=NULL)
{
JIDIANQI=0;
}
else if(strstr(aa,"k10")!=NULL)
{
JIDIANQI=1;
}
clearBuff();
Print_Str(CMGD);
delay(50);
while(Hand()==0);
clearBuff();
}
////////////////////////////////////////////////////////////////////////
}
}
void ser() interrupt 4
{
if(RI==1)
{ aa[j]=SBUF;//命令存到命令数组
RI=0; //软件清除接收中断
j++;
}
}