⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 uart.c

📁 一款交换机BSP开发代码
💻 C
📖 第 1 页 / 共 2 页
字号:
/*  B. Sellew         04/23/98     Created initial version.                 */
/*                                                                          */
/****************************************************************************/
VOID  UART_Put_Char(UCHAR ch)
{

    
   if(!VER_FLAG)
   {
	   /* Wait until the transmitter buffer is empty */
   		 while (!(READ2_USR_1() & USR_TXRDY));

    		/* Transmit the character */
    		SET2_UTB_1(ch);
   }
   else
   {
       	/* Wait until the transmitter buffer is empty */
  	  while (!(READ2_USR_2() & USR_TXRDY));

    		/* Transmit the character */
    		SET2_UTB_2(ch);
   }

}

/****************************************************************************/
/* FUNCTION                                                                 */
/*                                                                          */
/*    UART_Put_String                                                       */
/*                                                                          */
/* DESCRIPTION                                                              */
/*                                                                          */
/*    This writes a null-terminated string out to the serial port.          */
/*                                                                          */
/* CALLED BY                                                                */
/*                                                                          */
/*    Application                                                           */
/*                                                                          */
/* CALLS                                                                    */
/*                                                                          */
/*    UART_Put_Char                                                         */
/*                                                                          */
/* INPUTS                                                                   */
/*                                                                          */
/*    CHAR *      :   String to be written to the serial port.              */
/*                                                                          */
/* OUTPUTS                                                                  */
/*                                                                          */
/*    none                                                                  */
/*                                                                          */
/* HISTORY                                                                  */
/*                                                                          */
/*        NAME             DATE                  REMARKS                    */
/*                                                                          */
/*      B. Sellew        06-06-98         Created Initial version.          */
/*                                                                          */
/****************************************************************************/
VOID UART_Put_String(UCHAR *str)
{
    for (; *str != 0; str++)
		if (*str == '\n')
		{
			UART_Put_Char('\r');
			UART_Put_Char('\n');
		}
		else
			UART_Put_Char(*str);
}

/****************************************************************************/
/* FUNCTION                                                                 */
/*                                                                          */
/*    UART_Get_Char                                                         */
/*                                                                          */
/* DESCRIPTION                                                              */
/*                                                                          */
/*    This function reads the last received character from the UART.        */
/*                                                                          */
/* CALLED BY                                                                */
/*                                                                          */
/*    Application                                                           */
/*                                                                          */
/* CALLS                                                                    */
/*                                                                          */
/*    none                                                                  */
/*                                                                          */
/* INPUTS                                                                   */
/*                                                                          */
/*    none                                                                  */
/*                                                                          */
/* OUTPUTS                                                                  */
/*                                                                          */
/*    CHAR  :  Character read                                               */
/*                                                                          */
/* HISTORY                                                                  */
/*                                                                          */
/*         NAME               DATE              REMARKS                     */
/*                                                                          */
/*       B. Sellew          06-03-98      Created initial version.          */
/*                                                                          */
/****************************************************************************/
UCHAR  UART_Get_Char(VOID)
{

UCHAR    ch = NU_NULL;

    if ((uart_buf.status == UART_BUFFER_FULL) || 
        (uart_buf.status == UART_BUFFER_DATA))
    {
        /* Store the character to be returned */
        ch = *(uart_buf.read++);
 
        /* If read pointer is at end, wrap it around */
        if (uart_buf.read > uart_buf.tail)
            uart_buf.read = uart_buf.head;
      
        /* Set the status to reflect removal of the character */
        if (uart_buf.write == uart_buf.read)
            uart_buf.status = UART_BUFFER_EMPTY;
        else
            uart_buf.status = UART_BUFFER_DATA;
    }

    return (ch);

}    


int UART_Get_Char1(UCHAR *c)
{
	int i,j;
	//for ( i = 0; i < 10000 ; i ++)	// Wait 5S
	for ( i = 0; i < 100 ; i ++)	// Wait 5S
	{
		if (UART_Data_Ready())
		{
			*c = UART_Get_Char();
			return 1;				// Get Data
		}
		
		Wait_10ms();
	}
	*c = 0;
	return 0;						// No Data
}


int UART_Get_Bytes(UCHAR *buf,int cnt,int block)
{
	int i;

	i = cnt * block;
	while(i)
	{
		if (!UART_Get_Char1(buf))
			return NU_FALSE;
		i --;
		buf ++;
	}
	return NU_TRUE;
}





/****************************************************************************/
/* FUNCTION                                                                 */
/*                                                                          */
/*    UART_Data_Ready                                                       */
/*                                                                          */
/* DESCRIPTION                                                              */
/*                                                                          */
/*    This function checks to see if there are any characters in the        */
/*    receive buffer.  A status value is returned indicating whether        */
/*    characters are present in the receive buffer.                         */
/*                                                                          */
/* CALLED BY                                                                */
/*                                                                          */
/*    Application                                                           */
/*                                                                          */
/* CALLS                                                                    */
/*                                                                          */
/*    none                                                                  */
/*                                                                          */
/* INPUTS                                                                   */
/*                                                                          */
/*    none                                                                  */
/*                                                                          */
/* OUTPUTS                                                                  */
/*                                                                          */
/*    STATUS                                The status indicates the        */
/*                                          presence of characters.         */
/*                                                                          */
/* HISTORY                                                                  */
/*                                                                          */
/*         NAME               DATE              REMARKS                     */
/*                                                                          */
/*       B. Sellew          06-03-98      Created initial version.          */
/*                                                                          */
/*                                                                          */
/****************************************************************************/
STATUS UART_Data_Ready(VOID)
{
    if((uart_buf.status == UART_BUFFER_FULL) ||
       (uart_buf.status == UART_BUFFER_DATA))
        return NU_TRUE;
    else
        return NU_FALSE;
}

/****************************************************************************/
/* FUNCTION                                                                 */
/*                                                                          */
/*    UART_Set_Baud_Rate                                                    */
/*                                                                          */
/* DESCRIPTION                                                              */
/*                                                                          */
/*    This function sets the UART buad rate.                                */
/*                                                                          */
/* CALLED BY                                                                */
/*                                                                          */
/*    UART_Init_Port                                                        */
/*                                                                          */
/* CALLS                                                                    */
/*                                                                          */
/*    Serial port macros                                                    */
/*    NU_Local_Control_Interrupts                                           */
/*                                                                          */
/* INPUTS                                                                   */
/*                                                                          */
/*    UNSIGNED   :  The new baud rate.                                      */
/*                                                                          */
/* OUTPUTS                                                                  */
/*                                                                          */
/*    none                                                                  */
/*                                                                          */
/* HISTORY                                                                  */
/*                                                                          */
/*    NAME             DATE           REMARKS                               */
/*                                                                          */
/*  B. Sellew         04/23/98     Created initial version.                 */
/*                                                                          */
/****************************************************************************/
VOID  UART_Set_Baud_Rate(ULONG baud_rate)
{

unsigned short    baud_div;           /* baud rate divisor */
//INT               int_level;          /* old interrupt level */

    /* Lockout interrupts */
//    int_level = NU_Local_Control_Interrupts(NU_DISABLE_INTERRUPTS);

    /* Set the baud rate */
    if ((baud_rate >= 300) || (baud_rate <= 390625))
    {
       if(!VER_FLAG)
       {
       	 baud_div = SYSTEM_CLOCK / (32 * baud_rate);
        	SET2_UBG1_1((UCHAR)(baud_div >> 8));
        	SET2_UBG2_1((UCHAR)(baud_div));
       }
	else
	 {
	        baud_div = 18;//18 for 33M
        	SET2_UBG1_2(0x0);
        	SET2_UBG2_2(0x12);
	 }
    }

    /* Restore interrupts to previous level */
//    NU_Local_Control_Interrupts(int_level);

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -