当前位置:文档之家› 51单片机与蓝牙串口通信程序

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

#include <reg51.h>#include <intrins.h>#include<stdio.h>#include "LCD1602.h"#include "matrix_key.h"#define uint unsigned int#define uchar unsigned char#define Nop() _nop_()sbit P10 = P1^0; /*定义独立对地按键端口*/sbit P11 = P1^1; /*定义独立对地按键端口*/sbit P12 = P1^2; /*定义独立对地按键端口*/sbit P13 = P1^3; /*定义独立对地按键端口*/ //shift键bit shift_flag;bit call_flag ;bit CallIn_flag=0;bit reci_flag;bit reci_flag1;sbit sled_en_port = P3^6; /*定义数码管数据锁存器控制端口*/ sbit led_en_port = P2^5; /*定义发光二极管数据锁存器控制端口*/ sbit ds1302_en_port = P2^2; /*定义时钟的选片脚*/uchar CallIn_Num[15];//={"00000000000"};uchar CallOut_Num[15]={" "};uchar m=0; //拨号指针uchar temp='?';uchar code clr[16]={" "};uchar code lcd_table[16] = {"Ky: Cm: Re: "};//uchar send_buff[15];uchar reci_buff[15]={" "};uchar z; //接收缓冲区指针uchar time;//定时器中断次数uchar code mun_to_char[]={"0123456789ABCDEF"};/*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(); /*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!");}// else if(time>222){time=0;goto start;}//else m=0;}}}void init() /* 串口定时器外部中断初始化*/{TMOD =0X21;//|= 0x20; //定时器1为模式2,8位自动重装TH1 = 0xfd; //Baud:19200 fosc="11".0592MHzTL1=0XFD ;TH0=0;TL0=0;TR1 = 1; // timer 1 runSCON = 0x50; //UART为模式1,8位数据,允许接收PCON |= 0x80 ; //SMOD=1; Baud加倍IE |= 0x90 ; //Enable Serial InterruptTR1 = 1 ; // timer 1 runEA=1;ET0=1;}void send(uchar cc){SBUF=cc;while(TI==0);TI=0;}void send_f(uchar ccc){send(' ');send('<');send('F');send(ccc);send('>');}void call_out(){uchar i;send('(');for(i=0;i<m;i++){send(CallOut_Num[i]);}send(')');m=0;}void interrupt_pro(){string_write(0,1,reci_buff);lcd_char_write(14,1,mun_to_char[temp/10]); //for testlcd_char_write(15,1,mun_to_char[temp%10]); //for testif(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_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,"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(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;i<16;i++) lcd_char_write(i,1,lcd_table[i]); /*显示标题*/default:break;}//temp='?'; //for test}void key_pro(){uchar i,key_value_buff;key_value_buff = key_scan();if(key_value_buff != 0) //有按键动作{switch(key_value_buff) /*显示按键*/{case 0x18: //0if(call_flag) {CallOut_Num[m]='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_write(9,1,'P');}break;case 0x28://1if(call_flag) {CallOut_Num[m]='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 0x48://2if(call_flag) {CallOut_Num[m]='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://3if(call_flag) {CallOut_Num[m]='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://4if(call_flag) {CallOut_Num[m]='4';lcd_char_write(m+2,0,'4');m++;} else if(!shift_flag){ send(' ');send('U'); //V ol 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://5if(call_flag) {CallOut_Num[m]='5';lcd_char_write(m+2,0,'5');m++;} else if(!shift_flag){ send(' ');send('D');//V ol 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://6if(call_flag) {CallOut_Num[m]='6';lcd_char_write(m+2,0,'6');m++;} else if(!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;case 0x84://7if(call_flag) {CallOut_Num[m]='7';lcd_char_write(m+2,0,'7');m++;} else if(!shift_flag){ send(' ');send('V'); //V oice diallcd_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;case 0x12://8if(call_flag) {CallOut_Num[m]='8';lcd_char_write(m+2,0,'8');m++;} else if(!shift_flag){ send(' ');send('Z');//Call transferlcd_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;case 0x22://9if(call_flag) {CallOut_Num[m]='9';lcd_char_write(m+2,0,'9');m++;} else if(!shift_flag){ send(' ');send('Q'); //Disconnectlcd_char_write(3,1,'9');lcd_char_write(8,1,'Q');lcd_char_write(9,1,' ');}else{ send(' ');send('z'); //Call transferlcd_char_write(3,1,'9');lcd_char_write(8,1,'z');lcd_char_write(9,1,' ');}break;case 0x42://A *if(call_flag) {CallOut_Num[m]='*';lcd_char_write(m+2,0,'*');m++;} else{ send(' ');send('Y');//Versionlcd_char_write(3,1,'A');lcd_char_write(8,1,'Y');lcd_char_write(9,1,' ');}break;case 0x82://B #if(call_flag) {CallOut_Num[m]='#';lcd_char_write(m+2,0,'#');m++;}else{ send(' ');send('T'); //Set auto answerlcd_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_char_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_flag){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) {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;i<16;i++) CallOut_Num[i]=' ';string_write(0,0,clr);string_write(3,0,"Welcome!");}}else if(P12==0) {lcd_char_write(3,1,'c'); send(' ');send('P');lcd_char_write(8,1,'P');} //Pairelse if(P13==0){shift_flag=~shift_flag;if(shift_flag) lcd_char_write(15,0,'s');else lcd_char_write(15,0,' ');}while((P10==0)||(P11==0)||(P12==0)||(P13==0));/*等待按键放开*/}}delay_1ms(5);}void Timer0Interrupt() interrupt 1 //定时器0中断服务函数{TH0 =0;TL0 =0;TR0=0; if(reci_flag1==1) reci_flag=1;z=0;if(call_flag==1) time++;}//串口接收中断函数void serial () interrupt 4 using 3{if (RI){RI = 0 ;temp=SBUF; // if(temp!=10)reci_buff[z]=temp;z++; if(z>15) z=0;TR0=1;reci_flag1=1;}}/*void serial () interrupt 4 using 3 //串口接收字符串{if (RI){RI = 0 ;temp=SBUF;if(temp=='R'){CallIn_flag=1;}reci_flag=1;}}bool RIwait(uint i){ //等待时间到,返回1while(i--){if(RI) return 0;}; //等待时间内RI = 1 ,返回0 return 1; //串行接收停止位的中间时,RI 置1}uchar 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(RiWaitTimer)) goto ExitCom;switch(ReadSbuf()){case 'R'://current callfor(i=0;i<20;i++){if(RIwait(RLongiWaitTimer)) break;//goto ExitCom;BlueTooth.TelNum[i]=ReadSbuf();TelTmpNum[i]=BlueTooth.TelNum[i]; // new add 09.4.08if(BlueTooth.TelNum[i]=='\r')break;}BlueTooth.TelNum[i]='\n';TelTmpNum[i]='\n'; // new add 09.4.08if(StandbyStatus==IsStandby){BlueTooth._bIRKey=IR_BLUETOOTHPOWERON;}else{if(BlueTooth.WorkMode==BlueToothWorkInCalling)BlueTooth._bIRKey=IR_BLUETOOTHINTEL;else// BlueTooth._bIRKey=IR_BLUETOOTHIN;_bIRKey= IR_BTPHONECAllIN;// BlueTooth._bIRKey=IR_BTPHONECAllIN;}break;}ExitCom:SCON = 0x50; //模式1 ,REN = 1 ,允许接收数据ES=1;EA=1; //Enable UART}*//* if(call_flag==1&&key_value_buff==0)//拨号状态{n++;if(n>555){for(time=18;call_flag==1&&time>0&&CallOut_Num[0]>='#';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 startstring_write(3,0,"Welcome!");}}*/。

相关主题