📄 gsmmodem.c
字号:
#include <reg52.h>
#include <string.h>
#define uchar unsigned char
#define uint unsigned int
sfr WMCON = 0x96; /* Watchdog and Memory Control Register */
#define WDTC() (WMCON |= 0x02)
extern void put_chars(uchar *p);
extern void _AsyncPut(uchar ucValue);
//---------main.c-------------------------
sbit LED = P3^7;
extern unsigned char idata input[32];
extern unsigned long data TempCount ;
extern unsigned long GET_COUNT();
extern void Delay(uint x) ; /* x*10 ms */
//----------eeprom.c----------------------
extern void ReadUserBlock(uchar idata *input,uint Address,uchar Number);
//-----------comm.c-----------------------
extern void put_chars(uchar *p);
extern uchar _AsyncGet(void);
extern bit _AsyncGetTest(void);
extern void Vol_control(void);
extern uchar data Vol_Rec;
sbit MRST = P3^5; //reset mc35 at least 100 ms. can't use P1_4.
/*******************************
** output string through expcom
********************************/
void ModemSend(uchar *String)
{
put_chars(String);
}
void ModemRead(uchar *String,uchar Len) /* timeout * 10 ms */
{
uchar MatchPosition ;
uchar tempdata;
MatchPosition = 0;
TempCount = GET_COUNT();
while( 1)
{
if(Vol_Rec)
{
Vol_Rec = 0;
Vol_control();
}
if((GET_COUNT() - TempCount) > 500)
{
String[MatchPosition]=0;
return;
}
if(_AsyncGetTest())
{
tempdata = _AsyncGet();
if(tempdata > 0x20)
{
String[MatchPosition]=tempdata;
MatchPosition++;
break;
}
}
WDTC();
PCON = PCON | 0x01;
}
while( 1)
{
if(Vol_Rec)
{
Vol_Rec = 0;
Vol_control();
}
if((GET_COUNT() - TempCount) > 500)
{
String[MatchPosition]=0;
return;
}
if(_AsyncGetTest())
{
tempdata = _AsyncGet();
if(tempdata > 0x20)
{
String[MatchPosition]=tempdata;
MatchPosition++;
if(MatchPosition >= (Len-1))break;
}
else break;
}
WDTC();
PCON = PCON | 0x01;
}
String[MatchPosition]=0;
return ;
}
/*******************************
** return 1: match success.
** return 0: match error.
*******************************/
unsigned char ModemExpect(uchar *ExpectString,uint timeout) /* timeout * 10 ms */
{
uchar MatchPosition ;
uchar StringLenth;
uchar tempdata;
MatchPosition = 0;
StringLenth = strlen(ExpectString);
LED = 1;
TempCount = GET_COUNT();
while( MatchPosition < StringLenth)
{
if( GET_COUNT() - TempCount > timeout)
{
return 0;
}
if(_AsyncGetTest())
{
tempdata = _AsyncGet();
if(tempdata == ExpectString[MatchPosition]) MatchPosition++;
else MatchPosition = 0; /* match error,start again */
}
}
LED = 0;
return 1;
}
/**********************************
** restet MC35 and CO110
** return 1: rest success,support
at command.
** return 0: rest error,can't
support at command.
**********************************/
void Mc35Rst()
{
MRST = 0;
Delay(15);
MRST = 1;
}
/*********************************
** init the Modem
** ATTENTION: must save some parameter except "hsrv" !
*********************************/
unsigned char InitModem()
{
uchar i;
// uchar j;
uchar status = 1;
for(i = 0;i<4;i++)
{//a
switch(status)
{//b
case 1: //at-----OK
ModemSend("AT\r");
Delay(10);
if( ModemExpect("OK",200))
{
i = 0;
status = 2;
}
break;
case 2: //atz---OK
ModemSend("AT&F\r");
Delay(100); // after send "atz",delay 300 ms at least
if( ModemExpect("OK",200))
{
i = 0;
status =3;
}
break;
case 3: //atz---OK
ModemSend("ATE0\r");
Delay(300); // after send "atz",delay 300 ms at least
if( ModemExpect("OK",200))
{
i = 0;
status =4;
}
break;
case 4: //at+csq---+CSQ: xx,99
ModemSend("AT+CSQ\r");
Delay(100);
// if(ModemExpect("+CSQ: ",400))
// {
if(ModemExpect(",99",400))
{
status = 5;
i = 0;
}
// }
break;
case 5:
ModemSend("AT+CLIP=1\r"); //ati----I/OK
Delay(10);
if( ModemExpect("OK",200))
{
i = 0;
status = 6;
}
break;
case 6:
ModemSend("AT+IPR=9600\r");
Delay(10);
if( ModemExpect("OK",200))
{
i = 0;
status = 7;
}
break;
case 7:
ModemSend("AT^SBC=1\r");
Delay(10);
if( ModemExpect("OK",200))
{
i = 0;
status = 8;
}
break;
case 8:
ModemSend("AT^SNFS=4\r");
Delay(10);
if( ModemExpect("OK",200))
{
i = 0;
status = 9;
}
break;
case 9:
ModemSend("AT+CFUN=0\r");
Delay(10);
if( ModemExpect("OK",200))
{
i = 0;
return 1;
}
break;
}//b
}//a
return 0;
}
//AT^SMSO OFF
//AT+CFUN=0 SLEEP
uchar login_ack()
{
uchar i;
for(i = 0;i<3;i++)
{
if(InitModem()) return 1;
}
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -