|
STC89C52相關(guān)程序
0.png (47.91 KB, 下載次數(shù): 24)
下載附件
2018-4-29 00:53 上傳
1.短信控制繼電器
2.打電話(huà)控制蜂鳴器
3.給目標(biāo)手機(jī)打電話(huà)
4.給目標(biāo)手機(jī)發(fā)短信
單片機(jī)源程序如下:
- #include <reg52.h>
- #include <intrins.h>
- #include <string.h>
- #include <stdio.h>
- #include "lcd12864.h"
- #define uint unsigned int
- #define uchar unsigned char
- sbit Send_ENGLISH_key=P3^2; //發(fā)英文TEXT短信按鍵
- sbit Send_CALLING_key=P3^3; //打電話(huà)按鍵
- sbit Send_DS18B20_key=P3^4; //發(fā)溫度按鍵
- sbit Send_SMS_PDU_key=P3^5; //發(fā)中文PDU短信按鍵
- sbit LED0=P0^0;
- sbit LED1=P0^1;
- sbit LED2=P0^2;
- sbit LED3=P0^3;
- #define MAXCHAR 81
- uchar aa[MAXCHAR];
- code uchar ATE0[]="ATE0\r\n";
- code uchar CREG_CMD[]="AT+CREG?\r\n";
- code uchar SMS_send[]="AT+CMGS=18\r\n";
- code uchar ATCN[]="AT+CNMI=2,1\r\n";
- code uchar CMGF0[]="AT+CMGF=0\r\n";
- code uchar CMGF1[]="AT+CMGF=1\r\n";
- code uchar CMGR[12]="AT+CMGR=1\r\n";
- code uchar CMGD[12]="AT+CMGD=1\r\n";
- #define CALL_YOU_TELNUMBER "ATD18622944083;\r\n" //填入你手上的手機(jī)號(hào)碼 不是板子的
- #define SEND_MESSA_TO_YOUR "at+cmgs=\"18622944083\"\r\n" //填入你手上的手機(jī)號(hào)碼 不是板子的
- #define SEND_MESSA_CONTENT "tpytongxin.taobao.com"
- #define CALL_YOU_TELNUMBER "ATD18622944083;\r\n" //填入你手上的手機(jī)號(hào)碼 不是板子的
- #define CALL_YOU_TELNUMBER "ATD18622944083;\r\n" //填入你手上的手機(jī)號(hào)碼 不是板子的
- sbit JIDIANQI=P1^0;
- sbit FENGMINGQI=P1^1;
- /*-----------------------------------------------------------------------
- 1.如果你的晶振是11.0592M
- 只需要修改下面的號(hào)碼就可以了,給成你手上拿著的手機(jī)的號(hào)碼
- 修改方法 在下面這段字符中找到 5129021411F5
- 其實(shí)5129021411F5 --> 15922041115
- 18622044083 8126924480F3
- 看明白了嗎 電話(huà)是兩位兩位顛倒 將您手上的手機(jī)號(hào)碼替換即可
- -----------------------------------------------------------------------*/
- uchar code Sms2_Pdu[]="0891683108200205F011000B818126924480F30008A704521B601D";
- /*-----------------------------------------------------------------------
- 1.如果你的晶振不是11.0592M
- 則修改 Ini_UART 函數(shù) 波特率 9600
- 其他都不用動(dòng)
- -----------------------------------------------------------------------*/
- uchar a,j=0,flag=0;
- int g_timeout=0;
- void delay(uint ms)// 延時(shí)子程序
- {
- uchar i;
- while(ms--)
- {
- for(i=0;i<120;i++);
- }
- }
- /***********************************************************
- 函數(shù)名稱(chēng):Print_Char
- 函數(shù)功能:發(fā)送單個(gè)字符
- 入口參數(shù):ch 出口參數(shù):無(wú)
- ***********************************************************/
- Print_Char(uchar ch)//發(fā)送單個(gè)字符
- {
- SBUF=ch; //送入緩沖區(qū)
- while(TI!=1); //等待發(fā)送完畢
- TI=0; //軟件清零
- }
- /***********************************************************
- 函數(shù)名稱(chēng):Print_Str
- 函數(shù)功能:發(fā)送字符串
- 入口參數(shù):*str 出口參數(shù):無(wú)
- ***********************************************************/
- Print_Str(uchar *str)//發(fā)送字符串
- {
- while(*str!='\0')
- {
- Print_Char(*str);
- delay(2);
- str++;
- }
- }
- /***********************************************************
- 函數(shù)名稱(chēng):Ini_UART
- 函數(shù)功能:串口初始化、定時(shí)器初始化
- 入口參數(shù):無(wú) 出口參數(shù):無(wú)
- ***********************************************************/
- Ini_UART(void)//串口初始化、定時(shí)器初始化
- {
- SCON = 0x50 ; //SCON: serail mode 1, 8-bit UART, enable ucvr
- //UART為模式1,8位數(shù)據(jù),允許接收
- TMOD |= 0x20 ; //TMOD: timer 1, mode 2, 8-bit reload
- //定時(shí)器1為模式2,8位自動(dòng)重裝
- PCON |= 0x80 ; //SMOD=1;
- TH1 = 0xFA ; //Baud:19200 fosc="11".0592MHz
- TL1=0xFA;
- IE |= 0x90 ; //Enable Serial Interrupt
- TR1 = 1 ; // timer 1 run
- TI=1;
- ES=1;
- }
- void clearBuff(void)
- {
- for(j=0;j<MAXCHAR;j++)
- {
- aa[j]=0x00;
- }
- j=0;
- }
- void led(int i)
- {
- P2 |= i;
- delay(20);
- P2 &= ~i;
- delay(20);
- P2 |= i;
- delay(20);
- P2 &= ~i;
- }
- int Hand()
- {
- delay(10);
- if(strstr(aa,"OK")!=NULL) //檢測(cè)單片機(jī)和模塊的連接
- {
- g_timeout=0;
- return 1;
- }
- if(g_timeout>50)
- {
- g_timeout=0;
- return -1;
- }
- g_timeout++;
- return 0;
- }
- void AT(void)
- {
- clearBuff();
- Print_Str(ATE0);
- delay(50);
- while(strstr(aa,"OK")==NULL)
- {
- delay(50);
- led(0x01);
- v_Lcd12864PutString_f( 1,1, "模塊連接【..】") ;
- clearBuff();
- Print_Str(ATE0);
- }
- led(0x02);
- v_Lcd12864PutString_f( 1,1, "模塊連接【OK】") ;
- Print_Str(ATCN);
- delay(50);
- while(1)
- {
- clearBuff();
- Print_Str(CREG_CMD);
- delay(50);
- if(((aa[9]=='0')&&(aa[11]=='1'))||((aa[9]=='0')&&(aa[11]=='5')))
- {
- clearBuff();
- v_Lcd12864PutString_f( 1,1, "模塊注冊(cè)【OK】") ;
- led(0x08);
- break;
- }
- else
- {
- clearBuff();
- v_Lcd12864PutString_f( 1,1, "模塊注冊(cè)【..】") ;
- led(0x04);
- delay(50);
- }
- }
- }
- void send_english()
- {
- P2=0XFF;
- clearBuff();
- Print_Str(CMGF1);
- delay(100);
- led(0x01);
- Print_Str(SEND_MESSA_TO_YOUR);
- delay(300);
- led(0x02);
- Print_Str(SEND_MESSA_CONTENT); //發(fā)短信內(nèi)容
- delay(10);
- Print_Char(0x1A); //發(fā)送結(jié)束符號(hào)
- delay(10);
- led(0x04);
- clearBuff();
- }
- void send_call()
- {
- clearBuff();
- delay(100);
- Print_Str(CALL_YOU_TELNUMBER); //打電話(huà)
- clearBuff();
- delay(300);
- }
- void send_18B20()
- {
- }
- void send_pdu()
- {
- clearBuff();
- Print_Str(CMGF0);
- delay(100);
- led(0x01);
- Print_Str(SMS_send); //發(fā)送中文短信
- delay(500);
- led(0x02);
- Print_Str(Sms2_Pdu); //發(fā)短信內(nèi)容
- Print_Char(0x1A); //發(fā)送結(jié)束符號(hào)
- delay(500);
- led(0x04);
- clearBuff();
- }
- void main()
- {
- v_Lcd12864Init_f() ;
- v_Lcd12864PutString_f( 2,0, "") ;
- v_Lcd12864PutString_f( 2,1, "創(chuàng)思通信") ;
- v_Lcd12864PutString_f( 1,2, "") ;
- v_Lcd12864PutString_f( 1,3, "www.csgsm.com") ;
- delay(500);
- Ini_UART(); //初始化串口
- v_Lcd12864PutString_f( 1,1, "串口啟動(dòng)【OK】") ;
- AT(); //初始化模塊
- delay(10);
- Send_ENGLISH_key=1; //發(fā)英文TEXT短信按鍵
- Send_CALLING_key=1; //打電話(huà)按鍵
- Send_DS18B20_key=1; //發(fā)溫度按鍵
- Send_SMS_PDU_key=1; //發(fā)中文PDU短信按鍵
- clearBuff();
- Print_Str(CMGD);
- delay(50);
- while(Hand()==0);
- clearBuff();
- Print_Str(CMGF1);
- delay(30);
- Print_Str(ATCN);
- delay(30);
- v_Lcd12864PutString_f( 1,1, "給你電話(huà)【..】") ;
- send_call();
- v_Lcd12864PutString_f( 1,1, "給你電話(huà)【OK】") ;
- while(1)
- {
- ////////////////////////////////////////////////////////////////////////
- if(Send_ENGLISH_key==0) //發(fā)英文TEXT短信按鍵
- {
- v_Lcd12864PutString_f( 1,1, "英文短信【..】") ;
- send_english();
- v_Lcd12864PutString_f( 1,1, "英文短信【OK】") ;
- }
- else if(Send_CALLING_key==0) //打電話(huà)按鍵
- {
- v_Lcd12864PutString_f( 1,1, "給你電話(huà)【..】") ;
- send_call();
- v_Lcd12864PutString_f( 1,1, "給你電話(huà)【OK】") ;
- }
- else if(Send_SMS_PDU_key==0)
- {
- }
- ////////////////////////////////////////////////////////////////////////
- // +CMTI: "SM",1
- if(strstr(aa,"RING")!=NULL)
- {
- FENGMINGQI=0;
- delay(2);
- v_Lcd12864PutString_f( 1,1, "電話(huà)來(lái)了【親】") ;
- clearBuff();
- }
- else
- {
- FENGMINGQI=1;
- }
- if(strstr(aa,"+CMTI")!=NULL) //緩存中有+CMTI字符
- {
- clearBuff();
- Print_Str(CMGF1);
- delay(50);
- while(Hand()==0);
- clearBuff();
- Print_Str(CMGR); //讀取1號(hào)位置的短信
- delay(100);
- if(strstr(aa,"k1")!=NULL)
- {
- v_Lcd12864PutString_f( 1,1, "開(kāi)繼電器【OK】") ;
- JIDIANQI=0;
- }
- else if(strstr(aa,"k0")!=NULL)
- {
- v_Lcd12864PutString_f( 1,1, "關(guān)繼電器【OK】") ;
- JIDIANQI=1;
- }
- else
- {
- v_Lcd12864PutString_f( 0,3, strstr(aa,",,")) ;
- }
- clearBuff();
- Print_Str(CMGD);
- delay(50);
- while(Hand()==0);
- clearBuff();
- }
- ////////////////////////////////////////////////////////////////////////
- }
- }
- void ser() interrupt 4
- {
- if(RI==1)
- { aa[j]=SBUF;//命令存到命令數(shù)組
- ……………………
- …………限于本文篇幅 余下代碼請(qǐng)從51黑下載附件…………
復(fù)制代碼
所有資料51hei提供下載:
STC89C52程序.zip
(2.82 MB, 下載次數(shù): 59)
2018-4-27 14:02 上傳
點(diǎn)擊文件名下載附件
下載積分: 黑幣 -5
|
|