138
技術社區[雲棲]
LPC1768之串口UART0
*********************************************************************************************************/
#include "LPC17xx.h" /* LPC17xx外設寄存器 */
/********************************************************************************************************
變量與宏定義
*********************************************************************************************************/
extern uint32_t SystemFrequency; /* Clock Variable 時鍾變量 */
#define UART_BPS 9600 /* 串口通信波特率 */
/*********************************************************************************************************
** Function name: delayNS
** Descriptions: 延時函數
** input parameters: ulDly: 延時值
** output parameters: 無
** Returned value: 無
*********************************************************************************************************/
void delayNS (uint32_t ulDly)
{
uint32_t i;
for (; ulDly > 0; ulDly--) {
for (i = 0; i < 5000; i++);
}
}
/*********************************************************************************************************
** Function name: uartInit
** Descriptions: 串口初始化,設置為8位數據位,1位停止位,無奇偶校驗,波特率為9600
** input parameters: 無
** output parameters: 無
** Returned value: 無
*********************************************************************************************************/
void uart0Init (void)
{
uint16_t usFdiv;
LPC_PINCON->PINSEL0 |= (0x01 << 4)|(0x01 << 6); /* 設置P0.2(TXD0)為串口,P0.3(RXD0)為串口 */
LPC_SC->PCONP = LPC_SC->PCONP|0x08; /* 打開串口0功能 */
LPC_UART0->LCR = 0x83; /* 設置串口數據格式,8位字符長度,1個停止位,無校驗,
使能訪問除數鎖存器 ,設定波特率 */
usFdiv = (SystemFrequency/4 / 16) / UART_BPS; /* 設置波特率 */
LPC_UART0->DLM = usFdiv / 256; /*除數高8位,沒有小數點情況 */
LPC_UART0->DLL = usFdiv % 256; /*除數低8位,沒有小數點情況 */
LPC_UART0->LCR = 0x03; /* 鎖定波特率 */
LPC_UART0->FCR = 0x06; /* 複位TxFIFO和RXFIFIO */
}
/*********************************************************************************************************
** Function name: uart0GetByte
** Descriptions: 從串口接收1字節數據,使用查詢方式接收
** input para
meters: 無
** output parameters: 無
** Returned value: ucRcvData: 接收到的數據
*********************************************************************************************************/
uint8_t uart0GetByte (void) //查詢法
{
uint8_t ucRcvData;
while ((LPC_UART0->LSR & 0x01) == 0); //UnLSR[0] 該位是否置一,決定能否從FIFO中讀取數據
//UnLSR[0]為0 :RBR為空 ; UnLSR[0]為1:RBR中包含有效數據,從FIFO中讀走所有數據後,恢複為0
ucRcvData = LPC_UART0->RBR; //UnRBR為接收緩存寄存器 /* 讀取數據 */
return (ucRcvData);
}
/*********************************************************************************************************
** Function name: uart0GetStr
** Descriptions: 串口接收字符串
** input parameters: puiStr: 指向接收數據數組的指針
** ulNum: 接收數據的個數
** output parameters: 無
** Returned value: 無
*********************************************************************************************************/
void uart0GetStr (uint8_t *puiStr, uint32_t ulNum)
{
for (; ulNum > 0; ulNum--) {
*puiStr++ = uart0GetByte ();
}
}
/*********************************************************************************************************
** Function name: uart0SendByte
** Descriptions: 向串口發送子節數據,並等待數據發送完成,使用查詢方式
** input parameters: ucDat: 要發送的數據
** output parameters: 無
** Returned value: 無
*********************************************************************************************************/
void uart0SendByte (uint8_t ucDat)
{
LPC_UART0->THR = ucDat; //UnTHR為發送緩存寄存器 /* 寫入數據 * /
while ((LPC_UART0->LSR & 0x40) == 0); // UnLSR[6] 為0 時,表明UnTHR包含數據;
} // UnLSR[6]為1時,則UnTHR已空 /* 等待數據發送完畢*/
/*********************************************************************************************************
** Function name: uart0SendStr
** Descriptions: 向串口發送字符串
** input parameters: puiStr: 要發送的字符 串指針
** output parameters: 無
** Returned value: 無
*********************************************************************************************************/
void uart0SendStr (uint8_t const *puiStr, uint32_t len)
{
while (len -- > 0) {
uart0SendByte (*puiStr++);
}
}
/*********************************************************************************************************
** Function name: main
** Descriptions: 串口查詢方式接收發送數據。
** 全速運行程序用串口調試軟件發送字符串,並在接收窗口查看返回數據,波特率為9600,
** 上位機一次需要發送20個字符。
** input parameters: 無
** output parameters: 無
** Returned value: 無
*********************************************************************************************************/
int main (void)
{
uint8_t ucBuf[30];
SystemInit(); /* 初始化目標板 */
uart0Init (); /* 串口初始化 */
while (1){
uart0GetStr(ucBuf, 20); /* 從串口接收字符串 */
delayNS(1);
uart0SendStr(ucBuf, 20); /* 向串口發送字符串 */
delayNS(1);
}
}
/*********************************************************************************************************
End Of File
*********************************************************************************************************/
最後更新:2017-04-03 05:39:37