51单片机与蓝牙模块的串口通信.docx
- 文档编号:11220881
- 上传时间:2023-02-25
- 格式:DOCX
- 页数:21
- 大小:18.86KB
51单片机与蓝牙模块的串口通信.docx
《51单片机与蓝牙模块的串口通信.docx》由会员分享,可在线阅读,更多相关《51单片机与蓝牙模块的串口通信.docx(21页珍藏版)》请在冰豆网上搜索。
51单片机与蓝牙模块的串口通信
#include
#include
#include
#include"LCD1602.h"
#include"matrix_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;i<16;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;i<15;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(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]='';//清空
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()/*串口定时器外部中断初始化*/
{
TMOD=0X21;//|=0x20;//定时器1为模式2,8位自动重装
TH1=0xfd;//Baud:
19200fosc="11".0592MHz
TL1=0XFD;
TH0=0;
TL0=0;
TR1=1;//timer1run
SCON=0x50;//UART为模式1,8位数据,允许接收
PCON|=0x80;//SMOD=1;Baud加倍
IE|=0x90;//EnableSerialInterrupt
TR1=1;//timer1run
EA=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;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]);//fortest lcd_char_write(15,1,mun_to_char[temp%10]);//fortest if(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;i<16;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: //0 if(call_flag){CallOut_Num[m]='0';lcd_char_write(m+2,0,'0');m++;} elseif(! shift_flag) {send('');send('A');//Answer lcd_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: //1 if(call_flag){CallOut_Num[m]='1';lcd_char_write(m+2,0,'1');m++;} elseif(! shift_flag) {send('');send('H');//Hangup lcd_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: //2 if(call_flag){CallOut_Num[m]='2';lcd_char_write(m+2,0,'2');m++;} elseif(! shift_flag) {send('');send('R');//Reject lcd_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: //3 if(call_flag){CallOut_Num[m]='3';lcd_char_write(m+2,0,'3');m++;} elseif(! shift_flag) {send('');send('L');//Redial lcd_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: //4 if(call_flag){CallOut_Num[m]='4';lcd_char_write(m+2,0,'4');m++;} elseif(! shift_flag) {send('');send('U');//Volup lcd_char_write(3,1,'4'); lcd_char_write(8,1,'U'); lcd_char_write(9,1,''); } else {send_f('B'); lcd_char_write(3,1,'4'); lcd_char_write(8,1,'F'); lcd_char_write(9,1,'B'); } break; case0x24: //5 if(call_flag){CallOut_Num[m]='5';lcd_char_write(m+2,0,'5');m++;} elseif(! shift_flag) {send('');send('D');//Voldown lcd_char_write(3,1,'5'); lcd_char_write(8,1,'D'); lcd_char_write(9,1,''); } else {send_f('S'); lcd_char_write(3,1,'5'); lcd_char_write(8,1,'F'); lcd_char_write(9,1,'S'); } break; case0x44: //6 if(call_flag){CallOut_Num[m]='6';lcd_char_write(m+2,0,'6');m++;} elseif(! shift_flag) {send('');send('0');// lcd_char_write(3,1,'6'); lcd_char_write(8,1,'0'); lcd_char_write(9,1,''); } else {send('');send('G');send('x'); lcd_char_write(3,1,'6'); lcd_char_write(8,1,'G'); lcd_char_write(9,1,'x'); } break; case0x84: //7 if(call_flag){CallOut_Num[m]='7';lcd_char_write(m+2,0,'7');m++;} elseif(! shift_flag) {send('');send('V');//Voicedial lcd_char_write(3,1,'7'); lcd_char_write(8,1,'V'); lcd_char_write(9,1,''); } else {send('');send('n'); lcd_char_write(3,1,'7'); lcd_char_write(8,1,'n'); lcd_char_write(9,1,''); } break; case0x12: //8 if(call_flag){CallOut_Num[m]='8';lcd_char_write(m+2,0,'8');m++;} elseif(! shift_flag) {send('');send('Z');//Calltransfer lcd_char_write(3,1,'8'); lcd_char_write(8,1,'Z'); lcd_char_write(9,1,''); } else {send('');send('p'); lcd_char_write(3,1,'8'); lcd_char_write(8,1,'p'); lcd_char_write(9,1,''); } break; case0x22: //9 if(call_flag){CallOut_Num[m]='9';lcd_char_write(m+2,0,'9');m++;} elseif(! shift_flag) {send('');send('Q');//Disconnect lcd_char_write(3,1,'9'); lcd_char_write(8,1,'Q'); lcd_char_write(9,1,''); } else {send('');send('z');//Calltransfer lcd_char_write(3,1,'9'); lcd_char_write(8,1,'z'); lcd_char_write(9,1,''); } break; case0x42: //A* if(call_flag){CallOut_Num[m]='*';lcd_char_write(m+2,0,'*');m++;} else {send('');send('Y');//Version lcd_char_write(3,1,'A'); lcd_char_write(8,1,'Y'); lcd_char_write(9,1,'');} break; case0x82: //B# if(call_flag){CallOut_Num[m]='#';lcd_char_write(m+2,0,'#');m++;} else {send('');send('T');//Setautoanswer lcd_char_write(3,1,'B'); lcd_char_write(8,1,'T'); lcd_char_write(9,1,'');} break; case0x11: if(! call_flag) {send('');send('t');//Clrautoanswer lcd_char_write(3,1,'C'); lcd_char_write(8,1,'t'); lcd_char_write(9,1,'');} break; case0x21: if(! call_flag){ send('');send('M');//Setvolumeset lcd_char_write(3,1,'D'); lcd_char_write(8,1,'M'); lcd_char_write(9,1,'');}break; case0x41: if(! call_flag){ send('');send('m');//Clrvolumeset lcd_char_write(3,1,'E'); lcd_char_write(8,1,'m'); lcd_char_write(9,1,'');}break; case0x81: if(! call_flag){ send('');send('N');//Setautolink lcd_char_write(3,1,'F'); lcd_char_write(8,1,'N'); lcd_char_write(9,1,'');}break; default: break; } while(key_scan()! =0);/*等待按键放开*/ } scan_key_port=0xff;/*释放矩阵按键端口*/ delay_1ms
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 51 单片机 蓝牙 模块 串口 通信
![提示](https://static.bdocx.com/images/bang_tan.gif)