《51单片机与蓝牙串口通信程序.doc》由会员分享,可在线阅读,更多相关《51单片机与蓝牙串口通信程序.doc(13页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、#include #include #include#include LCD1602.h#include matrix_key.h#define uint unsigned int#define uchar unsigned char#define Nop() _nop_()sbit P10 = P10; /*定义独立对地按键端口*/sbit P11 = P11; /*定义独立对地按键端口*/sbit P12 = P12; /*定义独立对地按键端口*/sbit P13 = P13; /*定义独立对地按键端口*/ /shift键bit shift_flag;bit call_flag ;bit
2、CallIn_flag=0;bit reci_flag;bit reci_flag1;sbit sled_en_port = P36; /*定义数码管数据锁存器控制端口*/sbit led_en_port = P25; /*定义发光二极管数据锁存器控制端口*/sbit ds1302_en_port = P22; /*定义时钟的选片脚*/uchar CallIn_Num15;/=;uchar CallOut_Num15= ;uchar m=0;/拨号指针uchar temp=?;uchar code clr16= ;uchar code lcd_table16 = Ky: Cm: Re: ;/u
3、char send_buff15;uchar reci_buff15= ;uchar z; /接收缓冲区指针uchar time;/定时器中断次数uchar code mun_to_char=ABCDEF;/*1MS为单位的延时程序*/void init();void send(uchar cc);void send_f(uchar ccc);void interrupt_pro();void key_pro();void call_out();void main()uchar i,j; delay_1ms(5);init(); /定时器初始化lcd_system_reset();/*LCD1
4、602 初始化*/for(i=0;i16;i+) lcd_char_write(i,1,lcd_tablei); /*显示标题*/ 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_
5、flag1=1; if(CallIn_flag=1)/有电话打进来。 CallIn_flag=0; for(i=0,j=0;i=0x30 &reci_buffi=0&reci_buffi100&CallOut_Num0=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;i222)time=0;goto start;/else m=0; v
6、oid init() /* 串口定时器外部中断初始化 */ TMOD =0X21;/|= 0x20; /定时器1为模式2,8位自动重装 TH1 = 0xfd; /Baud:19200 fosc=11.0592MHzTL1=0XFD ;TH0=0;TL0=0;TR1 = 1;/ timer 1 run SCON = 0x50; /UART为模式1,8位数据,允许接收 PCON |= 0x80 ; /SMOD=1; Baud加倍 IE |= 0x90 ; /Enable Serial Interrupt TR1 = 1 ; / timer 1 run EA=1;ET0=1;void send(uc
7、har cc) SBUF=cc; while(TI=0); TI=0;void send_f(uchar ccc) send( ); send();void call_out() uchar i; send(); for(i=0;im;i+) send(CallOut_Numi); send(); m=0;void interrupt_pro() string_write(0,1,reci_buff);lcd_char_write(14,1,mun_to_chartemp/10); /for test lcd_char_write(15,1,mun_to_chartemp%10); /for
8、test if(temp=) CallIn_flag=1;/|temp=$ |temp=%else if(temp=$) lcd_delay(5);/if(temp=$) string_write(0,0,clr); string_write(0,0,reci_buff); else switch(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_wri
9、te(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:s
10、tring_write(0,0,clr);string_write(0,0,Talking); break; case L:string_write(0,0,clr);string_write(0,0,Call failed); break; case A:string_write(0,0,clr);string_write(0,0,MP3 playing); break; case E:string_write(0,0,clr);string_write(0,0,Call release ); break; case H:string_write(0,0,clr);string_write(
11、0,0,A2DP connected );break; case V:string_write(0,0,clr);string_write(0,0,A2DP disconnected);break; / case ):x=0;break;/for(i=0;i16;i+) lcd_char_write(i,1,lcd_tablei); /*显示标题*/ default:break; /temp=?; /for testvoid key_pro() uchar i,key_value_buff;key_value_buff = key_scan();if(key_value_buff != 0)
12、/有按键动作switch(key_value_buff) /*显示按键*/ case 0x18: /0 if(call_flag) CallOut_Numm=0;lcd_char_write(m+2,0,0);m+;else if(!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_
13、write(9,1,P);break;case 0x28:/1 if(call_flag) CallOut_Numm=1;lcd_char_write(m+2,0,1);m+; else if(!shift_flag) send( );send(H); /Hang uplcd_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;case 0
14、x48:/2 if(call_flag) CallOut_Numm=2;lcd_char_write(m+2,0,2);m+; else if(!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;case 0x88:/3 if(call_flag) CallO
15、ut_Numm=3;lcd_char_write(m+2,0,3);m+; else if(!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;case 0x14:/4 if(call_flag) CallOut_Numm=4;lcd_char_write(m+
16、2,0,4);m+;else if(!shift_flag) send( );send(U); /Vol uplcd_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;case 0x24:/5 if(call_flag) CallOut_Numm=5;lcd_char_write(m+2,0,5);m+; else if(!shift_fl
17、ag) send( );send(D);/Vol downlcd_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;case 0x44:/6 if(call_flag) CallOut_Numm=6;lcd_char_write(m+2,0,6);m+; else if(!shift_flag) send( );send(0); /lcd
18、_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;case 0x84:/7 if(call_flag) CallOut_Numm=7;lcd_char_write(m+2,0,7);m+; else if(!shift_flag) send( );send(V); /Voice diallcd_char_writ
19、e(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;case 0x12:/8 if(call_flag) CallOut_Numm=8;lcd_char_write(m+2,0,8);m+; else if(!shift_flag) send( );send(Z);/Call transferlcd_char_write(3,1,8); lcd_
20、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;case 0x22:/9 if(call_flag) CallOut_Numm=9;lcd_char_write(m+2,0,9);m+; elseif(!shift_flag) send( );send(Q); /Disconnectlcd_char_write(3,1,9); lcd_char_write(8,1,Q)
21、;lcd_char_write(9,1, ); else send( );send(z); /Call transfer lcd_char_write(3,1,9);lcd_char_write(8,1,z);lcd_char_write(9,1, ); break;case 0x42:/A * if(call_flag) CallOut_Numm=*;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,
22、);break;case 0x82:/B # if(call_flag) CallOut_Numm=#;lcd_char_write(m+2,0,#);m+;else send( );send(T);/Set auto answer lcd_char_write(3,1,B); lcd_char_write(8,1,T);lcd_char_write(9,1, );break;case 0x11: if(!call_flag)send( );send(t); /Clr auto answerlcd_char_write(3,1,C); lcd_char_write(8,1,t);lcd_cha
23、r_write(9,1, );break;case 0x21:if(!call_flag) send( );send(M);/Set volume setlcd_char_write(3,1,D); lcd_char_write(8,1,M);lcd_char_write(9,1, );break;case 0x41:if(!call_flag) send( );send(m); /Clr volume setlcd_char_write(3,1,E);lcd_char_write(8,1,m);lcd_char_write(9,1, );break;case 0x81:if(!call_fl
24、ag) send( );send(N); /Set auto linklcd_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(5);if(P10=0)|(P11=0)|(P12=0)|(P13=0)delay_1ms(10); /*延时去抖动*/if(P10=0)|(P11=0)|(P12=0)|(P13=0)if(P10=0)
25、lcd_char_write(3,1,a); send( );send(S);lcd_char_write(8,1,S); /Linkelse if(P11=0) call_flag=call_flag; if(call_flag)string_write(0,0,clr);lcd_char_write(0,0,0); /显示拨号状态符号 else for(i=0;i15) z=0; TR0=1;reci_flag1=1; /*void serial () interrupt 4 using 3 /串口接收字符串 if (RI) RI = 0 ; temp=SBUF;if(temp=R) Ca
26、llIn_flag=1; reci_flag=1; bool RIwait(uint i) /等待时间到,返回1 while(i-)if(RI) return 0; /等待时间内 RI = 1 ,返回0 return 1; /串行接收停止位的中间时,RI 置1uchar ReadSbuf(void)/从SBUF 读数据,可得到接收的数据 uchar TmpSbuf; TmpSbuf=SBUF; SCON=0x50; return TmpSbuf;void ComService(void) interrupt 4 uchar TmpSBUF,i=0; EA=0; ES=0; if(RIwait(
27、RiWaitTimer) goto ExitCom; switch(ReadSbuf() case R:/current call for(i=0;i555) for(time=18;call_flag=1&time0&CallOut_Num0=#;time-) if(P11!=0) delay_1ms(222); delay_1ms(222); string_write(2,0,CallOut_Num); delay_1ms(222); delay_1ms(222); delay_1ms(222); delay_1ms(222); string_write(2,0, ); delay_1ms(222); delay_1ms(222); else call_flag=0;string_write(0,0,clr);string_write(3,0,Welcome!); call_flag=0; n=0; string_write(0,0,clr);/ goto start string_write(3,0,Welcome!); */