近些天一直在研究XC-TC35 模块,此模块使用起来特别方便,只要熟悉一些AT指令及简单的单片机编程即可轻松使用
废话不多说,下面粘出我写的代码(基于PIC16F877A)
此程序可以实现用手机发送“开灯”或“关灯”,然后tc35会向手机返回“开灯”或“关灯”
#include<pic.h>
#include<string.h>
#include"serial.h"
__CONFIG(FOSC_HS&WDTE_OFF);
void set(char *str);//设置命令
void sendMessage(char *str1,char *str2);//发送函数
void main()
{
serialInit();
set("AT");//握手
set("AT+CSCS=\"UCS2\"");//PDU
set("AT+CMGF=0");
set("AT+CNMI=2,1");//设置短信接收存储位置
set("AT+CMGD=1");//删除1号位置短信
delay(100);
while(1)
{
while(strstr(Rec_Buf,"+CMTI"))
{
Clr_Buf();
sendStringln("AT+CMGR=1");
delay(100);
while(strstr(Rec_Buf,"OK") == NULL);
delay(100);
if(strstr(Rec_Buf,"5173")!=NULL)
{
Clr_Buf();
delay(100);
sendMessage("AT+CMGS=19","0011000D91685165950629F30008AA045173706F");
}
else if(strstr(Rec_Buf,"5F00")!=NULL)
{
Clr_Buf();
delay(100);
sendMessage("AT+CMGS=19","0011000D91685165950629F30008AA045F00706F");
}
Clr_Buf();
delay(100);
set("AT+CMGD=1");
}
}
}
void set(char *str)
{
sendStringln(str);
delay(100);
while(strstr(Rec_Buf,"OK") == NULL);
Clr_Buf();
}
void sendMessage(char *str1,char *str2)
{
sendStringln(str1);
delay(100);
while(strstr(Rec_Buf,">") == NULL);
sendString(str2);
delay(100);
SEND_ONE(0x1A);
delay(100);
while(strstr(Rec_Buf,"+CMGS")==NULL);
Clr_Buf();
}
#include<string.h>
#define uchar unsigned char
#define uint unsigned int
#define SEND_ONE(A) TXREG=A;
uchar N=0; //接收数据累计
uchar Rec_Buf[50]; //接收数据存放在此
void serialInit();
void delay(uint z)
{
uint x,y;
for(x=0;x<=z;x++)
for(y=0;y<=71;y++);
}
void serialInit()
{
SPBRG=0x17;//3.6864M 9600
TXSTA=0X24;
RCSTA=0X90;
RCIE=1;
GIE=1;
PEIE=1;
}
void interrupt usart()
{
if(RCIE && RCIF) //不必清RCIF,因为接收数据后RCIF自动清0
{
Rec_Buf[N]=RCREG;
N++;
if (N>=50)
N=0; //如果接收数据数超出,从0存放
}
}
void sendString(char *Rec_Buf)
{
uchar i;
for(i = 0; i < strlen(Rec_Buf); i ++)
{
while(!TXIF);
SEND_ONE(Rec_Buf[i]);
}
N = 0;
}
void sendStringln(char *Rec_Buf)
{
sendString(Rec_Buf);
sendString("\r\n");
}
void Clr_Buf()
{
uchar i;
for(i = 0; i < strlen(Rec_Buf); i++)
{
Rec_Buf[i] == 0;
N = 0;
}
}
来源:oschina
链接:https://my.oschina.net/u/2424848/blog/517778