?? uart.c
字號:
/****************************************Copyright (c)**************************************************
** Guangzou ZLG-MCU Development Co.,LTD.
** graduate school
** http://www.zlgmcu.com
**
**--------------File Info-------------------------------------------------------------------------------
** File name: Uart.c
** Last modified Date: 2007-01-08
** Last Version: 1.0
** Descriptions: Uart庫函數(shù)文件
**
**------------------------------------------------------------------------------------------------------
** Created by: Litiantian
** Created date: 2007-01-08
** Version: 1.0
** Descriptions: 原始版本
**
**------------------------------------------------------------------------------------------------------
** Modified by:
** Modified date:
** Version:
** Descriptions:
**
********************************************************************************************************/
#include "config.h"
/* 如下所定義的宏用于各串口發(fā)送和結(jié)束的判斷 */
#define UARTSND_END (UnLSR & 0x40) // 查詢方式下,當UARTSNDEND值不為0時,發(fā)送結(jié)束
#define UARTRCV_END (UnLSR & 0x01) // 查詢方式下,當UARTRCVEND值不為0時,發(fā)送結(jié)束
/* 定義串口n的各寄存器*/
#define UnLCR (*(volatile uint8 *)((&U0LCR) + n * 0x4000))
#define UnDLM (*(volatile uint8 *)((&U0DLM) + n * 0x4000))
#define UnDLL (*(volatile uint8 *)((&U0DLL) + n * 0x4000))
#define UnFCR (*(volatile uint8 *)((&U0FCR) + n * 0x4000))
#define UnIER (*(volatile uint8 *)((&U0IER) + n * 0x4000))
#define UnTHR (*(volatile uint8 *)((&U0THR) + n * 0x4000))
#define UnLSR (*(volatile uint8 *)((&U0LSR) + n * 0x4000))
#define UnRBR (*(volatile uint8 *)((&U0RBR) + n * 0x4000))
/*********************************************************************************************************
** 函數(shù)名稱 :UARTnIO_Ini(uint8 n)
** 函數(shù)功能 :初始化串口n的發(fā)送和接收腳。
** 入口參數(shù) : n :0-UART0 1-UART1 2-UART2 3-UART3
** 出口參數(shù) :無
**********************************************************************************************************/
void UARTnIO_Ini (uint8 n)
{
switch(n)
{
case 0:
SCS &= ~0x01; // 端口P0選擇低速GPIO
PINSEL0 = (0x01 << 4) | (0x01 << 6); // 設(shè)置P0.2和P0.3連接到UART0
break;
case 1:
PINSEL4 = 0x02 | (0x02 << 2); // 選擇P2.0和P2.1管腳為UART1
break;
case 2:
PINSEL4 = (0x02 << 16) | (0x02 << 18); // 設(shè)置P2.8和P2.9連接到UART2
break;
case 3:
PINSEL9 = (0x03 << 24) | (0x03 << 26); // 設(shè)置P2.8和P2.9連接到UART3
break;
default:
while(1);
}
}
/*********************************************************************************************************
** 函數(shù)名稱:UARTn_IniDft(uint8 n)
** 函數(shù)功能:按默認值初始化串口n的引腳和通訊參數(shù)。設(shè)置為8位數(shù)據(jù)位,1位停止位,無奇偶校驗,波特率115200。
** 入口參數(shù):n :0-UART0 1-UART1 2-UART2 3-UART3
** 出口參數(shù):無
**********************************************************************************************************/
void UARTn_IniDft (uint8 n)
{
uint32 Fdiv = 0;
/* 初始化相關(guān)的IO */
UARTnIO_Ini(n);
/* 初始化串口通訊參數(shù) */
UnLCR = 0x83; // 設(shè)置為8位數(shù)據(jù)位,1位停止位,無奇偶校驗,DLAB=1,允許設(shè)置波特率
Fdiv = (Fpclk / 16) / 115200; // 設(shè)置波特率
UnDLM = Fdiv / 256;
UnDLL = Fdiv % 256;
UnLCR = 0x03; // 令DLAB位為0
UnFCR &= ~0x07; // 禁用FIFO
UnIER &= ~0x07; // 禁止UART產(chǎn)生中斷
}
/*********************************************************************************************************
** 函數(shù)名稱: uint8 UARTn_Init(uint8 n,uint32 baud,uint8 datab,uint8 stopb,uint8 parity,uint8 INT_En)
** 功能描述: 對UARTn的通訊參數(shù)進行初始化
** 入口參數(shù): n :0-UART0 1-UART1 2-UART2 3-UART3
** baud 串口通信波特率
** datab 數(shù)據(jù)位個數(shù),有效值為:5,6,7,8
** stopb 停止位個數(shù),有效值為:1,2
** parity 奇偶校驗位,0-無校驗,1-奇校驗,2-偶校驗,3-強制為1,4-強制為0
** INT_En 中斷控制字節(jié),按位操作
** bit0 1--RBR中斷使能,0--RBR中斷禁止
** bit1 1--THRE中斷使能,0--THRE中斷禁止
** bit2 1--RX線狀態(tài)中斷使能,0--RX線狀態(tài)中斷禁止
** 出口參數(shù):為0時表示函數(shù)出錯
**********************************************************************************************************/
uint8 UARTn_Init (uint8 n,uint32 baud,uint8 datab,uint8 stopb,uint8 parity,uint8 INT_En)
{
uint32 bak = 0;
/* 參數(shù)過濾 */
if ((baud ==0 ) || (baud > 115200)) return (0); // 波特率:1~115200,否則出錯
if ((datab <5) || (datab > 8)) return (0); // 數(shù)據(jù)位數(shù):1~8,否則出錯
if ((stopb == 0) || (stopb > 2)) return (0); // 停止位:1、2,否則出錯
if (parity > 4) return (0); // 奇偶校驗位有錯
/* 設(shè)置串口波特率 */
UnLCR = 0x80; // DLAB = 1
bak = (Fpclk >> 4) / baud;
UnDLM = bak >> 8;
UnDLL = bak & 0xFF;
/* 設(shè)置串口模式 */
bak = datab - 5; // 設(shè)置字長
if (stopb == 2) bak |= 0x04; // 判斷是否為2位停止位
/* 設(shè)置奇偶校驗 */
if (parity != 0)
{
parity = parity - 1;
bak |= 0x08;
}
bak |= parity << 4;
UnLCR = bak;
/* 配置中斷 */
UnIER = INT_En & 0x07; // 設(shè)置中斷使能寄存器
return (1);
}
/*********************************************************************************************************
** 函數(shù)名稱: uint8 Set_FIFO(uint8 n,uint8 data)
** 功能描述: 設(shè)置FIFO
** 入口參數(shù): n :0-UART0 1-UART1 2-UART2 3-UART3
** data:FIFO觸發(fā)字節(jié)設(shè)定,只能為1,4,8、14個字節(jié)
** 出口參數(shù): 0 :初始化失敗
** 1 :初始化成功
********************************************************************************************************/
uint8 Set_FIFO(uint8 n,uint8 data)
{
switch(data)
{
case 1:
UnFCR = 0x01; // 緩沖區(qū)為1個字節(jié)
return(1);
case 4:
UnFCR = 0x41; // 緩沖區(qū)為4個字節(jié)
return(1);
case 8:
UnFCR = 0x81; // 緩沖區(qū)為8個字節(jié)
return(1);
case 14:
UnFCR = 0xc1; // 緩沖區(qū)為14個字節(jié)
return(1);
default:
return(0);
}
}
/*********************************************************************************************************
** 函數(shù)名稱: void UARTn_SendByte(uint8 n,uint8 data)
** 功能描述: 從串口n發(fā)送數(shù)據(jù)
** 入口參數(shù): n :0-UART0 1-UART1 2-UART2 3-UART3
** data:發(fā)送的數(shù)據(jù)
** 出口參數(shù): 無
********************************************************************************************************/
void UARTn_SendByte(uint8 n,uint8 data)
{
UnTHR = data;
while ( UARTSND_END == 0 );
}
/*********************************************************************************************************
** 函數(shù)名稱: void UARTn_SendData(uint8 n,uint8 *data_buf,uint8 count)
** 功能描述: 從串口發(fā)送數(shù)據(jù)
** 入口參數(shù): n :0-UART0 1-UART1 2-UART2 3-UART3
** data_buf:發(fā)送數(shù)據(jù)緩沖區(qū)首地址
** count :發(fā)送字節(jié)數(shù)
** 出口參數(shù): 無
********************************************************************************************************/
void UARTn_SendData(uint8 n,uint8 *data_buf,uint8 count)
{
uint8 i;
for(i=0;i<count;i++) UARTn_SendByte(n,data_buf[i]); //發(fā)送數(shù)據(jù)
}
/*********************************************************************************************************
** 函數(shù)名稱 :UARTn_SendStr()
** 函數(shù)功能 :通過串口n發(fā)送一個字符串
** 入口參數(shù) : n :0-UART0 1-UART1 2-UART2 3-UART3
** str 要發(fā)送的字符串的指針
** 出口參數(shù): 無
*********************************************************************************************************/
void UARTn_SendStr (uint8 n,uint8 const *str)
{
while (1)
{
if (*str == '\0') break; // 遇到結(jié)束符則退出
UARTn_SendByte(n,*str++); // 發(fā)送數(shù)據(jù)
}
}
/*********************************************************************************************************
** 函數(shù)名稱 :UARTn_RcvByte()
** 函數(shù)功能 :從串口n接收1字節(jié)數(shù)據(jù),使用查詢方式接收。
** 入口參數(shù) : n :0-UART0 1-UART1 2-UART2 3-UART3
** 出口參數(shù) :接收到的字節(jié)數(shù)據(jù)
*********************************************************************************************************/
uint8 UARTn_RcvByte (uint8 n)
{
uint8 rcv_dat;
while (UARTRCV_END == 0);
rcv_dat = UnRBR;
return (rcv_dat);
}
/*********************************************************************************************************
** 函數(shù)名稱: void UARTn_RcvData()
** 功能描述: 從串口n接收count個數(shù)據(jù)到緩存data_buf中
** 入口參數(shù) : n :0-UART0 1-UART1 2-UART2 3-UART3
** data_buf:接收數(shù)據(jù)緩沖區(qū)首地址
** count :接收字節(jié)數(shù)
** 出口參數(shù) : 無
********************************************************************************************************/
void UARTn_RcvData(uint8 n,uint8 *data_buf,uint8 count)
{
uint8 i;
for(i = 0;i < count;i++)
{
data_buf[i] = UARTn_RcvByte(n); // 保存接收到的數(shù)據(jù)
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -