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

📄 nios_irq.txt

📁 在niosII 中实现按键触发中断使数码管或LED显示
💻 TXT
字号:

#include "alt_types.h"
#include "io.h"
#include "sys/alt_irq.h"
#include "system.h"
#include <stdio.h>
#include <unistd.h>
#include "altera_avalon_pio_regs.h"

static volatile alt_u8 count;

static void handle_button_interrupts(void* context,alt_u32 id)
{count=IORD_ALTERA_AVALON_PIO_EDGE_CAP(PIO_BASE);
IOWR_ALTERA_AVALON_PIO_EDGE_CAP(PIO_BASE,0);
}

static void init_button_pio(){
    IOWR_ALTERA_AVALON_PIO_IRQ_MASK(PIO_BASE,0xf);
    IOWR_ALTERA_AVALON_PIO_EDGE_CAP(PIO_BASE,0x0);
    alt_irq_register(PIO_IRQ,NULL,handle_button_interrupts);
}


#include "alt_types.h"
#include "io.h"
#include "sys/alt_irq.h"
#include "system.h"
#include <stdio.h>
#include <unistd.h>
#include "altera_avalon_pio_regs.h"

 volatile int count;

static void handle_button_interrupts(void* context,alt_u32 id)
{count++;
IOWR_ALTERA_AVALON_PIO_EDGE_CAP(PIO_BASE,0);
}

static void init_button_pio(){
    IOWR_ALTERA_AVALON_PIO_IRQ_MASK(PIO_BASE,0xf);
    IOWR_ALTERA_AVALON_PIO_EDGE_CAP(PIO_BASE,0x0);
    alt_irq_register(PIO_IRQ,NULL,handle_button_interrupts);
}


int main()
{
     count=0;
    init_button_pio();
    while(1){
                 
            IOWR(SEG7_LUT_8_INST_BASE,0,count);
    }
}
            
int main()
{
    init_button_pio();
    while(1){
        if(count)
             
            IOWR(SEG7_LUT_8_INST_BASE,0,8);
    }
}
   



不给过多注释了(其实注释够多了)
#include <stdlib.h>
#include <io.h>
#include "sys/alt_irq.h" 
#include "altera_avalon_pio_regs.h"
#include "system.h"

/* A variable to hold the value of the button pio edge capture register. */
volatile int edge_capture;

#ifdef BUTTON_PIO_BASE
/* 按键中断服务程序*/
static void handle_button_interrupts(void* context, alt_u32 id)
{
    /* Cast context to edge_capture's type. It is important that this be 
     * declared volatile to avoid unwanted compiler optimization.
     */
    volatile int* edge_capture_ptr = (volatile int*) context;
    /* Store the value in the Button's edge capture register in *context. */
    *edge_capture_ptr = IORD_ALTERA_AVALON_PIO_EDGE_CAP(BUTTON_PIO_BASE);
    /* Reset the Button's edge capture register. */
    IOWR_ALTERA_AVALON_PIO_EDGE_CAP(BUTTON_PIO_BASE, 0);
}

/* Initialize the button_pio. */
static void init_button_pio()
{
    /* Recast the edge_capture pointer to match the alt_irq_register() function
     * prototype. */
    void* edge_capture_ptr = (void*) &edge_capture;
    /* Enable all 4 button interrupts. */
    IOWR_ALTERA_AVALON_PIO_IRQ_MASK(BUTTON_PIO_BASE, 0xf);
    /* Reset the edge capture register. */
    IOWR_ALTERA_AVALON_PIO_EDGE_CAP(BUTTON_PIO_BASE, 0x0);
    /* Register the interrupt handler. */
    alt_irq_register( BUTTON_PIO_IRQ, edge_capture_ptr,
                      handle_button_interrupts );
}
#endif

int main (void) __attribute__ ((weak, alias ("alt_main")));

