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

📄 uart.c

📁 周立功ARM9 2410试验平台的uCOS-II基础实验代码
💻 C
字号:
/****************************************Copyright (c)**************************************************
**                               Guangzhou ZHIYUAN electronics Co.,LTD.
**                                     
**                                 http://www.zyinside.com
**
**--------------File Info-------------------------------------------------------------------------------
** File Name: UART.c
** Last modified Date: 2005-12-31 
** Last Version: v1.0
** Description: S3C2410的串口软件包 (查询控制方式)
**
**------------------------------------------------------------------------------------------------------
** Created By: 黄绍斌
** Created date: 2005-12-31 
** Version: v1.0
** Descriptions:
**
**------------------------------------------------------------------------------------------------------
** Modified by:
** Modified date:
** Version:
** Description:
**
********************************************************************************************************/
#define  IN_UART
#include  "config.h"


// 串口选择 (为0时表示串口0,否则为串口1)
static uint8   g_uart_sel = 0;



/*********************************************************************************************************
** Function name: UART_Select
** Descriptions: 选择要操作的串口。(UART0--0,UART1--1)
**               选择串口后,必需调用一次UART_Init()进行初始化(只需要一次)。
** Input: no    要使用的串口
** Output: 返回上一次选用的串口
** Created by: 黄绍斌
** Created Date: 2005-12-31 
**-------------------------------------------------------------------------------------------------------
** Modified by:
** Modified Date: 
**------------------------------------------------------------------------------------------------------
********************************************************************************************************/
int  UART_Select(uint8  no)
{	
	int  ret;
	
	ret = g_uart_sel;
	g_uart_sel = no;
	return(ret);
}



/*********************************************************************************************************
** Function name: UART_Init
** Descriptions: 初始化串口。设置为8位数据位,1位停止位,无奇偶校验,波特率为UART_BPS
** Input: 无
** Output: 无
** Created by: 黄绍斌
** Created Date: 2005-12-31 
**-------------------------------------------------------------------------------------------------------
** Modified by:
** Modified Date: 
**------------------------------------------------------------------------------------------------------
********************************************************************************************************/
void  UART_Init(uint32 UART_BPS)
{  	
	int  i;

	if(g_uart_sel)			// 判断是否为串口1
	{	
		// IO口设置 (GPH5,GPH4)
		rGPHUP = rGPHUP | (0x03<<4);
		rGPHCON = (rGPHCON & (~0x00000F00)) | (0x00000A00);	
	
		// 串口模式设置
		rUFCON1 = 0x00;   	// 禁止FIFO功能
		rUMCON1 = 0x00;   	// AFC(流控制)禁能
		rULCON1 = 0x03; 	// 禁止IRDA,无奇偶校验,1位停止位,8位数据位
		rUCON1  = 0x245; 	// 使用PCLK来生成波特率,发送中断为电平触发模式,接收中断为边沿触发模式,
	                    	// 禁止接收超时中断,使能接收错误中断,正常工作模式,中断或查询方式(非DMA)	
		// 串口波特率设置
		rUBRDIV1=(int)(PCLK/16.0/UART_BPS + 0.5) -1; 
	}
	else
	{	
		// IO口设置 (GPH3,GPH2)
		rGPHUP = rGPHUP | (0x03<<2);
		rGPHCON = (rGPHCON & (~0x000000F0)) | (0x000000A0);	
	
		// 串口模式设置
		rUFCON0 = 0x00;   	// 禁止FIFO功能
		rUMCON0 = 0x00;   	// AFC(流控制)禁能
		rULCON0 = 0x03; 	// 禁止IRDA,无奇偶校验,1位停止位,8位数据位
		rUCON0  = 0x245; 	// 使用PCLK来生成波特率,发送中断为电平触发模式,接收中断为边沿触发模式,
	                    	// 禁止接收超时中断,使能接收错误中断,正常工作模式,中断或查询方式(非DMA)	
		// 串口波特率设置
		rUBRDIV0=(int)(PCLK/16.0/UART_BPS + 0.5) -1; 
	}
	
   	for(i=0;i<100;i++);
}



/*********************************************************************************************************
** Function name: UART_SendByte
** Descriptions: 向串口发送字节数据,并等待发送完毕。
** Input: data		要发送的数据
** Output: 无
** Created by: 黄绍斌
** Created Date: 2005-12-31 
**-------------------------------------------------------------------------------------------------------
** Modified by:
** Modified Date: 
**------------------------------------------------------------------------------------------------------
********************************************************************************************************/
void  UART_SendByte(uint8 data)
{  	
	int  i;
   
    if(g_uart_sel)
    {	
    	while(!(rUTRSTAT1 & 0x02));	// 等待发送器THR为空
   		for(i=0; i<10; i++);
   		rUTXH1 = data;				// 发送数据
    }
    else
   	{	
   		while(!(rUTRSTAT0 & 0x02));	// 等待发送器THR为空
   		for(i=0; i<10; i++);
   		rUTXH0 = data;				// 发送数据
   	}   	
}



/*********************************************************************************************************
** Function name: UART_SendStr
** Descriptions: 向串口发送一字符串。
**              对于'\n'字符,发送时会加入'\r'字符。
** Input: str		要发送的字符串的指针
** Output: 无
** Created by: 黄绍斌
** Created Date: 2005-12-31 
**-------------------------------------------------------------------------------------------------------
** Modified by:
** Modified Date: 
**------------------------------------------------------------------------------------------------------
********************************************************************************************************/
void  UART_SendStr(char const *str)
{ 	
	while(*str != '\0')
   	{ 	
   		if(*str == '\n') UART_SendByte('\r');   	  
     	  UART_SendByte(*str++);	    // 发送数据
   	}
}



/*********************************************************************************************************
** Function name: UART_GetKey
** Descriptions: 从UART口读取一字节按键数据。
**               会一直等待,直到接收到1字节数据。
** Input: 无
** Output: 返回值即是读出值
** Created by: 黄绍斌
** Created Date: 2005-12-31 
**-------------------------------------------------------------------------------------------------------
** Modified by:
** Modified Date: 
**------------------------------------------------------------------------------------------------------
********************************************************************************************************/
int  UART_GetKey(void)
{  	
	int	 i;

	if(g_uart_sel)
	{	
		while(!(rUTRSTAT1 & 0x1));
		for(i=0; i<10; i++);
		return(rURXH1);
	}
	else	
	{	
		while(!(rUTRSTAT0 & 0x1));
		for(i=0; i<10; i++);
		return(rURXH0);
	}
}

/*********************************************************************************************************
**                            End Of File
********************************************************************************************************/

⌨️ 快捷键说明

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