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

📄 c_entry.c

📁 at91rm9200中断例程
💻 C
字号:
/********************************************************************
*Name:C_Entry.c
*Brief:Main Routine
*Date:
*********************************************************************/

#include "AT91RM9200.H"
#include <string.h>


/*******************************************************************/
#define AIC_SRCTYPE_INT_LEVEL_SENSITIVE 0x00
#define AIC_SMR18 (( unsigned int *) 0xFFFFF048)
#define AIC_SVR18 (( unsigned int *) 0xFFFFF0C8)
#define AT91C_PMC_CGMR  ((AT91_REG *) 	0xFFFFFC20) // (PMC) Peripheral Clock Enable Register


static void LED_Test(void);
static void TC1_init(void);
extern void us0_int(void);
//extern void timer1_asm_irq_handler(void);
extern void us0_asm_irq_handler(void);
extern void us0_init(void);

char temp;
static int us0_flag=0;
__inline unsigned int AT91F_AIC_ConfigureIt (
	AT91PS_AIC pAic,  // \arg pointer to the AIC registers
	unsigned int irq_id,     // \arg interrupt number to initialize
	unsigned int priority,   // \arg priority to give to the interrupt
	unsigned int src_type,   // \arg activation and sense of activation
	void (*newHandler) (void) ) // \arg address of the interrupt handler
{
	unsigned int oldHandler;
    unsigned int mask ;

    oldHandler = pAic->AIC_SVR[irq_id];

    mask = 0x1 << irq_id ;
    //* Disable the interrupt on the interrupt controller
    pAic->AIC_IDCR = mask ;
    //* Save the interrupt handler routine pointer and the interrupt priority
    pAic->AIC_SVR[irq_id] = (unsigned int) newHandler ;
    //* Store the Source Mode Register
    pAic->AIC_SMR[irq_id] = src_type | priority  ;
    //* Clear the interrupt on the interrupt controller
    pAic->AIC_ICCR = mask ;

	return oldHandler;
}
__inline void AT91F_AIC_EnableIt (
	AT91PS_AIC pAic,      // \arg pointer to the AIC registers
	unsigned int irq_id ) // \arg interrupt number to initialize
{
    //* Enable the interrupt on the interrupt controller
    pAic->AIC_IECR = 0x1 << irq_id ;
}

void us0_init(void)
{
  int i;
/*  AT91F_US0_CfgPIO ();
 AT91F_US0_CfgPMC();
  AT91F_US_Configure((AT91PS_USART)AT91C_BASE_US0,MCK,AT91C_US_ASYNC_MODE,BAUD_RATE,TIME_GUARD);
  AT91F_US_EnableTx((AT91PS_USART)AT91C_BASE_US0);
  AT91F_US_EnableRx((AT91PS_USART)AT91C_BASE_US0);
  */
//Define RXD and TXD as peripheral
//	AT91C_PIOA_ASR=(((unsigned int)1<<17)|((unsigned int)1<< 18));
	*AT91C_PIOA_ASR=(((unsigned int)1<<17)|((unsigned int)1<< 18)|((unsigned int)1<<30)|((unsigned int)1<< 31));
	*AT91C_PIOA_BSR=0x0;
//	AT91C_PIOA_PDR=(((unsigned int)1<<17)|((unsigned int)1<<18));
	*AT91C_PIOA_PDR=(((unsigned int)1<<17)|((unsigned int)1<<18)|((unsigned int)1<<30)|((unsigned int)1<<31));

// First, enable the clock of the PIOB
	*AT91C_PMC_PCER=(1<<6);
	*AT91C_PMC_SCER=0x1;
	//*(AT91C_PMC_SCSR)=0x1;
//	AT91C_PMC_PCER=0xff;
	*(AT91C_PMC_CGMR)=0x01;

//* Usart Configure

//* Disable interrupts*/
	//AT91C_US0_IDR=(unsigned int) -1;

//* Reset receiver and transmitter
	*AT91C_US0_CR= (0x1 <<  2) | (0x1 <<  3) | (0x1 <<  5) | (0x1 <<  7);

//* Write the Timeguard Register*/
	*AT91C_US0_TTGR=0;

//* Clear Transmit and Receive Counters*/
	*AT91C_US0_RHR=0x0;
	*AT91C_US0_THR=0x0;

//* Define the USART mode ,8N1 
	*AT91C_US0_MR= ((0x0) + (0x1<<18) + (0x4 <<  9) + (0x3 <<  6));   
	//AT91C_US0_BRGR=AT91C_DBGU_BRGR;   
    *AT91C_US0_BRGR=0x21;
//* Enable usart
	*AT91C_US0_CR= (0x1 <<  4) | (0x1 <<  6); 

//* set time out US_RTOR
//* Arm time out after 255 * 4  bits time 
//* for 115200 bit time = 1/115200 = 8.6 micro sec time out unuti= 4* 8.6 = 34 micro 
  // AT91C_US0_IER=0x1;
 //  AT91F_US_EnableIt(AT91C_BASE_US0,AT91C_US_TIMEOUT | AT91C_US_FRAME | AT91C_US_OVRE |AT91C_US_ENDRX|AT91C_US_RXRDY);
   *AT91C_US0_RTOR=0xFFFF;

//* Enable usart SSTO
	*AT91C_US0_CR= (0x1 << 11);
 *AT91C_US0_IER=0x1;
//* Send Charactors to UART0
	//while(1)
	{
		//AT91C_US0_THR=0xaa;
		for(i=0;i<1000000;i++)
			;
	/*	*(AT91C_US0_THR)=0x55;
		for(i=0;i<1000000;i++)
			;*/
	}

	return;
}
void us0_int(void)
{
	    *AT91C_US0_IER=0x1;
   // temp=*AT91C_US0_RHR;
    // *AT91C_AIC_IDCR=(unsigned int)(1<<AT91C_ID_US0); 
      //安装串口0的中断服务程序
	AT91F_AIC_ConfigureIt (
							AT91C_BASE_AIC,                        // AIC base address
							AT91C_ID_US0,                          // System peripheral ID
							AT91C_AIC_PRIOR_HIGHEST,               // Max priority
							AT91C_AIC_SRCTYPE_INT_LEVEL_SENSITIVE,   // Level sensitive
							us0_asm_irq_handler );						
    
	// Enable ST interrupt
	AT91F_AIC_EnableIt(AT91C_BASE_AIC, AT91C_ID_US0);
}
extern  void C_Entry(void)
{ 
    char ch;
    int i;
//us0_init();
//us0_int();
 //  us0_flag=0;
	AT91F_US_Printf("44");
  	
//Loop:
//	AT91F_US_Printf("\n\rLED Test Using Timer Interrupt[Y/N]?\n\r");
	
	while(1)
	  {
	   AT91F_US_Printf("66");
	    //temp=*AT91C_US0_RHR;
	   for(i=0;i<500000;i++);
	   if(us0_flag==1)
        {
	     AT91F_US_Printf("55");
         us0_flag=0;       
        }
	  }
	/*{                              
         ch=AT91F_US_GetChar(AT91C_BASE_US0);
		switch(ch)
			{
			
			
			case 'Y'|'y':LED_Test();
				     
				break;     
            
            default: AT91F_US_Printf("\r\n");
				     goto Loop;   
	       }	
	}    
*/
 
}
/*

*/
void AT91F_US0_HANDLER(void)
{
	//       us0_sputs("C1");
 //  	if ( *AT91C_US0_CSR & 0x1)
          {
         temp=*AT91C_US0_RHR;
         *AT91C_AIC_ICCR=(1<<AT91C_ID_US0); 
          // us0_i_putc(temp);
//	       us0_sputs("CSC");
//AT91F_US_Printf("77");
	      us0_flag=1;
	      }
	    //  *AT91C_US0_RTOR=0xffff;
	 //     AT91F_US_ResetRx(AT91C_ID_US0);
	       
 	//AT91F_AIC_EnableIt(AT91C_BASE_AIC, AT91C_ID_US0);

 }


void timer1_c_irq_handler(void)

// *Begin
{
     unsigned int dummy;
     
     dummy=*AT91C_TC1_SR;                          /*Read TC1 Status Register to clear it */
     
        
     *AT91C_AIC_ICCR=(1<<AT91C_ID_TC1);                /*Clear the TC1 interrupt*/
     if((*(AT91C_PIOC_PDSR)&(unsigned int)(0x1<<14))&&(*(AT91C_PIOC_PDSR)&(unsigned int)(0x1<<15)))
     
     {*(AT91C_PIOC_CODR)=(unsigned int)(1<<15)|(unsigned int)(1<<14);}
     
     else
     
     {*(AT91C_PIOC_SODR)=(unsigned int)(1<<15)|(unsigned int)(1<<14);}
        
      
     //AT91F_US_Printf("\n\rhello!\r\n");   
}

void LED_Test(void)
{    
     int i;
     int k;
     //unsigned int dummy;
     /*Enable the pio pin*/
     *(AT91C_PIOC_PER)=(unsigned int)(1<<14)|(unsigned int)(1<<15);
     /*Enable the pio pin as output*/
     *(AT91C_PIOC_OER)=(unsigned int)(1<<14)|(unsigned int)(1<<15);
     /*Clear led*/
     *(AT91C_PIOC_CODR)=(unsigned int)(1<<14)|(unsigned int)(1<<15);
     /* enable peripheral clock */  
     *AT91C_PMC_PCER = 1 << AT91C_ID_TC1;  
     /*Init TC1*/  
   //  TC1_init();
     
    while(1)
    {
      //*AT91C_TC1_RC=0x0000;
      //*AT91C_AIC_IECR=(1<<AT91C_ID_TC1);
      //AT91F_US_Printf("test!");
      for(i=0;i<10;i++)
      {
         k=0;
      }
      *(AT91C_PIOC_SODR)=(unsigned int)(1<<14);
      Delay(1000000);
      *(AT91C_PIOC_CODR)=(unsigned int)(1<<14);
      Delay(1000000);    
    }
}

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

⌨️ 快捷键说明

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