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

📄 uart_test.c

📁 DM642串口的测试代码还有PC机的接受与发送程序
💻 C
字号:
//#define UARTA_BASE_ADDR		0x90080000
#define UARTA_BASE_ADDR		0xa0000000
#define ORDER				1
#define RBR					*((unsigned char *)(UARTA_BASE_ADDR+0*ORDER))	
#define THR					*((unsigned char *)(UARTA_BASE_ADDR+0*ORDER))	
#define IER					*((unsigned char *)(UARTA_BASE_ADDR+1*ORDER))
#define IIR					*((unsigned char *)(UARTA_BASE_ADDR+2*ORDER))	
#define FCR					*((unsigned char *)(UARTA_BASE_ADDR+2*ORDER))	
#define LCR					*((unsigned char *)(UARTA_BASE_ADDR+3*ORDER))
#define MCR					*((unsigned char *)(UARTA_BASE_ADDR+4*ORDER))
#define LSR					*((unsigned char *)(UARTA_BASE_ADDR+5*ORDER))
#define MSR					*((unsigned char *)(UARTA_BASE_ADDR+6*ORDER))
#define SCR					*((unsigned char *)(UARTA_BASE_ADDR+7*ORDER))
#define DLL					*((unsigned char *)(UARTA_BASE_ADDR+0*ORDER))
#define DLM					*((unsigned char *)(UARTA_BASE_ADDR+1*ORDER))
void wait(int nWait);//Hello PC!,Over|
char cString[17]={ "abcdefghijklmnop" },cReceive,cBuffer[17],cAnswer[16]={"Oh,you say"};
int bReceive,nLen;
main()
{
   
   	unsigned int uWork;
   	unsigned int test;//by oy
   	char teststring;//by oy
	int i,k;
    LCR = 0x80;
	//LCR=0x55;//
	//LCR=0xaa;//
	//DLL =0x55;//
	//DLL =0xaa;//
    DLL =10;    //BANDRATE
	DLM = 0x00;  
    LCR = 0x03;    
    FCR = 0x57;
    IER = 0x00;   /*8 bit data 115200 bps 1 bit stop 1 bit start */   
    bReceive=0;
/*    for(;;)
    {  
    	while(LSRA & 0x40 == 0);
    	
	    	THRA=;
    }        
  */
  while ( 1 )
	{
		if ( bReceive==0 )
		{
			//while(1)
			//{
			for ( i=0;i<16;i++ )
			{
				do
				{ 
					uWork=LSR;
				} while ( uWork&0x040 != 0x040 );
				THR=cString[i];
				//THR=0x60;
				teststring=cString[i];//test by oy
				wait(1024);		
			}
			//}
			
		}
		else
		{
			for ( i=0;i<10;i++ )
			{
				do
				{ 
					uWork=LSR;
				} while ( uWork&0x040 != 0x040 );
				THR=cAnswer[i];
				wait(1024);		
			}
			do
			{ 
				uWork=LSR;
			} while ( uWork&0x040 != 0x040 );
			THR='\"';
			for ( i=0;i<nLen;i++ )
			{
				do
				{ 
					uWork=LSR;
				} while ( uWork&0x040 != 0x040 );
				THR=cBuffer[i];
				wait(1024);		
			}
			do
			{ 
				uWork=LSR;
			} while ( uWork&0x040 != 0x040 );
			THR='\"';
			wait(1024);		
			for ( i=9;i<16;i++ )
			{
				do
				{ 
					uWork=LSR;
				} while ( uWork&0x040 != 0x040 );
				THR=cString[i];
				wait(1024);		
			}
		}
		k=0; bReceive=0;
		
		while ( 1 )
		{
			do
			{
				uWork=LSR;
				//test = uWork & 1;
			} while ( (uWork & 1)==0 );
			cReceive=RBR;
			cBuffer[k]=cReceive&0x0ff; 
			if ( cReceive=='.' )
			{
				cBuffer[k+1]='\0';
				nLen=k+1;
				bReceive=1;
				break;
			}
			k++; k%=16;
		}
	}
  
} 

void wait(int nWait)
{
	int i,j,k=0;
	for ( i=0;i<nWait;i++ )
		for ( j=0;j<64;j++ )
			k++;
}

⌨️ 快捷键说明

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