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

📄 uart.c

📁 ARM7 lpc2100 UART驱动API,源码有详细注解
💻 C
字号:
/****************************************************************************
* 文 件 名:UART.c
* 功    能:使用ADC模块的通道0、1进行电压的测量,然后将转换结果从串口输出,
			如果同时用到两个串口,先运行UARTxInit(1,uint32 bps),
			再运行UARTxInit(0,uint32 bps) ,否则,UART0设置无效;

****************************************************************************/
#include  "CFG.h"
char    str[20];

void UARTxInit(uint8 Int_UARTx,uint32 bps);
void  UARTxSendByte(uint8 Int_UARTx,uint8 data);
void  UARTxSendStr(uint8 Int_UARTx,char *str);
char UARTx_RecvByte(uint8 Int_UARTx);

/****************************************************************************
* 名    称:void UARTxInit(uint8 Int_UARTx,uint32 bps)
* 功    能:初始化串口。设置为8位数据位,1位停止位,无奇偶校验,如果同时用到
			两个串口,先运行UARTxInit(1,uint32 bps),再UARTxInit(0,uint32 bps)
			否则,UART0设置无效
* 入口参数:Int_UARTx 串口号UART0、UART1,bps 通讯波特率 
* 出口参数:无
****************************************************************************/
void UARTxInit(uint8 Int_UARTx,uint32 bps)
{   uint16 Fdiv;			
    PINSEL0 = ((PINSEL0 & (~0x0F)) | 0x05)<<(Int_UARTx*16);	// 不影响其它管脚连接,设置I/O连接到UART0、UART1
    Fdiv = (Fpclk / 16) / bps;				// 设置波特率
    if (!Int_UARTx)
    {	U0LCR = 0x83;							// DLAB = 1,可设置波特率    	   
    	U0DLM = Fdiv / 256;							
   	//U0DLL = Fdiv % 256;
   		U0DLL = Fdiv % 256+0x12;		 //在KEIL 中,需要调整数据,mdk3.15中,串口的波特率计算会出错
   		U0LCR = 0x03;
   	}
   	if (Int_UARTx==1)
   	{	U1LCR = 0x83;							// DLAB = 1,可设置波特率  
    	U1DLM = Fdiv / 256;							
   	//U0DLL = Fdiv % 256;
   		U1DLL = Fdiv % 256+0x12;
   		U1LCR = 0x03;
   	} 
}

/****************************************************************************
* 名    称:UARTxSendByte(uint8 Int_UARTx,uint8 data)
* 功    能:Int_UARTx 串口号UART0、UART1,向串口发送字节数据,并等待发送完毕。
* 入口参数:data  要发送的数据
* 出口参数:无
****************************************************************************/
void  UARTxSendByte(uint8 Int_UARTx,uint8 data)
{	if (Int_UARTx==0)
	{	U0THR = data;							// 发送数据
    while( (U0LSR&0x20)==0 );				// 等待数据发送完毕
  }
  if (Int_UARTx==1)
	{	U1THR = data;							// 发送数据
    while( (U1LSR&0x20)==0 );				// 等待数据发送完毕
  }
}
/****************************************************************************
* 名    称:UARTxSendStr(uint8 Int_UARTx,char *str)
* 功    能:Int_UARTx 串口号UART0、UART1,向串口发送一字符串
* 入口参数:srt		要发送的字符串的指针
* 出口参数:无
****************************************************************************/

void  UARTxSendStr(uint8 Int_UARTx,char *str)
{	while(1)
  {	if( *str == '\0' ) break;
    UARTxSendByte(Int_UARTx,*str++);				// 发送数据
  }  
}
/****************************************************************************
* 名    称:char UARTx_RecvByte(Int_UARTx)
* 功    能:向串口接收一字符串
* 入口参数:Int_UARTx 串口号UART0、UART1
* 出口参数:Int_ReciveData,返回串口字符
****************************************************************************/
char UARTx_RecvByte(uint8 Int_UARTx)
{  	uint8 Int_ReciveData;
	if (Int_UARTx==0)
		{	while(!(U0LSR&0x01));
			Int_ReciveData=U0RBR;
		}
	if (Int_UARTx==1)
		{	while(!(U1LSR&0x01));
			Int_ReciveData=U1RBR;
		}
   	return	Int_ReciveData ;
}

⌨️ 快捷键说明

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