|
单片机与SIM900A短信控制开关,用短信控制LED-51单片机-2
- /************************************************************
- 程序说明:
- 首先要确定模块已经注册到网络
- 然后正确的硬件连接 P3.0-----STXD或者5VT P3.1-----SRXD或者5VR GND---GND(只要保证公地即可,没必要单独接一次)
- 然后确认你单片机上的晶振,根据晶振修改自己的程序。
- 推荐先将单片机与电脑相连,确定单片机发送的数据是正确的。如果发送的是乱码,请检查晶振与单片机的串口波特率。
- 如果通过以上几条还解决不了问题,请看群共享文件 AN0004 。
- *************************************************************/
- #include <REG51.H>
- #include <string.H>
- #include <intrins.h>
- #define uchar unsigned char
- #define uint unsigned int
- //以下是板子上LED的配置,把Px_x改成自己对应的脚。
- //以下是你的51单片机的晶振大小
- #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;
- //以下是开机后发送到手机的内容,发送的号码在程序中修改。
- uchar flag_rec_message; //接收到短信标志位
- unsigned char flag_reced_mess=0;
- unsigned char fenhao_num; //数据中分号的个数,用来区分短信数据中各个字段的内容
- unsigned int rec_data_len_uart=0; //标记Buffer_Uart0接收数组
- unsigned char flag_rec_message=0; //收到了一条短信提醒时的标志位,我没有SIM900A模块,为了能让单片机处理,所以直接让此标志位为1,你做的时候
- //通过收到短信的提醒要置此标志位。
- unsigned char flag_rec_message_data=0; //开始接收短信数据
- unsigned char flag_start_rec_message; //开始处理短信数据标志位
- unsigned char flag_read_or_un_message; //1 未读取过,0 读取过
- unsigned char flag_send_result=0;
- unsigned char message_data[20]; //存储短信数据
- 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中断接收数组
-
- //注意,无论接收到信号还是发送完信号,都会进中断服务程序的
- /*初始化程序(必须使用,否则无法收发),次程序将会使用定时器1*/
- void SerialInti()//初始化程序(必须使用,否则无法收发)
- {
- TMOD=0x20;//定时器1操作模式2:8位自动重载定时器
- #ifdef FOSC_12M //在这里根据晶振大小设置不同的数值初始化串口
- TH1=0xf3;//装入初值,波特率2400
- TL1=0xf3;
- #else
- TH1=0xfd;//装入初值,波特率9600
- TL1=0xfd;
- #endif //end of SOC_12M
-
- TR1=1;//打开定时器
- SM0=0;//设置串行通讯工作模式,(10为一部发送,波特率可变,由定时器1的溢出率控制)
- SM1=1;//(同上)在此模式下,定时器溢出一次就发送一个位的数据
- REN=1;//串行接收允许位(要先设置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';
- }
- }
- /*串行通讯中断,收发完成将进入该中断*/
- void Serial_interrupt() interrupt 4
- {
- unsigned char temp_rec_data_uart0;
- temp_rec_data_uart0 = SBUF;//读取接收数据
- RI=0;//接收中断信号清零,表示将继续接收
- 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) //如果检测到收到一条短信,开始执行短信数据接收
- {
- if((temp_rec_data_uart0=='R')&&(rec_data_len_uart>5)) //如果接收到的数据‘R’,并且已经接了一些数据了,此时可能单片机开始接收短信数据
- {
- 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]=='+'))//说明收到了短信数据 +CMGR
- {
- flag_start_rec_message=1; //置开始接收短信数据标志位
- fenhao_num=0; //重新开始根据逗号个数查找短信内容
- rec_data_len_uart=0; //前接的数据无用了
- flag_rec_message_data=0;
-
- }
- }
- }
- if(flag_start_rec_message==1) //收到短信数据才进行后续短信数据的提取
- {
- if(temp_rec_data_uart0=='"') //如果收到的数据是 ‘,’
- {
- fenhao_num++;
- if(fenhao_num>11) //一次读取回来的短信数据中逗号的个数不可能超过11个,超过了还没处理完 说明数据有误
- {
- fenhao_num=0;
- flag_rec_message=0; //不对短信数据进行处理
- flag_start_rec_message=0; //无短信数据数据 ,好似不需要处理,待定
- flag_rec_message_data=0;
- }
- switch (fenhao_num)
- {
- case 1:
- rec_data_len_uart=0; //前面数据处理完毕,重新接
- 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; //前面数据处理完毕,重新接
- break;
- case 3: //空
- rec_data_len_uart=0; //前面数据处理完毕,重新接
- break;
- case 4: //temp_data_tele_num 获取电话号码
- //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; //前面数据处理完毕,重新接
- break;
- case 5: //空
- rec_data_len_uart=0; //前面数据处理完毕,重新接
- break;
- case 6: //空
- rec_data_len_uart=0; //前面数据处理完毕,重新接
- break;
- case 7: //时期开始
- rec_data_len_uart=0; //前面数据处理完毕,重新接
- break;
- case 8: //temp_data_date,日期
- memcpy(temp_data_date,Buffer_Uart0_Rec+1,20); //
- flag_rec_message_data=1; //置开始接收短信数据标志位
- fenhao_num=0;
- rec_data_len_uart=0;
- break;
- default:
- break;
- }
- }
- }
- if(flag_rec_message_data==1) //开始接收短信内容数据
- {
- 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)) //数据有误
- { //数据舍弃
- rec_data_len_uart=0;
- fenhao_num=0;
- flag_rec_message=0; //不对短信数据进行处理
- flag_start_rec_message=0; //无短信数据数据 ,好似不需要处理,待定
- }
- else //短信数据正确,接收保存
- {
- //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; //清来短信标志位
- flag_start_rec_message=0; //无短信数据数据 ,好似不需要处理,待定
- flag_send_result=1;
- //message_data[19]='\0';
- //P10=~P10; //用来指示收到短信提示
- }
- //clear_rec_data();
- }
- }
- Buffer_Uart0_Rec[rec_data_len_uart]=temp_rec_data_uart0; //接收数据
- rec_data_len_uart++;
- if(rec_data_len_uart>24)
- {
- rec_data_len_uart=0; //从头开始接收数据
- }
- }
- void Uart1Send(uchar c)
- {
- SBUF=c;
- while(!TI);//等待发送完成信号(TI=1)出现
- TI=0;
- }
- //串行口连续发送char型数组,遇到终止号/0将停止
- void Uart1Sends(uchar *str)
- {
- while(*str!='\0')
- {
- SBUF=*str;
- while(!TI);//等待发送完成信号(TI=1)出现
- TI=0;
- str++;
- }
- }
- //延时函数大概是1s钟,不过延时大的话不准...
- 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);//延时3秒
- Uart1Sends("AT\r\n");
- DelaySec(1);//延时3秒
- Uart1Sends("ATE0\r\n");
- DelaySec(1);//延时3秒
- //-----------配置接收短信方式----------------
- Uart1Sends("AT+CMGF=1\r\n");
- DelaySec(1);//延时3秒
- Uart1Sends("AT+CSCS=\"GSM\"\r\n");
- DelaySec(1);//延时3秒
- Uart1Sends("AT+CMGD=1,4\r\n");
- DelaySec(1);//延时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);//延时3秒
- // P15=0;
- Uart1Sends("AT+CMGR=1\r\n");
- flag_rec_message=1;
- }
- }
- }
复制代码
|
评分
-
查看全部评分
|