如下程序?qū)崿F(xiàn)的是開發(fā)板從PC端口接受一個字符,并把該字符加1后發(fā)送會PC端口。(在本開發(fā)板測試成功)對于不同的主頻要設置好UBRDIV0的值,在技術文檔中有這個值的計算公式。
#define ULCON0 (*(volatile unsigned *)0x50000000) //UART 0 Line control
#define UCON0 (*(volatile unsigned *)0x50000004) //UART 0 Control
#define UFCON0 (*(volatile unsigned *)0x50000008) //UART 0 FIFO control
#define UMCON0 (*(volatile unsigned *)0x5000000c) //UART 0 Modem control
#define UTRSTAT0 (*(volatile unsigned *)0x50000010) //UART 0 Tx/Rx status
#define UBRDIV0 (*(volatile unsigned *)0x50000028) //UART 0 Baud rate divisor
#define UTXH0 (*(volatile unsigned char *)0x50000020) //UART 0 Transmission Hold
#define URXH0 (*(volatile unsigned char *)0x50000024) //UART 0 Receive buffer
#define GPHCON (*(volatile unsigned *)0x56000070)
#define GPHDAT (*(volatile unsigned *)0x56000074)
#define GPHUP (*(volatile unsigned *)0x56000078)
int Main()
{
unsigned long TXH0READY = 0x2;
unsigned long RXH0READY = 0x1;
//init uart
GPHCON |= 0xa0;
GPHUP |= 0x0c;//PULLUP is enable;
//init uart controllor
ULCON0 = 0x03;
UCON0 = 0x245;
UFCON0 = 0x00;
UMCON0 = 0x00;
UBRDIV0= 26;
while(1)
{
while(!(UTRSTAT0 & RXH0READY));
c = URXH0;
while(!(UTRSTAT0 & TXH0READY));//wait
UTXH0 = c+1;
}
while(1);
return(0);
}