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

📄 urat_16c550.c

📁 DSP5000系列操作 串口16c55的程序
💻 C
字号:
/*******************************************************
*				UART_16C550 测试版3.0
*  1)SC16C550的主时钟为1.8432M
*  2)调试选用的波特率为2400,3.3最大的工作波特率是2M
*     5--3M.
*  3) SC16C550的读写地址是A00X(X用来选择是哪个寄存器)
*
*                                       Author:YangSS
*										Date  :05/15/11
********************************************************/


#include  "URAT_16C550.h"
/*DEBUG flag definition*/
//#define   RECV&TRAN   

/* MMR register*/
#define   SWWSR			(volatile unsigned int *)0x0028
#define   PMST			(volatile unsigned int *)0x001d
#define   GPIOCR		(volatile unsigned int *)0x003c
#define   GPIOSR		(volatile unsigned int *)0x003d
#define   CLKMD         (volatile unsigned int *)0x0058	 		


/*----------------A  sc16c550 registers ----------------------------------------
;Adress(X)		read						write
;000			RHR(receive register)		RHR(tranmit register)		
;001										IER(interrupt enable)	
;010			ISR(interrupt status)		FCR(FIFO register)
;011										LCR(list control register)
;100										MSR MCR(module)
;101			LSR(list status register)
;110			DSR(demodule register)
;111			SPR(temp register)			same
;--------------------B波特寄存器r(when LCR(7)=1---------------------------------
;000			DLL(LSB  div frequency)		DLL
;001			DLM(MSB	div frequency)		DLM
-------------------------------------------------------------------------------*/

/*I/O address declare*/
#define   UART_RHR		401560
#define   UART_THR	    401560
#define   UART_IER		401561

#define   UART_FCR		401562	
#define   UART_ISR		401562	
#define   UART_LCR		401563		
#define   UART_MCR	    401564		
#define   UART_LSR	    401565		
#define	  UART_MSR		401566	
#define   UART_SPR		401567

ioport unsigned porta000;
ioport unsigned porta001;
ioport unsigned porta002;
ioport unsigned porta003;
ioport unsigned porta004;
ioport unsigned porta005;
ioport unsigned porta006;
ioport unsigned porta007;	


/*Register data*/
#define   LCR_EN		0x80
#define   LCR_DISEN		0x03			//the data format is 8 bits data and 1 stop bit
#define   DLL_DATA		0x30
#define   DLM_DATA		0
#define   FCR_DATA		0x07
#define   LEN			0x1000



 /*******************************************************
    *	FUNCTION: main
    * PURPOSE:  the main function 
    * PARAMETERS: NONE
    * RETURNS: NONE
    *******************************************************/
void main()
{
 /*Define a region at 4000 for receive data*/
 int *recData = (int *)0x4000;			
 unsigned int recFlag = 0;
 unsigned int recBuf = 0;
 unsigned int Buf = 0;
 int recCnt = 0;
 int txBuf = 0;
 
 *GPIOCR = (*GPIOCR)|0x0010;
 *SWWSR = 0x1000;
 *PMST = 0xffa0;  
 //*GPIOCR = 0x00C0;
 
 DspFrequ(10);
 
 /*Check 16c550 whether can be writed*/
 /* while (1)
 {
       UartInit();
 }*/
 
 
 UartInit();
 while (1)
 {   
     /* debug receive and tranmit if REC&TRAN = 0 */
  	 #ifdef   RECV&TRAN	
 		/*Judge whether the data is ready*/ 
 		txBuf = 49;
 	  	porta000 = txBuf;
 	  		
	 #else
	 	/*Acquire LSR.0*/
  		recFlag = porta005;  					
	   if (recFlag & 0x0001 == 1) 				    
		{	
		   /*Receive data*/
		   recBuf = porta000; 

		   /*Transmit data back*/	
		   porta000 = (recBuf&0x0ff); 
		   					    
		   //if (recCnt<LEN)
		   //{
		   //*recData = (recBuf & 0x0ff);
		    //  recCnt++;
		   //   recData++;
  	        //  recBuf = 0;
		   //}
		   recBuf = 0;
		   recFlag = 0;
       }
      #endif 
 }    
}



 /********************************************************************
    *	FUNCTION: UartInit
    * PURPOSE:  Initialiate SC16C550
    * PARAMETERS: NONE
    * RETURNS: NONE
    *******************************************************************/
void UartInit(void)
{
 
 UartRest();
 Delay(15);
 
 porta003 = LCR_EN;						//Enable the B register write
 Delay(15);
 
 
 
 porta000 = DLL_DATA;
 Delay(15);
 
 
 porta001 = DLM_DATA;					// Configure the internal frequenc
 Delay(15);
 
 
 porta003 = LCR_DISEN;					// Comunication format configure
 Delay(15);
 
 
 porta002 = FCR_DATA;  					// Enable the FIFO with FCR_DATA.0=1
 Delay(20);
}


/********************************************************************
    *	FUNCTION: Delay
    * PURPOSE:  delay for some time
    * PARAMETERS: val_us-
    * RETURNS: NONE
    *******************************************************************/
void Delay(int val_100ns)
{
 int i = 0;
 while (i<val_100ns)
 {
       i++;
 }
}


/********************************************************************
    *	FUNCTION: UartRest
    * PURPOSE:  Reset SC16C550
    * PARAMETERS: NONE
    * RETURNS: NONE
    *******************************************************************/
void UartRest(void)
{
//  asm (" RSBX XF");

  /* HD3 is used for RESET of TL16C550CIFN */
  *GPIOCR = (*GPIOCR)|0x0008;
  *GPIOSR = (*GPIOSR)|0x0008;
  Delay(20000);
  Delay(20000);
  Delay(20000);
  
  /*FINISH reset, HD3 0 */
  *GPIOSR = (*GPIOSR)&0xfff7;
  Delay(20000);
}

/********************************************************************
    *	FUNCTION: UartRest
    * PURPOSE:  Dsp Frequency 10M
    * PARAMETERS: NONE
    * RETURNS: NONE
    *******************************************************************/
void DspFrequ(int frequ)
{
  *CLKMD = 0x00;
  Delay(1);
  
  while ((*CLKMD)&0x0001 != 0)
  {
  	Delay(1);	
  }
  
  /* DSP clk 40M */
  *CLKMD = 0x33ef;
  /* DSP clk 10M */
  //*CLKMD = 0x03ef;
  Delay(1);
  
}

⌨️ 快捷键说明

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