int alt_main(void)
{
    alt_irq_init(ALT_IRQ_BASE); //necessary,enable interrupts
    alt_sys_init();  //unnecessary
    alt_io_redirect(ALT_STDOUT,ALT_STDIN,ALT_STDERR);//unnecessary
    init_button_pio();
    while(1)
    {
        switch(edge_capture)
        {
        case 0x01:
            IOWR_ALTERA_AVALON_PIO_DATA(LED_PIO_BASE,0xFF);
            break;
        case 0x02:
            IOWR_ALTERA_AVALON_PIO_DATA(LED_PIO_BASE,0x00);
            break;
        case 0x04:
            IOWR_ALTERA_AVALON_PIO_DATA(LED_PIO_BASE,0xFF);
            break;
        case 0x08:
            IOWR_ALTERA_AVALON_PIO_DATA(LED_PIO_BASE,0x00);
            break;
        default:
            IOWR_ALTERA_AVALON_PIO_DATA(LED_PIO_BASE,0x0A);
            break;
        }
    }
    return 0;
}
 

 niosII button 中断

volatile int edge_capture;

static void handle_button_interrupts(void* context, alt_u32 id)
{
/* Cast context to edge_capture's type.
* It is important to keep this volatile,
* to avoid compiler optimization issues.
*/
volatile int* edge_capture_ptr = (volatile int*) context;
/* Store the value in the Button's edge capture register in *context. */
*edge_capture_ptr = IORD_ALTERA_AVALON_PIO_EDGE_CAP(BUTTON_PIO_BASE);
/* Reset the Button's edge capture register. */
IOWR_ALTERA_AVALON_PIO_EDGE_CAP(BUTTON_PIO_BASE, 0);
}

/* Initialize the button_pio. */

static void init_button_pio()
{
/* Recast the edge_capture pointer to match the alt_irq_register() function
* prototype. */
void* edge_capture_ptr = (void*) &edge_capture;
/* Enable all 4 button interrupts. */
IOWR_ALTERA_AVALON_PIO_IRQ_MASK(BUTTON_PIO_BASE, 0xf);
/* Reset the edge capture register. */
IOWR_ALTERA_AVALON_PIO_EDGE_CAP(BUTTON_PIO_BASE, 0x0);
/* Register the interrupt handler. */
alt_irq_register( BUTTON_PIO_IRQ, edge_capture_ptr, handle_button_interrupts ); 
}

static void TestButtons( void )
{
alt_u8 buttons_tested;
alt_u8 all_tested;
/* Variable which holds the last value of edge_capture to avoid 
* "double counting" button/switch presses
*/
int last_tested;
/* Initialize the Buttons/Switches (SW0-SW3) */
init_button_pio();
/* Initialize the variables which keep track of which buttons have been tested. */
buttons_tested = 0x0;
all_tested = 0xf;

/* Initialize edge_capture to avoid any "false" triggers from
* a previous run.
*/

edge_capture = 0;

/* Set last_tested to a value that edge_capture can never equal
* to avoid accidental equalities in the while() loop below.
*/

last_tested = 0xffff;

/* Print a quick message stating what is happening */

printf("\nA loop will be run until all buttons/switches have been pressed.\n\n");
printf("\n\tNOTE: Once a button press has been detected, for a particular button,\n\tany further presses will be ignored!\n\n");

/* Loop until all buttons have been pressed.
* This happens when buttons_tested == all_tested.
*/

while ( buttons_tested != all_tested )
{ 
if (last_tested == edge_capture)
{
continue;
}
else
{
last_tested = edge_capture;
switch (edge_capture)
{
case 0x1:
printf("\nButton 1 (SW0) Pressed.\n");
buttons_tested = buttons_tested | 0x1; 
break;
case 0x2:
printf("\nButton 2 (SW1) Pressed.\n");
buttons_tested = buttons_tested | 0x2;
break;
case 0x4:
printf("\nButton 3 (SW2) Pressed.\n");
buttons_tested = buttons_tested | 0x4;
break;
case 0x8:
printf("\nButton 4 (SW3) Pressed.\n");
buttons_tested = buttons_tested | 0x8;
break;
}
}
}
/* Disable button interrupts for anything outside this loop. */
IOWR_ALTERA_AVALON_PIO_IRQ_MASK(BUTTON_PIO_BASE, 0x0);
printf ("\nAll Buttons (SW0-SW3) were pressed, at least, once.\n");
usleep(2000000);
return;
}

⌨️ 快捷键说明

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