ARM9硬件接口学习之UART
2) 发送数据:
a.UTRSTAT0 ( UART channel 0 Tx/Rx status register ):
位[2]:无数据发送时,自动设为1。当我们要使用串口发送数据时,先读此位以判断是否有数据正在占用发送口。
位[1]:发送FIFO是否为空,本实验未用此位
位[0]:接收缓冲区是否有数据,若有,此位设为1。本实验中,需要不断查询此位一判断是否有数据已经被接收。
b.UTXH0 (UART channel 0 transmit buffer register ):
把要发送的数据写入此寄存器。
3) 接收数据:
a.UTRSTAT0:如上描述,我们用到位[0]
b.URXH0 (UART channel 0 receive buffer register ):
当查询到UTRSTAT0 位[0]=1时,读此寄存器获得串口接收到的数据。
4) 实验源代码
/* main.c */
#include "uart.h"
#include "clock.h"
#include "watchdog.h"
int Main(void)
{
char key = ' ';
clock_init(); //初始化时钟
uart_init(); //初始化串口
close_watchdog();
uart_send("uart communication success!");
while(1)
{
uart_send("If you want to quit ,please pess 'e'");
key = uart_get();
if (key == 'e')
{
uart_send ("you pressed 'e' and you'll quit!");
break;
}
else
{
uart_send("you pressed ");
uart_send(key);
uart_send(",retry!");
}
}
uart_send("the program exited by user!");
return 0;
}
下面是串口相关部分源码:
void uart_init(void)
{
ULCON0 = 0x03; //8N1
UCON0 = 0x005; //中断或查询方式
UFCON0 = 0x00; //不使用FIFO
UMCON0 = 0x00; //不使用流控
UBRDIV0 = 27; //波特率为115200
GPHCON |= 0xa0; //GPH2,GPH3 set as TXD0,RXD0
GPHUP = 0x0c; //GPH2,GPH3内部上拉
}
void uart_send(char * c)
{
for (; *c != ' '; c++)
{
while(!(UTRSTAT0 TXD0READY)) ; //不断查询,直到可以发送数据
UTXH0 = *c ; //发送数据
}
}
unsigned char uart_get(void)
{
while(!(UTRSTAT0 RXD0READY)) ; //不断查询,直到接收到了数据
return URXH0; //返回接收到的数据
评论