/******************************************************************
** 单片机与控制TC35I读短信并控制相应的继电器动作
** 晶 振 频 率:11.0592M
** 线路->单片机实验开发板B
** open1---继电器1打开
** open2---继电器2打开
** close1--继电器1关闭
** close2--继电器2关闭
******************************************************************
基本概念:
MEM1:读取和删除短信所在的内存空间。
MEM2:写入短信和发送短信所在的内存空间。
MEM3:接收到的短信的储存位置。
语句:
AT+CPMS=?
作用:
测试命令。用于得到模块所支持的储存位置的列表。
AT+CPMS=?
+CPMS: ("MT","SM","ME"),("MT","SM","ME"),("MT","SM","ME")
表示手机支持MT(模块终端),SM(SIM卡),ME(模块设备)
其他指令请查阅TC35I AT指令集
*/
#include
#include
#define buf_max 72//缓存长度72
sbit jdq1=P2^0;//继电器1
sbit jdq2=P2^1;//继电器2
unsigned char i=0;
unsigned char *send_data;//要发送的数据
unsigned char rec_buf[buf_max];//接收缓存
void delay(unsigned int delay_time);//延时函数
bit hand(unsigned char *a);//判断缓存中是否含有指定的字符串
void clr_buf(void);//清除缓存内容
void clr_ms(void);//清除信息
void send_ascii(unsigned char *b);//发送ascii数据
void send_hex(unsigned char b);//发送hex数据
unsigned int htd(unsigned int a);//十六进制转十进制
void Serial_init(void);//串口中断处理函数
void main(void)
{
unsigned char k;
TMOD=0x20; //定时器1--方式2
TL1=0xfa;
TH1=0xfa; //11.0592MHZ晶振,波特率为4800
SCON=0x50; //方式1
TR1=1; //启动定时
ES=1;
EA=1;
for(k=0;k<20;k++)
delay(65535);
while(!hand("OK"))
{
jdq1=0;//用于指示单片机和模块正在连接
send_ascii("AT");//发送联机指令
send_hex(0x0d);
for(k=0;k<10;k++)
delay(65535);
}
clr_buf();
send_ascii("AT+CPMS="MT","MT","MT"");//所有操作都在MT(模块终端)中进行
send_hex(0x0d);
while(!hand("OK"));
clr_buf();
send_ascii("AT+CNMI=2,1");//新短信提示
send_hex(0x0d);
while(!hand("OK"));
clr_buf();
send_ascii("AT+CMGF=1");//文本方式
send_hex(0x0d);
while(!hand("OK"));
clr_buf();
clr_ms();//删除短信
jdq1=1;//单片机和模块连接成功
while(1)
{
unsigned char a,b,c,j=0;
if(strstr(rec_buf,"+CMTI")!=NULL)//若字符串中含有"+CMTI"就表示有新的短信
{
j++;
a=*(strstr(rec_buf,"+CMTI")+12);
b=*(strstr(rec_buf,"+CMTI")+13);
c=*(strstr(rec_buf,"+CMTI")+14);
if((b==0x0d)||(c==0x0d))
{
clr_buf();
send_ascii("AT+CMGR=");//发送读指令
send_hex(a);
if(c==0x0d)
send_hex(b);
send_hex(0x0d);
while(!hand("OK"));
if(strstr(rec_buf,"open1")!=NULL)//继电器1打开
jdq1=0;
else if(strstr(rec_buf,"close1")!=NULL)//继电器1关闭
jdq1=1;
else if(strstr(rec_buf,"open2")!=NULL)//继电器2打开
jdq2=0;
else if(strstr(rec_buf,"close2")!=NULL)//继电器2关闭
jdq2=1;
clr_buf();
clr_ms();//删除短信
}
}
}
}
/**************************发送字符(ASCII码)函数*********************/
/*函数原型:void send_ascii(unsigned char *b)
/*函数功能:发送字符(ASCII码)
/*输入参数:unsigned char *b
/*输出参数:无
/*调用模块:无
/******************************************************************/
void send_ascii(unsigned char *b)
{
ES=0;
for (b; *b!='�';b++)
{
SBUF=*b;
while(TI!=1)
;
TI=0;
} ES=1;
}
/**************************发送字符(十六进制)函数*********************/
/*函数原型:void send_ascii(unsigned char b)
/*函数功能:发送字符(十六进制)
/*输入参数:unsigned char b
/*输出参数:无
/*调用模块:无
/******************************************************************/
void send_hex(unsigned char b)
{
ES=0;
SBUF=b;
while(TI!=1)
;
TI=0; ES=1;
}
/**************************清除缓存数据函数****************************/
/*函数原型:void clr_buf(void)
/*函数功能:清除缓存数据
/*输入参数:无
/*输出参数:无
/*调用模块:无
/**********************************************************************/
void clr_buf(void)
{
for(i=0;i<buf_max;i++)
rec_buf[i]=0;
i=0;
}
/****************************清除短信函数*****************************/
/*函数原型:void clr_ms(void)
/*函数功能:清除短信
/*输入参数:无
/*输出参数:无
/*调用模块:无
/**********************************************************************/
void clr_ms(void)
{
unsigned char a,b,c,j;
send_ascii("AT+CPMS?");//删除短信
send_hex(0x0d);
while(!hand("OK"));
a=*(strstr(rec_buf,"+CPMS")+12);
b=*(strstr(rec_buf,"+CPMS")+13);
c=*(strstr(rec_buf,"+CPMS")+14);
clr_buf();
if(b==',')
{
for(j=0x31;j<(a+1);j++)
{
send_ascii("AT+CMGD=");//
send_hex(j);
send_hex(0x0d);
while(!hand("OK"));
clr_buf();
}
}
else if(c==',')
{
for(j=1;j<((a-0x30)*10+(b-0x30)+1);j++)
{
send_ascii("AT+CMGD=");//
if(j<10)
send_hex(j+0x30);
else
{
send_hex((htd(j)>>4)+0x30);
send_hex((htd(j)&0x0f)+0x30);
}
send_hex(0x0d);
while(!hand("OK"));
clr_buf();
}
}
}
/*****************判断缓存中是否含有指定的字符串函数******************/
/*函数原型:bit hand(unsigned char *a)
/*函数功能:判断缓存中是否含有指定的字符串
/*输入参数:unsigned char *a 指定的字符串
/*输出参数:bit 1---含有 0---不含有
/*调用模块:无
/**********************************************************************/
bit hand(unsigned char *a)
{
if(strstr(rec_buf,a)!=NULL)
return 1;
else
return 0;
}
/**************************十六进制转十进制函数**************************/
/*函数原型:uint htd(uint a)
/*函数功能:十六进制转十进制
/*输入参数:要转换的数据
/*输出参数:转换后的数据
/*调用模块:无
/******************************************************************/
unsigned int htd(unsigned int a)
{
unsigned int b,c;
b=a%10;
c=b;
a=a/10;
b=a%10;
c=c+(b<<4);
a=a/10;
b=a%10;
c=c+(b<<8);
a=a/10;
b=a%10;
c=c+(b<<12);
return c;
}
/*******************************延时函数*********************************/
/*函数原型:delay(unsigned int delay_time)
/*函数功能:延时函数
/*输入参数:delay_time (输入要延时的时间)
/*输出参数:无
/*调用模块:无
/**********************************************************************/
void delay(unsigned int delay_time) //延时子程序
{for(;delay_time>0;delay_time--)
{}
}
/*******************************串口中断处理函数*********************************/
/*函数原型:void Serial(void)
/*函数功能:串口中断处理
/*输入参数:无
/*输出参数:无
/*调用模块:无
/**********************************************************************/
void Serial() interrupt 4 //串口中断处理
{
unsigned char k=0;
ES=0; //关中断
if(TI) //发送
{
TI=0;
}
else //接收,处理
{
RI=0;
rec_buf[i]=SBUF;
if(i<buf_max)
i++;
else
i=0;
RI=0;
TI=0;
}
ES=1; //开中断
}
上一篇:红外线遥控解码实验
下一篇:1602A液晶屏的驱动实验