急求高手帮忙看下这个程序存在的错误,从网上找的源程序不全(单片机识别GSM短信,控制继电器开关)

jackyslin 2011-03-29 09:34:26
#include <reg51.h>
#include <string.h>
#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"))//若没发现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!='\0';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(RI)//
i++;
else
i=0;
RI=0;
TI=0;
}
ES=1; //开中断
}
...全文
179 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
cndfzx 2012-02-10
  • 打赏
  • 举报
回复
给你个比较全的程序,这个例子很好,很好扩展!
http://mydev.so/bbs/viewtopic.php?f=8&t=14&sid=bc325331b61f8baf3af1a385dcd75784

27,520

社区成员

发帖
与我相关
我的任务
社区描述
硬件/嵌入开发 单片机/工控
社区管理员
  • 单片机/工控社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