




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)
文檔簡介
1、實 驗 報 告課程名稱 嵌入式系統(tǒng)設(shè)計 實驗儀器 清華同方辰源嵌入式系統(tǒng)實驗箱 實驗名稱 實驗四:串口傳輸實驗 系 別 計算機學院 專 業(yè) 計算機科學與技術(shù) 班級/學號 學生姓名 實驗日期 成 績 指導教師 實驗四:串口傳輸實驗1、 實驗問題回答(1) 本實驗引入了Send和Receive兩個變量表示上傳或下傳數(shù)據(jù),用狀態(tài)機怎么實現(xiàn)?答:switch(oledkey0)case F: state=1;/Receive = 1;/Send = 0;break;case E: state=2;/Send = 1;/Receive = 0;break;switch(state)case 0:brea
2、k;case 1: recive();break;case 2:send();break;(2) 了解串口通訊原理。答:串口按位(bit)發(fā)送和接收字節(jié),串口通信最重要的參數(shù)是波特率、數(shù)據(jù)位、停止位和奇偶校驗。對于兩個進行通信的端口,這些參數(shù)必須匹配。1、51系列單片機的串口有4個模式,可分別用作串并轉(zhuǎn)換、并串轉(zhuǎn)換、異步串行通信(2種模式)。異步串行通信中,有1+8+1和1+8+1+1兩種幀格式,多機通信是特殊的通信方式。2、基本原理是兩組移位寄存器。將并行通信轉(zhuǎn)換成串行通信模式(發(fā)送部分),或反之(接收部分)??扇p工運行。3、速度通過移位脈沖決定。具體一般通過定時器1的自動裝載模式產(chǎn)生的溢
3、出脈沖給出。4、電平上采用的是CMOS邏輯。5、以上是物理層和數(shù)據(jù)鏈路層的單片機串口模塊的約定,其他層需要軟件人員根據(jù)需要自行把握。另外,電平需要根據(jù)實際通信環(huán)境做變換,如232、485或紅外等。(3)了解在上位機(PC端)端,C程序中如何調(diào)用串口? 答:#include#includeHANDLEhCom;intmain(void)hCom=CreateFile(TEXT(COM3),/COM1口GENERIC_READ|GENERIC_WRITE,/允許讀和寫0,/獨占方式NULL,OPEN_EXISTING,/打開而不是創(chuàng)建0,/同步方式NULL);if(hCom=(HANDLE)-1)
4、printf(打開COM失敗!n);returnFALSE;elseprintf(COM打開成功!n);SetupComm(hCom,1024,1024);/輸入緩沖區(qū)和輸出緩沖區(qū)的大小都是1024COMMTIMEOUTSTimeOuts;/設(shè)定讀超時TimeOuts.ReadIntervalTimeout=1000;TimeOuts.ReadTotalTimeoutMultiplier=500;TimeOuts.ReadTotalTimeoutConstant=5000;/設(shè)定寫超時TimeOuts.WriteTotalTimeoutMultiplier=500;TimeOuts.Write
5、TotalTimeoutConstant=2000;SetCommTimeouts(hCom,&TimeOuts);/設(shè)置超時DCBdcb;GetCommState(hCom,&dcb);dcb.BaudRate=9600;/波特率為9600dcb.ByteSize=8;/每個字節(jié)有8位dcb.Parity=NOPARITY;/無奇偶校驗位dcb.StopBits=ONE5STOPBITS;/兩個停止位SetCommState(hCom,&dcb);DWORDwCount;/讀取的字節(jié)數(shù)BOOLbReadStat;while(1)PurgeComm(hCom,PURGE_TXCLEAR|PUR
6、GE_RXCLEAR);/清空緩沖區(qū)charstr9=0;printf(%sn,str);bReadStat=ReadFile(hCom,str,9,&wCount,NULL);if(!bReadStat)printf(讀串口失敗!);returnFALSE;elsestr8=0;printf(%sn,str);Sleep(100);(4) 了解在上位機(PC端)端,JAVA程序中如何調(diào)用串口?答:public static void process() try Enumeration portList = CommPortIdentifier.getPortIdentifiers(); wh
7、ile (portList.hasMoreElements() CommPortIdentifier portId = (CommPortIdentifier) portList.nextElement(); if (portId.getPortType() = CommPortIdentifier.PORT_SERIAL)/如果端口類型是串口則判斷名稱 if(portId.getName().equals(COM1)/如果是COM1端口則退出循環(huán) break; else portId=null; SerialPort serialPort = (SerialPort)portId.open(
8、Serial_Communication, 1000);/打開串口的超時時間為1000ms serialPort.setSerialPortParams(9600,SerialPort.DATABITS_8,SerialPort.STOPBITS_1,SerialPort.PARITY_NONE);/設(shè)置串口速率為9600,數(shù)據(jù)位8位,停止位1們,奇偶校驗無 InputStream in = serialPort.getInputStream();/得到輸入流 OutputStream out = serialPort.getOutputStream();/得到輸出流 /進行輸入輸出操作/操作
9、結(jié)束后 in.close(); out.close(); serialPort.close();/關(guān)閉串口 catch (PortInUseException e) e.printStackTrace(); catch (UnsupportedCommOperationException e) e.printStackTrace(); catch (IOException e) e.printStackTrace(); 2、 實驗?zāi)康暮托Ч麑嶒災(zāi)康模海?)學習、理解、掌握利用串口實現(xiàn)上位機和下位機之間的通訊(數(shù)據(jù)上傳和控制)。達到實驗要求,拓展二未實現(xiàn)。實驗效果:3、 實驗內(nèi)容和步驟實驗內(nèi)容:
10、(1) 默認在在OLED屏幕上分行顯示串口號、波特率、數(shù)據(jù)位、校驗位、停止位、“E”鍵發(fā)送模式、“F”鍵接收模式。思路:E鍵發(fā)送模式設(shè)置send=1 receive=0 F鍵接受模式設(shè)置 send=0 receive=1(2) 按下小鍵盤的“E”鍵,表示下位機(實驗箱)往上位機(PC)發(fā)送數(shù)據(jù),上位機開啟“超級終端”,下位機循環(huán)往上位機發(fā)送“0”“1”“2”。“9”字符,上位機在“超級終端”中可以看到。思路:定義一個全局變量字符數(shù)組,char rxchar2;GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_0,GPIO_PIN_0);SysCtlDelay();U
11、ARTSend(unsigned char *)&rxchar, 1);SysCtlDelay();GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_0,0);rxchar0值不斷由0x30-0x39之間進行變化。if(rxchar0 = 0x39) | (rxchar0 0x30)rxchar0 = 0x30;elserxchar0+;(3) 按下小鍵盤的“F”鍵,表示上位機(PC)往下位機(實驗箱)發(fā)送數(shù)據(jù),上位機開啟“超級終端”,輸入“A”、“B”、“C”、“D”等,下位機接受數(shù)據(jù),和小鍵盤輸入一樣進行相應(yīng)顯示。思路:if(UARTCharsAvail(UAR
12、T0_BASE)rxchar0 = UARTCharGet(UART0_BASE); / 等待接收字符rxchar1 = 0;GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_0,GPIO_PIN_0);RIT128x96x4StringDraw(Receive: , 12,80, 15); RIT128x96x4StringDraw(rxchar, 66,80,15);RIT128x96x4StringDraw( , 72,80,15); SysCtlDelay();GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_0,0);(4) 擴
13、展:按照狀態(tài)機模式修改程序。思路:根據(jù)按鍵的不同判斷狀態(tài),三種狀態(tài),空閑,接收,發(fā)送。根據(jù)狀態(tài)的不同調(diào)用相應(yīng)的方法。(5) 擴展:在另外一片CPU LM3S 2110上實現(xiàn)串口通信,接實驗箱第二個串口接口,因為這一片CPU不帶顯示器,可以只實現(xiàn)下位機(實驗箱)往上位機(PC)發(fā)送數(shù)據(jù),上位機開啟“超級終端”,下位機循環(huán)往上位機發(fā)送“0”“1”“2”?!?”字符,上位機在“超級終端”中可以看到。思路:理論上在創(chuàng)建項目時選擇cpu LM3S 2110,改變實驗箱上連線,連接UART2的串口即可實現(xiàn)。但實際操作中并未實現(xiàn)。實驗步驟:(1) 加載必要程序(2) 串口啟動配置(main)(3) 增加串口
14、相關(guān)函數(shù)(main.c中)(4) 修改主函數(shù),實現(xiàn)本次實驗要求的功能(5) 編譯、連接,生成可執(zhí)行程序文件(6) 反復(3)-(5)步驟,直到完成(7) 硬件連接,連接標志UART1的串口(8) 聯(lián)調(diào)4、 實驗總結(jié)與收獲實驗代碼#include #include #include #include #include #include #include #include #include #include rit128x96x4.h /#include utility.h #define MAX_SIZE 40 / 緩沖區(qū)最大限制長度#define UARTCharGetNB UARTCharG
15、etNonBlockingchar RxBuf1 + MAX_SIZE; / 接收緩沖區(qū)int BufP = 0; / 緩沖區(qū)位置變量tBoolean RxEndFlag = false; / 接收結(jié)束標志unsigned int Event=0,Event_key=0,Send=0,Receive=0;int type=0;int ki=0;char oledkey2;int state=0;unsigned char ucValue=0x00;long time=0;int Speed,count=0;unsigned char rxchar10;void timerset(void);v
16、oid UARTSend(const unsigned char *pucBuffer, unsigned long ulCount);void recive()if(UARTCharsAvail(UART0_BASE)rxchar0 = UARTCharGet(UART0_BASE); / 等待接收字符rxchar1 = 0;GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_0,GPIO_PIN_0);RIT128x96x4StringDraw(Receive: , 30,80, 15);RIT128x96x4StringDraw(rxchar, 66,80,15
17、);RIT128x96x4StringDraw( , 72,80,15);SysCtlDelay();GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_0,0);void send() RIT128x96x4StringDraw(sending1,0,60,15);GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_0,GPIO_PIN_0);SysCtlDelay();/RIT128x96x4StringDraw(sending2,20,1,15);UARTSend(unsigned char *)&rxchar, 1);SysCtlDe
18、lay();GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_0,0);if(rxchar0 = 0x39) | (rxchar0 = 0x39) | (rxchar0 1;ucValue = ucValue + ucKey;ucValue = ucValue4)4) + 48;elseoledkey0 = (ucValue4) - 10 + A; Event_key = 1;void timerset()SysCtlPeripheralEnable(SYSCTL_PERIPH_TIMER0);TimerConfigure(TIMER0_BASE, TIMER_CF
19、G_32_BIT_PER);TimerLoadSet(TIMER0_BASE, TIMER_A, SysCtlClockGet()/100); / 不除以100 則1 秒中斷一次IntEnable(INT_TIMER0A);TimerIntEnable(TIMER0_BASE, TIMER_TIMA_TIMEOUT);void Timer0IntHandler(void)unsigned long ulStatus;ulStatus = TimerIntStatus(TIMER0_BASE, true); / 獲取當前中斷狀態(tài)TimerIntClear(TIMER0_BASE, TIMER_T
20、IMA_TIMEOUT); / 清除全部中斷狀態(tài)if (ulStatus & TIMER_TIMA_TIMEOUT) / 如果是超時中斷/補充該部分代碼time+;/每次計時中斷,相應(yīng)計數(shù)值進行變化void UARTIntHandler(void)unsigned long uStatus;/ Get the interrrupt status.uStatus = UARTIntStatus(UART0_BASE, true);/ Clear the asserted interrupts.UARTIntClear(UART0_BASE, uStatus);/ Loop while there
21、 are characters in the receive FIFO.while(UARTCharsAvail(UART0_BASE)/ Read the next character from the UART and write it back to the UART.UARTCharPutNonBlocking(UART0_BASE, UARTCharGetNonBlocking(UART0_BASE); void UARTSend(const unsigned char *pucBuffer, unsigned long ulCount)/ Loop while there are
22、more characters to send./while(ulCount-)/ Write the next character to the UART./UARTCharPutNonBlocking(UART0_BASE, *pucBuffer+);拓展#include #include #include #include #include #include #include #include #include #include rit128x96x4.h /#include utility.h #define MAX_SIZE 40 / 緩沖區(qū)最大限制長度#define UARTCha
23、rGetNB UARTCharGetNonBlockingchar RxBuf1 + MAX_SIZE; / 接收緩沖區(qū)int BufP = 0; / 緩沖區(qū)位置變量tBoolean RxEndFlag = false; / 接收結(jié)束標志unsigned int Event=0,Event_key=0,Send=0,Receive=0;int type=0;int ki=0;char oledkey2;int state=0;unsigned char ucValue=0x00;long time=0;int Speed,count=0;unsigned char rxchar10;void
24、timerset(void);void UARTSend(const unsigned char *pucBuffer, unsigned long ulCount);void recive()if(UARTCharsAvail(UART0_BASE)rxchar0 = UARTCharGet(UART0_BASE); / 等待接收字符rxchar1 = 0;GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_0,GPIO_PIN_0);RIT128x96x4StringDraw(Receive: , 30,80, 15);RIT128x96x4StringDraw(
25、rxchar, 66,80,15);RIT128x96x4StringDraw( , 72,80,15);SysCtlDelay();GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_0,0);void send() RIT128x96x4StringDraw(sending1,0,60,15);GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_0,GPIO_PIN_0);SysCtlDelay();/RIT128x96x4StringDraw(sending2,20,1,15);UARTSend(unsigned char *)&rxc
26、har, 1);SysCtlDelay();GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_0,0);if(rxchar0 = 0x39) | (rxchar0 = 0x39) | (rxchar0 1;ucValue = ucValue + ucKey;ucValue = ucValue4)4) + 48;elseoledkey0 = (ucValue4) - 10 + A; Event_key = 1;void timerset()SysCtlPeripheralEnable(SYSCTL_PERIPH_TIMER0);TimerConfigure(TIMER0_BASE, TIMER_CFG_
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
- 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025-2030年中國除草劑市場運營狀況發(fā)展趨勢分析報告
- 2025-2030年中國鋯英砂行業(yè)供需現(xiàn)狀及投資發(fā)展規(guī)劃研究報告
- 2025-2030年中國連接器制造市場發(fā)展動態(tài)及前景趨勢預測報告
- 2025-2030年中國輪滑鞋行業(yè)發(fā)展現(xiàn)狀及前景趨勢分析報告
- 2025-2030年中國血漿增容劑行業(yè)運行動態(tài)與發(fā)展風險評估報告
- 2025-2030年中國葵花油市場運行態(tài)勢及發(fā)展盈利分析報告
- 2025-2030年中國藝術(shù)玻璃行業(yè)市場運行態(tài)勢及投資戰(zhàn)略研究報告
- 2025-2030年中國管道檢測行業(yè)供需現(xiàn)狀及投資發(fā)展規(guī)劃研究報告
- 2025-2030年中國空冷器市場運行現(xiàn)狀及發(fā)展策略分析報告
- 2025-2030年中國種衣劑市場運營狀況及發(fā)展趨勢研究報告
- 商業(yè)銀行信貸實務(wù):第一章 商業(yè)銀行信貸概述
- 灌注樁鋼筋籠自動計算套用表格
- 機械工業(yè)建設(shè)項目概算編制辦法及各項概算指標
- 蘇科版七年級數(shù)學下冊期末復習+10(專題-幾何圖形的證明)
- 人人都是產(chǎn)品經(jīng)理2 0:寫給泛產(chǎn)品經(jīng)理
- 西方經(jīng)濟學(第二版)完整整套教學課件
- 振動振動測試基礎(chǔ)知識培訓課件
- 《云南瀾滄鉛礦有限公司勐濱煤礦采礦權(quán)價款退還計算說明》
- sbl-ep16高低壓開關(guān)柜培訓中法文kyn6140.5安裝使用說明書
- GB/T 9113.1-2000平面、突面整體鋼制管法蘭
- GB/T 8947-1998復合塑料編織袋
評論
0/150
提交評論