功能:
PC通过发送一条指令,启动STC12单片机发送短信功能,给某个手机号发送一条短信。
另外为了演示如何发送一个变量数据短信,发送的短信内容和VALUE1和VALUE2两个变量有关,可以程序定义,或者比如替换成某个传感器数据。PC发送串口指令“sendmessage”后,STC12开始发送短信,最终发送给指定的电话号码。
功能演示:
链接:https://pan.baidu.com/s/16JnpxW0YtsANAVPt7GcrZA 密码:sllr
用到的STC12C5A60S2板子链接:
https://item.taobao.com/item.htm?id=524701473371
或者
https://item.taobao.com/item.htm?id=26410708738
硬件原理:
因为STC12单片机有双串口,串口1可以用于下载程序,或者PC发送指令给STC12,或者STC12发送调试信息个PC看当前状态。而串口2可以接A6或者A7模块去发送AT指令控制发送短信。
接线方式:
A6 STC12
U_RXD --- P1.3
U_TXD --- P1.2
GND --- GND
代码主程序流程演示:
#include "main.h"
#include "uart.h"
//常量
#define Success 1U
#define Failure 0U
//定义变量
unsigned long Time_Cont = 0; //定时器计数器
char phoneNumber[] = "176xxxxxxxx"; //替换成需要被拨打电话的号码
char msg[30] = "ILoveMCU.taobao.com"; //短信内容
int VALUE1 = 299;
int VALUE2 = 156;
//****************************************************
//主函数
//****************************************************
void main()
{
Uart_Init();//初始化串口是9600
Uart2_Init();//初始化串口是115200
if (sendCommand("AT\r\n", "OK\r\n", 3000, 10) == Success);
else errorLog();
delay_ms(10);
if (sendCommand("AT+CPIN?\r\n", "READY", 1000, 10) == Success);
else errorLog();
delay_ms(10);
if (sendCommand("AT+CREG?\r\n", ",1", 1000, 10) == Success);
else
{
delay_ms(10);
if (sendCommand("AT+CREG?\r\n", ",5", 1000, 10) == Success);
else errorLog();
}
if (sendCommand("AT+CMGF=1\r\n", "OK\r\n", 1000, 10) == Success);
else errorLog();
delay_ms(10);
if (sendCommand("AT+CSCS=\"GSM\"\r\n", "OK\r\n", 1000, 10) == Success);
else errorLog();
delay_ms(10);
while(1)
{
if(Hand("sendmessage"))
{
CLR_Buf();
UartPrintf("receive commond!\r\n");
memset(msg, 0, 30); //清空
sprintf(msg,"RECEIVE=%d,%d",VALUE1,VALUE2);
sendMessage(phoneNumber,msg); //发送短信
UartPrintf("send message OK!\r\n");
}
}
}
void sendMessage(char *number,char *msg)
{
xdata char send_buf[20] = {0};
memset(send_buf, 0, 20); //清空
strcpy(send_buf, "AT+CMGS=\"");
strcat(send_buf, number);
strcat(send_buf, "\"\r\n");
if (sendCommand(send_buf, ">", 3000, 10) == Success);
else errorLog();
if (sendCommand(msg, msg, 3000, 5) == Success);
else errorLog();
memset(send_buf, 0, 20); //清空
send_buf[0] = 0x1a;
if (sendCommand(send_buf, send_buf, 5000, 5) == Success);
else errorLog();
}
void errorLog()
{
while (1)
{
if (sendCommand("AT\r\n", "OK\r\n", 100, 10) == Success)
{
soft_reset();
}
delay_ms(200);
}
}
void soft_reset(void) //制造重启命令
{
((void (code *) (void)) 0x0000) ();
}
unsigned int sendCommand(char *Command, char *Response, unsigned long Timeout, unsigned char Retry)
{
unsigned char n;
CLR_Buf2();
for (n = 0; n < Retry; n++)
{
Uart2Printf(Command); //发送GPRS指令
UartPrintf("\r\nDEBUG::SEND\r\n");
UartPrintf(Command);
Time_Cont = 0;
while (Time_Cont < Timeout)
{
delay_ms(100);
Time_Cont += 100;
if (strstr(Rec_Buf2, Response) != NULL)
{
UartPrintf("\r\nDEBUG::RECEIVE\r\n");
UartPrintf(Rec_Buf2);
CLR_Buf2();
return Success;
}
}
Time_Cont = 0;
}
UartPrintf(Rec_Buf2);
UartPrintf("\r\nDEBUG::RECEIVE\r\n");
UartPrintf("!!!NO GET KEYWORD!!!");
CLR_Buf2();
return Failure;
}
//****************************************************
//MS延时函数(11.0592M晶振下测试)
//****************************************************
void delay_ms(unsigned int n)
{
unsigned int i,j;
for(i=0;i<n;i++)
for(j=0;j<1230;j++);
}