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

📄 attention.c.bak

📁 使用avr--cc2420模块程序,需要原理图的联系 QQ:120304948
💻 BAK
字号:
#include <mega128.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>


#include "typedef.h"

#include "app.h"
#include "attention.h"
#include "Flash.h"
#include "g20modem.h"
#include "g20serialdrv.h"
#include "ombro.h"

#include "rtc.h"
#include "serialdrv.h"
#include "timer.h"
#include "UDP.h"
#include "protocol.h"

#include "key.h"
#include "lcdzk.h"
#include "print.h"
#include "ombrodb.h"

#define RETRY_TIMES 5

unsigned char bnumber[25];


unsigned char chEnd[4] = 
{
	0x1A, 0x1D, 0x1A, 0x1A
};

IP_ADDR my_ipaddr;

unsigned char attention_at()
{
    bit bRet = FALSE;
    volatile UINT nLoop = RETRY_TIMES;
    g20_sp_init_buf();
    while(nLoop -- > 0)
    {
	    wdr();
        g20_sp_send("AT\r");
        
		bRet = g20_sp_waitfor("OK", NULL, "ERROR",NULL, NULL, 2000);
		if(bRet)
			return TRUE;
    }
    return FALSE;    
}

unsigned char attention_atz()
{
    bit bRet = FALSE;
    volatile UINT nLoop = RETRY_TIMES;
    g20_sp_init_buf();
    while(nLoop -- > 0)
    {
        wdr();
        g20_sp_send("ATZ\r");
        bRet = g20_sp_waitfor("OK", NULL, "ERROR", NULL, NULL, 2000);
        if(bRet)
			return TRUE;
    }
	return FALSE;
}

unsigned char attention_ate(unsigned char bDisplay)
{
    bit bRet = FALSE;
    volatile UINT nLoop = RETRY_TIMES;
    g20_sp_init_buf();   
    while(nLoop -- > 0)
    {
        wdr();
        if(bDisplay)
            g20_sp_send("ATE1\r");
        else
            g20_sp_send("ATE0\r");
        bRet = g20_sp_waitfor("OK",NULL, "ERROR", NULL, NULL, 2000);
        if(bRet)
            return TRUE;
    }
    return FALSE;    
}

unsigned char attention_cpin()
{
    bit bRet = FALSE;
    volatile UINT nLoop = RETRY_TIMES;
    g20_sp_init_buf();  
    while(nLoop -- > 0)
    {
        wdr();
        g20_sp_send("AT+CPIN?\r");
        bRet = g20_sp_waitfor("+CPIN: READY", "PIN: READY", "ERROR", NULL, NULL, 200);
        if(bRet)
        	return TRUE;
    }
	return FALSE;
}

unsigned char attention_csq()
{    
	bit bRet = FALSE;
	volatile UINT nLoop = RETRY_TIMES;
	volatile UINT nGetChar = 0;
	volatile BYTE chTemp = 0x00;
	volatile UINT nCSQ;

    nCSQ = 0;
    g20_sp_init_buf();
	while(nLoop -- > 0)
	{
        wdr();
		g20_sp_send("AT+CSQ?\r");
		bRet = g20_sp_waitfor("+CSQ:",NULL,"ERROR", NULL, NULL, 2000);
		if(bRet == TRUE)
		{
		    
			for(nGetChar = 0; nGetChar < 4; nGetChar ++)
			{
			    chTemp = 0x00;			 
				chTemp = g20_sp_getchar();
				if((chTemp >= (BYTE)0x30)&&(chTemp <= (BYTE)0x39))
				{
					//nCSQ = nCSQ + (UINT)(chTemp - 0x30)*(10^(3-nGetChar));
                    // (10^(3-nGetChar)    ~~~~~~~~~~~~~~~ ERROR: 10^0 = 10
                    switch(nGetChar)
                    {
                    case 1:
                        nCSQ = nCSQ +(UINT)(chTemp - 0x30)*100;
                        break;
                    case 2:
                        nCSQ = nCSQ + (UINT)(chTemp - 0x30)*10;
                        break;
                    case 3:
                        nCSQ = nCSQ + (UINT)(chTemp - 0x30);
                    }                  
				}
			}
//            memset(sztemp, 0x00, 10);
//            sprintf((unsigned char*)sztemp, "MY SEQ: %d\r\n", nCSQ);
//            sp_send(sztemp);
			return TRUE;
		}
	}	
    return FALSE;
}

/*unsigned char attention_cregset()
{
	bit bRet = FALSE;
	volatile UINT nLoop = RETRY_TIMES;
    g20_sp_init_buf();
    while(nLoop -- > 0)
    {
        wdr();
        g20_sp_send("AT+CREG=1\r");
        bRet = g20_sp_waitfor("OK",NULL,"ERROR",NULL,NULL,2000);
        if(bRet)
            return TRUE;
    }
    return FALSE;
}

unsigned char attention_cgregset()
{
	bit bRet = FALSE;
	volatile UINT nLoop = RETRY_TIMES;
    g20_sp_init_buf();
    while(nLoop -- > 0)
    {
        wdr();
        g20_sp_send("AT+CGREG=1\r");
        bRet = g20_sp_waitfor("OK",NULL,"ERROR",NULL,NULL,2000);
        if(bRet)
            return TRUE;
    }
    
    return FALSE;
}

unsigned char attention_cregchk()
{
    bit bRet = FALSE;
    volatile UINT nLoop = RETRY_TIMES;
    g20_sp_init_buf(); 
    while(nLoop -- > 0)
    {
        wdr();
        g20_sp_send("AT+CREG?\r");
        bRet = g20_sp_waitfor("+CREG: 001,001", "+CREG: 001,005","ERROR",NULL,NULL,200);
        if(bRet)
            return TRUE;
    }
    
    return FALSE;
}

unsigned char attention_cgregchk()
{
    bit bRet = FALSE;
    volatile UINT nLoop = RETRY_TIMES;
    g20_sp_init_buf();
    while(nLoop -- > 0)
    {
        wdr();
        g20_sp_send("AT+CGREG?\r");
        bRet = g20_sp_waitfor("+CGREG: 001,001", "+CGREG: 001,005","ERROR",NULL,NULL,200);
        if(bRet)
            return TRUE;
    }
    
    return FALSE;
}

unsigned char attention_gprscvr()
{
    bit bRet = FALSE;
    volatile UINT nLoop = RETRY_TIMES;
    g20_sp_init_buf();
    while(nLoop -- > 0)
    {
        wdr();
        g20_sp_send("AT+CGPRS?\r");
        bRet = g20_sp_waitfor("+CGPRS: 1", NULL,"ERROR",NULL,NULL,2000);
        if(bRet)
            return TRUE;
    }
    
    return FALSE;    
}*/

unsigned char attention_mipcall(unsigned char bSwitch)
{
    BYTE szBuffer[30];
    bit bRet = FALSE;
    volatile UINT nLoop = RETRY_TIMES;

    memset(szBuffer, 0x00, 30);
    if(bSwitch)
    {
	      sprintf((unsigned char*)szBuffer, "AT+MIPCALL=1,\"dyshl.sd\"\r");
//        sprintf((unsigned char*)szBuffer, "AT+MIPCALL=1,\"%s\",\"%s\",\"%s\"\r", "dyshl.sd","","");
    }
    else
    {
        sprintf((unsigned char*)szBuffer, "AT+MIPCALL=0\r");
    }
    
    g20_sp_init_buf();
    while(nLoop -- > 0)
    {
        wdr();
        g20_sp_sendb(szBuffer);        
        timer_delay(1500);
        bRet = g20_sp_waitfor("OK","ERROR",NULL,NULL,NULL,2000);
        if(bRet)
        {
            break;
        }
    }   
   
    if(bRet)
    {
        timer_delay(1000);
        nLoop = RETRY_TIMES - 2;
        while(nLoop -- > 0)
        {
            wdr();
            g20_sp_send("AT+MIPCALL?\r");
            
            if(bSwitch)
            {
                bRet = g20_sp_waitfor("+MIPCALL: 1,", NULL,"ERROR",NULL,NULL, 5000);
            }
            else
            {
                bRet = g20_sp_waitfor("+MIPCALL: 0", NULL, "ERROR", NULL, NULL, 1000);
            }        
            timer_delay(1000);            
            if(bRet)
                return TRUE;  
        }
    }
    
    return FALSE;
}

unsigned char attention_mipopen()
{
    BYTE szBuffer[32];
    bit bRet = FALSE;
    volatile UINT nLoop = RETRY_TIMES;
    
    memset(szBuffer, 0x00, 32);
//    sprintf((unsigned char*)szBuffer, "%d,\"10.10.10.10\",%d,1\r", 3700,3700);
	sprintf((unsigned char*)szBuffer, "AT+MIPOPEN=1,3700,\"10.14.83.156\",3700,1\r");
    g20_sp_init_buf();
    while(nLoop -- > 0)
    {
        wdr();
//        g20_sp_send("AT+MIPOPEN=1,");
        g20_sp_sendb(szBuffer);
        timer_delay(1000);    
        bRet = g20_sp_waitfor("+MIPOPEN: 1,1", NULL,"ERROR",NULL,NULL,2000);
        if(bRet)
            return TRUE;
    }
    g20_sp_init_buf();
    nLoop = RETRY_TIMES;
    while(nLoop -- > 0)
    {
        wdr();
        g20_sp_send("AT+MIPOPEN?\r");
        
        bRet = g20_sp_waitfor("+MIPOPEN: 2,3,4", NULL, 
                                "ERROR", "+MIPOPEN: 1,2,3,4", NULL, 2000);
        if(bRet)
            return TRUE;
        timer_delay(1000);    
    }
    return FALSE;
}

