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

📄 gsm.c

📁 基于8051的GSM模块的应用
💻 C
字号:
//
//
//
//
//

#include "mydefine.h"
#include "cpu.h"
#include "oled.h"
unsigned char CtrlZ=0x1A;                                                            // Ctrl+Z


extern My_state My_flag;
///////////////////////////////////////////////////////////////////////////////////////////                                                //  PDU码固定部分第二部分     这些前的PUD总长度为13 第一个00不计算在内

unsigned char code atf[]="at&f\r\n";                                               //系统复位指令
unsigned char code at[]="at\r\n";                                                   //握手信号       返回值at...OK..
unsigned char code ate0[]="ate0\r\n";                                               //关闭回显后其他的指令之返回..0K..
unsigned char code atipr[]="at+ipr=115200\r\n";                                     //波特率设置    返回值
unsigned char code atcmgf[]="at+cmgf=0\r\n";                                        //短信信息格式设置PDU格式
unsigned char code atcnmi[]="AT+CNMI=1,1,0,0,1\r\n";                                //设置短信接收并获取指令  返回 ..+CMTI: "SM",【index】..
unsigned char code atcmgr[]="at+cmgr=1\r\n";                                        //读出短信 索引号为1
unsigned char code atcmgd[]="at+cmgd=1\r\n";                                        //删除索引号为为1的短信
unsigned char code atcmgs[]="AT+CMGS=0";	                                        ////发短信指令 PDU码总长度-1  13+1+数据长度
unsigned char code enter[]="\r\n";

//下面这段为回应码
unsigned char code ACK_atf[11]={0x61,0x74, 0x26, 0x66,0x0D,0x0D, 0x0A, 0x4F, 0x4B, 0x0D, 0x0A};        //AT&F...OK.
unsigned char code ACK_at[9]={0x61,0x74, 0x0D, 0x0D, 0x0A, 0x4F, 0x4B, 0x0D, 0x0A};                     //at...OK..
unsigned char code ACK_ate0[11]={0x61,0x74,0x65,0x30, 0x0D, 0x0D, 0x0A, 0x4F, 0x4B, 0x0D, 0x0A};        //ate0...OK..
unsigned char code ACK_ok[6]={0x0D, 0x0A, 0x4F, 0x4B, 0x0D, 0x0A};                                      //..OK..

unsigned char code ACK_atcmgs[4]={0x0d,0x0a,0x3e,0x20};                                                 //..>   
unsigned char code ACK_send[7]={0x0D,0x0A,0x2B,0x43,0x4D,0x47,0x53} ;                                   //..+cmgs  
unsigned char code ACK_receive[7]={0x0d,0x0a,0x2b,0x43,0x4d,0x54,0x49};                                  //..+cmti  这是RxBuf[14]存的是短信索引号
unsigned char code PDU1[]="0011000BA1";                                             //  PDU码固定部分第一部分
unsigned char code Phone[]="3154670952F0";                                          //  目的手机号码 //张爱民 3107775375F4  阿彪  3154670952F0//13457690250  速  3184311382F3
unsigned char code PDU2[]="0008A7";
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
//*****************************************************************************
//函数名:unsigned char CheckAck(const unsigned char *str,unsigned char cnt) 
//功能描述:将指定的字符串和RxBuf中的字符串相比较,看是否相等
//*****************************************************************************
unsigned char Check_Ack(const unsigned char *str,unsigned char cnt)	     
{
	unsigned char i;
	rxCnt =0;			                                                             //队列数据长度在这个时候清0
    My_flag.Recive =0x00;                                                             //接收标致清除
	for(i=0;i<cnt;i++)
	{
		if(str[i] != RxBuf[i])
	    return 0;
	}
     Delay(5000);
	return 1;
}
//*****************************************************************************
//函数名:void GSM_IGT(void)
//功能描述:gsm模块点火
//*****************************************************************************
void GSM_IGT(void)
{
    IGT = 1;
    Delay(50000);
    Delay(50000);
    Delay(50000);
    IGT = 0;
    Delay(50000);
    Delay(50000);
    Delay(50000);
    Delay(50000);

}
//*****************************************************************************
//函数名:void InitGSM(void)
//功能描述:gsm模块初始化 
//*****************************************************************************
void Init_GSM(void)
{
    rxCnt = 0;
    UartSendString(atf);             //发送AT&f,握手  返回  at...OK..
    Delay(50000);
    UartSendString(at);              //发送AT,握手  返回  at...OK..
    Delay(50000);
    UartSendString(ate0);           
    Delay(50000);
    UartSendString(atipr);          ///设置波特率
    Delay(50000);
    UartSendString(atcmgf);         //设置PDU格式 
    Delay(50000);
    rxCnt=0;
    UartSendString(atcnmi);         //设置新信息到达时自动提示   
    Delay(50000);
    if(Check_Ack(ACK_ok,6));
}


