📄 smsdrive.c
字号:
#include <reg51.h>
#include <intrins.h>
#include <string.h>
#include <stdio.h> //一般IO/函数
#include <ctype.h>
#define U8 unsigned char
#define U16 unsigned int
#define MAX_TM 265
#define MAX_T 35
#define LED P0
sbit spk=P0^3;
sbit led2=P1^1;
sbit IO_OUT=P1^0;
//◇◇◇◇◇◇◇◇◇◇定义接收中断相关变量
U8 xdata rec[512];
U8 count=0;
bit flag=0;
bit ReceiveFlag=0;
bit sendflag;
U8 idata TEL_temp[15];
U8 data para_temp[512];
//△△△△△△△△△△定义接收中断相关变量
//---定时用-----------
U16 data timercount; //50MS定时器,
U8 data timer_1S_cnt;
U8 data timer_10S_cnt;
U8 data timer_S_cnt;
//--------------------
//◇◇◇◇◇◇◇◇◇◇定义有权发送短信的用户
U8 code SendUser1[]="13633634914F";
U8 code SendUser2[]="13633634914F";
U8 code order1[]="asd";
//△△△△△△△△△△定义有权发送短信的用户
U8 code Content_rule[] = {
0xCC,0xF4,0x19,0x4D,0x07,0xBD,0xDD
} ; //Light on
U8 code Command_At[3] = "AT\r";
/*发送:41 54 0d
接收到:41 54 0D 0D 0A 4F 4B 0D 0A
对应字符:
AT<CR>
OK
*/
U8 code Command_Csq[7] = "AT+CSQ\r"; //Signal quality test
/*发送:41 54 2B 43 53 51 0d
接收到:41 54 2B 43 53 51 0D 0D 0A 2B 43 53 51 3A 20 33 31 2C 39 39 0D 0A 0D 0A 4F 4B 0D 0A
对应字符:
AT+CSQ<CR>
+CSQ: 31,99
OK
*/
/////////////////////////////////////////
U8 code Command_CPIN[9] = "AT+CPIN?\r";
U8 code Command_COPS[9] = "AT+COPS?\r";
U8 code Command_ATE1[6] = "ATE1\r"; //Signal quality test
U8 code Command_ATV1[6] = "ATV1\r";
/////////////////////////////////////// //Signal quality test
U8 code Command_Cmgf1[10] = "AT+CMGF=1\r";
//选择短消息信息格式,设置为文本模式。若 "AT+CMGF=0\r",设置为PDU 模式
/*发送:41 54 2B 43 4D 47 46 3D 31 0D
接收到:41 54 2B 43 4D 47 46 3D 31 0D 0D 0A 4F 4B 0D 0A
对应字符:
AT+CMGF=1<CR>
OK
*/
U8 code Command_Cscs[12] = "AT+CSCS=GSM\r"; //设置为GSM 字符集
/*发送:41 54 2B 43 53 43 53 3D 47 53 4D 0D
接收到:41 54 2B 43 53 43 53 3D 47 53 4D 0D 0D 0A 4F 4B 0D 0A
对应字符:
AT+CSCS=GSM<CR>
OK
*/
U8 code Command_Cnmi[12] = "AT+CNMI=2,1\r"; //设置新短信提示
/*发送:41 54 2B 43 4E 4D 49 3D 32 2C 31 0D
接收到:41 54 2B 43 4E 4D 49 3D 32 2C 31 0D 0D 0A 4F 4B 0D 0A
对应字符:
AT+CNMI=2,1<CR>
OK
*/
U8 data Command_Cmgr[10] = "AT+CMGR=1\r"; //读取短消息
/*发送:41 54 2B 43 4D 47 52 3D */
U8 data Command_Cmgd[10] = "AT+CMGD=1\r"; //删除短消息
/*发送:41 54 2B 43 4D 47 44 3D */
U8 code Command_CmgsT1[20] = "AT+CMGS=13633634914\r"; //给用户1 发送文本短消息
/*发送:41 54 2B 43 4D 47 53 3D 31 33 34 38 35 33 34 34 36 33 34 0D
接收到:41 54 2B 43 4D 47 53 3D 31 33 32 33 33 36 34 37 31 34 33 0D 0D 0A 3E 20
对应字符:
AT+CMGS=13233647143<CR>
><SP>
*/
U8 code Command_CmgsT2[20] = "AT+CMGS=13633634914\r"; //给用户2 发送文本短消息
/*发送:41 54 2B 43 4D 47 53 3D 31 33 32 33 33 36 34 37 31 34 33 0D
接收到:41 54 2B 43 4D 47 53 3D 31 33 32 33 33 36 34 37 31 34 33 0D 0D 0A 3E 20
对应字符:
AT+CMGS=13233647143<CR>
><SP>
*/
/*===========================================
*函数名:ms级延时 子函数
*参 数:
*功 能:
*返回值:
*作 者:weishao2401 2009/4/29
*============================================*/
void DelayMs(U8 ms)
{
U8 i;
while(ms--)
{
for(i = 0; i < 120; i++);
}
}
////////////////////////////////////////////////////////////////
void Int_Timer0(void) interrupt 1 using 3
{
TH0 = 0xB8;
TL0 = 0x00; //20ms的时钟基准
timer_1S_cnt++; timercount++;
if(timer_1S_cnt==50) //20×50=1000MS =1S
{ timer_10S_cnt++; timer_1S_cnt=0;
timer_S_cnt++;
//LED_S0=~LED_S0;
} //1秒=1000毫秒
if(timer_10S_cnt==60) //60S
{
timer_10S_cnt=0;
}
//---------------------------------------------------------------------------------
}
////////////////////////////////////////////////////
/*===========================================
*函数名:TC35正确提示 子函数
*参 数:
*功 能:2声长响
*返回值:
*作 者:weishao2401 2009/4/29
*============================================*/
void deal_with_right(void)
{
U8 i;
for(i=0;i<2;i++)
{
DelayMs (60);
spk=0; //开启扬声器 让蜂鸣器工作
DelayMs (60);
spk=1;
}
}
/*===========================================
*函数名:TC35发送命令提示 子函数
*参 数:
*功 能:1声短响
*返回值:
*作 者:weishao2401 2009/4/29
*============================================*/
void deal_with_attention(void)
{
U8 i;
for(i=0;i<1;i++)
{
DelayMs (40);
spk=0; //开启扬声器 让蜂鸣器工作
DelayMs (40);
spk=1;
}
}
/*===========================================
*函数名:TC35出错处理 子函数
*参 数:
*功 能:5声短响
*返回值:
*作 者:weishao2401 2009/4/29
*============================================*/
void deal_with_error(void)
{
U8 i;
for(i=0;i<5;i++)
{
DelayMs (30);
spk=0; //开启扬声器 让蜂鸣器工作
DelayMs (30);
spk=1;
}
}
/*===========================================
*函数名:串口初始化 子函数
*参 数:
*功 能:
*返回值:
*作 者:weishao2401 2009/4/29
*============================================*/
void UART_Init(void)
{ TI=0; RI=0;
SCON|=0x50; /* mode 1: 8-bit UART, enable receiver */
TMOD|=0x21; /* timer 1 mode 2: 8-Bit reload */
PCON|=0x80; // baud x2
TH1=0xfa; //串口波特率初始化 0xfd=19200,0xfa=9600
ES=1; //打开串行中断
TR1=1; //打开定时器1
/////////////////////////////////////////
TH0 = 0xB8;
TL0 = 0x00; //20ms的时钟基准
TR0=1; //串口接收中断
ET0=1; //T0 中断
EA=1;
P0=0xff;
P1=0xff;
P2=0xff;
P3=0xff;
timer_1S_cnt=0;
///////////////////////
}
/*===========================================
*函数名:串口发送 子函数
*参 数:U8 data_buffer[], U8 data_len
*功 能:
*返回值:
*作 者:weishao2401 2009/4/29
*============================================*/
void uart_send(U8 data_buffer[],U8 data_len)
{
U8 i;
ES=0;
TI=0; //clear send flag 清除发送标志TI1
for(i=0;i<data_len;i++)
{
SBUF = data_buffer[i];
while(TI==0); //等待串口发送完毕
TI=0; //发送完后,软件清零发送标志TI
}
ES=1;
}
/*===========================================
*函数名:串口接收 子函数
*参 数:
*功 能:
*返回值:
*作 者:weishao2401 2009/4/29
*============================================*/
int ydl=0;
void ISRSER() interrupt 4 using 3 //串口中断
{
EA=0; //关中断,防止其它中断打扰
if (RI)
{
rec[count] = SBUF;
ydl++;
count++;
flag=1;
RI=0;
}
if(count>=50)
{
if(rec[13]=='C'&&rec[14]=='M'&&rec[15]=='G'&&rec[16]=='R')
if(rec[26]=='0'&&rec[27]=='8'&&rec[28]=='9'&&rec[29]=='1')
{ReceiveFlag=1;count=0;P0=0x1f;}
}
EA=1;//恢复中断
}
/*===========================================
*函数名:TC35 AT联机测试 子函数
*参 数:
*功 能:
*返回值:
*作 者:weishao2401 2009/4/29
*============================================*/
void TC35_AtSend(void) // at联机测试命令
{
while(1)
{
TI=0; //清除发送标志
RI=0; //清除接收标志
uart_send(Command_At,3); //串口发送 at联机测试命令
deal_with_attention();
DelayMs(250); //延时,等待TC35的反馈信息
DelayMs(250);
DelayMs(250);
DelayMs(250);
if(flag == 1 && count == 9)
{
if(rec[5]=='O'&&rec[6]=='K')
{
deal_with_right();
flag=0;
count=0;
break;
} //如果第6和第7个字符分别是“O和“K”,那联机正确,直接退出
else
{
deal_with_error();
flag=0;
count=0;
} //出错处理,并且进入下一场循环,再次发送AT测试命令,直到联机正确
}
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void TC35_ATE1(void) // ATE1测试命令
{
while(1)
{
TI=0; //清除发送标志
RI=0; //清除接收标志
uart_send(Command_ATE1,6); //串口发送ATE1测试命令
deal_with_attention();
DelayMs(250); //延时,等待TC35的反馈信息
DelayMs(250);
DelayMs(250);
DelayMs(250);
if(flag == 1 && count == 11)
{
if(rec[7]=='O'&&rec[8]=='K')
{
deal_with_right();
flag=0;
count=0;
break;
} //如果第8和第9个字符分别是“O和“K确,直接退出
else
{
deal_with_error();
flag=0;
count=0;
} //出错处理,并且进入下一场循环,再次发送AT测试命令,直到联机正确
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -