51单片机与蓝牙串口通信程序

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

#includereg51.h#includeintrins.h#includestdio.h#includeLCD1602.h#includematrix_key.h#defineuintunsignedint#defineucharunsignedchar#defineNop()_nop_()sbitP10=P1^0;/*定义独立对地按键端口*/sbitP11=P1^1;/*定义独立对地按键端口*/sbitP12=P1^2;/*定义独立对地按键端口*/sbitP13=P1^3;/*定义独立对地按键端口*///shift键bitshift_flag;bitcall_flag;bitCallIn_flag=0;bitreci_flag;bitreci_flag1;sbitsled_en_port=P3^6;/*定义数码管数据锁存器控制端口*/sbitled_en_port=P2^5;/*定义发光二极管数据锁存器控制端口*/sbitds1302_en_port=P2^2;/*定义时钟的选片脚*/ucharCallIn_Num[15];//={00000000000};ucharCallOut_Num[15]={};ucharm=0;//拨号指针uchartemp='?';ucharcodeclr[16]={};ucharcodelcd_table[16]={Ky:Cm:Re:};//ucharsend_buff[15];ucharreci_buff[15]={};ucharz;//接收缓冲区指针uchartime;//定时器中断次数ucharcodemun_to_char[]={0123456789ABCDEF};/*1MS为单位的延时程序*/voidinit();voidsend(ucharcc);voidsend_f(ucharccc);voidinterrupt_pro();voidkey_pro();voidcall_out();voidmain(){uchari,j;delay_1ms(5);init();//定时器初始化lcd_system_reset();/*LCD1602初始化*/for(i=0;i16;i++)lcd_char_write(i,1,lcd_table[i]);/*显示标题*/string_write(0,0,clr);string_write(0,0,Welcome!);sled_en_port=0;/*关闭数码管显示*/led_en_port=0;/*关闭发光二极管显示*/ds1302_en_port=0;/*关闭时钟通讯*/while(1){key_pro();//扫描键盘,按键盘处理if(reci_flag)//串口有数据过来{interrupt_pro();//分析处理reci_flag=0;reci_flag1=1;}if(CallIn_flag==1)//有电话打进来。{CallIn_flag=0;for(i=0,j=0;i15;i++)//&&reci_buff[i]=0x30&&reci_buff[i]=0x39{if(reci_buff[i]='0'&&reci_buff[i]='9'||reci_buff[i]==''){CallIn_Num[j]=reci_buff[i];j++;}reci_buff[i]='';}string_write(0,1,clr);string_write(0,1,CallIn_Num);}if(call_flag==1)//拨号状态{TR0=1;if(time100&&CallOut_Num[0]='0'){call_out();for(i=16;i1;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;i15;i++)CallOut_Num[i]='';//清空time=0;TR0=0;call_flag=0;string_write(0,0,clr);string_write(3,0,Welcome!);}//elseif(time222){time=0;gotostart;}//elsem=0;}}}voidinit()/*串口定时器外部中断初始化*/{TMOD=0X21;//|=0x20;//定时器1为模式2,8位自动重装TH1=0xfd;//Baud:19200fosc=11.0592MHzTL1=0XFD;TH0=0;TL0=0;TR1=1;//timer1runSCON=0x50;//UART为模式1,8位数据,允许接收PCON|=0x80;//SMOD=1;Baud加倍IE|=0x90;//EnableSerialInterruptTR1=1;//timer1runEA=1;ET0=1;}voidsend(ucharcc){SBUF=cc;while(TI==0);TI=0;}voidsend_f(ucharccc){send('');send('');send('F');send(ccc);send('');}voidcall_out(){uchari;send('(');for(i=0;im;i++){send(CallOut_Num[i]);}send(')');m=0;}voidinterrupt_pro(){string_write(0,1,reci_buff);lcd_char_write(14,1,mun_to_char[temp/10]);//fortestlcd_char_write(15,1,mun_to_char[temp%10]);//fortestif(temp==')')CallIn_flag=1;//|temp=='$'|temp=='%'elseif(temp=='$'){lcd_delay(5);//if(temp=='$')string_write(0,0,clr);string_write(0,0,reci_buff);}elseswitch(temp){case'X':string_write(0,0,clr);string_write(0,0,Disconnet);break;case'P':string_write(0,0,clr);string_write(0,0,Pairing);break;case'S':string_write(0,0,clr);string_write(0,0,Linking);break;case'O':string_write(0,0,clr);string_write(0,0,Connect);break;case'R':string_write(0,0,clr);string_write(0,0,Ring);//string_write(5,0,CallIn_Num);break;case'D':string_write(0,0,clr);string_write(0,0,Ding);//string_write(5,0,CallIn_Num);break;case'I':string_write(0,0,clr);string_write(0,0,Talking);break;case'L':string_write(0,0,clr);string_write(0,0,Callfailed);break;case'A':string_write(0,0,clr);string_write(0,0,MP3playing);break;case'E':string_write(0,0,clr);string_write(0,0,Callrelease);break;case'H':string_write(0,0,clr);string_write(0,0,A2DPconnected);break;case'V':string_write(0,0,clr);string_write(0,0,A2DPdisconnected);break;//case')':x=0;break;//for(i=0;i16;i++)lcd_char_write(i,1,lcd_table[i]);/*显示标题*/default:break;}//temp='?';//fortest}voidkey_pro(){uchari,key_value_buff;key_value_buff=key_scan();if(key_value_buff!=0)//有按键动作{switch(key_value_buff)/*显示按键*/{case0x18://0if(call_flag){CallOut_Num[m]='0';lcd_char_write(m+2,0,'0');m++;}elseif(!shift_flag){send('');send('A');//Answerlcd_char_write(3,1,'0');//可以不显示出来lcd_char_write(8,1,'A');lcd_char_write(9,1,'');}else{send_f('P');lcd_char_write(3,1,'0');lcd_char_write(8,1,'F');lcd_char_write(9,1,'P');}break;case0x28://1if(call_flag){CallOut_Num[m]='1';lcd_char_write(m+2,0,'1');m++;}elseif(!shift_flag){send('');send('H');//Hanguplcd_char_write(3,1,'1');lcd_char_write(8,1,'H');lcd_char_write(9,1,'');}else{send_f('L');lcd_char_write(3,1,'1');lcd_char_write(8,1,'F');lcd_char_write(9,1,'L');}break;case0x48://2if(call_flag){CallOut_Num[m]='2';lcd_char_write(m+2,0,'2');m++;}elseif(!shift_flag){send('');send('R');//Rejectlcd_char_write(3,1,'2');lcd_char_write(8,1,'R');lcd_char_write(9,1,'');}else{send_f('U');lcd_char_write(3,1,'2');lcd_char_write(8,1,'F');lcd_char_write(9,1,'U');}break;case0x88://3if(call_flag){CallOut_Num[m]='3';lcd_char_write(m+2,0,'3');m++;}elseif(!shift_flag){send('');send('L');//Rediallcd_char_write(3,1,'3');lcd_char_write(8,1,'L');lcd_char_write(9,1,'');}else{send_f('F');lcd_char_write(3,1,'3');lcd_char_write(8,1,'F');lcd_char_write(9,1,'F');}break;case0x14://4if(call_flag){CallOut_Num[m]='4';lcd_char_write(m+2,0,'4');m++;}elseif(!shift_flag){send('');send('U');//Voluplcd_char_write(3,1,'4');lcd_cha

1 / 13
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功