ARM9硬件接口學(xué)習(xí)之UART
2) 發(fā)送數(shù)據(jù):
a.UTRSTAT0 ( UART channel 0 Tx/Rx status register ):
位[2]:無數(shù)據(jù)發(fā)送時,自動設(shè)為1。當(dāng)我們要使用串口發(fā)送數(shù)據(jù)時,先讀此位以判斷是否有數(shù)據(jù)正在占用發(fā)送口。
位[1]:發(fā)送FIFO是否為空,本實驗未用此位
位[0]:接收緩沖區(qū)是否有數(shù)據(jù),若有,此位設(shè)為1。本實驗中,需要不斷查詢此位一判斷是否有數(shù)據(jù)已經(jīng)被接收。
b.UTXH0 (UART channel 0 transmit buffer register ):
把要發(fā)送的數(shù)據(jù)寫入此寄存器。
3) 接收數(shù)據(jù):
a.UTRSTAT0:如上描述,我們用到位[0]
b.URXH0 (UART channel 0 receive buffer register ):
當(dāng)查詢到UTRSTAT0 位[0]=1時,讀此寄存器獲得串口接收到的數(shù)據(jù)。
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;
}
下面是串口相關(guān)部分源碼:
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內(nèi)部上拉
}
void uart_send(char * c)
{
for (; *c != '