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

📄 sc16c554.c

📁 atmel 9200 外扩16C554的驱动测试程序
💻 C
字号:
#include "AT91C_MCI_Device.h"

/* SC16C554 */
#define SC16C554_BASE			( (unsigned char *) 0x30000000 )
#define SC16C554_PORTA_BASE		( (unsigned char *) 0x30000300 )
#define SC16C554_PORTB_BASE		( (unsigned char *) 0x30000200 )
#define SC16C554_PORTC_BASE		( (unsigned char *) 0x30000100 )
#define SC16C554_PORTD_BASE		( (unsigned char *) 0x30000000 )
char *SC16C554_RegPtrA = (char *)SC16C554_PORTA_BASE;
char *SC16C554_RegPtrB = (char *)SC16C554_PORTB_BASE;
char *SC16C554_RegPtrC = (char *)SC16C554_PORTC_BASE;
char *SC16C554_RegPtrD = (char *)SC16C554_PORTD_BASE;

extern void DelayMs(unsigned char Count);
/************************************************************************/
#define Port_RHR				0
#define Port_THR				0

#define Port_IER				1
#define Port_ISR				2
#define Port_FCR				2
#define Port_LCR				3
#define Port_MCR				4
#define Port_LSR				5
#define Port_MSR				6
#define Port_SR				7

#define Port_DLL				0
#define Port_DLM				1
/************************************************************************/
#define BAUD_2400_DLL				0xC0
#define BAUD_2400_DLM    			0x0

#define BAUD_4800_DLL				0x60
#define BAUD_4800_DLM    			0x0

#define BAUD_9600_DLL				0x30
#define BAUD_9600_DLM    			0x0

#define BAUD_19200_DLL				0x18
#define BAUD_19200_DLM   			0x0

#define BAUD_38400_DLL				0x0C
#define BAUD_38400_DLM   			0x0

#define BAUD_57600_DLL				0x08
#define BAUD_57600_DLM   			0x0

#define BAUD_76800_DLL				0x06
#define BAUD_76800_DLM   			0x0

#define BAUD_115200_DLL				0x04
#define BAUD_115200_DLM  			0x0

#define BAUD_153600_DLL				0x03
#define BAUD_153600_DLM  			0x0

#define BAUD_230400_DLL				0x02
#define BAUD_230400_DLM  			0x0

#define BAUD_460800_DLL				0x01
#define BAUD_460800_DLM  			0x0
/************************************************************************/
#define PORTA		0
#define PORTB		1
#define PORTC		2
#define PORTD		3
/************************************************************************/
void SC16C554_init(void)
{
	
	*(SC16C554_RegPtrA + Port_LCR) = 0x80;
	*(SC16C554_RegPtrA + Port_DLL) = BAUD_115200_DLL;
	*(SC16C554_RegPtrA + Port_DLM) = BAUD_115200_DLM;
	*(SC16C554_RegPtrA + Port_LCR) = 0x03;   // N,8,1
	*(SC16C554_RegPtrA + Port_FCR) = 0x00;
	*(SC16C554_RegPtrA + Port_MCR) = 0x08;
	*(SC16C554_RegPtrA + Port_IER) = 0x05;
	
	
	*(SC16C554_RegPtrB + Port_LCR) = 0x80;
	*(SC16C554_RegPtrB + Port_DLL) = BAUD_115200_DLL;
	*(SC16C554_RegPtrB + Port_DLM) = BAUD_115200_DLM;
	*(SC16C554_RegPtrB + Port_LCR) = 0x03;   // N,8,1
	*(SC16C554_RegPtrB + Port_FCR) = 0x00;
	*(SC16C554_RegPtrB + Port_MCR) = 0x08;
	*(SC16C554_RegPtrB + Port_IER) = 0x05;
	
	*(SC16C554_RegPtrC + Port_LCR) = 0x80;
	*(SC16C554_RegPtrC + Port_DLL) = BAUD_115200_DLL;
	*(SC16C554_RegPtrC + Port_DLM) = BAUD_115200_DLM;
	*(SC16C554_RegPtrC + Port_LCR) = 0x03;   // N,8,1
	*(SC16C554_RegPtrC + Port_FCR) = 0x00;
	*(SC16C554_RegPtrC + Port_MCR) = 0x08;
	*(SC16C554_RegPtrC + Port_IER) = 0x05;
	
	*(SC16C554_RegPtrD + Port_LCR) = 0x80;
	*(SC16C554_RegPtrD + Port_DLL) = BAUD_115200_DLL;
	*(SC16C554_RegPtrD + Port_DLM) = BAUD_115200_DLM;
	*(SC16C554_RegPtrD + Port_LCR) = 0x03;   // N,8,1
	*(SC16C554_RegPtrD + Port_FCR) = 0x00;
	*(SC16C554_RegPtrD + Port_MCR) = 0x08;
	*(SC16C554_RegPtrD + Port_IER) = 0x05;
	
}
/************************************************************************/
void Reset16C554(void)
{
	AT91F_PIO_SetOutput(AT91C_BASE_PIOD, AT91C_PIO_PD8);
	DelayMs(50);
	AT91F_PIO_ClearOutput(AT91C_BASE_PIOD, AT91C_PIO_PD8);
	DelayMs(50);
}
/************************************************************************/
void SC16C554_Send(unsigned char Port,unsigned char *buf,unsigned short len)
{
	unsigned short i;
	if(Port == PORTA)
	{
		for(i=0;i<len;i++)
		{
			*(SC16C554_RegPtrA + Port_THR) = buf[i];		
		}
	}
	if(Port == PORTB)
	{
		for(i=0;i<len;i++)
		{
			*(SC16C554_RegPtrB + Port_THR) = buf[i];		
		}
	}
	if(Port == PORTC)
	{
		for(i=0;i<len;i++)
		{
			*(SC16C554_RegPtrC + Port_THR) = buf[i];		
		}
	}
	if(Port == PORTD)
	{
		for(i=0;i<len;i++)
		{
			*(SC16C554_RegPtrD + Port_THR) = buf[i];		
		}
	}
}
/************************************************************************/
unsigned char SC16C554_Receive(unsigned char Port)
{
	unsigned char TempChar;
	unsigned char IntStatus;
	
	if(Port == PORTA)
	{
		TempChar = *(SC16C554_RegPtrA + Port_RHR);
		IntStatus = *(SC16C554_RegPtrA + Port_ISR);
	}
	
	if(Port == PORTB)
	{
		TempChar = *(SC16C554_RegPtrB + Port_RHR);
		IntStatus = *(SC16C554_RegPtrB + Port_ISR);
	}
	
	if(Port == PORTC)
	{
		TempChar = *(SC16C554_RegPtrC + Port_RHR);
		IntStatus = *(SC16C554_RegPtrC + Port_ISR);
	}
	
	if(Port == PORTD)
	{
		TempChar = *(SC16C554_RegPtrD + Port_RHR);
		IntStatus = *(SC16C554_RegPtrD + Port_ISR);
	}
	return TempChar;
}
/************************************************************************/

⌨️ 快捷键说明

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