⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 lpc2210uart.c

📁 Read MP3 from sd,send vs1003 decode
💻 C
字号:


#include "LPC2210Uart.h"
#include "config.h"
#include "common.h"

#include <stdarg.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>


static int whichUart = 0;

void Uart_Select(int ch)
{
    whichUart=ch;
}


/*
***************************************************************************
* 名    称:UART0_Ini()
* 功    能:初始化串口0。设置为8位数据位,1位停止位,无奇偶校验,波特率为115200
* 入口参数:无
* 出口参数:无
***************************************************************************
*/
void UART0_Init(void)
{
	uint16 Fdiv;

    PINSEL0 |= 0x00000005;		    // 设置I/O连接到UART0

	U0LCR = 0x83;						// DLAB = 1,可设置波特率
    Fdiv = (Fpclk / 16) / UART_BPS;		// 设置波特率
    U0DLM = Fdiv / 256;							
    U0DLL = Fdiv % 256;						
    U0LCR = 0x03;
    
//    return 0;
}

void Uart_TxEmpty(int ch)
{
    if(ch==0)
		while(!(U0LSR& 0x40)); //wait until tx shifter is empty.
    else
    	while(!(U1LSR & 0x40)); //wait until tx shifter is empty.
}


char Uart_Getch(void)
{
    if(whichUart==0)
    {	    
		while(!(U0LSR & 0x1)); //Receive data ready
		return RdU0RBR();
    }
    else
    {
		while(!(U1LSR & 0x1)); //Receive data ready
		return	RdU1RBR();
    }
		
}


char Uart_GetKey(void)
{
    if(whichUart==0)
    {	    
		if(U0LSR & 0x1)    //Receive data ready
    	    return RdU0RBR();
		else
		    return 0;
    }
    else
    {
		if(U1LSR & 0x1)    //Receive data ready
		    return RdU1RBR();
		else
		    return 0;
    }
}


void Uart_GetString(char *string)
{
    char *string2=string;
    char c;
    while((c=Uart_Getch())!='\r')
    {
		if(c=='\b')
		{
		    if(	(int)string2 < (int)string )
		    {
				Uart_Printf("\b \b");
				string--;
		    }
		}
		else 
		{
		    *string++=c;
		    Uart_SendByte(c);
		}
    }
    *string='\0';
    Uart_SendByte('\n');
}


int Uart_GetIntNum(void)
{
    char str[30];
    char *string=str;
    int base=10;
    int minus=0;
    int lastIndex;
    int result=0;
    int i;
    
    Uart_GetString(string);
    
    if(string[0]=='-')
    {
        minus=1;
        string++;
    }
    
    if(string[0]=='0' && (string[1]=='x' || string[1]=='X'))
    {
		base=16;
		string+=2;
    }
    
    lastIndex=strlen(string)-1;
    if( string[lastIndex]=='h' || string[lastIndex]=='H' )
    {
		base=16;
		string[lastIndex]=0;
		lastIndex--;
    }

    if(base==10)
    {
		result=atoi(string);
		result=minus ? (-1*result):result;
    }
    else
    {
		for(i=0;i<=lastIndex;i++)
		{
    	    		if(isalpha(string[i]))
			{
				if(isupper(string[i]))
					{
						result=(result<<4)+string[i]-'A'+10;
					}
				else
					{
				    		result=(result<<4)+string[i]-'a'+10;
					}
			}
			else
			{
				result=(result<<4)+string[i]-'0';
			}
		}
		result=minus ? (-1*result):result;
    }
    return result;
}

/*
*********************************************************************************************************
*                                              Uart_SendByte
*
* Description : Send the string byte by byte.
*
* Arguments  : none
*
* Returns    : none
*
* Notes      : 	UTRSTAT[2]:
*				This bit is automatically set to 1 when the transmit shift register
*				has no valid data to transmit and the transmit shift register is	empty.
*					0 = Not empty	1 = Transmit holding & shifter register empty
*			UTRSTAT[1]:
*				This bit is automatically set to 1 when the transmit buffer
*				register does not contain valid data.
*					0 =The buffer register is not empty	1 = Empty
*				If the UART uses the FIFO, users should check Tx FIFO Count
*				bits and Tx FIFO Full bit in the UFSTAT register instead of this bit.
*			UTRSTAT[0]:
*				This bit is automatically set to 1 whenever the receive buffer
*				register contains valid data, received over the RXDn port.
*					0 = Completely empty	1 = The buffer register has a received data
*				If the UART uses the FIFO, users should check Rx FIFO Count
*				bits in the UFSTAT register instead of this bit.
*
*
********************************************************************************************************
*/
void Uart_SendByte(int data)
{
	if(whichUart == 0)
    	{
		if(data == '\n')
		{
		    while(!(U0LSR & 0x20));
		    delay_Nus(10);			//because the slow response of hyper_terminal 
		    WrU0THR('\r');		//UTXH0 = '\r',UART channel 0 transmit holding register
		}
		while(!(U0LSR & 0x20)); //Wait until THR is empty.
		delay_Nus(10);
		WrU0THR(data);
	}
	else
	{
		if(data == '\n')
		{
			while(!(U1LSR & 0x20));
			delay_Nus(10);	//because the slow response of hyper_terminal 
			WrU1THR('\r');
		}
		while(!(U1LSR & 0x20));  //Wait until THR is empty.
		delay_Nus(10);
		WrU1THR(data);
	}	
}		

/*
*********************************************************************************************************
*                                              Uart_SendString
*
* Description : Send the string byte by byte.
*
* Arguments  : none
*
* Returns    : none
*
* Notes      : Calling Uart_SendByte().
*			
*
********************************************************************************************************
*/

void Uart_SendString(char *pt)
{
    while(*pt)
	Uart_SendByte(*pt++);
}

/*
*********************************************************************************************************
*                                              Uart_Printf
*
* Description : 
*
* Arguments  : none
*
* Returns    : none
*
* Notes      : if you don't use vsprintf(), the code size is reduced very much.
*			
*
********************************************************************************************************
*/

void Uart_Printf(char *fmt,...)
{
    va_list ap;
    char string[256];

    va_start(ap,fmt);
    vsprintf(string,fmt,ap);
    Uart_SendString(string);
    va_end(ap);
}



⌨️ 快捷键说明

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