guiyiwofo 发表于 2010-6-25 02:54:16

谁能告诉我程序哪有问题,关于TC35i

功能说明 :发送open1可以控制继电器1的开,open2可以控制继电器2的开,close1控制继电器1的闭,close2控制继电器2的闭
程序如下
#include<reg51.h>       
#include <string.h>       

#define buf_max 72//缓存长度72

sbit jdq1=P0^0;//继电器1
sbit jdq2=P0^1;//继电器2

unsigned char i=0;
unsigned char *send_data;//要发送的数据
unsigned char rec_buf;//接收缓存

void delay(unsigned int delay_time);//延时函数   
bit hand(unsigned char *a);//判断缓存中是否含有指定的字符串
void clr_buf(void);//清除缓存内容
void clr_ms(void);//清除信息
voidSendString(unsigned char Data_buffer[]);
void send_hex(unsigned char b);//发送hex数据
unsigned int htd(unsigned int a);//十六进制转十进制
voidSerial_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;//用于指示单片机和模块连接
      SendString("AT");//发送联机指令
          send_hex(0x0d);
          for(k=0;k<100;k++)
                delay(250);       
                //        break ;       
        }             
        clr_buf();
        SendString("AT+CPMS=\"SM\",\"SM\",\"SM\"");//所有操作都在MT(模块终端)中进行
        send_hex(0x0d);
        while(!hand("+CPMS"));
        clr_buf();
        SendString("AT+CNMI=2,1");//新短信提示
        send_hex(0x0d);
        while(!hand("OK"));
        clr_buf();
        SendString("AT+CMGF=0");//文本方式
        send_hex(0x0d);
        while(!hand("OK"));       
        clr_buf();
    //clr_ms();//删除短信
        //jdq1=0;//单片机和模块连接成功
        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();
                        SendString("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 SendString(unsigned char data_buffer[])
{   unsigned char i=0;
    while(data_buffer!='\0')
   {
        SBUF=data_buffer;
        while(TI==0);
        TI=0;
   }
}


/**************************发送字符(十六进制)函数*********************/
/*函数原型: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=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=SBUF;
                if(i<buf_max)
                  i++;
                else
                  i=0;
                RI=0;
                TI=0;
        }
        ES=1;       //开中断
}

guiyiwofo 发表于 2010-6-25 02:55:26

帮帮我呀,我快被TC35i弄死了。

guiyiwofo 发表于 2010-6-25 06:53:28

自己顶一顶

guiyiwofo 发表于 2010-6-25 13:28:58

来人呀。

newc 发表于 2010-6-25 13:30:35

你不来点儿心得体会?

guiyiwofo 发表于 2010-6-25 21:38:31

995

guiyiwofo 发表于 2010-6-26 00:58:23

自己支持

guiyiwofo 发表于 2010-6-26 19:20:36

895

guiyiwofo 发表于 2010-6-27 19:16:51

996

guiyiwofo 发表于 2010-7-3 17:29:49

空间没有人

guiyiwofo 发表于 2010-7-4 07:45:09

看来要自己慢慢来研究了。

god-father 发表于 2010-7-4 09:30:50

其它功能先去掉 先调通TC35I再加上

guiyiwofo 发表于 2010-7-4 13:33:42

程序可以实现接收短信了,但是不能读短信里德内容,和进行电话号码的判断。现在是我改后的程序
功能:能够判定是谁发来的号码,并且根据接收到的内容进行操作,如我手机发“解防”,单片机的P0^0灯会先亮在灭,别的手机发短信,灯只会亮而不灭。
#include<reg51.h>       
#include <string.h>
#include"intrins.h"       
sbit Spk=P0^4;
#define buf_max 90//缓存长度72
sbit jdq1=P0^0;// 灯1
sbit jdq2=P0^1;//灯2
unsigned char i=0;        
sbit StartTc35=P3^2;//输出100mS低电平
unsigned char rec_buf;//接收缓存
void delay(unsigned int ms);//延时函数   
bit hand(unsigned char *a);//判断缓存中是否含有指定的字符串
void clr_buf(void);//清除缓存内容
voidSendString(unsigned char data_buffer[]);//发送命令
void send_hex(unsigned char b);//发送hex数据
voidSerial_init(void);//串口中断处理函数
unsigned char AT="AT\r";       
unsigned char   BO[]="AT+IPR=0\r";
unsigned char SM[]="AT+CPMS=\"SM\",\"SM\",\"SM\"\r"          ;
unsignedchar CNMI[]="AT+CNMI=2,1\r";       //有短信息提示
unsigned char CMGF[]="AT+CMGF=0\r";          //PDU编码
unsigned char CMGR[]="AT+CMGR=";//读取短信息
unsigned char *p;
void main(void)
{
    TMOD=((TMOD&0x0F)|0x20);//定时器1--方式1
    TL1=0xfd;            
    TH1=0xfd;            //11.0592MHZ晶振,波特率为9600
    SCON=0x50;         //方式1
    TR1=1;               //启动定时       
        ES=1;
        EA=1;
        jdq1=1;
        StartTc35=0;
        delay(100) ;
        StartTc35=1;
        Spk=0;delay(120);Spk=1;delay(120);
        Spk=0;delay(120);Spk=1;delay(120);//启动TC35i模块
        SendString(AT);//发送联机指令
        delay(1000);if(hand("OK")) jdq1=0;delay(1000);             
        clr_buf();SendString(BO);delay(1000); clr_buf();
        SendString(SM);delay(1000);//所有操作都在SM中操作
        while(!hand("+CPMS"));delay(1000);
        clr_buf();
        SendString(CNMI);delay(1000);//新短信提示
        while(!hand("OK"));
        clr_buf();
        SendString(CMGF);jdq1=0;delay(1000);//PDU
        while(!hand("OK"));        jdq1=1;
        clr_buf();
    //clr_ms();//删除短信
        //jdq1=0;//单片机和模块连接成功
        while(1)
        {
          unsigned char a,b,c;       
          if(strstr(rec_buf,"+CMTI")!=NULL)//若字符串中含有"+CMTI"就表示有新的短信
                {
                  
                  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();
                        SendString(CMGR);//发送读指令
                          send_hex(a);
                                if(c==0x0d)
                                send_hex(b);
                                send_hex(0x0d);//        send_hex(0x0A);
                          delay(1000);   while(!hand("OK"));        jdq1=0;
                               if(strstr(rec_buf,"2125107312088418")!=NULL)
                        {delay(100);jdq1=1;if(strstr(rec_buf,"89E39632")!=NULL)
                                  jdq1=0;
                               }

                clr_buf();                       
                }


}
}
}


/*************************发送函数********/
void SendString(unsigned char data_buffer[])
{   unsigned char j=0;
    while(data_buffer!=0x00)
   {
        SBUF=data_buffer;
        while(TI==0);
        TI=0;
   } j=0;
}


/**************************发送字符(十六进制)函数******************/
/******************************************************************/
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=0;
    i=0;

}

