?? uart.c
字號:
/*******************************************************************************************************
**uart.c
********************************************************************************************************/
#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: 返回上一次選用的串口
********************************************************************************************************/
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: 無
********************************************************************************************************/
void UART_Init(void)
{
int i;
if(g_uart_sel) // 判斷是否為串口1
{
// I/O口設置 (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
{
// I/O口設置 (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: 無
********************************************************************************************************/
void UART_SendByte(uint8 data)
{
int i;
if(g_uart_sel)
{
while(!(rUTRSTAT1 & 0x02)); // 等待發送器為空
for(i=0; i<10; i++);
rUTXH1 = data; // 發送數據
}
else
{
while(!(rUTRSTAT0 & 0x02)); // 等待發送器為空
for(i=0; i<10; i++);
rUTXH0 = data; // 發送數據
}
}
/*********************************************************************************************************
** Function name: UART_SendStr
** Descriptions: 向串口發送一字符串。
** 對于'\n'字符,發送時會加入'\r'字符。
** Input: str 要發送的字符串的指針
** Output: 無
********************************************************************************************************/
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: 返回值即是讀出值
********************************************************************************************************/
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 + -