www.pudn.com > RS485_USART-ok.rar > 485.c, change:2007-09-13,size:3443b


 
 
 
#include "includes.h" 
 
COMM_REC_DATA_FIFO   USART_FIFO; 
 
 
void COMM_USART0_Interrupt_Handler(void)__irq 
{ 
	AT91C_BASE_AIC->AIC_IVR   = 0;            // Debug variant of vector read (protect mode is used)   
	                                          // Clear  timer #0 interrupt                             
	AT91C_BASE_AIC->AIC_ICCR  = 1 << AT91C_ID_US0; 
	AT91C_BASE_AIC->AIC_EOICR = 0;            // Signal end of interrupt          
	         
	USART_DATA_IRQ_Handle(AT91C_BASE_US0);	 
	     
} 
	   
 
 void US_Init(void) 
{ 
   
	// Init the US0 USART 
	// step 1 : Config the PIO 
    AT91F_PIO_CfgPeriph( 
                        AT91C_BASE_PIOA, // PIO controller base address 
                        ((unsigned int) AT91C_PA0_RXD0    ) | 
                        ((unsigned int) AT91C_PA3_RTS0    ) | 
                        ((unsigned int) AT91C_PA1_TXD0    ), // Peripheral A 
                        0); // Peripheral B 
	AT91F_US0_CfgPMC(); 
	 
	// step 2 : Reset the TX and RX 
	// 复位发送和接收功能 
	AT91F_US_ResetTx((AT91PS_USART)AT91C_BASE_US0); 
	AT91F_US_ResetRx((AT91PS_USART)AT91C_BASE_US0); 
     
	// 配置 RS485 
	AT91F_US_Configure (                          
                        (AT91PS_USART) AT91C_BASE_US0,    
                        AT91B_MCK,						 
                        AT91C_US_485_MODE ,              		// 模式积存器 
                        baud_rate ,                  	// 波特率 9600 
                        0); 
	 
	// 允许串口发送 
	AT91F_US_EnableTx ((AT91PS_USART)AT91C_BASE_US0); 
	// 允许串口接受 
	AT91F_US_EnableRx ((AT91PS_USART)AT91C_BASE_US0); 
 
	// 设置 USART0 中断 
	AT91F_AIC_ConfigureIt(AT91C_BASE_AIC,                          //基地址 
                          AT91C_ID_US0,                            	//ID 
                          PC_USART_INTERRUPT_LEVEL,                	//中断优先级为6 
                          AT91C_AIC_SRCTYPE_INT_HIGH_LEVEL,        	//内部高电平触发 
                          0);                 		//中断处理函数 
      AT91C_BASE_AIC->AIC_SVR[AT91C_ID_US0] = (unsigned int) COMM_USART0_Interrupt_Handler ; 
 
     
	// PDC   
	//AT91F_PDC_Open(AT91C_BASE_PDC_US0); 
	//AT91F_PDC_SetRx(AT91C_BASE_PDC_US0, PCRxBuf, DEBUG_RX_BUF_SIZE); 
	//AT91F_PDC_SetNextRx(AT91C_BASE_PDC_US0,  (char *)0, 0); 
	//PCRxBuf_Index = 0; 
     
	// 禁止该串口的所有中断 
	AT91F_US_DisableIt((AT91PS_USART)AT91C_BASE_US0, 0xffffffff); 
    // 只允许PDC接收缓冲区满中断,采用2缓冲区交替方式 
	AT91F_US_EnableIt((AT91PS_USART)AT91C_BASE_US0, AT91C_US_RXRDY); 
	AT91F_AIC_EnableIt (AT91C_BASE_AIC, AT91C_ID_US0); 
  
} 
 
 
void PUT_Char(INT8U *character) 
{ 
	//int achar; 
	//int flag; 
	//achar = character; 
	while(*character !='\0') 
		 
		{ 
			while( !AT91F_US_TxReady (AT91C_BASE_US0)); 
				AT91C_BASE_US0->US_THR = *character++; 
		}	 
} 
 
void USART_DATA_IRQ_Handle(AT91PS_USART pUSART) 
{ 
	INT8U buff_ptr; 
	INT32U status; 
	INT8U i; 
	buff_ptr = USART_FIFO.comm_ptr; 
	status = pUSART-> US_CSR; 
//	if(status & 0x00000001) //有一个字符未读取 
// 读取状态积存器/中断屏蔽积存器,以清除中断标志	 
	status = AT91C_BASE_US0->US_CSR & AT91C_BASE_US0->US_IMR; 
	if(status & AT91C_US_RXRDY) 
 
		       USART_FIFO.DATA_BUF[buff_ptr] = (INT8S)AT91F_US_GetChar(AT91C_BASE_US0); 
			    
			USART_FIFO.comm_ptr +=1; 
				buff_ptr = USART_FIFO.comm_ptr; 
				 
				if(buff_ptr == COMM_REC_DATA_FIFO_SIZE) 
					USART_FIFO.comm_ptr = 0; 
 
}