⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 debug.lst

📁 51单片机收发短信的程序
💻 LST
📖 第 1 页 / 共 5 页
字号:
 787   2                 RcvIndex=0;
 788   2         }
 789   1        EA=1;
 790   1      }
 791          /////////////////////function: send string////////////////////////////////////////
 792          void send_string(char  * str)
 793          {
 794   1        char idata i=0;
 795   1        uchar idata len=strlen(str);
 796   1        for(i=0;i<len;i++)
 797   1        {
 798   2          SBUF=str[i];
 799   2          while(!TI){}
C51 COMPILER V7.02a   DEBUG                                                                04/01/2005 10:28:51 PAGE 14  

 800   2          TI=0;
 801   2        }
 802   1      }
 803          /////////////手机模块初始化函数///////////////////////////////////////////////////////////
 804          char init_GSM()
 805          {
 806   1        char idata result=0 ,i;
 807   1        IGT=0;
 808   1        delay_1s();
 809   1        delay_1s();
 810   1        delay_1s();
 811   1        send_string("AT\r");            //test  AT command
 812   1        delay_1s();
 813   1        send_string("ATE0\r");            //Enable command echo
 814   1        delay_160ms();
 815   1        put_string(0x80,RcvBuf);    ///////for debug
 816   1        result=find_OK_ERROR()+result;
 817   1        send_string("AT+CMGF=0\r");       //Select SMS message formate(选择短消息信息格式:0-PDU;1-文本)
 818   1        delay_160ms();
 819   1        put_string(0x80,RcvBuf);    ///////for debug
 820   1        delay_1s();      //////////for debug
 821   1        result=find_OK_ERROR()+result;
 822   1        send_string("AT+CNMI=0,0,0,0,1\r");
 823   1        delay_160ms();
 824   1        put_string(0x80,RcvBuf);    ///////for debug
 825   1        delay_1s();      //////////for debug
 826   1        result=find_OK_ERROR()+result;
 827   1        send_string("AT+CMEE=2\r");       //Report mobile equipment error
 828   1        delay_160ms();
 829   1        put_string(0x80,RcvBuf);    ///////for debug
 830   1        delay_1s();      //////////for debug
 831   1        result=find_OK_ERROR()+result;
 832   1        send_string("AT+CPMS?\r");
 833   1        delay_160ms();
 834   1        put_string(0x80,RcvBuf);    ///////for debug
 835   1        delay_1s();      //////////for debug
 836   1        result=find_OK_ERROR()+result;
 837   1        send_string("AT+CREG?\r");  //simply assume registered,refine later
 838   1        delay_160ms();
 839   1        put_string(0x80,RcvBuf);    ///////for debug
 840   1        delay_1s();      //////////for debug
 841   1        result=find_OK_ERROR()+result;
 842   1        send_string("AT+CFUN=1\r");  //set ME to full function
 843   1      //  send_string("AT+CFUN=1,1\r");  //reset and restart ME to full function
 844   1        delay_160ms();
 845   1        put_string(0x80,RcvBuf);    ///////for debug
 846   1        delay_1s();      //////////for debug
 847   1        send_string("AT+COPS=0\r");  //GSM network operator is to be selected automatically
 848   1        for(i=0;i<9;i++)
 849   1          delay_1s();
 850   1        send_string("AT+COPS?\r");  //read the status to GSM network operator
 851   1        delay_160ms();
 852   1      //  send_string("AT+CPAS\r");  //Mobile equipment activity status 0:ready 3:incoming call
 853   1      //  delay_160ms();               //for debug
 854   1      //  send_string("AT+WS46=12\r");  //select GSM network
 855   1      //  delay_160ms();      //////////for debug
 856   1        if(result==0||result==1)
 857   1        {
 858   2          put_string(0x80,"success");      /////////for debug
 859   2          delay_1s();        /////////////for debug
 860   2          return 0 ;
 861   2        }
C51 COMPILER V7.02a   DEBUG                                                                04/01/2005 10:28:51 PAGE 15  

 862   1        else
 863   1        {
 864   2          put_string(0x80,"failed") ;       /////////for debug
 865   2          return 1;
 866   2        }
 867   1      }
 868          /////在RcvBuf的前80个字符中寻找第一个出现的字符串是"OK"或者"ERROR"还是都没有////
 869          //////////////如果是OK就返回0,是ERROR就返回1,没找到就返回2///////////////////
 870          char find_OK_ERROR()
 871          {
 872   1        char i;
 873   1        for(i=0;i<140;i++)
 874   1        {
 875   2          if(RcvBuf[i]==0) return 2;
 876   2          if(strncmp(RcvBuf+i,"OK",2)==0)  return 0;
 877   2          else if(strncmp(RcvBuf+i,"ERROR",5)==0)  return 1;
 878   2        }
 879   1        return 3;
 880   1      }
 881          
 882          //////////////function:           ///////////////////////////////////////////
 883          void receive_SMS()
 884          {
 885   1        uint  ii;
 886   1        char  cmgl_state=0;
 887   1        uchar  j;
 888   1        RcvIndex=0;
 889   1        bResponsed=0;
 890   1        memset(RcvBuf,0,sizeof(RcvBuf));   //接收前清空接收缓冲区
 891   1        send_string("AT+CMGL=4\r");
 892   1        wait_5s();
 893   1        if(bListEnd)                       //如果有短信存在,则在GSM中断中会将bListEnd置1
 894   1        {
 895   2          for(ii=0;ii<listchar_count;ii++)
 896   2          {
 897   3                  if(cmgl_state==0)
 898   3            {
 899   4              if(ListBuf[ii]=='L')
 900   4                    {
 901   5                      cmgl_state=1;
 902   5                    list_index_begin=ii;
 903   5              }
 904   4                  }
 905   3            else if(cmgl_state==1)
 906   3                  {
 907   4                          if(ListBuf[ii]==LF)
 908   4                          {
 909   5                            delay();
 910   5                          list_index_end=strpos(ListBuf+list_index_begin,',');
 911   5                      list_index_end=list_index_begin+list_index_end;
 912   5                            memcpy(list_indexlist+listcmdindex,ListBuf+list_index_begin+2,list_index_end-list_index_begin-2);
 913   5                listcmdindex+=list_index_end-list_index_begin-1;
 914   5                            list_indexlist[listcmdindex-1]=0;
 915   5                            listcmd_count++;                               //短信条数
 916   5                            list_PDU_begin=ii+1;                           //pdu的起始地址即LF后一字节
 917   5                            if(ListBuf[list_index_end+1]=='0')             //判断短信是否为已读状态
 918   5                          cmgl_state=2;
 919   5                            else
 920   5                                    cmgl_state=0;
 921   5                          }
 922   4                        }
 923   3              else if(cmgl_state==2)                                      //处理未读的短信
C51 COMPILER V7.02a   DEBUG                                                                04/01/2005 10:28:51 PAGE 16  

 924   3                    {
 925   4                            if(ListBuf[ii]==LF)
 926   4                          {
 927   5                              list_PDU_end=ii-2;
 928   5                              memcpy(PDUBuf,ListBuf+list_PDU_begin,list_PDU_end-list_PDU_begin+1);
 929   5                              PDUBuf[list_PDU_end-list_PDU_begin+1]=0;
 930   5                              PDU2SMS(PDUBuf);
 931   5                        Data_processing();
 932   5                        cmgl_state=0;
 933   5                  wait_5s();
 934   5                }
 935   4              }
 936   3                }
 937   2                //delete message
 938   2                listcmdindex=0;
 939   2                for(j=0;j<listcmd_count;j++)
 940   2                {
 941   3                  strcpy(CMDBuf,"AT+CMGD=");
 942   3            strcpy(CMDBuf+strlen(CMDBuf),list_indexlist+listcmdindex);
 943   3                  listcmdindex+=strlen(list_indexlist+listcmdindex)+1;
 944   3                  CMDBuf[strlen(CMDBuf)]='\r';
 945   3                  CMDBuf[strlen(CMDBuf)]=0;
 946   3                  send_string(CMDBuf);
 947   3            wait_5s();
 948   3                }
 949   2           // state variable reset
 950   2          listcmd_count=0;
 951   2          bListEnd=0;
 952   2          listchar_count=0;
 953   2          list_index_begin=0;
 954   2          list_index_end=0;
 955   2          listcmdindex=0;
 956   2          memset(ListBuf,0,sizeof(ListBuf));
 957   2          memset(PDUBuf,0,sizeof(PDUBuf));
 958   2          memset(list_indexlist,0,sizeof(list_indexlist));
 959   2        }
 960   1      }
 961          /////////////function: 把两ascii字节合并成一char 例如"35"变为0x35;//////////////
 962          uchar hex2int(uchar * str)
 963          {
 964   1          uchar  highbyte;
 965   1          uchar    lowbyte;
 966   1      
 967   1          highbyte = toint(str[0]);   //asii to 0x00~0x09 OR 0x0a~0xf
 968   1          lowbyte  = toint(str[1]);
 969   1          return highbyte*16+lowbyte;
 970   1      }
 971          /////////function: 把char变为字符串 例如0x36 "36"/////////////////////////////
 972          void int2hex(uchar  num,uchar *dest)
 973          {
 974   1          uchar   highbyte;
 975   1          uchar   lowbyte;
 976   1      
 977   1          highbyte = num/16;
 978   1          lowbyte  = num-highbyte*16;
 979   1          if(highbyte>9)
 980   1            dest[0]=highbyte+ALPHABASE;
 981   1          else
 982   1            dest[0]=highbyte+NUMBASE;
 983   1      
 984   1          if(lowbyte>9)
 985   1            dest[1]=lowbyte+ALPHABASE;
C51 COMPILER V7.02a   DEBUG                                                                04/01/2005 10:28:51 PAGE 17  

 986   1          else
 987   1            dest[1]=lowbyte+NUMBASE;
 988   1      
 989   1          dest[2]=0;
 990   1      }
 991          ///function: PDU 7位编码程序,将源字符串数组str转换为手机可以识别的dest字符串 SMS->PDU///
 992          uchar PDU7BitEncode(char * str,char  *  dest)
 993          {
 994   1        uchar  nSrc;  // 源字符串的计数值
 995   1        uchar  nChar; // 当前正在处理的组内字符字节的序号,范围是0-7
 996   1        uchar  nLeft; // 上一字节残余的数据
 997   1        uchar  nSrcLength = strlen(str);
 998   1        char   * pSrc = str;
 999   1        char   * pDst = dest;
1000   1        // 计数值初始化
1001   1        nSrc = 0;
1002   1        // 将源串每8个字节分为一组,压缩成7个字节
1003   1        // 循环该处理过程,直至源串被处理完
1004   1        // 如果分组不到8字节,也能正确处理
1005   1        while(nSrc<=nSrcLength)
1006   1        {
1007   2          // 取源字符串的计数值的最低3位
1008   2          nChar = nSrc&7;
1009   2          // 处理源串的每个字节
1010   2          if(nChar == 0)
1011   2          {
1012   3            // 组内第一个字节,只是保存起来,待处理下一个字节时使用
1013   3            nLeft = *pSrc;
1014   3          }
1015   2          else
1016   2          {
1017   3            // 组内其它字节,将其右边部分与残余数据相加,得到一个目标编码字节
1018   3            uchar  temp;
1019   3                  temp  = (*pSrc<<(8-nChar))|nLeft;
1020   3                  int2hex(temp,pDst);
1021   3            // 将该字节剩下的左边部分,作为残余数据保存起来
1022   3            nLeft = *pSrc >> nChar;
1023   3            // 修改目标串的指针和计数值
1024   3            pDst=pDst+2;
1025   3          }
1026   2          // 修改源串的指针和计数值
1027   2          pSrc++;
1028   2          nSrc++;
1029   2        }
1030   1        *pDst=0;
1031   1              return (pDst-dest)/2;
1032   1      }
1033          ///////////////function: decode PDU to SMS//////////////////////////////////////
1034          void PDU7BitDecode(char  * str,char  * dest)
1035          {
1036   1        int  length=strlen(str)/2;
1037   1        uchar   remainder=0;
1038   1        uchar   j=0;
1039   1        uchar   i=0;
1040   1        uchar   tt=0;
1041   1        uchar   original;
1042   1        uchar   index;
1043   1        uchar   tmp;
1044   1        for(i=0;i<length;i++)
1045   1        {
1046   2          original=hex2int(str+2*i);
1047   2      
C51 COMPILER V7.02a   DEBUG                                                                04/01/2005 10:28:51 PAGE 18  

1048   2                      index=i%7;
1049   2          tt=(i+1)%7;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -