main.c

来自「基于TI公司Cortex-M3的uart超级通信开发」· C语言 代码 · 共 319 行

C
319
字号
//*****************************************************************************
//
// uart_echo.c - UART文本响应实验
//
//*****************************************************************************
#include <string.h>
#include "inc/hw_ints.h"
#include "inc/hw_memmap.h"
#include "inc/hw_types.h"
#include "driverlib/debug.h"
#include "driverlib/gpio.h"
#include "driverlib/interrupt.h"
#include "driverlib/sysctl.h"
#include "driverlib/uart.h"
#include "grlib/grlib.h"
#include "drivers/kitronix320x240x16_ssd2119_8bit.h"
#include "drivers/set_pinout.h"


#define CHAR_LENGTH 5

//*****************************************************************************
//
//! \addtogroup example_list
//! <h1>UART (uart_echo)</h1>
//!
//! This example application utilizes the UART to echo text.  The first UART
//! (connected to the FTDI virtual serial port on the evaluation board) will be
//! configured in 115,200 baud, 8-n-1 mode.  All characters received on the
//! UART are transmitted back to the UART.
//
//*****************************************************************************

//*****************************************************************************
//
// Graphics context used to show text on the CSTN display.
//
//*****************************************************************************

//*****************************************************************************
//
// The error routine that is called if the driver library encounters an error.
//
//*****************************************************************************
// #ifdef DEBUG
// void
// __error__(char *pcFilename, unsigned long ulLine)
// {
// }
// #endif


//*****************************************************************************
//
// Send a string to the UART.
//
//*****************************************************************************
void
UARTSend(const unsigned char *pucBuffer, unsigned int ulCount)
{
	unsigned int a = ulCount - 1;
    while(ulCount--)
    {
			if((ulCount == a) && (*pucBuffer == '0')) //去掉高位为0的情况
			{
				pucBuffer ++;
				continue;
			}
      UARTCharPut(UART0_BASE,*pucBuffer++);
    }
}

void ASCTOChar(unsigned char *p, int length)     //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 --;
	}
}

void CharToAsc(unsigned char *p, int length)   //字符串转换为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 --;
	}
}
//*****************************************************************************
//
// This example demonstrates how to send a string of data to the UART.
//
//*****************************************************************************
main(void)
{
	  unsigned char ch, numFirst[CHAR_LENGTH] ={0}, numSecond[CHAR_LENGTH]={0}, numValue[CHAR_LENGTH]={0}, trueValue[CHAR_LENGTH] = {0};//定义存放第一和第二,第三个数的数组以及输出时的第四个的数组
    int ipos1 = 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((unsigned char *)"Enter A: ", strlen("Enter A: "));
		 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((unsigned char *)"Enter B: ", strlen("Enter B: "));
		 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;
			 }
			 else
				 ipos3 ++;     
    ipos2 --;
			   ipos1 --;
		 }
		 while(ipos1 >= 0)     // 第一个操作数还有高位没有执行操作
		 {
			 
			 numValue[ipos3] = numValue[ipos3] + numFirst[ipos1];  //将结果与第一个操作数剩余的高位项进行加和
			if(numValue[ipos3] > 9)
			 {
				 numValue[ipos3] = numValue[ipos3] - 10; //进行进位位的判读
				 numValue[ ++ipos3] += 1;
			 }
			 else
				 ipos3 ++; //指针移动
			 ipos1 --;
			 ipos2 --;
		}
		while(ipos2 >= 0) 
		 {
			  numValue[ipos3] = numValue[ipos3] + numSecond[ipos2];
			 
			if(numValue[ipos3] > 9)
			 {
				 numValue[ipos3] = numValue[ipos3] - 10;  //进行进位判断
				 numValue[ ++ipos3] += 1;
			 }
			 else
				 ipos3 ++;     //指针移动
			 ipos1 --;
			 ipos2 --;
		}
		
		ipos4= 0; //进行数组的倒置,输出
		ipos3 ++;
		while(ipos3 > 0)
		{
			ipos3 --;
			trueValue[ipos4] = numValue[ipos3];
			ipos4 ++;
		}
		
		CharToAsc(trueValue, ipos4);  //将字符串转换为ASC码输出

		 UARTCharPut(UART0_BASE,'\n');   
		 UARTSend((unsigned char *)"Enter A+B :", strlen("Enter A+B: "));  //在超级终端显示结果
		 UARTSend(trueValue, ipos4);
		for(ipos3 = 0; ipos3 < CHAR_LENGTH; ipos3 ++)
		{
			numValue[ipos3] = 0;
			trueValue[ipos3] = 0;
			numSecond[ipos3] = 0;
			numFirst[ipos3] = 0;
		}
	 }
 }

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?