?? uart1.c
字號:
/******************** (C) COPYRIGHT 2010 Embest Info&Tech Co.,LTD. ************
* 文件名: UART.c
* 作者 : Wuhan R&D Center, Embest
* 日期 : 01/22/2010
* 描述 : NXP LPC11xx 系列處理器 UART API 文件
*******************************************************************************
*******************************************************************************
* 歷史:
* 01/18/2010 : V1.0 初始版本
*******************************************************************************/
/* Includes ------------------------------------------------------------------*/
#include "LPC11xx.h"
#include "UART.h"
volatile uint32_t UARTStatus;
volatile uint8_t UARTTxEmpty = 1;
volatile uint8_t UARTBuffer[UART_BUFSIZE];
volatile uint32_t UARTCount = 0;
/**
* @函數(shù)名: UART_IRQHandler
* @描述: UART 中斷處理程序 handler
* @參數(shù): 無
* @返回值: 無
*/
void UART_IRQHandler(void)
{
uint8_t IIRValue, LSRValue;
uint8_t Dummy = Dummy;
IIRValue = LPC_UART->IIR;
/* 跳過 IIR 掛起位 */
IIRValue >>= 1;
IIRValue &= 0x07;
/* 檢查 1~3 位, 確定中斷 */
/* 接收行狀態(tài) */
if (IIRValue == IIR_RLS)
{
LSRValue = LPC_UART->LSR;
/* 接收行狀態(tài)s */
if (LSRValue & (LSR_OE | LSR_PE | LSR_FE | LSR_RXFE | LSR_BI))
{
/* 存在錯(cuò)誤或產(chǎn)生了中斷 */
/* 讀 LSR 將清除中斷 */
UARTStatus = LSRValue;
Dummy = LPC_UART->RBR;
return;
}
if (LSRValue & LSR_RDR) /* 接收數(shù)據(jù)準(zhǔn)備好 */
{
/* 如果 RLS 沒有產(chǎn)生錯(cuò)誤, 準(zhǔn)備正常, 保存到數(shù)據(jù)緩沖區(qū)。 */
/* 注意: 讀 RBR 將清除中斷 */
UARTBuffer[UARTCount++] = LPC_UART->RBR;
if (UARTCount == UART_BUFSIZE)
{
UARTCount = 0; /* buffer overflow */
}
}
}
/* 接收數(shù)據(jù)可用 */
else if (IIRValue == IIR_RDA)
{
/* 接收數(shù)據(jù)可用 */
UARTBuffer[UARTCount++] = LPC_UART->RBR;
if (UARTCount == UART_BUFSIZE)
{
/* 緩沖區(qū)溢出 */
UARTCount = 0;
}
}
/* 字符超時(shí)指示 */
else if (IIRValue == IIR_CTI)
{
/* 字符超時(shí)指示 */
/* 第 9 位 CTI 錯(cuò)誤 */
UARTStatus |= 0x100;
}
/* THRE, 發(fā)送保持寄存器空 */
else if (IIRValue == IIR_THRE)
{
/* THRE interrupt */
LSRValue = LPC_UART->LSR; /* 檢查 LSR 狀態(tài)查看
U0THR 是否已經(jīng)包含有效數(shù)據(jù) */
if (LSRValue & LSR_THRE)
{
UARTTxEmpty = 1;
}
else
{
UARTTxEmpty = 0;
}
}
return;
}
/**
* @函數(shù)名: ModemInit
* @描述: 初始化 UART0 端口為 modem, 設(shè)置選定的引腳。
* @參數(shù): 無
* @返回值: 無
*/
void ModemInit( void )
{
LPC_IOCON->PIO2_0 &= ~0x07; /* UART I/O 配置 */
LPC_IOCON->PIO2_0 |= 0x01; /* UART DTR */
LPC_IOCON->PIO0_7 &= ~0x07; /* UART I/O 配置 */
LPC_IOCON->PIO0_7 |= 0x01; /* UART CTS */
LPC_IOCON->PIO1_5 &= ~0x07; /* UART I/O 配置 */
LPC_IOCON->PIO1_5 |= 0x01; /* UART RTS */
#if 1
LPC_IOCON->DSRLOC = 0;
LPC_IOCON->PIO2_1 &= ~0x07; /* UART I/O 配置 */
LPC_IOCON->PIO2_1 |= 0x01; /* UART DSR */
LPC_IOCON->DCDLOC = 0;
LPC_IOCON->PIO2_2 &= ~0x07; /* UART I/O 配置 */
LPC_IOCON->PIO2_2 |= 0x01; /* UART DCD */
LPC_IOCON->RILOC = 0;
LPC_IOCON->PIO2_3 &= ~0x07; /* UART I/O 配置 */
LPC_IOCON->PIO2_3 |= 0x01; /* UART RI */
#else
LPC_IOCON->DSRLOC = 1;
LPC_IOCON->PIO3_1 &= ~0x07; /* UART I/O 配置 */
LPC_IOCON->PIO3_1 |= 0x01; /* UART DSR */
LPC_IOCON->DCDLOC = 1;
LPC_IOCON->PIO3_2 &= ~0x07; /* UART I/O 配置 */
LPC_IOCON->PIO3_2 |= 0x01; /* UART DCD */
LPC_IOCON->RILOC = 1;
LPC_IOCON->PIO3_3 &= ~0x07; /* UART I/O 配置 */
LPC_IOCON->PIO3_3 |= 0x01; /* UART RI */
#endif
LPC_UART->MCR = 0xC0; /* 使能 Auto RTS 和 Auto CTS. */
return;
}
/**
* @函數(shù)名: UARTInit
* @描述: 初始化 UART0 端口,設(shè)置選中引腳、
* 時(shí)鐘,校驗(yàn)、停止位、FIFO、等等。
* @參數(shù): UART 波特率
* @返回值: 無
*/
void UARTInit(uint32_t baudrate)
{
uint16_t usFdiv;uint16_t regVal;
LPC_SYSCON->SYSAHBCLKCTRL |= (1 << 16); /* 使能IOCON時(shí)鐘 */
LPC_IOCON->PIO1_6 |= 0x01; /* 將P1.6 1.7配置為RXD和TXD */
LPC_IOCON->PIO1_7 |= 0x01;
LPC_SYSCON->SYSAHBCLKCTRL |= (1<<12); /* 打開UART功能部件時(shí)鐘 */
LPC_SYSCON->UARTCLKDIV = 0x01; /* UART時(shí)鐘分頻 */
LPC_UART->LCR = 0x83; /* 允許設(shè)置波特率 */
usFdiv = ((SystemAHBFrequency/regVal)/16)/baudrate; /* 設(shè)置波特率 */
LPC_UART->DLM = usFdiv / 256;
LPC_UART->DLL = usFdiv % 256;
LPC_UART->LCR = 0x03; /* 鎖定波特率 */
LPC_UART->FCR = 0x87; /* 使能FIFO,設(shè)置8個(gè)字節(jié)觸發(fā)點(diǎn) */
NVIC_EnableIRQ(UART_IRQn); /* 使能UART中斷,并配置優(yōu)先級 */
NVIC_SetPriority(UART_IRQn, 1);
LPC_UART->IER = 0x01;
}
/**
* @函數(shù)名: UARTSend
* @描述: 根據(jù)數(shù)據(jù)長度發(fā)送一串?dāng)?shù)據(jù)到 UART 0 端口
* @參數(shù): buffer pointer, and data length
* @返回值: 無
*/
void UARTSend(uint8_t *BufferPtr, uint32_t Length)
{
while ( Length != 0 )
{
/* THRE 狀態(tài), 包含有效數(shù)據(jù) */
#if !TX_INTERRUPT
while ( !(LPC_UART->LSR & LSR_THRE) );
LPC_UART->THR = *BufferPtr;
#else
/* 以下數(shù)據(jù)在中斷處理程序中設(shè)置當(dāng) THRE 產(chǎn)生時(shí)。 */
while ( !(UARTTxEmpty & 0x01) );
LPC_UART->THR = *BufferPtr;
UARTTxEmpty = 0; /* not empty in the THR until it shifts out */
#endif
BufferPtr++;
Length--;
}
return;
}
/**
* @}
*/
/**
* @}
*/
/************* (C) COPYRIGHT 2010 Wuhan R&D Center, Embest *****文件結(jié)束*******/
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -