LPC1114 UART收发实验
2016-07-15 来源:eefocus
// 发送字符函数
/*****************************************************************************
** Function name: UARTSendByte
**
** Descriptions: Send a block of data to the UART 0 port based
** on the data Byte
**
** parameters: send data
** Returned value: None
**
*****************************************************************************/
void UARTSendByte(uint8_t dat)
{
while ( !(LPC_UART->LSR & LSR_THRE) )
{
; // 等待数据发送完毕
}
LPC_UART->THR = dat;
}
// 接受字符函数
/*****************************************************************************
** Function name: UARTReceiveByte
**
** Descriptions: Receive a block of data to the UART 0 port based
** on the data Byte
**
** parameters: None
** Returned value: Byte
**
*****************************************************************************/
uint8_t UARTReceiveByte(void)
{
uint8_t rcvData;
while (!(LPC_UART->LSR & LSR_RDR))
{
; // 查询数据是否接收完毕
}
rcvData = LPC_UART->RBR; // 接收数据
return (rcvData);
}
// 接收字符串函数
/*****************************************************************************
** Function name: UARTReceive
**
** Descriptions: Receive a block of data to the UART 0 port based
** on the data Length
**
** parameters: buffer pointer, and data length
** Returned value: Note
**
*****************************************************************************/
void UARTReceive(uint8_t *BufferPtr, uint32_t Length)
{
while (Length--)
{
*BufferPtr++ = UARTReceiveByte(); // 把数据放入缓冲
}
}
// 主函数
int main(void) {
// TODO: insert code here
uint8_t ch = 0;
UARTInit(9600);
LPC_UART->IER = IER_THRE | IER_RLS; // 设置中断使能寄存器
UARTSend((uint8_t *)Buffer, 10);
while (1)
{
ch = UARTReceiveByte(); // 接收字符
if (ch != 0x00)
{
UARTSendByte(ch); // 发送接收数据
}
}
// Enter an infinite loop, just incrementing a counter
volatile static int i = 0 ;
while(1) {
i++ ;
}
return 0 ;
}
2010-5-14 06:54
IMG_3381.JPG (76.79 KB)2010-5-14 06:54
IMG_3382.JPG (72.08 KB)2010-5-14 06:54
串口我用的是USB转串口进行调试的,如果先打开串口进行,再连接实验板时会提示如下:
此时如果点击'OK',将提示如下错误:
此时应该点击如下图红圈的地方,LPC-Link,然后点击“OK”即可正常连接。
下一篇:ARM学习1-LED流水灯