1、/=GPRS MODULE PROGRAM=/=FROM 2003.9.=/=by cfj=#include/#include#include#include#include#include#include#define Uint unsigned int#define _3NOP() _NOP();_NOP();_NOP();#define _4NOP() _NOP();_NOP();_NOP();_NOP();#define _5NOP() _NOP();_NOP();_NOP();_NOP();_NOP();#define Estart_Addr 0 /0 EEprom起始地址#defi
2、ne Work_Mode_Addr Estart_Addr+6 /6 工作模式设置:1字节#define MAPN_Addr Work_Mode_Addr+1 /7 移动网关接入点名称:20字节 ASCI#define DSC_Ip_Addr MAPN_Addr+20 /27 数据服务中心IP地址:12 字节 ASC#define DSC_Port_Addr DSC_Ip_Addr+12 /39 数据中心端口:5 字节 ASCII#define Net_Protocol_Addr DSC_Port_Addr+5 /44 网络数据传输协议#define Terminal_SIM_Addr Net
3、_Protocol_Addr+1 /45 终端SIM#define Beat_Addr Terminal_SIM_Addr+11 /56 心跳周期地址#define PassWord_Addr Beat_Addr+1 /57 设置密码#define SCA_SIM_Addr PassWord_Addr+6 /63 短信服务中心号码#define DSC_SIM_Addr SCA_SIM_Addr+11 /74 数据中心号码#define BackUp_Addr DSC_SIM_Addr+11 /85 备用#define Set_Crc BackUp_Addr+4 /89 CRC校验码#defi
4、ne Estart2_Addr Estart_Addr+100/*/函数:main()/功能:主函数/输入参数:/输出参数:/=/常量定义/=char *AT=“ATr“;/AT命令用于检测 GPRS是否存在或开机。char AT_Echo12=A,T,E,=,0,V,=,1,/命令码不返回。/char *AT_Echo=“ATE=0V=1/命令码不返回。char *AT_O=“ATOr“;/从命令状态返回到数据状态。char *AT_3Plus=“+ATr“;char *AT_W=“AT /保存设置。char *AT_ActivateIp=“AT*E2IPA=1,1r“;char OpenI
5、p11=A,T,*,E,2,I,P,O,=,1,;char *AT_CloseIp=“AT*E2IPCr“;/关闭 GPRS通道char *AT_CMGF=“AT+CMGF=0r“;/短信 PDU MODEchar Terminal_Command19=0x68,0x0d,0x0d,0x68,0x4a,0x99,0x99,0x07,0x00,0x0b,0x80,0x30char Echo_Array=0X2F,0X00,0X07,0XFF,0XFF,0XFD,0XA3,0X03;/设置正确信号char Alarm_Switch_Echo=0X2F,0X00,0X08,0XFF,0XFF,0XC
6、E,0XEC,0X8F,0X14;char Data_Switch_Echo=0X2F,0X00,0X08,0XFF,0XFF,0XCD,0XEC,0XDA,0X47;/=/函数定义/=void Port_Initial(void);void Delay(Uint);void First_Wr_EEprom(void);void Variable_Initial(void);void Turn_On_Gprs(void);void Turn_Off_Gprs(void);/打开 GPRS电源void USART0_Initial(void);void USART1_Initial(void);
7、void Timer_Initial(void);void Data_Cmnd_Snd(Uint L,char *Temp_Pointer,char Sport,Uint);void Gprs_At_Command(Uint,char *Pcommand,Uint WTime);void Rx1Data_Judge_Trasmit(void);void Rx0Data_Judge_Trasmit(void);/extern void EWEN_93lc66(void);extern void Start_93lc66(char Op_Code);extern char Rd_Data(char
8、 Start_Addr,char Data_Len,Uint *Pointer);extern void Wr_Data(char Start_Addr,char Data_Len,Uint *Pointer);void Cal_Crc(char *Pointer,Uint L);/计算返回 CRC值。char Read_Parameters_Initial(char);/从 EEPROM中读取工作参数、初始化。void WorkMode_Initial_Log(void);extern void Construct_PDU_SMS(char UDL,char *User_Data);/构造
9、SMS内容。extern void Echo_SMS_Master(char);void Asci_To_Hex(char *Pointer,Uint);extern void Echo_Sms_Mobile(void);void Echo_Rx0(char *Pointer,char Lenth);extern void Mobile_SMS_Get(char);extern void Mobile_SMS_Set(char);extern void Encode7Bit(char);extern void Decode8Bit(char);extern char ReturnHex(cha
10、r);void Heart_Beat(void);void Read_Delete_SMS(void);void Feed_WatchDog(void);void Gprs_Mode_SMS(Uint);void Judge_SMS_Counter(void);void Data_Status(void);void Connect_Master_Station(void);extern void Waiting_Result(Uint Wresult_Time);extern void ZheJiang_Protocol_Down(Uint);extern void ZheJiang_Prot
11、ocol_Up(Uint);void Inqury_Signal(void);/=/全局变量定义/=char VIO;Uint Tx1_Snd_Lenth,Tx1_Pointer,Rx1_Pointer; /发送长度、发送指针、接收指针 Uint Tx0_Snd_Lenth,Tx0_Pointer,Rx0_Pointer; /发送长度、发送指针、接收 char *Pointer0,*Pointer1;char Command_Data_Status;char Rx1_Start_Flag; /串口接收开始标志Uint Rx1_Time_Counter;/串口接收时间长度char Rx1_Tim
12、eOut_Flag;/接收超时标志char Rx0_Start_Flag; /串口接收开始标志char Rx0_Time_Counter;/串口接收时间长度char Rx0_TimeOut_Flag;/接收超时标志Uint Wait_Result_Time;char Wait_Result_TimeOut;char Flash_Timer_Len; /CPU正常运行指示灯。char Led1_Flash;Uint Heart_Beat_Interval;/自动检测 IP连接的间隔时间char Heart_Beat_Flag; /自动检测标志char nMinute_Beat_Flag;char
13、 Heart_Beat_Time;char AT_OpenIp035;char Rx1_Data_Buffer360;/GPRS串口数据缓冲区 char Rx0_Data_Buffer256;/终端数据缓冲区 char Terminal_SIM12;/终端手机号码。char Center_SIM12; /短信通信手机号码。char Data_Center_Sim12;/数据中心手机号码。char SCA_SIM4; /短信服务中心号码 Uint Temp_iArray100;/临时缓冲区:整数 char Temp_cArray200;/临时缓冲区:字符 char SMS_TPDU300;cha
14、r User_Data140;char PassWord6;char AT_CGDCONT40;char AT_Set_Ok;Uint PDU_Len;char Retry_Counter;char AT_Test_Flag;char IpConnect_Flag;char Rx1_Led,Tx1_Led;/串口 1收发指示灯 Uint CRC;/CRC校验值。char Signal_Val;/信号强度Uint Beat_Rcv_Fail;/心跳未接收到次数(若连续发送三次未收到,则重新连接)Uint Read_SMS_Time;char Work_Mode;/模块工作模式char ,Tx1_
15、Pointer,Rx1_Pointer; /发送长度、发送指针、接收指针 char Msg_Snd_Flag;char Tx_Head; /发送引导头char SMS_Mode;char Terminal_Addr_High;char Terminal_Addr_Low;char Read_SMS_Flag;char SIM_SMS_Flag;char Received_Read_Msg_Flag;char DSR;Uint SMS_Index;/连接失败次数。char Read_Flag;char Ring_Counter;char Beat_Rcved_Flag;/心跳接收到标志。char
16、 Beat_Test_Start;/心跳测试标志。char Beat_Test_Fail;Uint Beat_Test_Interval;char First_Connect_Flag;/上电第一次连接标志。Uint Minute2_Interval;char Minute2_Interval_Flag;char Inqury_Signal_Flag;char Terminal_Request_Flag;char ii;/清缓冲区指针。Uint Second;Uint Signalmscnt;char Minute;char Connect_Counter;Uint Rx1_Time_Inte
17、rval;char extern Msg_Snd_Success;Uint Tel_Ring_Counter;char Ring_Start_Flag;Uint Ring_Time;char Zj_Protocol_Flag;char SMS_Counter;char Load_Master_Flag;char Heart_Flag;char No_Carrier_Flag;/=/主程序/=void main(void)char i;_3NOP();/WDTCTL=WDTPW+WDTHOLD;BCSCTL1|=XT2OFF+XTS; /启动 LFTX1 4M高速晶振,关闭 XT2晶振 BCSC
18、TL2|=SELM1+SELM0; /LFTTX1CLK:4MMCLKdoIFG1for(i=0xFF;i0;i-);while(IFG1/等待时钟源转换完。P2DIR|=0X08;/P2.0:LED5;P2.1:LED6;p2.2-ring P2OUTP1DIR|=0X10;P1OUTWDTCTL=WDTPW+WDTSSEL;/看门狗开启。Delay(20000);Port_Initial();/初始化 P1、P2、P3 口等 while(VIO=0X00) /若 GPRS尚未开机,则打开 GPRS电源Turn_On_Gprs();VIO=P5IN /VIO=P5.4_3NOP();Variable_Initial();/变量初始化。Delay(10);