您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 信息化管理 > 嵌入式基于UART的加法器的实现
实验四基于UART的加法器的实现一、实验目的:学习UART。二、实验原理:采用超级终端作为外部输入与输出的接口,实现多位数的相加。即通过UART串口分别输入需要相加的多位数A与B,最后把A和B两个多位数相加的过程和结果,回显给用户。具体实现方法:既可以采用轮询的方式也可以应用中断。三、实验内容:1、根据实验原理,在开发板上编写满足实验要求功能的程序。2、程序功能检验。实验报告必须包含以下内容:1、程序框图;2、程序代码,并在代码上附上一定的注释。//*****************************************************************************//uart_echo.c-UART文本响应实验//*****************************************************************************#includestring.h#includeinc/hw_ints.h#includeinc/hw_memmap.h#includeinc/hw_types.h#includedriverlib/debug.h#includedriverlib/gpio.h#includedriverlib/interrupt.h#includedriverlib/sysctl.h#includedriverlib/uart.hUARTSendASCTOCharCharToAsc开启使能,UART配置超级终端显示读取第二个数字,存放numSecond读取第一个数字,存放到numFirst进行加法运算,存放在numValue中倒序输出信号输入设置numFirst[],numSecond[]以及输出,numValue[],trueValue[]#includegrlib/grlib.h#includedrivers/kitronix320x240x16_ssd2119_8bit.h#includedrivers/set_pinout.h#defineCHAR_LENGTH5//*****************************************************************************//!\addtogroupexample_list//!h1UART(uart_echo)/h1//!ThisexampleapplicationutilizestheUARTtoechotext.ThefirstUART//!(connectedtotheFTDIvirtualserialportontheevaluationboard)willbe//!configuredin115,200baud,8-n-1mode.Allcharactersreceivedonthe//!UARTaretransmittedbacktotheUART.////*****************************************************************************//*****************************************************************************////GraphicscontextusedtoshowtextontheCSTNdisplay.////Theerrorroutinethatiscalledifthedriverlibraryencountersanerror.////*****************************************************************************//#ifdefDEBUG//void//__error__(char*pcFilename,unsignedlongulLine)//{//}//#endifvoidUARTSend(constunsignedchar*pucBuffer,unsignedintulCount){unsignedinta=ulCount-1;while(ulCount--){if((ulCount==a)&&(*pucBuffer=='0'))//去掉高位为0的情况{pucBuffer++;continue;}UARTCharPut(UART0_BASE,*pucBuffer++);}}voidASCTOChar(unsignedchar*p,intlength)//ASC码转换为字符串{if(length=0)return;while(length){switch(*p){case'0':{*p=0;break;}case'1':{*p=1;break;}case'2':{*p=2;break;}case'3':{*p=3;break;}case'4':{*p=4;break;}case'5':{*p=5;break;}case'6':{*p=6;break;}case'7':{*p=7;break;}case'8':{*p=8;break;}case'9':{*p=9;break;}default:break;}*p++;length--;}}voidCharToAsc(unsignedchar*p,intlength)//字符串转换为ASC码{if(length=0)return;while(length){switch(*p){case0:{*p='0';break;}case1:{*p='1';break;}case2:{*p='2';break;}case3:{*p='3';break;}case4:{*p='4';break;}case5:{*p='5';break;}case6:{*p='6';break;}case7:{*p='7';break;}case8:{*p='8';break;}case9:{*p='9';break;}default:break;}*p++;length--;}}//*****************************************************************************////ThisexampledemonstrateshowtosendastringofdatatotheUART.////*****************************************************************************main(void){unsignedcharch,numFirst[CHAR_LENGTH]={0},numSecond[CHAR_LENGTH]={0},numValue[CHAR_LENGTH]={0},trueValue[CHAR_LENGTH]={0};//定义存放第一和第二,第三个数的数组以及输出时的第四个的数组intipos1=0,ipos2=0,ipos3=0,ipos4=0;SysCtlClockSet(SYSCTL_SYSDIV_1|SYSCTL_USE_OSC|SYSCTL_OSC_MAIN|SYSCTL_XTAL_16MHZ);//系统外围使能PinoutSet();SysCtlPeripheralEnable(SYSCTL_PERIPH_UART0);//SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA);GPIOPinTypeUART(GPIO_PORTA_BASE,GPIO_PIN_0|GPIO_PIN_1);UARTConfigSetExpClk(UART0_BASE,SysCtlClockGet(),9600,(UART_CONFIG_WLEN_8|UART_CONFIG_STOP_ONE|UART_CONFIG_PAR_NONE));//UART配置:端口的基址,系统时钟频率9600//UARTEnable(UART0_BASE);进行两个数字的输入和其求和的输出。while(1){ipos1=0;//记录输入的第一个数字的长度ipos2=0;//记录输入的第二个数字的长度UARTCharPut(UART0_BASE,'\r');UARTCharPut(UART0_BASE,'\n');//通过UART发送字符串UARTCharPut(UART0_BASE,'\n');UARTSend((unsignedchar*)EnterA:,strlen(EnterA:));while((ch=UARTCharGet(UART0_BASE))!='\r')//读取第一个操作数,并将其存放在numFirst中{numFirst[ipos1]=ch;ipos1++;UARTCharPut(UART0_BASE,ch);//发送ch到UART0_BASE端口}UARTCharPut(UART0_BASE,'\r');UARTCharPut(UART0_BASE,'\n');UARTCharPut(UART0_BASE,'\n');UARTSend((unsignedchar*)EnterB:,strlen(EnterB:));while((ch=UARTCharGet(UART0_BASE))!='\r')//读取第二个操作数,并将其存放在numSecond中{numSecond[ipos2]=ch;ipos2++;UARTCharPut(UART0_BASE,ch);}ASCTOChar(numFirst,ipos1);//将ASC码转换为字符串ASCTOChar(numSecond,ipos2);//将字符串转换为ASC码ipos1--;ipos2--;ipos3=0;//输出两个数字之和while((ipos2=0)&&(ipos1=0)){numValue[ipos3]=numValue[ipos3]+numFirst[ipos1]+numSecond[ipos2];//地位进行加法:第一个操作数与第二个操作数以及其上一次的进位位进行加和。if(numValue[ipos3]9){numValue[ipos3]=numValue[ipos3]-10;//进行进位位的判断numValue[++ipos3]+=1;}elseipos3++;ipos2--;ipos1--;}while(ipos1=0)//第一个操作数还有高位没有执行操作{numValue[ipos3]=numValue[ipos3]+numFirst[ipos1];//将结果与第一个操作数剩余的高位项进行加和if(numValue[ipos3]9){numValue[ipos3]=numValue[ipos3]-10;//进行进位位的判读numValue[++ipos3]+=1;}elseipos3++;//指针移动ipos1--;ipos2--;}while(ipos2=0){numValue[ipos3]=numValue[ipos3]+numSecond[ipos2];if(numValue[ipos3]9){numValue[ipos3]=numValue[ipos3]-10;//进行进位判断numValue[++ipos3]+=1;}elseipos3++;//指针移动ipos1--;ipos2--;}ipos4=0;//进行数组的倒置,输出ipos3++;while(ipos30){ipos3--;trueValue[ipos4]=numValue[ipos3];ipos4++;}CharToAsc(trueValue,ipos4);//将字符串转换为ASC码输出UARTCharPut(UART0_BASE,'\n');UARTSend((unsignedchar*)Ent
本文标题:嵌入式基于UART的加法器的实现
链接地址:https://www.777doc.com/doc-2517741 .html