資源描述:
《51單片機(jī)與藍(lán)牙串口通信程序》由會(huì)員上傳分享,免費(fèi)在線閱讀,更多相關(guān)內(nèi)容在行業(yè)資料-天天文庫。
1、#include#include#include#include"LCD1602.h"#include"matrix_key.h"#defineuintunsignedint#defineucharunsignedchar#defineNop()_nop_()sbitP10=P1^0;/*定義獨(dú)立對(duì)地按鍵端口*/sbitP11=P1^1;/*定義獨(dú)立對(duì)地按鍵端口*/sbitP12=P1^2;/*定義獨(dú)立對(duì)地按鍵端口*/sbitP13=P1^3;/*定義獨(dú)立對(duì)地按鍵端口*///shift鍵bitshift_flag;
2、bitcall_flag;bitCallIn_flag=0;bitreci_flag;bitreci_flag1;sbitsled_en_port=P3^6;/*定義數(shù)碼管數(shù)據(jù)鎖存器控制端口*/sbitled_en_port=P2^5;/*定義發(fā)光二極管數(shù)據(jù)鎖存器控制端口*/sbitds1302_en_port=P2^2;/*定義時(shí)鐘的選片腳*/ucharCallIn_Num[15];//={"00000000000"};ucharCallOut_Num[15]={""};ucharm=0;//撥號(hào)指針uchartemp='?';ucharcodeclr[16]={""}
3、;ucharcodelcd_table[16]={"Ky:Cm:Re:"};//ucharsend_buff[15];ucharreci_buff[15]={""};ucharz;//接收緩沖區(qū)指針uchartime;//定時(shí)器中斷次數(shù)ucharcodemun_to_char[]={"0123456789ABCDEF"};/*1MS為單位的延時(shí)程序*/voidinit();voidsend(ucharcc);voidsend_f(ucharccc);voidinterrupt_pro();voidkey_pro();voidcall_out();voidmain(){uc
4、hari,j;delay_1ms(5);init();//定時(shí)器初始化lcd_system_reset();/*LCD1602初始化*/for(i=0;i<16;i++)lcd_char_write(i,1,lcd_table[i]);/*顯示標(biāo)題*/string_write(0,0,clr);string_write(0,0,"Welcome!");sled_en_port=0;/*關(guān)閉數(shù)碼管顯示*/led_en_port=0;/*關(guān)閉發(fā)光二極管顯示*/ds1302_en_port=0;/*關(guān)閉時(shí)鐘通訊*/while(1){key_pro();//掃描鍵盤,按鍵盤處理i
5、f(reci_flag)//串口有數(shù)據(jù)過來{interrupt_pro();//分析處理reci_flag=0;reci_flag1=1;}if(CallIn_flag==1)//有電話打進(jìn)來。{CallIn_flag=0;for(i=0,j=0;i<15;i++)//&&reci_buff[i]>=0x30&&reci_buff[i]<=0x39{if(reci_buff[i]>='0'&&reci_buff[i]<='9'
6、
7、reci_buff[i]==''){CallIn_Num[j]=reci_buff[i];j++;}reci_buff[i]='';}strin
8、g_write(0,1,clr);string_write(0,1,CallIn_Num);}if(call_flag==1)//撥號(hào)狀態(tài){TR0=1;if(time>100&&CallOut_Num[0]>='0'){call_out();for(i=16;i>1;i--){string_write(2,0,CallOut_Num);delay_1ms(222);delay_1ms(222);string_write(2,0,"");delay_1ms(222);delay_1ms(222);}for(i=0;i<15;i++)CallOut_Num[i]='';//清
9、空time=0;TR0=0;call_flag=0;string_write(0,0,clr);string_write(3,0,"Welcome!");}//elseif(time>222){time=0;gotostart;}//elsem=0;}}}voidinit()/*串口定時(shí)器外部中斷初始化*/{TMOD=0X21;//
10、=0x20;//定時(shí)器1為模式2,8位自動(dòng)重裝TH1=0xfd;//Baud:19200fosc="11".0592MHzTL1=0XFD;TH0=0;TL0=0;TR1=1;//timer1ru