NIOS II IO中断

自己写的sopc.h头文件:

#ifndef SOPC_H_
#define SOPC_H_


#include "system.h"

typedef struct
{
    unsigned long int DATA;
    unsigned long int DIRECTION;
    unsigned long int INTERRUPT_MASK;
    unsigned long int EDGE_CAPTURE;
    
}PIO_STR;


#define LED          ((PIO_STR *) LED_BASE)      



#define KEY          ((PIO_STR *) KEY_BASE)

#endif /*SOPC_H_*/

main函数:

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

/*-----------------------------------------------------------------------------
 * Variable 
 *-----------------------------------------------------------------------------*/
volatile unsigned char key_flag1 = 0;
volatile unsigned char key_flag2 = 0;
volatile unsigned char key_flag3 = 0;
volatile unsigned char key_flag4 = 0;

/* 
 * ===  FUNCTION  ======================================================================
 *         Name:  ISR_key
 *  Description:  
 * =====================================================================================
 */
void ISR_key(void * context,unsigned long id)
{
    unsigned int key_data;
    usleep(100000);
    key_data = IORD(KEY_BASE,0x03);
    key_data &= 0x000f;
    KEY->EDGE_CAPTURE = 0;
    //key_flag = !key_flag;   
    switch(key_data)
    {
    case 0x01:
        key_flag1 = !key_flag1;
        break;
    case 0x02:
        key_flag2 = !key_flag2;
        break;
    case 0x04:
        key_flag3 = !key_flag3;
        break;
    case 0x08:
        key_flag4 = !key_flag4;
        break;
    default:
        key_flag1 = 0;
        key_flag2 = 0;
        key_flag3 = 0;
        key_flag4 = 0;

    }

}

/* 
 * ===  FUNCTION  ======================================================================
 *         Name:  init_key
 *  Description:  
 * =====================================================================================
 */
int init_key(void)
{
    KEY->INTERRUPT_MASK = 0x0f;           //打开低四位IO中断
    KEY->EDGE_CAPTURE = 0;                //清除捕获中断
    
    return  alt_irq_register(KEY_IRQ,NULL,ISR_key);        
}

/* 
 * ===  FUNCTION  ======================================================================
 *         Name:  main
 *  Description:  
 * =====================================================================================
 */
int main()
{
    if(!init_key()){
        printf("register successfully!\n");
    }
    else{
        printf("Error: register failure!\n");
    }
    
    while(1){
        if(key_flag1){
            LED->DATA |= 0x03;
        }
        else{
            LED->DATA &= ~0x03;
        }
        if(key_flag2){
            LED->DATA |= 0x0c;
        }
        else{
            LED->DATA &= ~0x0c;
        }
        if(key_flag3){
            LED->DATA |= 0x30;
        }
        else{
            LED->DATA &= ~0x30;
        }
        if(key_flag4){
            LED->DATA |= 0xc0;
        }
        else{
            LED->DATA &= ~0xc0;
        }
    }
    
    return 0;
}

实验原理:通过4个按键控制8个LED灯的亮灭,每个按键控制两个LED,按一下其中的按键,对应的两个LED亮,再按一下,LED暗,我们通过读捕获标志位,来识别不同的按键中断,注意按键防抖,在中断服务程序中一定要清除中断标志位,否则将不停的中断,通过IORD函数读取捕获寄存器的值,赋值给key_data,并将没有用到的高位清零,防止出错。

本程序参考了NIOS II 那些事的部分程序。

posted on 2012-04-10 15:12  chenfengfei  阅读(1114)  评论(0编辑  收藏  举报

导航