NIOSII按键中断实验

接着在上一个NIOSII流水灯实验的基础上进行了一个按键中断控制流水灯的实验,增加一个按键开关PIO(only input):按一下开关,四个灯开始循环点亮;再按一下开关,四个灯停止点亮,全熄灭。

头文件SOPC.h
#ifndef SOPC_H_
#define SOPC_H_

#include "system.h"
#define _LED
#define _KEY

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

#ifdef _LED
#define LED ((PIO_STR*)LED_BASE)
#endif

#ifdef _KEY
#define KEY ((PIO_STR*)KEY_BASE)
#endif

#endif /*SOPC_H_*/
/*
 * "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<unistd.h>

#include"../inc/sopc.h"
#include"system.h"
#include"sys/alt_irq.h"

int key_flag =0;
void ISR_key(void*context,unsigned long id)
{
    key_flag = ~key_flag;
    usleep(500000);
}

int init_key(void)
{
    KEY->INRERRUPT_MASK =1;
    
    return alt_irq_register(KEY_IRQ,NULL,ISR_key);
}

int main()
{
  int i;
  if(!init_key())
  {
    printf("register successfully!\n");
  }
   else
    {
       printf("Error:register failure!\n"); 
    }
   while(1)
   {
    if(key_flag)
    {
        for(i=0;i<4;i++)
        {
            LED->DATA=1<<i;
            usleep(500000);
        }
        
    }
    else
    {
        LED->DATA=0;
        
    }
   }
   return 0;
}


在NIOSII中若用电平中断,则要注意它只对高电平敏感,若想实现低电平敏感则要加一个非门。




posted on 2010-08-07 15:02  齐威王  阅读(1361)  评论(0编辑  收藏  举报

导航