//2008.12.3.9.34改
/**********************************************************/
#include "reg52.h"
#define uint unsigned int
#define uchar unsigned char
#define RESPTIME 10000
void sendByte(uchar byte);
void WriData(uchar wdata);
idata unsigned char RxBuf[150];
unsigned char rxCnt = 0;
uchar mysbuf;
bit mybit=0;
void delay(unsigned int t) //通用延时程序
{
for(;t>0;t--);
}
//*****************************UART*************************/
void initUart(void) //初始化串口
{ //波特率:9600 bit/s
TMOD|=0x20;
SCON=0x50;
PCON|=0x80;
TH1=0xfa;
TL1=0xfa;
TR1=1;
//ET1=1;
ES=1;
EA=1;
}
void uart(void) interrupt 4 //串口中服务程序
{
if(RI)
{
EA=0;
RI=0;
mysbuf=SBUF;
//mybit=1;
//WriData(mysbuf);
RxBuf[rxCnt] = mysbuf;
rxCnt ++;
EA=1;
}
}
void sendByte(uchar byte) //串口发送一个字节
{
SBUF=byte;
while(TI==0);
TI=0;
}
//*****************************UART*************************/
/****************LCD程序*******************/
#define dataport P2
sbit RS=P3^5;
sbit RW=P3^6;
sbit ET=P3^7;
void Wait()
{uint j;
for(j=0;j<300;j++){;}
}
void WriCom(uchar comm) //向LCD发送一个命令字
{ RS=0;
RW=0;
ET=0;
dataport=comm;
ET=1;
Wait();
ET=0;
}
void WriData(uchar wdata) //向LCD发送一字节的数据
{
RS=1;
RW=0;
ET=0;
dataport=wdata;
ET=1;
Wait();
ET=0;
}
void InitLcd(void) //初始化LCD
{
Wait();
WriCom(0x38);
Wait();
WriCom(0x38);
Wait();
WriCom(0x38);
Wait();
WriCom(0x38);
Wait();
WriCom(0x08);
WriCom(0x01);
WriCom(0x06);
WriCom(0x0C);
}
void SetCursor(uchar row) //LCD设置光标即字符的插入点
{
if(row>15)
row+=(0x40-16);
WriCom(row | 0x80);
}
void ClrLCD() //LCD清屏
{
WriCom(0x01);
Wait();
}
/*
void CursorGlint(uchar NoOff)
{
uchar com;
com=NoOff<<2;
com|=13;
WriCom(com);
}
*/
void WriStr(unsigned char *str) //向LCD发送一串字符串
{
while(*str)
{
WriData(*str);
str ++;
}
}
/*****************LCD程序END****************/
/********************LED模块************************/
sbit LED1 = P0^0;
sbit LED2 = P0^2;
sbit LED3 = P0^4;
sbit LED4 = P0^6;
sbit MOC = P0^5;
/****************end LED模块************************/
//下面这段为发送码
code unsigned char atf[]="at&f\r\n" ; //系统复位指令
code unsigned char at[]="at\r\n";////握手信号 返回值at...OK..
code unsigned char atcmgf[]="at+cmgf=0\r\n";//选择消息的发送格式0-PDU 1文本
code unsigned char atcnmi[]="at+cnmi=1,1,0,0,1\r\n";//帮助:at+cnmi=?这时会提示格式
code unsigned char atipr[]="at+ipr=9600\r\n";//设置波特率
code unsigned char atcmgr[]="at+cmgr=";//读存储在SIM卡中的短消息
code unsigned char atcmgd[]="at+cmgd=";//删除SIM卡内存的短消息
code unsigned char atcmss_1[]="at+cmss=";//这两条语句是相关的,中间加入短信号 从SIN|M内存中发送短消息 //@@@+
code unsigned char atcmss_2[]=",\"13481330526\",145\r\n"; //这两条语句是相关的,中间加入短信号 //@@@+ 13481394130
//下面这段为回应码
code unsigned char at1[11]={0x61,0x74, 0x26, 0x66,0x0D,0x0D, 0x0A, 0x4F, 0x4B, 0x0D, 0x0A}; //AT&F...OK.
code unsigned char at2[9]={0x61, 0x74, 0x0D, 0x0D, 0x0A, 0x4F, 0x4B, 0x0D, 0x0A};//at OK
code unsigned char atcmgf2[16]={0x61 ,0x74 ,0x2B ,0x63 ,0x6D ,0x67 ,0x66 ,0x3D ,0x30 ,0x0D ,0x0D ,0x0A ,0x4F ,0x4B ,0x0D ,0x0A };//at+cmgf=0 OK 短消息格式
code unsigned char atcnmi2[24]={0x61 ,0x74 ,0x2B ,0x63 ,0x6E ,0x6D ,0x69 ,0x3D ,0x31 ,0x2C ,0x31 ,0x2C ,0x30 ,0x2C ,0x30 ,0x2C ,0x31 ,0x0D ,0x0D ,0x0A ,0x4F ,0x4B ,0x0D ,0x0A };//at+cnmi=?这时会提示格式
code unsigned char atipr2[18]={0x61, 0x74 ,0x2b ,0x69 ,0x70 ,0x72,0x3d ,0x39,0x36 ,0x30 ,0x30 ,0x0d,0x0d ,0x0a ,0x4f ,0x4b,0x0d ,0x0a};//设置波特率
code unsigned char atcmti2[14]={0x0D,0x0A,0x2B,0x43,0x4D,0x54,0x49,0x3A,0x20,0x22,0x4D,0x54,0x22,0x2C};//,0x35,0x0D,0x0A};//后面是数据和回车换行
/********************************************************/
void SendStr(const unsigned char *str) //用串口发送一串字符串
{
while(*str)
{
sendByte(*str);
str ++;
}
}
unsigned char Check(const unsigned char *str,unsigned char cnt) //将指定的字符串和RxBuf中的字符串相比较,看是否相等
{
unsigned char i;
rxCnt = 0; //队列数据长度在这个时候清0
for(i=0;i<cnt;i++)
{
if(str[i] != RxBuf[i])
return 0;
}
return 1;
}
void SendATATF(void)//?
{
SendStr(atf);//?
}
void SendAT(void) //发送AT,测试
{
SendStr(at);
}
void SendATIPR(void) //设置波特率
{
SendStr(atipr);
}
void SendATCMGF(void) //设置PDU格式
{
SendStr(atcmgf);
}
void SendATCNMI(void) //设置新信息到达时自动提示
{
SendStr(atcnmi);
}
void SendATCMGR(void) //读短信
{
SendStr(atcmgr);
//sendByte('0'+n); //这样的话只处理短信数量小于10的情况
//sendByte('\r');
//sendByte('\n');
}
void SendATCMGD(void) //删除短信
{
SendStr(atcmgd);
//sendByte('0'+n); //这样的话只处理短信数量小于10的情况
//sendByte('\r');
//sendByte('\n');
}
//@@@+{
void SendATCMSS(unsigned char num)
{
SendStr(atcmss_1);
sendByte(num + '0');
SendStr(atcmss_2);//手机号码
}
//@@@+}
/********************************************************/
void InitSystem(void) //初始化
{ //unsigned char a;
WriStr("init mobile ...");
SendATATF(); //系统复位指令
// delay(60000);
// delay(60000);
// delay(60000);
//delay(6000);
// delay(6000);
while(rxCnt<11);
SetCursor(16);
//a=rxCnt+48;
//WriData(a);
if(Check(at1,11)) //对回应码进行比较
{
WriStr("ATF ok! ");
}
else
{
WriStr("ATF error! ");
while(1); //错误死机
}
//WriStr("init mobile ...");
////////////////////////////////////////
SendAT(); //发送AT命令
while(rxCnt<9);
SetCursor(16);//
if(Check(at2,9)) //对回应码进行比较 这是AT回应码
{
WriStr("AT ok! ");
}
else
{
WriStr("AT error! ");
while(1);
}
///////////////////////////////////////
SendATIPR(); //发送设置波特率命令
SetCursor(16);
while(rxCnt<18);
if(Check(atipr2,18)) //对回应码进行比较
{
WriStr("IPR ok! ");
}
else
{
WriStr("IPR error! ");
while(1); //错误死机
}
///////////////////////////////////////
SendATCMGF(); //设置PDU格式
SetCursor(16);
while(rxCnt<16);
if(Check(atcmgf2,16)) //对回应码进行比较
{
WriStr("CMGF ok! ");
}
else
{
WriStr("CMGF error! ");
while(1); //错误死机
}
///////////////////////////////////////
SendATCNMI(); //设置新信息到达时自动提示
SetCursor(16);
while(rxCnt<24);
if(Check(atcnmi2,24)) //对回应码进行比较
{
WriStr("CNMI ok! ");
}
else
{
WriStr("CNMI error! ");
while(1); //错误死机
}
}
/********************************************************/
code unsigned char g_aucOpen[8]={"62535F00"}; //"打开"码
code unsigned char g_aucClose[8]={"517395ED"}; //"关闭"码
code unsigned char g_aucQuery[8]={"67E58BE2"}; //"查询"码
code unsigned char g_aucPhoneNum[12]={"3184310325F6"};//手机号 //@@@+ 13 48 13 30 52 6 3184314931F0
//@@@+{
unsigned char IsPhoneNum(void)
{
unsigned char i,k;
for(i=0,k=(rxCnt-52);i<12;i++,k++) //如果不是打开则返回
{
if(RxBuf[k] != g_aucPhoneNum[i])
return 0;
}
return 1;
}
//@@@+}
void IsOpen(void) //是否是"打开"
{
unsigned char i,k;
for(i=0,k=(rxCnt-20);i<8;i++,k++)//如果不是打开则返回
{
if(RxBuf[k] != g_aucOpen[i])
return;
}
if(RxBuf[k] == '0' && RxBuf[k+1] == '0' && RxBuf[k+2] == '3' && RxBuf[k+3] == '1') //判断是哪个通道,并控制相应的通道
{
LED1 = 0;
}
else if(RxBuf[k] == '0' && RxBuf[k+1] == '0' && RxBuf[k+2] == '3' && RxBuf[k+3] == '2') //判断是哪个通道,并控制相应的通道
LED2 = 0;
else if(RxBuf[k] ==
评论1
最新资源