在线时间194 小时
UID361789
注册时间2008-4-28
NXP金币0
TA的每日心情 | 开心 2020-4-30 21:50 |
---|
签到天数: 17 天 连续签到: 1 天 [LV.4]偶尔看看III
金牌会员
 
- 积分
- 1208
- 最后登录
- 2023-2-28
|
接上篇板载GPIO 连接的LED驱动,https://www.nxpic.org.cn/module/forum/thread-616178-1-1.html。
本周也学习了LPC1768 UART部分,总结如下:
LPC1768内置了4个串口通讯模块,都是异步的。串口1相对普通串口UART0/2/3增加了MODEM, RS-486/EA-486(支持RS-485) 布置IrDA。所以如果只是普通的串口使用的话,只要寄存器改为对应的串口号寄存器即可。 工作中经常用到485通讯,后续会利用这个板子修改并学习下。
IRD-LPC1768-DEV 随开发板附带有ULINK-ME调试器,支持JTAG 和SW模式,可以用来配合开发环境进行调试。但一般个人在调试目前都带串口的MCU是,还是喜欢使用串口输出调试信息,具有直观,高效的特性。当然对于大的系统,涉及到程序流程的,建议还是用仿真器配合调试。
IRD-LPC1768-DEV开发板提供了所有LPC1768的4个UART, UART0/1/2/3。其中UART 0 和 UART1 通过DB9输出RS232电平信号, UART2 通过左侧排针P0.10, P0.11引出,UART3通过右下JP3引出。UART2 和UART3输出为TTL电平, 各引脚和功能选择对应如下:
- /* Pin muxing configuration */
- STATIC const PINMUX_GRP_T pinmuxing[] = {
- {0, 2, IOCON_MODE_INACT | IOCON_FUNC1}, /* TXD0 */
- {0, 3, IOCON_MODE_INACT | IOCON_FUNC1}, /* RXD0 */
- {0, 15, IOCON_MODE_INACT | IOCON_FUNC1}, /* TXD1 */
- {0, 16, IOCON_MODE_INACT | IOCON_FUNC1}, /* RXD1 */
- {0, 10, IOCON_MODE_INACT | IOCON_FUNC1}, /* TXD2 */
- {0, 11, IOCON_MODE_INACT | IOCON_FUNC1}, /* RXD2 */
- {4, 28, IOCON_MODE_INACT | IOCON_FUNC3}, /* TXD3 */
- {4, 29, IOCON_MODE_INACT | IOCON_FUNC3}, /* RXD3 */
复制代码 这里通过RS232信号线直接与电脑的扩展坞串口连接,值得注意的是在没有公母转接头的情况下,可以通过JP9和JP12断开并跳线到RS232线连接的端口来调试UART0 和UART1,不如断开JP9, 通过跳线连接JP12芯片侧至JP9输出侧。
说到串口,大家都不陌生,几乎所有的MCU都有串口,论坛相关的介绍文档也非常多。这里不再具体展开。LPC1768作为十年前的片子有多达4个UART,非常适合多个UART应用的坏境,比如工业应用。
测试UART初始化代码如下,通过宏定义来决定UART输出通道。
- #define DEBUG_UART LPC_UART3
- /* Initialize debug output via UART for board */
- void Board_Debug_Init(void)
- {
- #if defined(DEBUG_ENABLE)
- Board_UART_Init(DEBUG_UART);
- Chip_UART_Init(DEBUG_UART);
- Chip_UART_SetBaud(DEBUG_UART, 115200);
- Chip_UART_ConfigData(DEBUG_UART, UART_LCR_WLEN8 | UART_LCR_SBS_1BIT | UART_LCR_PARITY_DIS);
- /* Enable UART Transmit */
- Chip_UART_TXEnable(DEBUG_UART);
- #endif
- }
复制代码 其中Board_UART_Init(DEBUG_UART); 用来通过以上的PIN Muxing初始化端口。Chip_UART_Init(DEBUG_UART);用于初始化对应UART模块,代码如下。Chip_UART_SetBaud(DEBUG_UART, 115200); 和Chip_UART_ConfigData(DEBUG_UART, UART_LCR_WLEN8 | UART_LCR_SBS_1BIT | UART_LCR_PARITY_DIS); 用于波特率和传输格式的设定。具体的函数已在LPCOPEN库中体现。
- /* Initializes the pUART peripheral */
- void Chip_UART_Init(LPC_USART_T *pUART)
- {
- uint32_t tmp;
- (void) tmp;
-
- /* Enable UART clocking. UART base clock(s) must already be enabled */
- Chip_Clock_EnablePeriphClock(Chip_UART_GetClockIndex(pUART));
- /* Enable FIFOs by default, reset them */
- Chip_UART_SetupFIFOS(pUART, (UART_FCR_FIFO_EN | UART_FCR_RX_RS | UART_FCR_TX_RS));
-
- /* Disable Tx */
- Chip_UART_TXDisable(pUART);
-
- /* Disable interrupts */
- pUART->IER = 0;
- /* Set LCR to default state */
- pUART->LCR = 0;
- /* Set ACR to default state */
- pUART->ACR = 0;
- /* Set RS485 control to default state */
- pUART->RS485CTRL = 0;
- /* Set RS485 delay timer to default state */
- pUART->RS485DLY = 0;
- /* Set RS485 addr match to default state */
- pUART->RS485ADRMATCH = 0;
-
- /* Clear MCR */
- if (pUART == LPC_UART1) {
- /* Set Modem Control to default state */
- pUART->MCR = 0;
- /*Dummy Reading to Clear Status */
- tmp = pUART->MSR;
- }
- /* Default 8N1, with DLAB disabled */
- Chip_UART_ConfigData(pUART, (UART_LCR_WLEN8 | UART_LCR_SBS_1BIT | UART_LCR_PARITY_DIS));
- /* Disable fractional divider */
- pUART->FDR = 0x10;
- }
复制代码 串口输出和输入代码如下:
- /* Sends a character on the UART */
- void Board_UARTPutChar(char ch)
- {
- #if defined(DEBUG_ENABLE)
- while ((Chip_UART_ReadLineStatus(DEBUG_UART) & UART_LSR_THRE) == 0) {}
- Chip_UART_SendByte(DEBUG_UART, (uint8_t) ch);
- #endif
- }
- /* Gets a character from the UART, returns EOF if no character is ready */
- int Board_UARTGetChar(void)
- {
- #if defined(DEBUG_ENABLE)
- if (Chip_UART_ReadLineStatus(DEBUG_UART) & UART_LSR_RDR) {
- return (int) Chip_UART_ReadByte(DEBUG_UART);
- }
- #endif
- return EOF;
- }
复制代码 LPCOPEN 库实例中提供了串口环形缓冲区的示例代码。串口环形缓冲区收发:一般串口收发都是:接收数据,触发中断,数据发回或处理。当传输数据量较大或较快时,如果来不及处理收到的数据,那么,当再次收到数据的时候,就会将之前未处理的数据覆盖掉。会出现丢包的现象。通过环形队列的可以避免这样的情况。贴下来供快速参考。
- #include "chip.h"
- #include "board.h"
- #include "string.h"
- /*****************************************************************************
- * Private types/enumerations/variables
- ****************************************************************************/
- #if defined(BOARD_EA_DEVKIT_1788) || defined(BOARD_EA_DEVKIT_4088)
- #define UART_SELECTION LPC_UART0
- #define IRQ_SELECTION UART0_IRQn
- #define HANDLER_NAME UART0_IRQHandler
- #elif defined(BOARD_NXP_LPCXPRESSO_1769)
- #define UART_SELECTION LPC_UART3
- #define IRQ_SELECTION UART3_IRQn
- #define HANDLER_NAME UART3_IRQHandler
- #else
- #error No UART selected for undefined board
- #endif
- /* Transmit and receive ring buffers */
- STATIC RINGBUFF_T txring, rxring;
- /* Transmit and receive ring buffer sizes */
- #define UART_SRB_SIZE 128 /* Send */
- #define UART_RRB_SIZE 32 /* Receive */
- /* Transmit and receive buffers */
- static uint8_t rxbuff[UART_RRB_SIZE], txbuff[UART_SRB_SIZE];
- const char inst1[] = "LPC17xx/40xx UART example using ring buffers\r\n";
- const char inst2[] = "Press a key to echo it back or ESC to quit\r\n";
- /**
- * @brief UART 0 interrupt handler using ring buffers
- * @return Nothing
- */
- void HANDLER_NAME(void)
- {
- /* Want to handle any errors? Do it here. */
- /* Use default ring buffer handler. Override this with your own
- code if you need more capability. */
- Chip_UART_IRQRBHandler(UART_SELECTION, &rxring, &txring);
- }
- /**
- * @brief Main UART program body
- * @return Always returns 1
- */
- int main(void)
- {
- uint8_t key;
- int bytes;
- SystemCoreClockUpdate();
- Board_Init();
- Board_UART_Init(UART_SELECTION);
- Board_LED_Set(0, false);
- /* Setup UART for 115.2K8N1 */
- Chip_UART_Init(UART_SELECTION);
- Chip_UART_SetBaud(UART_SELECTION, 115200);
- Chip_UART_ConfigData(UART_SELECTION, (UART_LCR_WLEN8 | UART_LCR_SBS_1BIT));
- Chip_UART_SetupFIFOS(UART_SELECTION, (UART_FCR_FIFO_EN | UART_FCR_TRG_LEV2));
- Chip_UART_TXEnable(UART_SELECTION);
- /* Before using the ring buffers, initialize them using the ring
- buffer init function */
- RingBuffer_Init(&rxring, rxbuff, 1, UART_RRB_SIZE);
- RingBuffer_Init(&txring, txbuff, 1, UART_SRB_SIZE);
- /* Reset and enable FIFOs, FIFO trigger level 3 (14 chars) */
- Chip_UART_SetupFIFOS(UART_SELECTION, (UART_FCR_FIFO_EN | UART_FCR_RX_RS |
- UART_FCR_TX_RS | UART_FCR_TRG_LEV3));
- /* Enable receive data and line status interrupt */
- Chip_UART_IntEnable(UART_SELECTION, (UART_IER_RBRINT | UART_IER_RLSINT));
- /* preemption = 1, sub-priority = 1 */
- NVIC_SetPriority(IRQ_SELECTION, 1);
- NVIC_EnableIRQ(IRQ_SELECTION);
- /* Send initial messages */
- Chip_UART_SendRB(UART_SELECTION, &txring, inst1, sizeof(inst1) - 1);
- Chip_UART_SendRB(UART_SELECTION, &txring, inst2, sizeof(inst2) - 1);
- /* Poll the receive ring buffer for the ESC (ASCII 27) key */
- key = 0;
- while (key != 27) {
- bytes = Chip_UART_ReadRB(UART_SELECTION, &rxring, &key, 1);
- if (bytes > 0) {
- /* Wrap value back around */
- if (Chip_UART_SendRB(UART_SELECTION, &txring, (const uint8_t *) &key, 1) != 1) {
- Board_LED_Toggle(0);/* Toggle LED if the TX FIFO is full */
- }
- }
- }
- /* DeInitialize UART0 peripheral */
- NVIC_DisableIRQ(IRQ_SELECTION);
- Chip_UART_DeInit(UART_SELECTION);
- return 1;
- }
复制代码
以上通过修改宏定义,测试了所有UART0/1/2/3发送。具体发送接收后数据的处理在以后模块中继续扩展。下面是调试RTC时通过串口UART0测试的信息:
|
|