彩虹等各位帅哥靓妹,请进来帮忙指点下TC35的C51驱动程序啊.【恢复】

2020-02-04 09:26发布

   看了网上有关TC35控制的介绍文章,但对其中一些内容还是不甚了解..特此请教....原文提供的源码驱动,我逐条学习加了注释,但由于缺少部分程序,无法构成完整的思路......特请予以帮助为谢.   特别是缺失的函数调用就更不明白了....

//TC35i驱动程序



//==============AT有关指令的定义(用于发送/接收英文短信)==========================

char code    AT_tc35[]="AT+";                                //连机

char code   Bps_tc35[]="IPR=9600";                        //波特率

char code  Text_tc35[]="CMGF=1";                        // 1为TEXT格式,0为PDU格式

char code  CSMP_tc35[]="CSMP=17,167,0,0";        //设置TEXT模式参数

char code  CSCS_tc35[]="CSCS=GSM";                        //设置为GSM字符集

char code  CMGS_tc35[]="CMGS=";                                //设置对方电话号码



char code  CMGL_tc35[]="CMGL=";                                //列出SIM卡中的短信



char code  Read_tc35[]="CMGR=";                                //读取信息

char code Erase_tc35[]="CMGD=";                                //删除信息

char code  Send_tc35[]="CMGS=";                                //发送信息

char code  Creg_tc35[]="CREG?";                                //注册





//*********************************************************

//串口初始化                                              *

//晶振11.0592MHz,方式1时,波特率由T1的溢出率和SMOD位确定.  *

//置SMOD=0时,9600波特率,对应T1的计数初值为0xfd.           *

//*********************************************************

void Uart_init(void)

{

        TMOD  = 0x21;        //T1工作于方式2(计数常数自动重装的8位计数),T0工作于方式1(16位计数).均为内部计数和启停

        SCON  = 0x50;        //01010000b=>串口工作于模式1.方式1,8位UART,波特率需设置,允许接收,发送/接收中断清零.

    PCON &= 0x7f;        //置串口波特率的倍增位SMOD=0,即不加倍.

    TL1   = 0xfd;   //9600BPS,11.0592MHz xtal,SMOD=0时,T1采用方式2计数的初值为0XFD.

    TH1   = 0xfd;

    TR1   = 1;                //启动计数器T1

        ES=1;                        //允许串行口发送/接收中断

}





//*********************************************************

//发送一个ASCII字符                                                                                  *

//*********************************************************

void SendASC(unsigned char ASC)

{

        bit es;                        //

        es=ES;                        //保护串口中断设置状态

        ES=0;                        //关闭串口中断

        TI=0;                        //发送/接收完一帧后TI会自动置位,请求中断.需要再次发送/接收时,要通过指令清零

        SBUF=ASC;                //发送内容送SBUF

        while(!TI);                //等待一帧发送完.

        TI=0;                        //TI清零,为下次发送/接收做准备

        ES=es;                        //恢复串口中断设置状态

}





//*******************************************************

//通过串口向TC35发送长度为Len的字符串.字符串指针为*p.   *

//*******************************************************

void Send2tc35(unsigned char *p,unsigned char Len)

{

        while(Len--)                //Len--为先使用Len的值,再执行Len-1运算

        {

                SendASC(*p++);

        }

}







//*******************************************************

//通信中断接收程序

//*******************************************************

unsigned char RsBuq[];



void Rs232_do(void) interrupt 4 using 1

{

//        unsigned char RsPoint=0;                        //新增的=====

        if(RI==1)                                                        //如果接收中断标志为1,则进入

                {

                        RI=0;                                                //将RI清零

                        RsBuq[RsPoint++]=SBUF;                //接收到的内容送RsBuq[]中

                        if (Rspoint>=sizeof(RsBuf))        //如果长度达到

                                {

                                        RsPoint=0;                        //FlagRs232=0

                                }                                                //数据处理

                }

}





//*******************************************************

//发送AT连机命令

//*******************************************************

char code AT_code[]="OK";



void Send_AT(void)

{

        unsigned char *p;

        while(1)

        {

                ClrRsBuf(RsBuf,sizeof(RsBuf));                //清缓存

                Send2tc35(AT_tc35,2);                                //发送"AT"

                SendASC(OVER);                                                //??发送结束标志

                        //等待应答"OK"

                ES=1;                                                                //串行口必须开中断

                delayms(50);                                                //

                P=strstr(RsBuf,AT_code);                        //

                if(p!=NULL)break;                                        //

        }

}



//********************************************

//发送bps连机命令                            *

//********************************************

void Send_BPS(void)

{

        Send2tc35(AT_tc35,3);                                        //"AT+"

        Send2tc35(Bps_tc35,sizeof(Bps_tc35)-1);        //"IPR=9600"

        SendASC(OVER);                                                        //回车符

}





//******************************************************

//功能:设置TEXT格式

//******************************************************

void SetText(void)

{

        Send2tc35(AT_tc35,3);                                                //"AT+"

        Send2tc35(Text_tc35,sizeof(Text_tc35)-1);        //"CMGF=1"

        SendASC(OVER);                                                                //回车符

        delayms(100);

}





//*****************************************************

//功能:删除短信息

//参数:index  拟删除的短信序号

//*****************************************************

unsigned char EraseMsg(unsigned char index)

{

        unsigned char *p,i=20;

        Send2tc35(AT_tc35,3);                                                //"AT+"

        Send2tc35(Erase_tc35,sizeof(Erase_tc35)-1);        //"CMGD="

        SendASC(index);                                                                //需要删除的短信序号

        SendASC(OVER);                                                                //回车符

        ES=1;                                                                                //允许串口中断

        while(i--)                                                                        //??等待应答OK

        {

                delayms(200);

                P=strstr(RsBuf,AT_code);                                //??判断是否应答OK

                if(P!=NULL){return 1;}

        }

        return 0;

}



//*****************************************************

//功能:读取短信息

//参数:index  拟读取的短信序号

//*****************************************************

char code Ask_no[]="+CMGR:0,,0";

char code ERROR[]="ERROR";

char code Ask_tc35[]="/";



unsigned char ReadMsg(unsigned char index)

{

        unsigned char *p,i;

        unsigned char Buf[40];

        Send2tc35(AT_tc35,3);                                                //"AT+"

        Send2tc35(Read_tc35,sizeof(Read_tc35)-1);        //"CMGR="

        SendASC(index);                                                                //序号

        SendASC(OVER);                                                                //回车符

        ES=1;                                                                                //必须串口允许中断

        delayms(600);

        /*------

        ES=0;

        Send2tc35(RsBuf,99);

        ES=1;

        */

        p=strstr(RsBuf,ERROR);                                //???判断是否应答ERROR?

        if(P!=NULL)

        {

                Send_AT();                                                //???

                return 0;

                p=strstr(RsZBuf,Ask_no);                //应答为"+CMGR:0,,0",表示无信息.

                if(p!=NULL)return 0;

                p=strstr(RsBuf,Ask_tc35);                //20个字节后是MSG

                if(p==NULL)return 0;

                p=p+21;

                for(i=0,i<sizeof(Buf);i++)

                {

                        Buf=*p++;

                }

                if(EraseMsg(index)==0)return 0;

                p=strcpy(RsZBuf,Buf);                        //放回RsBuf

                return 1;

        }

}







//***********************************************************

//发送短信息

//***********************************************************

char code SK[]=">";



void SendMsgStart(void)

{

        unsigned char *p,i=10;

        Send2tc35(AT_tc35,3);                                                //"AT+"

        Send2tc35(Send_tc35,sizeof(Send_tc35)-1);        //"CMGS="

        SendASC(YinHao);                                                        //"

        Send2tc35(Mp1.Hand,sizeof(Mp1.Hand));                //号码?????

        SendASC(YinHao);                                                        //"

        SendASC(OVER);                                                                //回车符

        ES=1;                                                                                //必须开串口中断

        while(i--)                                                                        //等待应答"> "

        {

                delayms(100);                                                        //Get">"

                p=strstr(RsBuf,SK);                                                //">"

                if(p!=NULL)

                {

                        ClrRsBuf(RsBuf,sizeof(RsBuf));

                        delayms(150);                                                //Get">"

                        break;

                }

        }

}







   
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。