#include<reg52.h>
#define uchar unsigned char
#define uint unsigned int
void gsm_uart();
unsigned int uart_delay(unsigned int z);
void GSM_init();
void GSM_CMGF();
void GSM_ATCMGS();
void TELNUM();
void GSM_TEXT();
void GSM_test();
void BUTTON();
unsigned int d;
unsigned char code ATEnter[]={0x41,0x54,0x0d};
unsigned char code ATCMGFEnter[]={0x41,0x54,0x2b,0x43,0x4d,0x47,0x46,0x3d,0x31,0x0d};
unsigned char code ATCMGS[]={0x41,0x54,0x2b,0x43,0x4d,0x47,0x53,0x3d};
unsigned char code TEL_NUM[]={0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39};
unsigned char code TEXT[]={0x4b,0x4f};
unsigned char code Enter[]={0x0d};
unsigned char code oneA[]={0x31,0x41};
unsigned char idata buf[20];
int flag;
int bufnum=0;
uchar str1[]={"AT"};
uchar str2[]={"ATD"};
uchar str3[]= {"ATH"};
uchar str5[]={"AT+CMGS="};
sbit bell=P1^6;
sbit betton1_1=P3^3;
sbit betton1=P3^4;
void gsm_uart()
{ EA=1;
ES=1;
SCON=0x50; //UART方式1:8位UART; REN=1:允許接收
PCON=0x00; //SMOD=0:波特率不加倍
TMOD=0x20; //T1方式2,用於UART波特率
TH1=0xFD;
TL1=0xFD; //UART波特率設置:9600
TI = 0;
RI = 0;
EA = 1;
TR1=1;
}
unsigned int uart_delay(unsigned int z)
{ unsigned int a,b;
for(a=z;a>0;a--)
{for(b=1000;b>0;b--);}
return z;
}
void ddd() interrupt 4 using 3
{
if(RI) //處理接收中斷
{ buf[bufnum]=SBUF; //接收串口數據
RI=0;
bufnum++; //清除中斷標誌位
}
}
void GSM_init()
{
uint i;
uchar code test[]= {0x41,0x54,0X0D};
ES = 0;
for (i=0;i<3;i++)
{
SBUF = test[i];
while (TI == 0);
TI = 0;
}
ES = 1;
}
void GSM_CMGF()
{
uchar code sel[10]= {0x41,0x54,0x2B,0x43,0x4D,0x47,0x46,0x3D,0x31,0x0D}; //AT+CMGF=1回車
uint i;
for(i=0;i<10;i++)
{
SBUF=sel[i];
while(TI==0);
TI=0;
}
}
void GSM_ATCMGS()
{ uint i;
uchar code tel[20]= {0x41,0x54,0x2B,0x43,0x4D,0x47,0x53,0x3D,0x31,0x33,0x38,0x33,
0x36,0x30,0x32,0x33,0x33,0x37,0x39,0x0D};
for(i=0;i<20;i++)
{
SBUF=tel[i];
while(TI==0);
TI=0;
}
}
void GSM_TEXT()
{
uchar code neirong[10]={0x48,0x69,0x21,0x6E,0x69,0x68,0x61,0x6F,0x2E,0x1a}; //hi!nihao.
uint i;
ES =0;
for(i=0;i<10;i++)
{
SBUF=neirong[i];
while(TI==0);
TI=0;
}
ES = 1;
}
void send()
{
SBUF=oneA[0];while(TI==0);TI=0;
SBUF=oneA[1];while(TI==0);TI=0;
}
void GSM_test()
{
gsm_uart();
uart_delay(400);
GSM_init();
uart_delay(1000);
ES =0;
bufnum = 0;
if(buf[5]=='O' && buf[6] =='K')
{
P0=0xfe;uart_delay(1000);P0=0xff;
}
else
{
P0=0x00;uart_delay(4000);P0=0xff;
}
}
void BUTTON()
{ betton1=1;
betton1_1=0;
if(betton1==0)
{
GSM_test();
uart_delay(1200);
GSM_CMGF();
uart_delay(1200);
GSM_ATCMGS();
uart_delay(1200);
GSM_TEXT();
uart_delay(800);
uart_delay(500);
betton1=1;
}
}
main()
{
BUTTON();
uart_delay(10);
}