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

📄 main.c

📁 AT91SAM7SE512cpu的中断示例
💻 C
字号:
// ----------------------------------------------------------------------------
//         ATMEL Microcontroller Software Support  -  ROUSSET  -
// ----------------------------------------------------------------------------
// DISCLAIMER:  THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR
// IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
// DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT,
// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
// OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
// EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
// ----------------------------------------------------------------------------
// File Name           : main.c
// Object              : main application written in C
// Creation            : JPP   08-Sep-2005
// ----------------------------------------------------------------------------

// Include Standard LIB  files
#include "project.h"

//*   Waiting time between AT91B_LED1 and AT91B_LED2
#define     WAIT_TIME       AT91B_MCK

#define PIO_INTERRUPT_LEVEL     6
#define SOFT_INTERRUPT_LEVEL	2
#define FIQ_INTERRUPT_LEVEL     7  // Always high

//* Global variable
int count_timer0_interrupt;
int count_timer1_interrupt;

// Use the Library Handler defined in file periph/pio/pio_irq/irq_pio.s

extern void FIQ_init_handler(void);

// External Function Prototype
extern void timer_init (void );
extern void Usart_init (void);

//*----------------------------------------------------------------------------
//* Function Name       : aic_software_interrupt
//* Object              : Software interrupt function
//*----------------------------------------------------------------------------
void aic_software_interrupt(void)
{
    //* Read the output state
    if ( (AT91F_PIO_GetInput(AT91D_BASE_PIO_LED) & AT91B_LED2 ) == AT91B_LED2 )
    {
        AT91F_PIO_ClearOutput( AT91D_BASE_PIO_LED, AT91B_LED2 );
    }
}

//*----------------------------------------------------------------------------
//* Function Name       : pio_c_irq_handler
//* Object              : Irq Handler calAT91B_LED by the irq_pio.s
//*----------------------------------------------------------------------------
void pio_c_irq_handler ( void )
{
int dummy;
    //* Read the output state
    if ( (AT91F_PIO_GetInput(AT91D_BASE_PIO_LED) & AT91B_LED1 ) == AT91B_LED1 )
    {
       AT91F_PIO_ClearOutput( AT91D_BASE_PIO_LED, AT91B_LED1);
    }  else     {
       AT91F_PIO_SetOutput( AT91D_BASE_PIO_LED, AT91B_LED1);
    }
    //* enable the next PIO IRQ
    dummy =AT91D_BASE_PIO_SW->PIO_ISR;
    //* suppress the compilation warning
    dummy =dummy;
    //* while SW3 is push wait
    while ( (AT91F_PIO_GetInput(AT91D_BASE_PIO_SW) & AT91B_BP2 ) != AT91B_BP2 );
}

//*----------------------------------------------------------------------------
//* Function Name       : delay
//* Object              : Wait
//*----------------------------------------------------------------------------
void delay ( void )
{
//* Set in Volatile for Optimisation
    volatile unsigned int    i ;
//* loop delay
    for ( i = 0 ;(i < WAIT_TIME/100 );i++ ) ;
}

//*----------------------------------------------------------------------------
//* Function Name       : main
//* Object              : Main interrupt function
//*----------------------------------------------------------------------------
int main( void )
//* Begin
{
    unsigned int   loop_count ;
    AT91PS_AIC     pAic;
    //* Load System pAic Base address
        pAic = AT91C_BASE_AIC;

    //* Enable User Reset and set its minimal assertion to 960 us
        AT91C_BASE_RSTC->RSTC_RMR = AT91C_RSTC_URSTEN | (0x4<<8) | (unsigned int)(0xA5<<24);

     	loop_count = 0 ;
    // First, enable the clock of the PIO for led and PIO for Switch
        AT91F_PMC_EnablePeriphClock ( AT91C_BASE_PMC, 1 << AT91D_ID_PIO_LED ) ;
    //* then, we configure the PIO Lines corresponding to AT91B_LEDx
    //* to be outputs. No need to set these pins to be driven by the
    //* PIO because it is GPIO pins only.
        AT91F_PIO_CfgOutput( AT91D_BASE_PIO_LED, AT91B_LED_MASK ) ;
   	//* Clear the AT91B_LED's. On the EK we must apply a "1" to turn off AT91B_LEDs
     	AT91F_PIO_SetOutput( AT91D_BASE_PIO_LED, AT91B_LED_MASK ) ;
    //* Clear the AT91B_LED's. On the EK we must apply a "1" to turn off AT91B_LEDs

    //* open external PIO interrupt
    //* define switch BP1 at PIO input for interrupt IRQ loop
        AT91F_PMC_EnablePeriphClock ( AT91C_BASE_PMC, 1 << AT91D_ID_PIO_SW ) ;

        AT91F_PIO_CfgInput(AT91D_BASE_PIO_SW, AT91B_BP1 );
        AT91F_AIC_ConfigureIt ( pAic, AT91D_ID_PIO_SW,
	                                  PIO_INTERRUPT_LEVEL,
	                                  AT91C_AIC_SRCTYPE_INT_HIGH_LEVEL,
	                                  pio_c_irq_handler);
        AT91F_PIO_InterruptEnable(AT91D_BASE_PIO_SW,AT91B_BP1);
        AT91F_AIC_EnableIt (pAic, AT91D_ID_PIO_SW);

    //* Open the software interrupt on the AIC
        AT91F_AIC_ConfigureIt ( pAic, AT91C_ID_SYS,
                                      SOFT_INTERRUPT_LEVEL,
                                      AT91C_AIC_SRCTYPE_INT_POSITIVE_EDGE,
                                      aic_software_interrupt);
        AT91F_AIC_EnableIt (pAic, AT91C_ID_SYS);

    //* open  FIQ interrupt
        AT91F_AIC_ConfigureIt ( pAic, AT91C_ID_FIQ,
	                              FIQ_INTERRUPT_LEVEL,
	                              AT91C_AIC_SRCTYPE_EXT_NEGATIVE_EDGE,
	                              FIQ_init_handler);
        AT91F_AIC_EnableIt (pAic, AT91C_ID_FIQ);
    //* generate FIQ interrupt by software
        AT91F_AIC_Trig (pAic,AT91C_ID_FIQ) ;

    //* Init timer interrupt
        timer_init();

    //* Init Usart
        Usart_init();

    //* generate software interrupt
        AT91F_AIC_Trig (pAic,AT91C_ID_SYS) ;

for (;;)
    {
        AT91F_PIO_ClearOutput( AT91D_BASE_PIO_LED, AT91B_POWERLED );
        delay () ;
        AT91F_PIO_SetOutput( AT91D_BASE_PIO_LED, AT91B_POWERLED );
        delay () ;

        loop_count ++ ;
    //* Set AT91B_LED by software interrupt
        if (loop_count == 10)
        {
             loop_count=0;
         //* Software interrupt
             AT91F_AIC_Trig (pAic,AT91C_ID_SYS) ;
        }
    }

//* End
}

⌨️ 快捷键说明

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