?? uart.c
字號:
/****************************************Copyright (c)****************************************************
** Guangzhou ZHIYUAN electronics Co.,LTD.
**
** http://www.embedtools.com
**
**--------------File Info---------------------------------------------------------------------------------
** File name: uart.c
** Latest modified Date: 2008-8-20
** Latest Version: 1.0
** Descriptions: 串口外設的操作函數
**
**--------------------------------------------------------------------------------------------------------
** Created by: CaiWenqi
** Created date: 2008-8-20
** Version: 1.0
** Descriptions: The original version
**
**--------------------------------------------------------------------------------------------------------
** Modified by:
** Modified date:
** Version:
** Descriptions:
**
*********************************************************************************************************/
#include "config.h"
/*********************************************************************************************************
** Function name: siciwrSetup
** Descriptions: 設置系統中斷喚醒使能寄存器
** Input parameters: uiPara: 見頭文件,多個設置使用或操作然后傳入此參數
** usEnable: ENABLE -- 使能
** DISABLE -- 禁能
** Output parameters: NONE
** Returned value: NONE
*********************************************************************************************************/
void uartBaudSet (uint16 usBaud)
{
uint16 divisor; /* 保存計算的除數值 */
uint16 msel;
uint16 ssel;
msel = ((*pPLL_CTL) >> 9) & 0x003f; /* 獲取PLL中VCO的倍頻數 */
ssel = (*pPLL_DIV) & 0x000f; /* 獲取分頻SSEL,SCLK = VCO/SSEL */
divisor = ((msel * CLKIN)/(ssel * usBaud)) >> 4; /* 計算除數 */
if (*pPLL_CTL & 0x1) { /* PLL入口時鐘是否為CLKIN/2 */
divisor = divisor >> 1;
}
*pUART_LCR |= (1 << 7); /* 使能除數鎖存 */
ssync(); /* 命令立即生效 */
*pUART_DLL = divisor & 0x00ff; /* 賦低八位 */
ssync();
*pUART_DLH = (divisor >> 8); /* 賦高八位 */
ssync();
*pUART_LCR &= (~((uint16)1<<7)); /* 禁能除數鎖存 */
ssync();
}
/*********************************************************************************************************
** Function name: uartConfig
** Descriptions: 設置UART基本通訊參數
** Input parameters: usBaud : 設置的波特率
** usByteLength : 數據長度,可取 5, 6 ,7 ,8
** usStopbit : 停止位寬度,可取 1, 2
** usParityCheck : PARITY_DIS -- 不進行校驗
** PARITY_ODD -- 進行奇校驗
** PARITY_EVEN-- 進行偶校驗
** Output parameters: NONE
** Returned value: true -- 成功設置,false -- 設置失敗
*********************************************************************************************************/
uint16 uartConfig (uint16 usBaud,
uint16 usByteLength,
uint16 usStopbit,
uint16 usParityCheck)
{
uint16 usTmpLCR;
uint16 usTmpRBR;
usTmpLCR = 0;
uartBaudSet(usBaud); /* 設置波特率 */
switch (usByteLength) { /* 設置數據長度 */
case 5:
usTmpLCR = LEN_5BIT;
break;
case 6:
usTmpLCR = LEN_6BIT;
break;
case 7:
usTmpLCR = LEN_7BIT;
break;
usTmpLCR = LEN_8BIT;
break;
default:
return false;
}
switch (usStopbit) {
case 1:
break;
case 2:
usTmpLCR |= STOP_2BIT;
break;
default:
return false;
}
switch (usParityCheck) { /* 設置奇偶校驗 */
case PARITY_DIS:
break;
case PARITY_ODD:
usTmpLCR |= PARITY_EN;
break;
case PARITY_EVEN:
usTmpLCR |= (PARITY_E | PARITY_EN);
break;
default:
return false;
}
usTmpRBR = *pUART_RBR; /* 清除可能的殘留數據 */
*pUART_LCR = usTmpLCR; /* 回寫UART_LCR寄存器,完成配置 */
ssync();
*pUART_GCTL |= 1; /* 打開UART時鐘 */
ssync();
return true;
}
/*********************************************************************************************************
** Function name: uartSendChar
** Descriptions: 發送單個字符
** Input parameters: cChar : 發送字符
** Output parameters: NONE
** Returned value: NONE
*********************************************************************************************************/
void uartSendChar(char cChar)
{
uint16 i;
while ((*pUART_LSR & (1<<5)) == 0) {
asm("nop;");
}
for (i = 0; i<0x0fff; i ++) {
asm("nop;");
}
*pUART_THR = cChar;
ssync();
}
/*********************************************************************************************************
** Function name: uartSendStr
** Descriptions: 發送字符串
** Input parameters: cStr : 字符串指針
** Output parameters: NONE
** Returned value: NONE
*********************************************************************************************************/
void uartSendStr(char *cStr)
{
uint32 i;
while (cStr[i] != '\0') {
uartSendChar(cStr[i++]);
}
}
/*********************************************************************************************************
** Function name: uartConfigDMA
** Descriptions: 設置DMA方式下的UART基本通訊參數
** Input parameters: usBaud : 設置的波特率
** usByteLength : 數據長度,可取 5, 6 ,7 ,8
** usStopbit : 停止位寬度,可取 1, 2
** usParityCheck : PARITY_DIS -- 不進行校驗
** PARITY_ODD -- 進行奇校驗
** PARITY_EVEN-- 進行偶校驗
** Output parameters: NONE
** Returned value: true -- 成功設置,false -- 設置失敗
*********************************************************************************************************/
/*uint16 uartConfigDMA (uint16 usBaud,
uint16 usByteLength,
uint16 usStopbit,
uint16 usParityCheck
uint16 usDMAx)
{
uartConfig(usBaud, usByteLength, usStopbit, usParityCheck);
}*/
/*********************************************************************************************************
** Function name: uartSendStrDMA
** Descriptions: 通過DMA發送字符串
** Input parameters: cStr : 字符串指針
** Output parameters: NONE
** Returned value: NONE
*********************************************************************************************************/
/*void uartSendStrDMA(char *cStr)
{
uint32 i;
i = 0;
while (cStr[i] != '\0') {
uartSendChar(cStr[i++]);
}
}*/
/*********************************************************************************************************
** Function name: uartISR
** Descriptions: 設置UART基本通訊參數
** Input parameters: usBaud : 設置的波特率
** usByteLength : 數據長度,可取 5, 6 ,7 ,8
** usStopbit : 停止位寬度,可取 1, 2
** usParityCheck : PARITY_DIS -- 不進行校驗
** PARITY_ODD -- 進行奇校驗
** PARITY_EVEN-- 進行偶校驗
** Output parameters: NONE
** Returned value: true -- 成功設置,false -- 設置失敗
*********************************************************************************************************/
void uartISR (void)
{
uint16 usIIRv;
uint16 usIIR;
uint16 usLSR;
char ucRcv;
usIIR = *pUART_IIR; /* 同時清除可能的THR中斷請求 */
usIIRv = usIIR & 0x60;
if (!(usIIR & HAVE_INT)) { /* 有中斷產生 */
if (usIIRv == HAVE_LSR_INT) { /* 發生線狀態中斷 */
usLSR = *pUART_LSR; /* 同時清除線狀態中斷請求 */
}
if (usIIRv == HAVE_RBR_INT) { /* 接收數據可用中斷 */
ucRcv = *pUART_RBR; /* 同時清除中斷請求 */
uartSendChar(ucRcv);
}
if (usIIRv == HAVE_THRE_INT) { /* 發送數據結束中斷 */
}
}
}
/*********************************************************************************************************
END FILE
*********************************************************************************************************/
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -