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