unsigned char attention_mipsend(BYTE * pBuf)
{
	//BYTE szBuffer[64];
    bit bRet = FALSE;
//    volatile UINT nLoop = RETRY_TIMES;
    
    //memset((unsigned char*)szBuffer, 0x00, 64);
    //sprintf((unsigned char*)szBuffer, "AT+MIPSEND=1,\"%s\"\r", pBuf);
   
    g20_sp_init_buf();
    
//    while(nLoop -- > 0)
//    {
        wdr();
        g20_sp_send("AT+MIPSEND=1,\"");
        g20_sp_sendb(pBuf);
        g20_sp_send("\"\r");       
                
        bRet = g20_sp_waitfor("+MIPSEND: 1", NULL,"ERROR",NULL,NULL,2000);
        if(bRet)
            return TRUE;
//    }
//    timer_delay(20);    
    return FALSE;
}

unsigned char attention_mippush(void)
{
    bit bRet = FALSE;
    volatile UINT nLoop = RETRY_TIMES; 
    
    g20_sp_init_buf();
    while(nLoop -- > 0)
    {
        wdr();
        g20_sp_send("AT+MIPPUSH=1,\"10.14.83.156\",3700\r");    //10.14.89.183
        bRet = g20_sp_waitfor("+MIPPUSH: 0",NULL, "ERROR", NULL, NULL,2000);
        if(bRet)
            return TRUE;            
    }
    
    return FALSE;
}

/*unsigned char attention_mippush(IP_ADDR *pAdd, UINT nPort)
{
    BYTE szBuffer[30];
    bit bRet = FALSE;
    volatile UINT nLoop = RETRY_TIMES;
    
    g20_sp_init_buf();
    memset(szBuffer, 0x00, 30);
    sprintf((unsigned char*)szBuffer, "\"%d.%d.%d.%d\",%d", pAdd->classA,pAdd->classB,pAdd->classC,pAdd->classD, nPort);
    while(nLoop -- > 0)
    {
        wdr();
        g20_sp_send("AT+MIPPUSH=1,");
        g20_sp_sendb(szBuffer);
        g20_sp_send("\r");
        bRet = g20_sp_waitfor("+MIPPUSH: 0",NULL, "ERROR", NULL, NULL,2000);
        if(bRet)
            return TRUE;            
    }
    
    return FALSE;
}*/

unsigned char attention_cmgf()
{
    bit bRet = FALSE;
    volatile UINT nLoop = RETRY_TIMES;
    
    g20_sp_init_buf();
    while(nLoop -- > 0)
    {
        wdr();
        g20_sp_send("AT+CMGF=0\r");
        bRet = g20_sp_waitfor("OK", NULL,"ERROR", NULL, NULL, 2000);
        if(bRet)
            return TRUE;
    }
    return FALSE;
}

void exch_mobilephone(void)
{
	memset(bnumber, 0x00, 25);

	bnumber[0]=MobilePhone[1];
	bnumber[1]=MobilePhone[0];
	bnumber[2]=MobilePhone[3];
	bnumber[3]=MobilePhone[2];
	bnumber[4]=MobilePhone[5];
	bnumber[5]=MobilePhone[4];
	bnumber[6]=MobilePhone[7];
	bnumber[7]=MobilePhone[6];
	bnumber[8]=MobilePhone[9];
	bnumber[9]=MobilePhone[8];
	bnumber[10]='F';
	bnumber[11]=MobilePhone[10];
}

void unlicode_hex(BYTE * pBuf, unsigned char nLoop)
{
    unsigned char i;

	memset(bnumber, 0x00, 25);
	
	for (i=0;i<nLoop;i++)
	{
		bnumber[i*4+0] = 0x30;	
		bnumber[i*4+1] = 0x30;	
		bnumber[i*4+2] = 0x33;	
		bnumber[i*4+3] = *(pBuf+i) ;
		if (*(pBuf+i)=='.')
		{
			bnumber[i*4+2] = 0x32;	
			bnumber[i*4+3] = 0x45;
		}
		if (*(pBuf+i)==' ')
		{
			bnumber[i*4+2] = 0x32;	
			bnumber[i*4+3] = 0x30;
		}
	}	
}

unsigned char attention_cmgs(unsigned char value)
{
    bit bRet = FALSE;
    volatile UINT nLoop = RETRY_TIMES;
    unsigned char strdata[15];

//    attention_cmgf();
//	delay_ms(1000);
	g20_sp_init_buf();
    while(nLoop -- > 0)
    {
		g20_sp_send("AT+CMGS=123\r");           //5+6+3+1+108=123

		delay_ms(10);
//		bRet = g20_sp_waitfor(">", NULL,"ERROR", NULL, NULL, 2000);
//		g20_sp_send("0891683108506405F0  11000D9168  3116218102F4  000800  08 8C228C2200330034");

		g20_sp_send("0891683108506405F011000D9168");  //5

		exch_mobilephone();  //6
		g20_sp_sendb(bnumber);


		g20_sp_send("000800");  //3

		g20_sp_send("6C");  //1        //24+10+10+5*2+6+10+5*2+6+6+5*2+6=108 (0x6c)

		g20_sp_send("621090FD5C4081EA8D216BB582CD576A5DE5533A002C0020");    //24  /*"成都局自贡段苍坪工区,"*/
		                   
		switch (value)
		{
			case 0:
				g20_sp_send("4E0052076B635E380021");  //10  /*一切正常!*/
				break;
			case 1:
				g20_sp_send("6CE8610F8B6662120021");  //10  /*注意警戒!*/
				break;
			case 2:
				g20_sp_send("537160258B6662120021");  //10  /*危急警戒!*/
				break;
			case 3:
				g20_sp_send("5C019501533A95F40021");  //10  /*封锁区间!*/
				break;
		}

		g20_sp_send("53415206949F964D96E8");  //10  /*十分钟降雨*/
		memset(strdata, 0x00, 25);
		sprintf(strdata,"%5.1f",(float)(ombro_ten*fGrid));
		unlicode_hex(strdata,5);    //5*2
		g20_sp_sendb(bnumber);
		g20_sp_send("006D006D002C");    //6   /*mm,*/
		
		g20_sp_send("4E005C0F65F6964D96E8");  //10  /*一小时降雨*/
		memset(strdata, 0x00, 25);
		sprintf(strdata,"%5.1f",(float)(ombro_hour*fGrid));
		unlicode_hex(strdata,5);    //5*2
		g20_sp_send(num);
		g20_sp_send("006D006D002C");    //6   /*mm,*/

		g20_sp_send("65E5964D96E8");  //6  /*日降雨*/
		memset(strdata, 0x00, 25);
		sprintf(strdata,"%5.1f",(float)(ombro_day*fGrid));
		unlicode_hex(strdata,5);    //5*2
		g20_sp_sendb(bnumber);
		g20_sp_send("006D006D0021");    //6   /*mm!*/
		
//		g20_sp_send("8FDE7EED964D96E8");  //8  /*连续降雨*/
//		memset(strdata, 0x00, 25);
//		sprintf(strdata,"%5.1f",(float)(ombro_continue*fGrid));
//		unlicode_hex(strdata,5);    //5*2
//		g20_sp_send(num);
//		g20_sp_send("006D006D0021");    //6   /*mm!*/

		g20_sp_send(chEnd);
		delay_ms(50);
		bRet = g20_sp_waitfor("+CMGS:", NULL,"ERROR", NULL, NULL, 2000);
		if(bRet)
		{
			return TRUE;
		}
    
	return FALSE;
}

unsigned char attention_init()
{
#if defined (DEBUG_MODE)	
	sp_send("\r\nAT指令初始化\r\n");
#endif 
//	disp_my_print("    GSM   ........");
    disp_stringf(4,72,"GSM   ........"); 
    if(attention_at() == TRUE)
    {
		if(attention_atz() == TRUE)
		{
			if(attention_ate(ECHO_SWITCH) == TRUE)
			{
				if(attention_cmgf() == TRUE)
				{
//					disp_my_print("正常\n");
                    disp_hzstr(18,72,"正常"); 
					return TRUE;
				}
			}
		}
	}
//	disp_my_print("错误\n");
    disp_hzstr(18,72,"错误"); 
    return FALSE;
}

unsigned char attention_dialproc()
{
//	disp_my_print("    GPRS  ........");
    disp_stringf(4,92,"GPRS  ........"); 
#if defined (DEBUG_MODE)	
	sp_send("\r\nGPRS挂断\r\n");
#endif 
    while(!attention_mipcall(FALSE))
    {
        wdr();
        timer_delay(1000);
    }
#if defined (DEBUG_MODE)	
	sp_send("\r\nGPRS连接\r\n");
#endif 
    while(!attention_mipcall(TRUE))
    {
        wdr();
        timer_delay(1000);
    }

    return TRUE;
}

unsigned char attention_udpproc()
{
#if defined (DEBUG_MODE)	
	sp_send("\r\nUDP连接\r\n");
#endif 
    while(!attention_mipopen())
    {
        wdr();
        timer_delay(1000);
    }
//    disp_my_print("正常\n");    
    disp_hzstr(18,92,"正常"); 
    return TRUE;
}

⌨️ 快捷键说明

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