//*****************************************************************************
//函数名:unsigned char SendMy(unsigned char *pbuf,unsigned char cnt)
//功能描述:发送短信  pbuf为想要发送的数据缓冲区的数据,cnt将发送数据的长度-普通字节的长度
//          转化为PDU是长度要除以2
//*****************************************************************************

unsigned char datalgh; 
unsigned char Send_Msg(unsigned char *pbuf,unsigned char cnt)    
{
     unsigned char i,x,y;
     unsigned char dlength;
     rxCnt=0;
     UartSendString(atcmgs);
     dlength =13+1+cnt;                                           //总的PDU编码长度
     UartSendChar(0x30|(dlength/10));                             //十进制转换为字符
     UartSendChar(0x30|(dlength%10));
     UartSendString(enter);                                          //命令发送确认
     Delay(50000);
    if(Check_Ack(ACK_atcmgs,4))
    {
        UartSendString(PDU1);                                       //发送数据的编码过程
        UartSendString(Phone);
        UartSendString(PDU2);                          
        x = cnt/16;                                                 //区分数字和字母
        if(x<0x0a)  
            {    UartSendChar(0x30|x); }                            //将我的数据长度高四位16进制转换为字符  
        else     UartSendChar(0x37+x);
        y = cnt%16;
        if(y<0x0a)
            {   UartSendChar(0x30|y);  }                            //将我的数据长度低四位16进制转换为字符  
        else    UartSendChar(0x37+y);
        for(i=0;i<cnt;i++)
        {
            /* UartSendChar(0x30);
            UartSendChar(0x30);
            UartSendChar(0x33);
            UartSendChar(0x30|(*pbuf));
            pbuf++;*/
           x= *pbuf/16;
            if(x<0x0a)  
                {    UartSendChar(0x30|x); }                        //将我的数据长度高四位16进制转换为字符  
            else     UartSendChar(0x37+x);
            y = *pbuf%16;
            if(y<0x0a)
                {   UartSendChar(0x30|y);  }                        //将我的数据长度低四位16进制转换为字符  
            else    UartSendChar(0x37+y);
                pbuf++;
        }

        UartSendChar(CtrlZ);                                        //发送确认  
        rxCnt =0;                               
    }
    else return 0;                                                  //启动发送短信失败
    Delay(50000);                                                   //延时要足够
    if(Check_Ack(ACK_send,7))
    {
        rxCnt=0;
        return 1;                                                   //发送成功
    }
    else return 1;                                 
}
//*****************************************************************************
//函数名:unsigned char ReadMy(void)
//功能描述:读出短信
//*****************************************************************************
unsigned char Read_Msg(void)	
{

    if(Check_Ack(ACK_receive,7))                                      //确认是接收到短信提示                               
    {
//	    UartSendString(atcmgr);                                       //读出短信INDEX为1的短信   
        Delay(50000);                                                 //需要接一定的延时来
       
	    UartSendString(atcmgd);                                       //删除短信INDEX为1的短信  
        Delay(50000); 
        Check_Ack(ACK_ok,6);
        return 1 ;      
    }   
    else  return 0;
}

⌨️ 快捷键说明

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