嵌入式实验四:串口传输实验.docx_第1页
嵌入式实验四:串口传输实验.docx_第2页
嵌入式实验四:串口传输实验.docx_第3页
嵌入式实验四:串口传输实验.docx_第4页
嵌入式实验四:串口传输实验.docx_第5页
已阅读5页,还剩43页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

实 验 报 告课程名称 嵌入式系统设计 实验仪器 清华同方辰源嵌入式系统实验箱 实验名称 实验四:串口传输实验 系 别 计算机学院 专 业 计算机科学与技术 班级/学号 学生姓名 实验日期 成 绩 指导教师 实验四:串口传输实验1、 实验问题回答(1) 本实验引入了Send和Receive两个变量表示上传或下传数据,用状态机怎么实现?答:switch(oledkey0)case F: state=1;/Receive = 1;/Send = 0;break;case E: state=2;/Send = 1;/Receive = 0;break;switch(state)case 0:break;case 1: recive();break;case 2:send();break;(2) 了解串口通讯原理。答:串口按位(bit)发送和接收字节,串口通信最重要的参数是波特率、数据位、停止位和奇偶校验。对于两个进行通信的端口,这些参数必须匹配。1、51系列单片机的串口有4个模式,可分别用作串并转换、并串转换、异步串行通信(2种模式)。异步串行通信中,有1+8+1和1+8+1+1两种帧格式,多机通信是特殊的通信方式。2、基本原理是两组移位寄存器。将并行通信转换成串行通信模式(发送部分),或反之(接收部分)。可全双工运行。3、速度通过移位脉冲决定。具体一般通过定时器1的自动装载模式产生的溢出脉冲给出。4、电平上采用的是CMOS逻辑。5、以上是物理层和数据链路层的单片机串口模块的约定,其他层需要软件人员根据需要自行把握。另外,电平需要根据实际通信环境做变换,如232、485或红外等。(3)了解在上位机(PC端)端,C程序中如何调用串口? 答:#include#includeHANDLEhCom;intmain(void)hCom=CreateFile(TEXT(COM3),/COM1口GENERIC_READ|GENERIC_WRITE,/允许读和写0,/独占方式NULL,OPEN_EXISTING,/打开而不是创建0,/同步方式NULL);if(hCom=(HANDLE)-1)printf(打开COM失败!n);returnFALSE;elseprintf(COM打开成功!n);SetupComm(hCom,1024,1024);/输入缓冲区和输出缓冲区的大小都是1024COMMTIMEOUTSTimeOuts;/设定读超时TimeOuts.ReadIntervalTimeout=1000;TimeOuts.ReadTotalTimeoutMultiplier=500;TimeOuts.ReadTotalTimeoutConstant=5000;/设定写超时TimeOuts.WriteTotalTimeoutMultiplier=500;TimeOuts.WriteTotalTimeoutConstant=2000;SetCommTimeouts(hCom,&TimeOuts);/设置超时DCBdcb;GetCommState(hCom,&dcb);dcb.BaudRate=9600;/波特率为9600dcb.ByteSize=8;/每个字节有8位dcb.Parity=NOPARITY;/无奇偶校验位dcb.StopBits=ONE5STOPBITS;/两个停止位SetCommState(hCom,&dcb);DWORDwCount;/读取的字节数BOOLbReadStat;while(1)PurgeComm(hCom,PURGE_TXCLEAR|PURGE_RXCLEAR);/清空缓冲区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程序中如何调用串口?答:public static void process() try Enumeration portList = CommPortIdentifier.getPortIdentifiers(); while (portList.hasMoreElements() CommPortIdentifier portId = (CommPortIdentifier) portList.nextElement(); if (portId.getPortType() = CommPortIdentifier.PORT_SERIAL)/如果端口类型是串口则判断名称 if(portId.getName().equals(COM1)/如果是COM1端口则退出循环 break; else portId=null; SerialPort serialPort = (SerialPort)portId.open(Serial_Communication, 1000);/打开串口的超时时间为1000ms serialPort.setSerialPortParams(9600,SerialPort.DATABITS_8,SerialPort.STOPBITS_1,SerialPort.PARITY_NONE);/设置串口速率为9600,数据位8位,停止位1们,奇偶校验无 InputStream in = serialPort.getInputStream();/得到输入流 OutputStream out = serialPort.getOutputStream();/得到输出流 /进行输入输出操作/操作结束后 in.close(); out.close(); serialPort.close();/关闭串口 catch (PortInUseException e) e.printStackTrace(); catch (UnsupportedCommOperationException e) e.printStackTrace(); catch (IOException e) e.printStackTrace(); 2、 实验目的和效果实验目的:(1)学习、理解、掌握利用串口实现上位机和下位机之间的通讯(数据上传和控制)。达到实验要求,拓展二未实现。实验效果:3、 实验内容和步骤实验内容:(1) 默认在在OLED屏幕上分行显示串口号、波特率、数据位、校验位、停止位、“E”键发送模式、“F”键接收模式。思路:E键发送模式设置send=1 receive=0 F键接受模式设置 send=0 receive=1(2) 按下小键盘的“E”键,表示下位机(实验箱)往上位机(PC)发送数据,上位机开启“超级终端”,下位机循环往上位机发送“0”“1”“2”。“9”字符,上位机在“超级终端”中可以看到。思路:定义一个全局变量字符数组,char rxchar2;GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_0,GPIO_PIN_0);SysCtlDelay(2000000);UARTSend(unsigned char *)&rxchar, 1);SysCtlDelay(2000000);GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_0,0);rxchar0值不断由0x30-0x39之间进行变化。if(rxchar0 = 0x39) | (rxchar0 0x30)rxchar0 = 0x30;elserxchar0+;(3) 按下小键盘的“F”键,表示上位机(PC)往下位机(实验箱)发送数据,上位机开启“超级终端”,输入“A”、“B”、“C”、“D”等,下位机接受数据,和小键盘输入一样进行相应显示。思路:if(UARTCharsAvail(UART0_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(1000000);GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_0,0);(4) 扩展:按照状态机模式修改程序。思路:根据按键的不同判断状态,三种状态,空闲,接收,发送。根据状态的不同调用相应的方法。(5) 扩展:在另外一片CPU LM3S 2110上实现串口通信,接实验箱第二个串口接口,因为这一片CPU不带显示器,可以只实现下位机(实验箱)往上位机(PC)发送数据,上位机开启“超级终端”,下位机循环往上位机发送“0”“1”“2”。“9”字符,上位机在“超级终端”中可以看到。思路:理论上在创建项目时选择cpu LM3S 2110,改变实验箱上连线,连接UART2的串口即可实现。但实际操作中并未实现。实验步骤:(1) 加载必要程序(2) 串口启动配置(main)(3) 增加串口相关函数(main.c中)(4) 修改主函数,实现本次实验要求的功能(5) 编译、连接,生成可执行程序文件(6) 反复(3)-(5)步骤,直到完成(7) 硬件连接,连接标志UART1的串口(8) 联调4、 实验总结与收获实验代码#include #include #include #include #include #include #include #include #include #include rit128x96x4.h /#include utility.h #define MAX_SIZE 40 / 缓冲区最大限制长度#define UARTCharGetNB UARTCharGetNonBlockingchar RxBuf1 + MAX_SIZE; / 接收缓冲区int BufP = 0; / 缓冲区位置变量tBoolean RxEndFlag = false; / 接收结束标志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);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(rxchar, 66,80,15);RIT128x96x4StringDraw( , 72,80,15);SysCtlDelay(100000);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(200000);/RIT128x96x4StringDraw(sending2,20,1,15);UARTSend(unsigned char *)&rxchar, 1);SysCtlDelay(200000);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_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); / 获取当前中断状态TimerIntClear(TIMER0_BASE, TIMER_TIMA_TIMEOUT); / 清除全部中断状态if (ulStatus & TIMER_TIMA_TIMEOUT) / 如果是超时中断/补充该部分代码time+;/每次计时中断,相应计数值进行变化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 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 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 / 缓冲区最大限制长度#define UARTCharGetNB UARTCharGetNonBlockingchar RxBuf1 + MAX_SIZE; / 接收缓冲区int BufP = 0; / 缓冲区位置变量tBoolean RxEndFlag = false; / 接收结束标志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);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(rxchar, 66,80,15);RIT128x96x4StringDraw( , 72,80,15);SysCtlDelay(100000);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(200000);/RIT128x96x4StringDraw(sending2,20,1,15);UARTSend(unsigned char *)&rxchar, 1);SysCtlDelay(200000);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 +

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

最新文档

评论

0/150

提交评论