功用剖析
寄存器剖析
代碼剖析
///////////////////////////////////////////////////////////////////////UART 初始化/////////////////////////////////////////////////////////////////////////////////////
voidUart_Init(int pclk,int baud)
{
int i;
if(pclk == 0)
pclk = PCLK; //APB IS USED FOR APB
rUFCON0 = 0x0;//封闭UART0的FIFO
rUFCON1 = 0x0;//封闭UART1的FIFO
rUFCON2 = 0x0;//封闭UART2的FIFO
rUMCON0 = 0x0;//封闭UART0主动流操控AFC (一般不进行握手协议操控)
rUMCON1 = 0x0;//封闭UART1主动流操控AFC (UART2没有主动流操控功用)
//UART0
rULCON0 = 0x3; //Normal,No parity,1 stop bit,8 bits da
rUCON0 = 0x245;
Transmit MODE:发送形式有恳求中止发送和恳求DMA发送
TX interrupt Type :当发送坚持寄存器为空时(Non-FIFO MODE)或到达TX FIFO触发水平常触发中止的电平类型
(TX FIFO触发水平:00 = Empty 01 = 4-byte 10 = 8-byte 11 = 12-byte in UFCONn)
Receive MODE 接纳形式有恳求中止读取和恳求DMA读取
RX interrupt Type :当接纳坚持寄存器为空时(Non-FIFO MODE)或到达RX FIFO触发水平常触发中止的电平类型(RX FIFO触发水平:00 = 4-byte 01 = 8-byte 10 = 12-byte 11 = 16-byte in UFCONn)
rUBRDIV0=( (int)(pclk/16/baud) -1 );
//UART1
rULCON1 = 0x3;
rUCON1 = 0x245;
rUBRDIV1=( (int)(pclk/16/baud) -1 );
//UART2
rULCON2 = 0x3;
rUCON2 = 0x245;
rUBRDIV2=( (int)(pclk/16/baud) -1 );
for(i=0;i<100;i++);
}
总结UART的操控寄存器:有ULCON、UCON、UFCON、UMCON、UBRDIV
///////////////////////////////////////////////////////////////////////UART 初始化////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////UART 发送////////////////////////////////////////////////////////////////////////////////////
voidUart_SendByte(int da
{
if(whichUart==0)
{
if(da
{
while(!(rUTRSTAT0 & 0x2));
non FIFO=1 Byte BUFFER REGISTER(HOLDING REGISTER)+SHIFTER REGISTER
FIFO mode = 16 Byte BUFFER REGISTER(FIFO REGISTER)+SHIFTER REGISTER
Delay(10); //because the slow response of hyper_terminal
WrUTXH0( );
}
while(!(rUTRSTAT0 & 0x2)); //Wait until THR is empty.
Delay(10);
WrUTXH0(da
}
else if(whichUart==1)
{
if(da
{
while(!(rUTRSTAT1 & 0x2));
Delay(10); //because the slow response of hyper_terminal
rUTXH1 = ;
}
while(!(rUTRSTAT1 & 0x2)); //Wait until THR is empty.
Delay(10);
rUTXH1 = da
}
else if(whichUart==2)
{
if(da
{
while(!(rUTRSTAT2 & 0x2));
Delay(10); //because the slow response of hyper_terminal
rUTXH2 = ;
}
while(!(rUTRSTAT2 & 0x2)); //Wait until THR is empty.
Delay(10);
rUTXH2 = da
}
}
///////////////////////////////////////////////////////////////////////UART 发送////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////UART 接纳////////////////////////////////////////////////////////////////////////////////////
charUart_Getch(void)
{
if(whichUart==0)
{
while(!(rUTRSTAT0 & 0x1)); //Receive da
return RdURXH0();
}
else if(whichUart==1)
{
while(!(rUTRSTAT1 & 0x1)); //Receive da
return RdURXH1();
}
else if(whichUart==2)
{
while(!(rUTRSTAT2 & 0x1)); //Receive da
return RdURXH2();
}
}
///////////////////////////////////////////////////////////////////////UART 接纳////////////////////////////////////////////////////////////////////////////////////
总结UART有状况寄存器UTRSTAT、UERSTAT、UFSTAT、UMSTAT
有数据寄存器UTXH和URXH