新闻中心

EEPW首页>嵌入式系统>设计应用> LPC1114 UART收发实验

LPC1114 UART收发实验

作者: 时间:2016-11-10 来源:网络 收藏
串口是很多实验的基础,可以作为其他实验运行的验证。在做LPC1114UART实验时,发现的例程只有发送函数,而且是发送字符串的,所以本人完善了一些,添加了一个发送字符串的函数UARTSendByte(),一个接受字符的函数UARTReceiveByte(),一个接受字符串函数UARTReceive()。具体的代码如下:

// 发送字符函数

本文引用地址://m.amcfsurvey.com/article/201611/317367.htm

/*****************************************************************************
** Function name:UARTSendByte
**
** Descriptions:Send ablock 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 ;
}

实验图片如下:

c510d9abb6b297dd281662e4c8c&t=1274344745¬humb=yes" rel="nofollow" target="_blank">IMG_3380.JPG(70.52 KB)

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”即可正常连接。

2010-5-14 07:10




关键词:LPC1114UART收发实

评论


技术专区

关闭