近些天一直在研究XC-TC35 模塊,此模塊使用起來特別方便,只要熟悉一些AT指令及簡單的單片機編程便可輕鬆使用編程
廢話很少說,下面粘出我寫的代碼(基於PIC16F877A)函數
此程序能夠實現用手機發送「開燈」或「關燈」,而後tc35會向手機返回「開燈」或「關燈」ui
#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; } }