|
單片機(jī)與SIM900A短信控制開關(guān),用短信控制LED-51單片機(jī)-2
- /************************************************************
- 程序說明:
- 首先要確定模塊已經(jīng)注冊到網(wǎng)絡(luò)
- 然后正確的硬件連接 P3.0-----STXD或者5VT P3.1-----SRXD或者5VR GND---GND(只要保證公地即可,沒必要單獨(dú)接一次)
- 然后確認(rèn)你單片機(jī)上的晶振,根據(jù)晶振修改自己的程序。
- 推薦先將單片機(jī)與電腦相連,確定單片機(jī)發(fā)送的數(shù)據(jù)是正確的。如果發(fā)送的是亂碼,請檢查晶振與單片機(jī)的串口波特率。
- 如果通過以上幾條還解決不了問題,請看群共享文件 AN0004 。
- *************************************************************/
- #include <REG51.H>
- #include <string.H>
- #include <intrins.h>
- #define uchar unsigned char
- #define uint unsigned int
- //以下是板子上LED的配置,把Px_x改成自己對應(yīng)的腳。
- //以下是你的51單片機(jī)的晶振大小
- #define FOSC_110592M
- //#define FOSC_12M
- sbit P10=P1^0;
- sbit P11=P1^1;
- sbit P12=P1^2;
- sbit P13=P1^3;
- sbit P14=P1^4;
- sbit P15=P1^5;
- //以下是開機(jī)后發(fā)送到手機(jī)的內(nèi)容,發(fā)送的號(hào)碼在程序中修改。
- uchar flag_rec_message; //接收到短信標(biāo)志位
- unsigned char flag_reced_mess=0;
- unsigned char fenhao_num; //數(shù)據(jù)中分號(hào)的個(gè)數(shù),用來區(qū)分短信數(shù)據(jù)中各個(gè)字段的內(nèi)容
- unsigned int rec_data_len_uart=0; //標(biāo)記Buffer_Uart0接收數(shù)組
- unsigned char flag_rec_message=0; //收到了一條短信提醒時(shí)的標(biāo)志位,我沒有SIM900A模塊,為了能讓單片機(jī)處理,所以直接讓此標(biāo)志位為1,你做的時(shí)候
- //通過收到短信的提醒要置此標(biāo)志位。
- unsigned char flag_rec_message_data=0; //開始接收短信數(shù)據(jù)
- unsigned char flag_start_rec_message; //開始處理短信數(shù)據(jù)標(biāo)志位
- unsigned char flag_read_or_un_message; //1 未讀取過,0 讀取過
- unsigned char flag_send_result=0;
- unsigned char message_data[20]; //存儲(chǔ)短信數(shù)據(jù)
- unsigned char xdata temp_data_read[11],temp_data_tele_num[15],temp_data_date[21];
- unsigned char idata Buffer_Uart0_Rec[25]={0}; //Uart0中斷接收數(shù)組
-
- //注意,無論接收到信號(hào)還是發(fā)送完信號(hào),都會(huì)進(jìn)中斷服務(wù)程序的
- /*初始化程序(必須使用,否則無法收發(fā)),次程序?qū)?huì)使用定時(shí)器1*/
- void SerialInti()//初始化程序(必須使用,否則無法收發(fā))
- {
- TMOD=0x20;//定時(shí)器1操作模式2:8位自動(dòng)重載定時(shí)器
- #ifdef FOSC_12M //在這里根據(jù)晶振大小設(shè)置不同的數(shù)值初始化串口
- TH1=0xf3;//裝入初值,波特率2400
- TL1=0xf3;
- #else
- TH1=0xfd;//裝入初值,波特率9600
- TL1=0xfd;
- #endif //end of SOC_12M
-
- TR1=1;//打開定時(shí)器
- SM0=0;//設(shè)置串行通訊工作模式,(10為一部發(fā)送,波特率可變,由定時(shí)器1的溢出率控制)
- SM1=1;//(同上)在此模式下,定時(shí)器溢出一次就發(fā)送一個(gè)位的數(shù)據(jù)
- REN=1;//串行接收允許位(要先設(shè)置sm0sm1再開串行允許)
- EA=1;//開總中斷
- ES=1;//開串行口中斷
- }
- unsigned char hand(unsigned char *data_source,unsigned char *ptr)
- {
- if(strstr(data_source,ptr)!=NULL)
- return 1;
- else
- return 0;
- }
- void clear_rec_data()
- {
- uchar i,temp_len;
- temp_len=strlen(Buffer_Uart0_Rec);
- if(temp_len>25)
- {
- temp_len=25;
- }
- for(i=0;i<25;i++)
- {
- Buffer_Uart0_Rec[i]='0';
- }
- rec_data_len_uart=0;
- }
- void clear_message_data()
- {
- unsigned char temp_len,i;
- temp_len=strlen(message_data);
- if(temp_len>20)
- {
- temp_len=20;
- }
- for(i=0;i<temp_len;i++)
- {
- message_data[i]='\0';
- }
- }
- /*串行通訊中斷,收發(fā)完成將進(jìn)入該中斷*/
- void Serial_interrupt() interrupt 4
- {
- unsigned char temp_rec_data_uart0;
- temp_rec_data_uart0 = SBUF;//讀取接收數(shù)據(jù)
- RI=0;//接收中斷信號(hào)清零,表示將繼續(xù)接收
- if((temp_rec_data_uart0=='I')&&(rec_data_len_uart>3))
- {
- if((Buffer_Uart0_Rec[rec_data_len_uart-1]=='T')&&(Buffer_Uart0_Rec[rec_data_len_uart-2]=='M')&&(Buffer_Uart0_Rec[rec_data_len_uart-3]=='C')&&(Buffer_Uart0_Rec[rec_data_len_uart-4]=='+'))
- {
- flag_reced_mess=1;
- }
- }
- if(flag_rec_message==1) //如果檢測到收到一條短信,開始執(zhí)行短信數(shù)據(jù)接收
- {
- if((temp_rec_data_uart0=='R')&&(rec_data_len_uart>5)) //如果接收到的數(shù)據(jù)‘R’,并且已經(jīng)接了一些數(shù)據(jù)了,此時(shí)可能單片機(jī)開始接收短信數(shù)據(jù)
- {
- if((Buffer_Uart0_Rec[rec_data_len_uart-1]=='G')&&(Buffer_Uart0_Rec[rec_data_len_uart-2]=='M')&&(Buffer_Uart0_Rec[rec_data_len_uart-3]=='C')&&(Buffer_Uart0_Rec[rec_data_len_uart-4]=='+'))//說明收到了短信數(shù)據(jù) +CMGR
- {
- flag_start_rec_message=1; //置開始接收短信數(shù)據(jù)標(biāo)志位
- fenhao_num=0; //重新開始根據(jù)逗號(hào)個(gè)數(shù)查找短信內(nèi)容
- rec_data_len_uart=0; //前接的數(shù)據(jù)無用了
- flag_rec_message_data=0;
-
- }
- }
- }
- if(flag_start_rec_message==1) //收到短信數(shù)據(jù)才進(jìn)行后續(xù)短信數(shù)據(jù)的提取
- {
- if(temp_rec_data_uart0=='"') //如果收到的數(shù)據(jù)是 ‘,’
- {
- fenhao_num++;
- if(fenhao_num>11) //一次讀取回來的短信數(shù)據(jù)中逗號(hào)的個(gè)數(shù)不可能超過11個(gè),超過了還沒處理完 說明數(shù)據(jù)有誤
- {
- fenhao_num=0;
- flag_rec_message=0; //不對短信數(shù)據(jù)進(jìn)行處理
- flag_start_rec_message=0; //無短信數(shù)據(jù)數(shù)據(jù) ,好似不需要處理,待定
- flag_rec_message_data=0;
- }
- switch (fenhao_num)
- {
- case 1:
- rec_data_len_uart=0; //前面數(shù)據(jù)處理完畢,重新接
- break;
- case 2: //后取短信是否讀取
- //memcpy(temp_data_read,Buffer_Uart0_Rec+rec_data_len_uart-8,rec_data_len_uart-4); //多存一些
- memcpy(temp_data_read,Buffer_Uart0_Rec+1,7); //多存一些
- // if(hand(temp_data_read,"UNREAD"))
- // {
- // flag_read_or_un_message=1; //沒讀取過
- // }
- // else
- // {
- // flag_read_or_un_message=0; //讀取過
- // }
- //temp_data_read[rec_data_len_uart-3]='\0';
- temp_data_read[10]='\0';
- rec_data_len_uart=0; //前面數(shù)據(jù)處理完畢,重新接
- break;
- case 3: //空
- rec_data_len_uart=0; //前面數(shù)據(jù)處理完畢,重新接
- break;
- case 4: //temp_data_tele_num 獲取電話號(hào)碼
- //memcpy(temp_data_tele_num,Buffer_Uart0_Rec+(rec_data_len_uart-13),13); //多存一些
- memcpy(temp_data_tele_num,Buffer_Uart0_Rec+1,14); //多存一些
- rec_data_len_uart=0; //前面數(shù)據(jù)處理完畢,重新接
- break;
- case 5: //空
- rec_data_len_uart=0; //前面數(shù)據(jù)處理完畢,重新接
- break;
- case 6: //空
- rec_data_len_uart=0; //前面數(shù)據(jù)處理完畢,重新接
- break;
- case 7: //時(shí)期開始
- rec_data_len_uart=0; //前面數(shù)據(jù)處理完畢,重新接
- break;
- case 8: //temp_data_date,日期
- memcpy(temp_data_date,Buffer_Uart0_Rec+1,20); //
- flag_rec_message_data=1; //置開始接收短信數(shù)據(jù)標(biāo)志位
- fenhao_num=0;
- rec_data_len_uart=0;
- break;
- default:
- break;
- }
- }
- }
- if(flag_rec_message_data==1) //開始接收短信內(nèi)容數(shù)據(jù)
- {
- if((temp_rec_data_uart0==0x0a)&&(Buffer_Uart0_Rec[rec_data_len_uart-1]==0x0d)&&(Buffer_Uart0_Rec[rec_data_len_uart-2]==0X4B)&&(Buffer_Uart0_Rec[rec_data_len_uart-3]==0x4F)) //短信接收完畢
- {
- if((Buffer_Uart0_Rec[0]!=0x22)||(Buffer_Uart0_Rec[1]!=0x0d)||(Buffer_Uart0_Rec[2]!=0x0a)) //數(shù)據(jù)有誤
- { //數(shù)據(jù)舍棄
- rec_data_len_uart=0;
- fenhao_num=0;
- flag_rec_message=0; //不對短信數(shù)據(jù)進(jìn)行處理
- flag_start_rec_message=0; //無短信數(shù)據(jù)數(shù)據(jù) ,好似不需要處理,待定
- }
- else //短信數(shù)據(jù)正確,接收保存
- {
- //memcpy(message_data,Buffer_Uart0_Rec+3,rec_data_len_uart-10);
- memcpy(message_data,Buffer_Uart0_Rec+3,6);
- rec_data_len_uart=0;
- fenhao_num=0;
- flag_rec_message=0; //清來短信標(biāo)志位
- flag_start_rec_message=0; //無短信數(shù)據(jù)數(shù)據(jù) ,好似不需要處理,待定
- flag_send_result=1;
- //message_data[19]='\0';
- //P10=~P10; //用來指示收到短信提示
- }
- //clear_rec_data();
- }
- }
- Buffer_Uart0_Rec[rec_data_len_uart]=temp_rec_data_uart0; //接收數(shù)據(jù)
- rec_data_len_uart++;
- if(rec_data_len_uart>24)
- {
- rec_data_len_uart=0; //從頭開始接收數(shù)據(jù)
- }
- }
- void Uart1Send(uchar c)
- {
- SBUF=c;
- while(!TI);//等待發(fā)送完成信號(hào)(TI=1)出現(xiàn)
- TI=0;
- }
- //串行口連續(xù)發(fā)送char型數(shù)組,遇到終止號(hào)/0將停止
- void Uart1Sends(uchar *str)
- {
- while(*str!='\0')
- {
- SBUF=*str;
- while(!TI);//等待發(fā)送完成信號(hào)(TI=1)出現(xiàn)
- TI=0;
- str++;
- }
- }
- //延時(shí)函數(shù)大概是1s鐘,不過延時(shí)大的話不準(zhǔn)...
- void DelaySec(int sec)
- {
- uint i , j= 0;
- for(i=0; i<sec; i++)
- {
- for(j=0; j<65535; j++)
- {
- }
- }
- }
- void main()
- {
- uchar i = 0;
- SerialInti();
- P1=0XFF; //所有LED 都滅
- Uart1Sends("AT\r\n");
- DelaySec(1);//延時(shí)3秒
- Uart1Sends("AT\r\n");
- DelaySec(1);//延時(shí)3秒
- Uart1Sends("ATE0\r\n");
- DelaySec(1);//延時(shí)3秒
- //-----------配置接收短信方式----------------
- Uart1Sends("AT+CMGF=1\r\n");
- DelaySec(1);//延時(shí)3秒
- Uart1Sends("AT+CSCS=\"GSM\"\r\n");
- DelaySec(1);//延時(shí)3秒
- Uart1Sends("AT+CMGD=1,4\r\n");
- DelaySec(1);//延時(shí)3秒
- P10=0; //提示初始化完成
- while(1)
- {
- if(flag_send_result==1)
- {
- flag_send_result=0;
- flag_rec_message=0;
- Uart1Sends("AT+CMGD=1,4\r\n"); //解析完畢,刪除短信
- _nop_();_nop_();_nop_();
- _nop_();_nop_();_nop_();
- for(i=0;i<strlen(temp_data_read);i++)
- {
- Uart1Send(*(temp_data_read+i));
- }
- Uart1Send(0x0d);
- Uart1Send(0x0a);
- for(i=0;i<14;i++)
- {
- Uart1Send(*(temp_data_tele_num+i));
- }
- Uart1Send(0x0d);
- Uart1Send(0x0a);
- for(i=0;i<20;i++)
- {
- Uart1Send(*(temp_data_date+i));
- }
- Uart1Send(0x0d);
- Uart1Send(0x0a);
- for(i=0;i<strlen(message_data);i++)
- {
- Uart1Send(*(message_data+i));
- }
- if(hand(message_data,"LED1"))
- {
- P10=~P10;
- }
- else if(hand(message_data,"LED2"))
- {
- P11=~P11;
- }
- else if(hand(message_data,"LED3"))
- {
- P12=~P12;
- }
- else if(hand(message_data,"LED4"))
- {
- P13=~P13;
- }
- else if(hand(message_data,"LEDON"))
- {
- P1=0;
- }
- else if(hand(message_data,"LEDOFF"))
- {
- P1=0XFF;
- }
- // Uart1Send(0x0d);
- // Uart1Send(0x0a);
- clear_message_data();
- }
- if(flag_reced_mess==1)
- {
- _nop_();_nop_();
- _nop_();_nop_();
- _nop_();_nop_();
- flag_reced_mess=0;
- clear_rec_data();
- //P10=~P10; //用來指示收到短信提示
- //DelaySec(1);//延時(shí)3秒
- // P15=0;
- Uart1Sends("AT+CMGR=1\r\n");
- flag_rec_message=1;
- }
- }
- }
復(fù)制代碼
0.png (47.63 KB, 下載次數(shù): 134)
下載附件
2016-6-5 00:32 上傳
|
評(píng)分
-
查看全部評(píng)分
|