首先是配置串口引脚,选择的是串口0.
/* UART Pin mux function - note that SystemInit() may already setup your
pin muxing at system startup */
static void Init_UART_PinMux(void)
{
#if defined(BOARD_NXP_LPCXPRESSO_1549)
/* UART signals on pins PIO0_13 (FUNC0, U0_TXD) and PIO0_18 (FUNC0, U0_RXD) */
Chip_IOCON_PinMuxSet(LPC_IOCON, 0, 13, (IOCON_FUNC0 | IOCON_MODE_INACT | IOCON_DIGMODE_EN));
Chip_IOCON_PinMuxSet(LPC_IOCON, 0, 18, (IOCON_FUNC0 | IOCON_MODE_INACT | IOCON_DIGMODE_EN));
/* UART signal muxing via SWM */
Chip_SWM_MovablePortPinAssign(SWM_UART0_RXD_I, 0, 13);
Chip_SWM_MovablePortPinAssign(SWM_UART0_TXD_O, 0, 18);
#else
#warning "No UART nmuxing defined for this example"
#endif
}
同时打开三色led灯,进行相应的测试指示。
Board_LED_Set(0, false);
Board_LED_Set(1, false);
Board_LED_Set(2, false);
串口初始化配置:
/* Setup UART */
Chip_UART_Init(LPC_USART);
Chip_UART_ConfigData(LPC_USART, UART_CFG_DATALEN_8 | UART_CFG_PARITY_NONE | UART_CFG_STOPLEN_1);
Chip_UART_SetBaud(LPC_USART, UART_TEST_DEFAULT_BAUDRATE);
/* Optional for low clock rates only: Chip_UART_SetBaudWithRTC32K(LPC_USART, 300); */
Chip_UART_Enable(LPC_USART);
Chip_UART_TXEnable(LPC_USART);
/* Before using the ring buffers, initialize them using the ring
buffer init function */
RingBuffer_Init(&rxring, rxbuff, 1, UART_RB_SIZE);
RingBuffer_Init(&txring, txbuff, 1, UART_RB_SIZE);
/* Enable receive data and line status interrupt */
Chip_UART_IntEnable(LPC_USART, UART_INTEN_RXRDY);
Chip_UART_IntDisable(LPC_USART, UART_INTEN_TXRDY); /* May not be needed */
/* Enable UART interrupt */
NVIC_EnableIRQ(LPC_IRQNUM);
/* Initial message sent using blocking method to prevent ring
buffer overflow */
Chip_UART_SendBlocking(LPC_USART, inst3, sizeof(inst3) - 1);
Chip_UART_SendBlocking(LPC_USART, inst1, sizeof(inst1) - 1);
Chip_UART_SendRB(LPC_USART, &txring, inst2, sizeof(inst2) - 1);
其中inst1,2,3的三个数据为:
const char inst3[] = "\r\nyouzizhile test lpc1549 xpresso v2 uart! \r\n";
const char inst1[] = "LPC15xx UART example using ring buffers\r\n";
const char inst2[] = "Press a key to echo it back or ESC to quit\r\n";
接收PC的串口数据,如果为2,则进行相应的led灯指示:
Chip_UART_Read(LPC_USART, &data1,1);
if( data1==0x30)
Board_LED_Toggle(0);
if( data1==0x31)
Board_LED_Toggle(1);
if( data1==0x32)
Board_LED_Toggle(2);
串口显示结果:
红色为在原文件上的修改部分。
LPC1500体验+开发环境的构建
LPC1500体验+驱动步进电机设计
LPC1500体验+led闪烁