ARM9硬件接口學(xué)習(xí)之UART
2) 發(fā)送數(shù)據(jù):
a.UTRSTAT0 ( UART channel 0 Tx/Rx status register ):
位[2]:無(wú)數(shù)據(jù)發(fā)送時(shí),自動(dòng)設(shè)為1。當(dāng)我們要使用串口發(fā)送數(shù)據(jù)時(shí),先讀此位以判斷是否有數(shù)據(jù)正在占用發(fā)送口。
位[1]:發(fā)送FIFO是否為空,本實(shí)驗(yàn)未用此位
位[0]:接收緩沖區(qū)是否有數(shù)據(jù),若有,此位設(shè)為1。本實(shí)驗(yàn)中,需要不斷查詢(xún)此位一判斷是否有數(shù)據(jù)已經(jīng)被接收。
b.UTXH0 (UART channel 0 transmit buffer register ):
把要發(fā)送的數(shù)據(jù)寫(xiě)入此寄存器。
3) 接收數(shù)據(jù):
a.UTRSTAT0:如上描述,我們用到位[0]
b.URXH0 (UART channel 0 receive buffer register ):
當(dāng)查詢(xún)到UTRSTAT0 位[0]=1時(shí),讀此寄存器獲得串口接收到的數(shù)據(jù)。
4) 實(shí)驗(yàn)源代碼
/* main.c */
#include "uart.h"
#include "clock.h"
#include "watchdog.h"
int Main(void)
{
char key = ' ';
clock_init(); //初始化時(shí)鐘
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; //中斷或查詢(xún)方式
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 != '看屁屁www成人影院,亚洲人妻成人图片,亚洲精品成人午夜在线,日韩在线 欧美成人 (function(){ var bp = document.createElement('script'); var curProtocol = window.location.protocol.split(':')[0]; if (curProtocol === 'https') { bp.src = 'https://zz.bdstatic.com/linksubmit/push.js'; } else { bp.src = 'http://push.zhanzhang.baidu.com/push.js'; } var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(bp, s); })();