每天进步一点点NIOS II按键中断程序

/*
 * "Hello World" example.
 *
 * This example prints 'Hello from Nios II' to the STDOUT stream. It runs on
 * the Nios II 'standard', 'full_featured', 'fast', and 'low_cost' example
 * designs. It runs with or without the MicroC/OS-II RTOS and requires a STDOUT
 * device in your system's hardware.
 * The memory footprint of this hosted application is ~69 kbytes by default
 * using the standard reference design.
 *
 * For a reduced footprint version of this template, and an explanation of how
 * to reduce the memory footprint for a given application, see the
 * "small_hello_world" template.
 *
 */
//***************************************************************************************************//
#include <stdio.h>
#include "system.h"                  //包含基本的硬件描述信息
#include "altera_avalon_pio_regs.h"  //包含基本的I/O口信息 
#include "sopc.h" 
#include "sys/alt_irq.h"             //中断函数               
#include "unistd.h"                  //延时函数usleep
#include "altera_avalon_timer_regs.h"//定义内核寄存器的映射,提供对底层硬件的符号化访问
#include "alt_types.h"               //Altera定义的数据类型(alt_8等)
#include "io.h"
//***************************************************************************************************//
volatile int edge_capture;//定义变量
//***************************************************************************************************//
void button_ISR0(void* context,alt_u32 id) //中断处理
{
    //volatile int* edge_capture_ptr=(volatile int*)context;//捕获外部中断,进行分别处理,争对多个中断源的程序
    //*edge_capture_ptr=IORD_ALTERA_AVALON_PIO_EDGE_CAP(K_KEY0_BASE);//当系统进入中断处理程序内部时,要将中断清除,以便下一次的中断产生.
    IOWR_ALTERA_AVALON_PIO_EDGE_CAP(K_KEY0_BASE,0x0);//对多个中断源进行分别处理,采用switch语句进行
    IOWR_ALTERA_AVALON_PIO_DATA(LED_BASE,0X07);
    usleep(1000000);
    IOWR_ALTERA_AVALON_PIO_DATA(LED_BASE,0X01);
    usleep(1000000);
}
void button_ISR1(void* context,alt_u32 id) //中断处理
{
    //volatile int* edge_capture_ptr=(volatile int*)context;//捕获外部中断,进行分别处理,争对多个中断源的程序
    //*edge_capture_ptr=IORD_ALTERA_AVALON_PIO_EDGE_CAP(K_KEY1_BASE);//当系统进入中断处理程序内部时,要将中断清除,以便下一次的中断产生.
    IOWR_ALTERA_AVALON_PIO_EDGE_CAP(K_KEY1_BASE,0x0);//对多个中断源进行分别处理,采用switch语句进行
    IOWR_ALTERA_AVALON_PIO_DATA(LED_BASE,0X38);
    usleep(1000000);
}
//***************************************************************************************************//
void init_keyISR(void)//中断初始化
{
    //其实alt_irq_register的第二个形参和ISR的形参是关联的,如果需要向ISR传递参数,
    //就可以写这个值,然后在ISR里处理context,就上面的程序而言,都可以写为NULL。
    void *edge_capture_ptr=(void*) &edge_capture;
    IOWR_ALTERA_AVALON_PIO_EDGE_CAP(K_KEY0_BASE,0x0);//清除中断
    IOWR_ALTERA_AVALON_PIO_IRQ_MASK(K_KEY0_BASE,0x0f);//中断使能
    IOWR_ALTERA_AVALON_PIO_EDGE_CAP(K_KEY1_BASE,0x0);//清除中断
    IOWR_ALTERA_AVALON_PIO_IRQ_MASK(K_KEY1_BASE,0x0f);//中断使能
    alt_irq_register(K_KEY0_IRQ,0,button_ISR0);//中断函数注册
    alt_irq_register(K_KEY1_IRQ,0,button_ISR1);//中断函数注册
}
//***************************************************************************************************//
int main(void)
{
    init_keyISR();//按键中断初始化
    while(1)
    {
        IOWR_ALTERA_AVALON_PIO_DATA(LED_BASE,0X20);
    }
}
//**********************************   END  *********************************************************//
//***************************************************************************************************//
人有两条路要走,一条是必须走的,一条是想走的,你必须把必须走的路走漂亮,才可以走想走的路~~~
原文地址:https://www.cnblogs.com/kongqiweiliang/p/2588192.html