UART总结

来源:互联网 发布:php yaf导出excel表格 编辑:程序博客网 时间:2024/06/14 04:22

UART的 设置和使用

通用异步收发传输器(Universal Asynchronous Receiver/Transmitter),通常称作UART,是一种异步收发传输器。将资料由串行通信并行通信间作传输转换,作为并行输入成为串行输出的芯片,通常集成于其他通讯接口的连结上。其原理就是,串并转换和并串转换。

基本结构:




部分代码代码展示:

INT32U UART2Init(INT32U baudrate)
{  

INT32U Fdiv;

PCONP |= (1 << 24);
    
    PINSEL0 |= 0x00500000;                                          /* RxD2 and TxD2 */
    
    U2LCR = 0x83;                                            /* 8 bits, 1 Stop bit */

    Fdiv = ( Fpclk / 16 * 5 / 7) / baudrate ;                       //波特率   6=2+5
    U2DLM = Fdiv / 256;

    U2DLL = Fdiv % 256;

U2LCR = 0x1B;                                                   /* DLAB = 0 */

    U2FCR = 0x07;                                                   /* Enable and reset TX and RX FIFO. */

if ( Install_IRQ( UART2_INT, (void *)UART2_Handler, UART_PRIO ) == FALSE )
{
return FALSE;
    }

    U2IER = IER_RBR | IER_THRE | IER_RLS;                           /* Enable UART2 interrupt */
return TRUE;
}

中断处理:

void UART2Handler (void) __irq 
{
  BYTE IIRValue, LSRValue;
  BYTE Dummy = Dummy;


 OS_ENTER_CRITICAL();
  IIRValue = U2IIR;
    
  IIRValue >>= 1; /* skip pending bit in IIR */
  IIRValue &= 0x07; /* check bit 1~3, interrupt identification */
  if ( IIRValue == IIR_RLS ) /* Receive Line Status */
  {
LSRValue = U2LSR;
/* Receive Line Status */
if ( LSRValue & (LSR_OE|LSR_PE|LSR_FE|LSR_RXFE|LSR_BI) )
{
 /* There are errors or break interrupt */
 /* Read LSR will clear the interrupt */
 UART2Status = LSRValue;
 Dummy = U2RBR;/* Dummy read on RX to clear 
interrupt, then bail out */
 IDISABLE;
 VICVectAddr = 0;/* Acknowledge Interrupt */
 return;
}
if ( LSRValue & LSR_RDR )/* Receive Data Ready */
{
 /* If no error on RLS, normal ready, save into the data buffer. */
 /* Note: read RBR will clear the interrupt */
 UART1Buffer[UART2Count] = U2RBR;
 UART2Count++;
 if ( UART2Count == BUFSIZE )
 {
UART2Count = 0;/* buffer overflow */
 }
}
  }
  else if ( IIRValue == IIR_RDA ) /* Receive Data Available */
  {
/* Receive Data Available */
UART2Buffer[UART1Count] = U1RBR;
UART2Count++;
if ( UART2Count == BUFSIZE )
{
 UART2Count = 0;/* buffer overflow */
}
  }
  else if ( IIRValue == IIR_CTI ) /* Character timeout indicator */
  {
/* Character Time-out indicator */
UART2Status |= 0x100;/* Bit 9 as the CTI error */
  }
  else if ( IIRValue == IIR_THRE ) /* THRE, transmit holding register empty */
  {
/* THRE interrupt */
LSRValue = U2LSR;/* Check status in the LSR to see if
valid data in U0THR or not */
if ( LSRValue & LSR_THRE )
{
 UART2TxEmpty = 1;
}
else
{
 UART2TxEmpty = 0;
}
  }
    
  OS_EXIT_CRITICAL();
  VICVectAddr = 0; /* Acknowledge Interrupt */
}

发送:

void UART2Send(INT8U *BufferPtr, INT32U Length)
{
void  *Msg = NULL;
INT8U UART2_TxEmpty; 

while(Length!=0)
{
                while ( !(UART0TxEmpty & 0x01) );
 U0THR = *BufferPtr;
 UART0TxEmpty = 0;/* not empty in the THR until it shifts out */
 BufferPtr++;
 Length--;
            }

}

波特率的设置:

波特率的计算按照计算公式进行,在设置最高波特率时一定要考虑模拟串口程序代码的执行时间,该定时时间必须大于模拟串口的程序的规定时间。单片机的执行速度越快,则可以实现更高的串口通讯速度。

UARTn除数锁存是波特率发生器的一部分,它保存了用于产生波特率时钟的APB时钟(PCLK)分频值,波特率时钟必须是目标波特率的16倍。UnDLL和UnDLM寄存器一起构成一个16位除数。其中,UnDLL包含的是除数的低8位,UnDLM包含的是除数的高8位。0x0000被看作是0x0001,因为除数是不允许为0的。在访问UARTn除数锁存寄存器时,除数锁存访问位(DLAB)必须为1。、

例:使用上面的“UARTn波特率”公式,我们可以确定:当PCLK=20MHz,UnDL=93 (UnDLM=0x00和UnDLL=0x5D),DIVADDVAL=2且MULVAL=5时,系统将可以得到波特率为9600波特的UARTn。



原创粉丝点击