www.pudn.com > RS485_USART-ok.rar > 复件 usart.c, change:2007-09-13,size:3702b


 
#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) 
{ 
   
	AT91F_PIO_CfgPeriph( 
		AT91C_BASE_PIOA, // PIO controller base address 
		((unsigned int) AT91C_PIO_PA0) |//  USART 0 Receive Data| 
		((unsigned int) AT91C_PIO_PA1) |//  USART 0 Receive Data, // Peripheral A 
		((unsigned int) AT91C_PIO_PA3), //  USART 0 Receive Data 
		0); // Peripheral B 
 
	AT91F_US0_CfgPMC(); 
 
   //* Disable interrupts 
    AT91C_BASE_US0->US_IDR = (unsigned int) -1;		//		 禁止0US0的所有中断 
 
   //* Reset receiver and transmitter 
   AT91C_BASE_US0->US_CR = AT91C_US_RSTRX | AT91C_US_RSTTX | AT91C_US_RXDIS | AT91C_US_TXDIS ; 
 
   //* Define the baud rate divisor register 
   AT91F_US_SetBaudrate(AT91C_BASE_US0, (unsigned int)AT91B_MCK, (unsigned int)baud_rate); 
 
  
 
   //* Clear Transmit and Receive Counters         
   //AT91F_PDC_Open((AT91PS_PDC) &(AT91C_BASE_US0->US_RPR)); 
 
   //* Define the USART mode 
   AT91C_BASE_US0->US_MR =  
   		AT91C_US_USMODE_RS485| 
   		AT91C_US_CLKS_CLOCK  | 
   		AT91C_US_CHRL_8_BITS;  //| 
   		// ((unsigned int) 0x1 << 9);   // Odd  
   		 
   		// ((unsigned int) 0x2 << 14);             //本地回环用于测试; 
   		  
   //设置 USART0 中断 
	AT91F_AIC_ConfigureIt(AT91C_BASE_AIC,                          //基地址 
	                AT91C_ID_US0 ,                            			//ID 
	                SPEED_US0_INTERRUPT_LEVEL,                	//中断优先级为6 
	                AT91C_AIC_SRCTYPE_INT_HIGH_LEVEL,        	//内部高电平触发 
	                0 );                 			//中断处理函数 
  
     AT91C_BASE_AIC->AIC_SVR[AT91C_ID_US0] = (unsigned int) COMM_USART0_Interrupt_Handler ; 
 
    AT91F_US_EnableTx(AT91C_BASE_US0);     // \arg IT to be enabled 
  // AT91F_US_EnableRx(AT91C_BASE_US0); 
 
	         
    AT91F_US_EnableIt (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; 
	 
	//flag = AT91C_BASE_US0 -> US_CSR  &   AT91C_US_TXEMPTY ; 
 
	//while  (AT91C_BASE_US0 -> US_CSR  &   AT91C_US_TXRDY  != 0x2) 
	while( !AT91F_US_TxReady (AT91C_BASE_US0)); 
		AT91C_BASE_US0->US_THR = achar; 
			 
} 
 
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; 
				 
		//   *AT91C_US0_THR=*AT91C_US0_RHR&0xff; 
           //*AT91C_AIC_EOICR = 0x00; 
//		} 
//	else if (status & 0x00000010)  //发送器空 
//		{ 
			//for(i=0;i<10;i++) 
			//AT91C_BASE_US0->US_THR =i; 
//			return;	 
			 
//		} 
	 
//	else 
//		return; 
} 
 
INT8S GET_Char() 
{ 
       INT8S achar; 
	while(AT91F_US_RxReady(AT91C_BASE_US0)) 
 
	achar = (INT8S)AT91F_US_GetChar(AT91C_BASE_US0); 
	return achar ; 
	 
}