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

📄 uart.c

📁 USBN9603的DEMO程序
💻 C
字号:
/*--------------------------------------------------------------------------
 * Copyright (c) 2001 by National Semiconductor Corporation
 * All rights reserved.
 *<<<-----------------------------------------------------------------------
 * File Contents: uart.c - UART driver
 * Notes:
 *      o ...
 *      o ...
 *
 * Project: CR Peripherals Driver Set		
 *--------------------------------------------------------------------->>>*/

#include "..\include\all.h"
#include "..\loopback\command_api.h"


u_char RxBuffer[BUF_SIZE];
u_char TxHeadPtr=0, RxHeadPtr=0;
u_char TxTailPtr=0, RxTailPtr=0;
UARTStatus uartStatus = {0xff,0,0,0};

#define MIN_PRESCALER  1
#define MAX_PRESCALER  16
#define PRESCALER_STEP 0.5



/*<<<---------------------------------------------------------------------------------------------------
 * Function 	: 	UART_init     
 *				   	The method initialises the UART interface
 * [parameters:]   	None
 * [return:]       	None
 *-----------------------------------------------------------------------------------------------------*/


void UART_init (void) 
{
 	word c;
	
	UFRS = 0;
	UMDSL =0;
    TxHeadPtr = RxHeadPtr = TxTailPtr = RxTailPtr;
 
 	/*---------------------------------------------------------------------------*/
	/* select alternate port function											 */
	/*---------------------------------------------------------------------------*/

   PBALT |= (BIT0 | BIT1 | BIT2);     // URXD, UTXD, USCLK

     /*---------------------------------------------------------------------------*/
	/*  enable interrupts														 */
	/*---------------------------------------------------------------------------*/

	UICTRL |=  UERI;
	c = URBUF;				//empty RX buffer

}

/*<<<---------------------------------------------------------------------------------------------------
 * Function 	: 	UART_config     
 *				   	The method configueres the UART to specified parameters
 *					format - number of data bits (DATA_BITS_8 |	DATA_BITS_7 | DATA_BITS_9 | LOOPBACK)
 *					parity - (NONE | ODD_PARITY | EVEN_PARITY | MARK | SPACE)
 *					flowcntl - boolean indicating wheter flowcontrol mechanism should be used
 *					stopbit - number of stopbits (STOP_BITS_1 |	STOP_BITS_2)
 *					baudrate - baudrate to be used  (UART_2400 | UART_9600 | UART_19200 | UART_115200)
 * [parameters:]   	None
 * [return:]       	None
 *-----------------------------------------------------------------------------------------------------*/


void UART_config	(uart_data_bits_t format, uart_parity_t parity, boolean flowcntl, 
					uart_stop_bits_t stopbit, uart_br_t baudrate)

{

	u_char FrameSelect = 0, ModeSelect;

	
	
	UPSR =  0xc8; //Psr;
	UBAUD =  0x4 ;//(u_char)Div;
	/*---------------------------------------------------------------------------*/
	/* Configure data bits - bits 0:1											 */
	/*---------------------------------------------------------------------------*/
	
	FrameSelect &= 0xfc;
	FrameSelect |= format;

	/*---------------------------------------------------------------------------*/
	/* Configure parity - bits 4:5												 */
	/*---------------------------------------------------------------------------*/
	
	if (parity == NONE)
			FrameSelect &= ~BIT6;//disable parity
	else {
		FrameSelect &= 0xcf; 
		switch(parity)   {
			case ODD_PARITY:
				FrameSelect |= 0<<4;    
				break;
			case EVEN_PARITY:
				FrameSelect |= 1<<4;    
				break;			
			case MARK:
				FrameSelect |= 2<<4;    
				break;						
			case SPACE:
				FrameSelect |= 3<<4;    
				break;									
		}
		FrameSelect |= BIT6;  		//enable parity
	}

	/*---------------------------------------------------------------------------*/
	/* Configure stop bits - bit 2												 */
	/*---------------------------------------------------------------------------*/

	FrameSelect &= 0xfb;
	FrameSelect |= stopbit<<2;    

    UFRS = FrameSelect;


	ModeSelect = UMDSL;
	ModeSelect &= 0xbf;	
	
	/*---------------------------------------------------------------------------*/
	/* Configure flow control - bit 6											 */
	/*---------------------------------------------------------------------------*/

	ModeSelect |= flowcntl<<6;
	UMDSL = ModeSelect;
    
 	/*---------------------------------------------------------------------------*/
	/* Enable USART																 */
	/*---------------------------------------------------------------------------*/	
    UMCTRL = BIT0;
}	


/*<<<---------------------------------------------------------------------------------------------------
 * Function 	: 	UART_handler     
 *				   	This is a UART interrupt service routine. It checks whether the UART is ready to receice
 *					or transmit, and perform Read/Write of ine byte from RxFIFo/to TxFIFO
 * [parameters:]   	None
 * [return:]       	None
 *-----------------------------------------------------------------------------------------------------*/

#pragma interrupt(UART_handler , save_regs=int_regs)

void UART_handler()
{
	u_char status = UICTRL;
	_disable_();

	if (status & BIT1)  { //check RBF bit
		if (uartStatus.command == 0xff)
		{
			uartStatus.command = URBUF;
			uartStatus.recBytes = 1;
		}
		else
		{ 
			if (uartStatus.recBytes == 1)
			{
				uartStatus.regAdd = URBUF;
				if (uartStatus.command == READ_USB_REG)
				{
					UTBUF = read_usb(uartStatus.regAdd);
					uartStatus.command = 0xff;
					uartStatus.recBytes = 0;
				}
				else
					uartStatus.recBytes++;
			}
			else
			{
				uartStatus.regVal = URBUF;
				write_usb(uartStatus.regAdd,uartStatus.regVal);
				uartStatus.command = 0xff;
				uartStatus.recBytes = 0;
			}
		}
	}
	_enable_();
}


/*<<<---------------------------------------------------------------------------------------------------
 * Function 	: 	UART_close     
 *				   	The method closes the UART connection and restores original alternate function settings
 * [parameters:]   	None
 * [return:]       	None
 *-----------------------------------------------------------------------------------------------------*/

void UART_close()
{

	/*---------------------------------------------------------------------------*/
	/* disable USART															 */
	/*---------------------------------------------------------------------------*/
    UMCTRL = 0;

	/*---------------------------------------------------------------------------*/
	/* reset interrupt flags													 */
	/*---------------------------------------------------------------------------*/
    UICTRL = 0;
	
	
	/*---------------------------------------------------------------------------*/
	/* Disable clock															 */
	/*---------------------------------------------------------------------------*/
	
	UPSR = 0;
      
	/*---------------------------------------------------------------------------*/
	/* select normal port function											 	 */
	/*---------------------------------------------------------------------------*/

    PBALT &= ~(BIT0 | BIT1 | BIT2);     // UEXD, UTXD, USCLK

}









⌨️ 快捷键说明

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