51單片機(jī)與藍(lán)牙模塊的串口通信_第1頁
51單片機(jī)與藍(lán)牙模塊的串口通信_第2頁
51單片機(jī)與藍(lán)牙模塊的串口通信_第3頁
已閱讀5頁,還剩8頁未讀 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡介

1、.#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 = P10; sbit P11 = P11; sbit P12 = P12; sbit P13 = P13;/* 定義獨(dú)立對地按鍵端口*/* 定義獨(dú)立對地按鍵端口

2、*/* 定義獨(dú)立對地按鍵端口*/* 定義獨(dú)立對地按鍵端口*/shift 鍵bit shift_flag;bit call_flag ;bit CallIn_flag=0;bit reci_flag;bit reci_flag1;sbit sled_en_port = P36; /*定義數(shù)碼管數(shù)據(jù)鎖存器控制端口*/sbit led_en_port = P25; /*定義發(fā)光二極管數(shù)據(jù)鎖存器控制端口*/sbit ds1302_en_port = P22; /* 定義時鐘的選片腳 */uchar CallIn_Num15;/="00000000000"uchar CallOut_

3、Num15=""uchar m=0;/ 撥號指針uchar temp='?'uchar code clr16=""uchar code lcd_table16= "Ky: Cm: Re: "/uchar send_buff15;uchar reci_buff15=""uchar z; / 接收緩沖區(qū)指針uchar time;/定時器中斷次數(shù)uchar code mun_to_char="0123456789ABCDEF"/*1MS 為單位的延時程序*/void init();vo

4、id send(uchar cc);void send_f(uchar ccc);void interrupt_pro();void key_pro();void call_out();Word 文檔.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_tablei); /*顯示標(biāo)題 */string_write(0,0,clr);string_write(0,0,"Welcome!&q

5、uot;);sled_en_port = 0; /*關(guān)閉數(shù)碼管顯示*/led_en_port = 0; /*關(guān)閉發(fā)光二極管顯示*/ds1302_en_port = 0;/*關(guān)閉時鐘通訊 */while(1)key_pro(); / 掃描鍵盤,按鍵盤處理if(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_buffi>=0x30 &&re

6、ci_buffi<=0x39 if(reci_buffi>='0'&&reci_buffi<='9'|reci_buffi=' ')CallIn_Numj=reci_buffi;j+;reci_buffi=' 'string_write(0,1,clr);string_write(0,1,CallIn_Num);if(call_flag=1)/撥號狀態(tài)TR0=1;if(time>100&&CallOut_Num0>='0') call_out(); fo

7、r(i=16;i>1;i-) string_write(2,0,CallOut_Num);Word 文檔.delay_1ms(222);delay_1ms(222);string_write(2,0,"");delay_1ms(222);delay_1ms(222); for(i=0;i<15;i+) CallOut_Numi=' ' /清空time=0;TR0=0;call_flag=0;string_write(0,0,clr);string_write(3,0,"Welcome!");/ elseif(time>2

8、22)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 位數(shù)據(jù),允許接收PCON |= 0x80 ; /SMOD=1; Baud加倍IE |= 0x90 ;/Enable Serial InterruptTR1 = 1

9、 ;/ 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);Word 文檔.send('>');void call_out()uchar i;send('(');for(i=0;i<m;i+)send(CallOut_Numi);send(')');m=0;void interru

10、pt_pro()string_write(0,1,reci_buff);lcd_char_write(14,1,mun_to_chartemp/10); /for testlcd_char_write(15,1,mun_to_chartemp%10); /for testif(temp=')') CallIn_flag=1;/|temp='$' |temp='%'else if(temp='$') lcd_delay(5);/if(temp='$')string_write(0,0,clr);string_writ

11、e(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

12、':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

13、 '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_

14、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_ta

15、blei);/* 顯示標(biāo)題 */Word 文檔.default:break;/temp='?' /for testvoid 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_Numm='0'lcd_char_write(m+2,0,'0');m+;elseif(!shift_flag)s

16、end(' ');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)

17、 CallOut_Numm='1'lcd_char_write(m+2,0,'1');m+; else if(!shift_flag) send(' ');send('H'); /Hang up 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

18、,1,'F');lcd_char_write(9,1,'L');break;case 0x48:/2Word 文檔.if(call_flag) CallOut_Numm='2'lcd_char_write(m+2,0,'2');m+; else if(!shift_flag) send(' ');send('R'); /Reject lcd_char_write(3,1,'2'); lcd_char_write(8,1,'R'); lcd_char_write(9,1

19、,' ');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_Numm='3'lcd_char_write(m+2,0,'3');m+; else if(!shift_flag) send(' ');send('L'); /Rediallcd_ch

20、ar_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_Numm='4'lcd_char_write(m+2,0,'4')

21、;m+;elseif(!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');Word 文檔.break;cas

22、e 0x24:/5if(call_flag) CallOut_Numm='5'lcd_char_write(m+2,0,'5');m+; else if(!shift_flag) send(' ');send('D');/Vol down 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&

23、#39;);lcd_char_write(8,1,'F');lcd_char_write(9,1,'S');break;case 0x44:/6if(call_flag) CallOut_Numm='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_w

24、rite(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_Numm='7'lcd_char_write(m+2,0,'7');m+; else if(!shift_flag) send(

25、' ');send('V'); /Voice dial 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,' ');Word 文檔.break;case 0x1

26、2:/8if(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_char_write(8,1,'Z');lcd_char_write(9,1,' ');else send(' ');send('p');lcd_char_writ

27、e(3,1,'8');lcd_char_write(8,1,'p');lcd_char_write(9,1,' ');break;case 0x22:/9if(call_flag) CallOut_Numm='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,'

28、Q');lcd_char_write(9,1,' ');elsesend(' ');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_Numm='*'lcd_char_write(m+2,0,'*');m+;elsesend('

29、 ');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_Numm='#'lcd_char_write(m+2,0,'#');m+;Word 文檔.elsesend(' ');send('T');/Set auto answerlcd_char_wr

30、ite(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(

31、' ');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_wri

32、te(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(P

33、10=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)Word 文檔.call_flag=call_flag;if(call_flag)string_write(0,0,clr);lcd_char_write(0,0,0);

34、/ 顯示撥號狀態(tài)符號elsefor(i=0;i<16;i+)CallOut_Numi=''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_w

35、rite(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 中斷服務(wù)函數(shù)TH0 =0;TL0 =0;TR0=0;if(reci_flag1=1) reci_flag=1;z=0;if(call_flag=1) time+;/ 串口接收中斷函數(shù)void serial () interrupt 4 using 3if (RI)RI=0;

36、temp=SBUF;/if(temp!=10)reci_buffz=temp;z+;if(z>15) z=0;TR0=1;reci_flag1=1;Word 文檔./*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;/ 等待時間內(nèi)RI = 1,返回 0return 1;/ 串行接收停止位的中間時,RI置 1uchar ReadSbuf(void)/ 從 SBUF 讀數(shù)據(jù),可得到接收的數(shù)據(jù) uchar TmpSbuf; TmpSbuf=SBUF;SCON=0x50; return TmpSbuf;void ComService(void) in

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論