/*****************判断缓存中是否含有指定的字符串函数******************/
/*函数原型: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 ms)
{
int k ;
while(ms--)
{
for(k=0 ; k<250 ; k++)
{
_nop_() ;
_nop_() ;
_nop_() ;
_nop_() ;
}
}}

/*******************************串口中断处理函数*********************************/
/*函数原型:void Serial(void)
/*函数功能:串口中断处理
/*输入参数:无
/*输出参数:无
/*调用模块:无
/**********************************************************************/
void Serial() interrupt 4//串口中断处理
{       
    //unsigned char k=0;
        ES=0;       //关中断
if(RI)       //接收,处理
        {       
                RI=0;
                rec_buf=SBUF;
                if(i<buf_max)
                  i++;
                else
                  i=0;
                RI=0;
        }
        ES=1;       //开中断
}

guiyiwofo 发表于 2010-7-7 15:36:41

问题已经解决了

biaogreat 发表于 2010-11-18 21:28:06

怎么解决的?能否透露一些??、

guiyiwofo 发表于 2011-5-26 20:56:31

是程序的问题,见上面的程序。

comway 发表于 2011-5-27 19:05:30

comway的w801g模块可以直接实现lz的功能,无需编程
页: [1]
查看完整版本: 谁能告诉我程序哪有问题,关于TC35i